how to install yii php framework on centos

How to Install Yii PHP Framework on CentOS

Need a tutorial on how to install Yii PHP framework on CentOS? Then this guide is for you.

Yii stands for Yes, it is! It’s a fast, secure, and efficient PHP framework allowing maximum web programming reusability. As a result, you can achieve a rapid development process even for large-scale web applications. 

Being a generic PHP framework, its best use cases are in creating web applications such as portals, forums, content management systems (CMS), e-commerce projects, RESTful Web services, etc. This is thanks to its component-based architecture and sophisticated caching support.

Now let’s see how you can install it on a CentOS system. Let’s get started!

Prerequisites

Before you can install Yii, you need to fulfill these requirements:

  • CentOS server installed
  • PHP 7.4.0 or above installed
  • Composer installed
  • PDO PHP Extension installed
  • Database driver (such as pdo_mysql for MySQL databases) if your app needs a database
  • Internet connection
  • Web browser
  • Root user or Sudo

How to Install Yii PHP Framework on CentOS

If you have fulfilled all the requirements, we can start going through each step and install Yii on your device.

Install PHP(LAMP Stack or LEMP Stack)

First, we need to install PHP. Considering that we will use Yii for web application development, it’s a good idea to install a development stack. And since we’re dealing with PHP, you can either use the LAMP stack or the LEMP stack. For this tutorial, we’ll be installing the LAMP stack

To install the LAMP stack, run this command:

$ sudo yum install httpd php php-mysqlnd php-pdo php-gd php-mbstring zip git

Output:

install php lamp stack

If you prefer a LEMP stack, install it with this command:

$ sudo yum install nginx php php-mysqlnd php-pdo php-gd php-mbstring zip git

After the installation, enable and start the web server. For that, use these commands:

$ systemctl start httpd
$ systemctl enable httpd

Output:

enable and start apache httpd

Both will require you to enter your password for authentication. To check if it’s working, run this command:

$ systemctl status httpd
httpd status

The status is active, which means it’s running, as shown in the screenshot above.

Install Composer

We will use Composer to install Yii on our device. To do that, we first need to install PHP Composer. But before you do that, you must install a JSON extension. You can do that with this command:

$ sudo yum install php-json

Output:

install php-json

Once done, install Composer with this command:

$ curl -sS https://getcomposer.org/installer | php

Output:

downloading composer for yii

Then move the file to /usr/local/bin/composer. You can do that by running this command:

$ sudo mv composer.phar /usr/local/bin/composer

Next, change the file permissions by running this command:

$ chmod +x /usr/local/bin/composer

Install Yii PHP Framework

After that, you can start the actual installation process for Yii. First, let’s go to the Apache root directory. To do that, run this command:

$ cd /var/www/html/

Then install the Yii application template under a Web-accessible folder. For this tutorial, we will name that directory ‘distroid’. Feel free to name it as you wish. Install Yii using Composer with this command:

$ composer create-project --prefer-dist yiisoft/yii2-app-basic distroid 

Output:

How to Install Yii PHP Framework on CentOS

Test Yii

After the installation process is over, go into the created directory. In our case, the command looks like this:

$ cd distroid

Now you can use the built-in PHP web server. To do that, execute this command:

$ php yii serve

Output:

How to Install Yii PHP Framework on CentOS

By default, it will try to use port 8080. However, if another application already occupies that port, then you can use a different port by mentioning it after the –port argument, like this:

$ php yii serve --port=8888

This command will use port 8888 instead of 8080.

Now let’s check if Yii is working as intended. We can verify that by launching the localhost on our browser. Go to Activities and open Firefox web browser.

firefox browser

In the search bar, type in this address:

http://localhost:8888

Now press “Enter”. If everything went well, this should launch the installed Yii application.

How to Install Yii PHP Framework on CentOS

If you see a congratulations page like in the above picture, this means that the Yii PHP framework is now installed on your CentOS. Next, you can play around or move to the next section, where we show you how to configure your web servers.

Configuring Web Servers for Yii

Since we used Apache for this tutorial, let’s deal with that first. To use the recommended Apache configuration, you need to edit the httpd.conf file, which is in /etc/httpd/conf/. Another way to configure is to use a virtual host configuration.

Use the below configuration:

# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"

<Directory "path/to/basic/web">

    # use mod_rewrite for pretty URL support

    RewriteEngine on

    # if $showScriptName is false in UrlManager, do not allow accessing URLs with script name

    RewriteRule ^index.php/ - [L,R=404]

    # If a directory or a file exists, use the request directly

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    # Otherwise forward the request to index.php

    RewriteRule . index.php

    # ...other settings...

</Directory>

Remember to change path/to/basic/web with the actual path you have for basic/web. Then restart Apache with this command:

$ systemctl restart httpd

If you use Nginx, then this is the recommended configuration:

server {
charset utf-8;

    client_max_body_size 128M;

    listen 80; ## listen for ipv4

    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mysite.test;

    root        /path/to/basic/web;

    index       index.php;

    access_log  /path/to/basic/log/access.log;

    error_log   /path/to/basic/log/error.log;

    location / {

        # Redirect everything that isn't a real file to index.php

        try_files $uri $uri/ /index.php$is_args$args;

    }

    # uncomment to avoid processing of calls to non-existing static files by Yii

    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {

    #    try_files $uri =404;

    #}

    #error_page 404 /404.html;

    # deny accessing php files for the /assets directory

    location ~ ^/assets/.*\.php$ {

        deny all;

    }

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        fastcgi_pass 127.0.0.1:9000;

        #fastcgi_pass unix:/var/run/php5-fpm.sock;

        try_files $uri =404;

    }

    location ~* /\. {

        deny all;

    }

}

Lastly, don’t forget to restart Nginx with this command:

$ systemctl restart nginx

Final Thoughts

This tutorial shows you how to install Yii PHP framework on CentOS. We covered the best method for the installation, which is using Composer. The process may feel a bit technical. But if you followed all the steps as we instructed, you should get it installed in no time. And if you’re stuck, feel free to let us know in the comments below.

If this guide helped you, please share it.

Related Posts