PHP

Run Multiple PHP versions with Nginx and Apache on Ubuntu


Introduction

We can host multiple sites in apache2 server.PHP-FPM is used for manage multiple PHP version same server or system.

Using PHP-FPM we have run different PHP version sites on the same server or system.

Use Case

If I have two setup two projects with two domains like one is example1.oneclickitsolution.com and second is example2.oneclickitsolution.com 

In this both sites are run on different different version so using PHP-FPM We will do it.

Sometime We have working in multiple project with different php version then it hard

to change the php version of every single project. This is the solution to this problem.

Installation and Steps

Step 1

sudo apt-get install software-properties-common -y

Step 2

sudo add-apt-repository ppa:ondrej/php

Step 3

sudo apt-get update -y

Step 4

I have to use PHP 7.4 and 8.1. Below are the steps; you can use any PHP version. This is for example. You just need to change the PHP version in the commands below.

 

Install PHP7.4 with FPM Command for install:

sudo apt-get install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 libapache2-mod-fcgid -y 

Install PHP8.1 with FPM Command for install:

sudo apt-get install php8.1 php8.1-fpm php8.1-mysql libapache2-mod-php8.1 libapache2-mod-fcgid -y 

Step 5

Start PHP-FPM service for a particular version:

sudo systemctl enable php7.4-fpm sudo systemctl start php7.4-fpm 

Below are helpful commands for PHP-FPM:

Step 6

Check PHP 7.4 FPM status:

sudo systemctl status php7.4-fpm

Step 7

Start PHP 8.1 FPM service:

sudo systemctl enable php8.1-fpm sudo systemctl start php8.1-fpm 

Step 8

Check PHP 8.1 FPM status:

sudo systemctl status php8.1-fpm

Step 9

Enable Apache modules:

sudo a2enmod actions fcgid alias proxy_fcgi

Step 10

Restart Apache:

sudo systemctl restart apache2

Step 11

Create folders in /var/www/html:

sudo mkdir /var/www/html/example1.oneclickitsolution.com sudo mkdir /var/www/html/example2.oneclickitsolution.com

Step 12

Create info.php file and add code:

sudo nano /var/www/html/example1.oneclickitsolution.com/info.php

Add the following line:

<?php phpinfo(); ?> 

Step 13

Copy the same file to example2.oneclickitsolution.com folder:

sudo cp /var/www/html/example1.oneclickitsolution.com/info.php /var/www/html/example2.oneclickitsolution.com/info.php

Step 14

Create a virtual host file for example1.oneclickitsolution.com:

sudo nano /etc/apache2/sites-available/example1.oneclickitsolution.com.conf Add the following content:<VirtualHost *:80> ServerName example1.oneclickitsolution.com ServerAdmin admin@oneclickitsolution.com DocumentRoot /var/www/html/example1.oneclickitsolution.com/ <Directory /var/www/html/example1.oneclickitsolution.com/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example1.oneclickitsolution.com_error.log CustomLog ${APACHE_LOG_DIR}/example1.oneclickitsolution.com_access.log combined <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch></VirtualHost> 

Step 15

Create a virtual host file for example2.oneclickitsolution.com:

sudo nano /etc/apache2/sites-available/example2.oneclickitsolution.com.conf

Add the following content:

<VirtualHost *:80> ServerName example2.oneclickitsolution.com ServerAdmin admin@example2.oneclickitsolution.com DocumentRoot /var/www/html/example2.oneclickitsolution.com/ <Directory /var/www/html/example2.oneclickitsolution.com/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/example2.oneclickitsolution.com_error.log CustomLog ${APACHE_LOG_DIR}/example2.oneclickitsolution.com_access.log combined <FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost" </FilesMatch></VirtualHost>

Step 16

Check the configuration for errors:

sudo apachectl configtest

 

Expected Output:

$OutputSyntax OK 

Step 17

Enable the virtual host for example1.oneclickitsolution.com:

sudo a2ensite example1.oneclickitsolution.com.conf

Step 18

Enable the virtual host for example2.oneclickitsolution.com:

sudo a2ensite example2.oneclickitsolution.com.conf

Step 19

Restart Apache:

sudo systemctl restart apache2

 

Test Sites

Open the browser and check website 

  1. https://example1.oneclickitsolution.com/info.php

  2. https://example2.oneclickitsolution.com/info.php

Switch PHP Versions on commandline ubuntu

Step 1 

Command : sudo update-alternatives --config php PHP

 

After fire this command you have give option all php version 

Step 2

If I want to enable php7.0 version then just check number in selection and enter number in my terminal it’s

Number 2

PHP

 

Step 3

Now you can check php version

Command : php -v

PHP

 

ConclusionConfigure Nginx for Multiple PHP Versions

Setting Up Virtual Hosts for Nginx

Step 1

Create a virtual host configuration file for example1.oneclickitsolution.com:

sudo nano /etc/nginx/sites-available/example1.oneclickitsolution.com

Add the following content:

server { listen 80; server_name example1.oneclickitsolution.com; root /var/www/html/example1.oneclickitsolution.com; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; }}

 

Step 2

Create a virtual host configuration file for example2.oneclickitsolution.com:

sudo nano /etc/nginx/sites-available/example2.oneclickitsolution.com 

Add the following content:

server { listen 80; server_name example2.oneclickitsolution.com; root /var/www/html/example2.oneclickitsolution.com; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; }}

Step 3

Test the Nginx configuration to ensure there are no errors:

sudo nginx -t 

Step 4

Restart Nginx to apply the changes:

sudo systemctl reload nginx 

Ready to transform your business with our technology solutions? Contact Us today to Leverage Our PHP Expertise. 

   

0