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.
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.
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update -y
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
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:
Check PHP 7.4 FPM status:
sudo systemctl status php7.4-fpm
Start PHP 8.1 FPM service:
sudo systemctl enable php8.1-fpm
sudo systemctl start php8.1-fpm
Check PHP 8.1 FPM status:
sudo systemctl status php8.1-fpm
Enable Apache modules:
sudo a2enmod actions fcgid alias proxy_fcgi
Restart Apache:
sudo systemctl restart apache2
Create folders in /var/www/html:
sudo mkdir /var/www/html/example1.oneclickitsolution.com
sudo mkdir /var/www/html/example2.oneclickitsolution.com
Create info.php file and add code:
sudo nano /var/www/html/example1.oneclickitsolution.com/info.php
Add the following line:
<?php phpinfo(); ?>
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
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>
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>
Check the configuration for errors:
sudo apachectl configtest
Expected Output:
$
Output
Syntax OK
Enable the virtual host for example1.oneclickitsolution.com:
sudo a2ensite example1.oneclickitsolution.com.conf
Enable the virtual host for example2.oneclickitsolution.com:
sudo a2ensite example2.oneclickitsolution.com.conf
Restart Apache:
sudo systemctl restart apache2
Open the browser and check website
https://example1.oneclickitsolution.com/info.php
https://example2.oneclickitsolution.com/info.php
Switch PHP Versions on commandline ubuntu
Step 1
Command : sudo update-alternatives --config 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
Step 3
Now you can check php version
Command :
php -v
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