Ubuntu Configure Nginx with Multiple PHP Versions




1- Install Nginx to install nginx
sudo apt update
sudo apt install nginx
make sure that you dont have anything running on port 80, otherwise you will need to either configure nginx to another port b4 running or disable any active app server running on port 80 to disable apache2 for example
sudo systemctl disable apache2
enable nginx to be started automatically
sudo systemctl enable nginx
start nginx and check its status
sudo systemctl start nginx
systemctl status nginx
if status shows that nginx is running then your good to go


2- Install PHP Versions

the defualt version of php is always the newest and you can install it by running the below command:
sudo apt-get install php php-fpm
for older versions you can get it from: A: old version rebositoy to add and install using apt-get
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php5.6 php5.6-fpm
B: install fro source downloaded from php.net archive "google it (:"


3- Configure Hosts for each php version
sudo nano /etc/hosts
add multiple hosts each one will point to a version of php, below is a example



4- Configure PHP-FPM for each version open the config file for fpm
/etc/php/{version-number}/fpm/pool.d/www.conf
for php 7.1 it would be
/etc/php/7.1/fpm/pool.d/www.conf
now find the line where it has
listen = /run/php/php{php-version}-fpm.sock
comment it out by a hash and enter this line below
listen = 127.0.0.1:9000
Note: the ip address must match the /etc/hosts input you did earlier for the desired host now do the same to all php version you want to run and keep note of all the input, at the end you should have a list of php-fpms addreses like this for example:
listen = 127.0.0.1:9000 for php 7.1 | matching host php-7-1.co
listen = 127.0.0.2:9000 for php 7.0 | matching host php-7-0.co
listen = 127.0.0.3:9000 for php 5.6 | matching host php-5-6.co
listen = 127.0.0.4:9000 for php 4.6 | matching host php-4-6.co


5- Configure Nginx
sudo nano /etc/nginx/sites-available/default
- uncomment the block that start with:
location ~ \.php$ {
- comment out the line that start with:
fastcgi_pass unix:/var/run/php/;
- un comment or add this line inside "location ~ \.php$"
fastcgi_pass 127.0.0.1:9000;
- copy and past the server{} code for as many as php version you have - change each server block config following detail to match your php-fpm and host config
- listen {ip}:{port} default_server
- server_name {ip} {domain}
- fastcgi_pass {ip}:{port}


6- Restart All Services
sudo systemctl restart nginx
sudo systemctl restart php{vrsion}-fpm ( repeat for all versions)

Done!

Comments