How to install multi PHP server on Ubuntu 20.04 and NGINX
There are several reasons you want to use multiple versions of PHP with Nginx on your server. If you have two applications, one application that runs on PHP 5.6 and a second application that runs on PHP 7.4, then you must use multiple PHP versions on your server. You can also use a separate server for each PHP version, but it will increase your hosting cost. Installing multiple versions of PHP with Nginx is the best choice to reduce hosting costs.
In this tutorial, I will install multiple versions of PHP and configure Nginx to work with both versions of PHP.
What you need:
- A fresh Ubuntu 20.04 server VPS or on your own machine or Visaul workStation like VirtualBox with minimum 2 GB RAM.
- Two valid domain names pointed with your VPS IP address. In this tutorial, we will use php56.example.com and php74.example.com
STEP 1
Once you are logged into your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.
sudo apt-get update -y
STEP 2
after you update the latest packages for your server, now you need to install the NGINX webserver to your server in order to run your web application on your ubuntu server. now run the command below to install NGINX on ubuntu
sudo apt-get install nginx -y
After you install Nginx completed you can you test whether you nginx work. to do so enter you server ip address you should properly see the page below:
STEP 3
Now our nginx server is run, it’s time to install php 5.6 and php 7.4 on our ubunu server. to do we need to run the command below:
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
Next, update the repository with the following command:
sudo apt-get update -y
Once the repository is up to date, install PHP 5.6, PHP 7.4 and PHP-FPM with the following command:
apt-get install php5.6 php5.6-fpm php7.4 php7.4-fpm -y
STEP 4 — install MYSQL
Now that you have a web server, and php on your server and if you willing to connect your php to the database, you need to install MySQL (a database management system) to store and manage the data for your site. to install the latest version of Mysql you need run the commad below:
sudo apt install mysql-server
The MySQL database software is now installed, but its configuration is not yet complete. To secure the installation, MySQL comes with a script that will ask whether we want to modify some insecure defaults. Initiate the script by typing:
sudo mysql_secure_installation
The following installation mysql will ask you a few question which lead you to choose strong password. simply enter “Y” as the following.
STEP 5 — Create site folders
as we run two version of php in the purpose of serve two applications one using php5.6 and other one using php7.4 now we need to create two directories one is php5.6 and other is php7.4 and we will create index.php in to each directory to test. now we need to go to the file structure below to create them.
cd /var/www
Now we are in www directory, remember we will create two folders one is php5.6 and php7.4 by following he the command below:
sudo mkdir php5.6 && cd php56
nano index.php
Now we have create folder php5.6 and we create an index.php but the file is empty and we need to write php function to show the version of php. in the file index.php, I enter the code to it.
<?php
phpinfo();
?>
Ok, now we will create php7.4 folder with the same process
sudo mkdir php74 && cd php74
nano index.php
and we write the same code to index.php but in difference php7.4 folder
<?php
phpinfo();
?>
STEP 6 — configure NGINX to server two applications
Here is where things will start to get a little tricky. Get your thinking caps on and lets configure our server engine. All the configuration we need to make is in the following config files. First we need to go to the following directory call sites-available
cd /etc/nginx/sites-available
by default you will see the file name default which currently server the Nginx default website as we have test in step 2. because we will run two applications on single webserver will create two files to tell the server that each file will point to each application one is using php5.6 and other using php 7.4 and these two file will tell the server point to specific directories as we have create above.
as you are in the sites-available directory and now we are going to create a cofiguration file call php56.example.com.conf as the following
Go ahead and open it up in Nano using the following command (use another editor if you prefer).
nano php56.example.com.conf
as you are on this file please enter the following:
server {
listen 80;
root /var/www/php56/;
index index.php;
server_name php56.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
ctrl+c and “y” and enter to save the file.
next you we need create other configuration file call php74.example.com.conf
nano php74.example.com.conf
and enter the following:
server {
listen 80;
root /var/www/php74/;
index index.php;
server_name php74.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if you are looking to the configuration file, you will see the deference between them. first is “root /var/www/php56/;” and “root /var/www/php74/;”. this line of code will tell the nginx where the application located when the user request through the url below:
www.php56.example.com
www.php74.example.com
Example when the user request www.php56.example.com the nginx will open application in php56 folder and also tell php5.6 version to run by using the line below:
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
Now the nignx config files are setup and we have one more thing to do by enable both virtual host files with the following command:
ln -s /etc/nginx/sites-available/php56.example.com.conf /etc/nginx/sites-enabled/
And also
ln -s /etc/nginx/sites-available/php74.example.com.conf /etc/nginx/sites-enabled/
Ok, look like everything is almost done now we need to restart the nginx server to make our new config files up and running by the following command:
systemctl restart nginx
systemctl restart php5.6-fpm
systemctl restart php7.4-fpm
STEP 7 — TEST BOTH WEBSITE
Both websites are now installed and configured run with multiple versions of PHP. Now, open your web browser and type the URL php56.example.com. you will will see php 5.6 is running.
and php74.example.com using php 7.4
CONCLUTION
In the above article, you have figured out how to have various sites with various PHP version on Ubuntu 20.04. You should now have sufficient information to multiple PHP version as you need for your applications. In case you’re prepared to begin with a virtual private network(VPS) for facilitating your sites. visit digital ocean with my reference here, you will get 100$ credit to try https://m.do.co/c/19496a56b40f