Friday, September 24, 2021
HomeLinuxHow To Install and Set up Yii PHP Framework on Ubuntu Linux

How To Install and Set up Yii PHP Framework on Ubuntu Linux

The Yii is the acronym of Yes It Is, which is a great free and open-source (FOSS) Model–view–controller, a PHP framework tool for PHP development. It’s a tool for developing PHP modules and applications. The Yii PHP framework is available for both Ubuntu and Windows. It can rapidly build up large PHP modules, and its component-oriented work functionalities have made it unique and user-friendly. The Yii PHP Framework works with an entry script, application, controller, filter, and model layer.

Yii PHP Framework on Ubuntu Linux


The Yii cross-platform PHP Framework is written in PHP, which is licensed under the New BSD License. Installing and running the Yii framework is hassle-free. Installing the Yii PHP composer is easy and straightforward on Linux. You do not need to worry about dependencies and repositories. You can install the Yii PHP Framework on Ubuntu through the composer tool. In this post, we will see how to install the Yii PHP Framework on Ubuntu Linux.

Method 1: Install Yii Php Framework via Composer


The Composer method demonstrates the command-based installation process via the PHP composer tool on Linux. In this method, we will see how to install the PHP composer tool and then how to deploy the Composer tool to pull the Yii PHP framework on a Ubuntu machine.

Step 1: Install the PHP Composer


Installing the PHP composer is hassle-free; it can pull major PHP frameworks and modules easily. If you’re using a Debian or Ubuntu system, you can quickly install the Composer tool on your machine by running the following commands. If you don’t have the PHP tool installed on your machine, please ensure that you have the PHP installed on the system.

install PHP on Ubuntu

First, run the cURL command to get the Composer installer file on your filesystem. Once the file is loaded, you may now move the file inside the /usr/local/bin/composer.

get composer tool on Ubuntu

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Then, run the following change mode command to make the file executable on the Ubuntu system.

php composer phar

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

Now, browse the PHP directory by running the following CD command on your terminal shell. Based on your project’s name, you may need to replace the word testproject in the command.

$ cd /var/www/html/

Now, run the composer command to the shell for creating a new Yii project on your Ubuntu system.

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

composer create test project for Yii

Since we are doing this entire project based on the PHP and PHP frameworks, so it is necessary to have a PHP server installed/running on the system. So, if you do not have any PHP servers (localhost server) installed, you can install either the Nginx or the Apache server.

Step 2: Run Nginx on Your Machine


Now, run the following Nginx command to get the status of the server on your machine. If you need to re-route your server’s IP address and block/unblock any port, please go through this post to know more about Nginx basics.

$ sudo nginx -t

Now, execute the next Nginx command to edit the Nginx configuration script on your machine for the Yii PHP framework.

$ sudo ln -s /etc/nginx/sites-available/testprojects.me.conf /etc/nginx/sites-enabled/testprojects.me.conf

Finally, you can copy and paste the following script lines inside the configuration file. Then save and exit the file. Once the configuration finishes, please run the following system control command to restart the Nginx PHP server on your machine.

$ sudo systemctl restart nginx

Step 3: Run Yii PHP Framework with Nginx


To integrate the Yii PHP framework with the Nginx server, we need to edit the sites-available script of the PHP server so that the Nginx can load the framework. You can run the following Nano command on the shell to execute the script and create a domain name inside the file.

If you don’t want to deploy your Yii environment into a public domain, you can also assign a localhost IP address for test purposes.

$ sudo nano /etc/nginx/sites-available/testproject.me.conf

site availables testproject

server {
set $host_path "/var/www/html/testproject";
#access_log /www/testproject/log/access.log main;

server_name testprojects.me www.testprojects.me;
root $host_path/web;
set $yii_bootstrap "index.php";

charset utf-8;

location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
}

location ~ ^/(protected|framework|themes/\w+/views) {
deny all;
}

#avoid processing of calls to unexisting static files by yii
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}

# pass the PHP scripts to FastCGI server listening on UNIX socket 
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;

#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;

#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}

# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}

You can now load your server’s address on your browser to load the Yii PHP framework.

Step 4: Running Yii Using PHP Development Server


We are now a few steps behind in using the Yii PHP framework on the Ubuntu machine. Run the following command to deploy the Yii PHP framework on your Ubuntu machine.

$ cd /var/www/html/testproject/
$ php yii serve

If you need to change the default network port, you can assign another port via the following Php Yii command. By default, the Yii PHP framework uses the network port 500. Please be mindful, if you can not load the Yii framework on your environment, please check the firewall system, and allow your network IP and port.

$ php yii serve --port=8080

You may now load your server IP along with the assigned port to load the Yii PHP framework on your Ubuntu machine.

yii on firefox

Step 5: Add HTTPS to Yii on Nginx


To add the secure HTTPS protocol on your Nginx server for making your server more secure, you need to install the Certbot application on your Ubuntu system. Later, the Certbot will enable HTTPS on your Nginx server. You can run the following Snap command on your shell to install the Certbot tool.

$ sudo snap install --classic certbot

Once the Certbot installation ends up, run Certbot for Nginx. Then the Certbot will ask for your email address and other credentials. Please input valid values for enabling the HTTPS successfully.

$ sudo certbot --nginx

Method 2: Install Yii Php Framework via Archive File


In this method of installing the Yii PHP framework, we will see how to download the Yii archive file directly on our local machine and install it manually on our Ubuntu system. You can also follow this method on distributions as well.

Step 1: Download the Yii Zip File 


First, you need to download the Yii PHP framework from the official Yii website. Once the file is downloaded, you may now extract the Archie file on your file system. You can also try using an older version of the Yii framework if your system does not support the latest version. Now, you may move the file inside your root directory where you want to keep the Yii framework files.

Yii PHP Framework on Ubuntu download

Here, we are renaming the extracted folder as Yii, and later we will move the folder inside the /var/www/ directory.

sudo mv yii /var/www/

move yii to var www directory

Now, we need to create a project folder for the Yii framework. For instance, we will now manually create a folder named test inside the /var/www/ directory. If you’re not acting as a root user, you may need to load the following change mode command to proceed.

sudo chmod -R 777 /var/www/

Please note, as we are going to move the files inside the root directory, so we might need to grant the read-write permission to the directory since we are doing it on our local machine, so we don’t need to worry about the security. But if you’re setting up this in a public server, please be mindful of reverting the permission setting after you are done with configuring.

Now, allow the test folder read-write access.

sudo chmod 777 -R test

Step 2: Execute Yii PHP Framework on Ubuntu


Finally, run the following command in the shell to install the Yii PHP framework. Here, we are locating the Framework directory on the Yii folder, then we will install the Yii PHP framework inside the test directory.

php yii/framework/yiic.php webapp /var/www/test/

install yii on ubuntu

Now, if we load the Yii directory on our localhost server, we will probably encounter a cookie error. To solve the error, you can open the PHP directory where you kept the Yii files.

Now, open the Yii configuration script and add a cookie name inside the script. When the script opens, please uncomment the Gii array function and MySQL database functions and give it a username and password. Then save and exit the file.

sudo nano /var/www/yii/test/protected.config

Once the cookie configuration is set, you may now refresh the localhost server to load the Yii PHP framework on your Ubuntu machine.

Extra Tip: Check If The Composer Works Fine


You might face an issue with the Composer while installing the Yii PHP framework on Ubuntu. As you can see in the below screenshot, there is a Composer Transport error. Basically, this issue occurs due to a bad network or missing composer files. In most cases, the issue is solved by either switching the Linux server or removing and re-installing the Composer tool on Linux.

composer error on ubuntu Yii PHP Framework on Ubuntu

You might also need to allow the read-write permission to access your project directory.

sudo chmod -R 777 /var/www/html/testproject/

check composer on ubuntu

Then, you may need to run the following commands to wipe the Composer tool from your Ubuntu Linux machine.

sudo apt-get remove composer
sudo apt-get remove --auto-remove composer
sudo apt-get purge composer
sudo apt-get purge --auto-remove composer

When the removal ends up, please follow step 1 again to install the Composer tool, or you can also follow this post to know more details about how to install Composer on Linux. After a fresh installation, please run a composer update command on your terminal shell to make sure that the Composer tool is fully updated.

composer self-update
composer diagnose

Final Words


Installing the Yii Php framework is not a hard task. You can use any of the above-mentioned methods to install the Yii framework on Ubuntu Linux. Moreover, the Extension and documentation will help you to understand the basics of the Yii tool. In the entire post, we have seen how to install the Yii PHP Framework on Ubuntu.

Please share this post with your friends and the Linux community if you find it helpful and handy. You can also write down your opinions regarding this post in the comment section.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

spot_img

Latest Post

Must Read