Вы находитесь на странице: 1из 8

8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.

04

 HOME UBUNTU 18.04 TUTORIALS FUNNY TOOLS FEEDBACK SUBMIT ARTICLE

ABOUT US

How to Install  Search Now

Multiple PHP Version


with Nginx on Ubuntu
18.04 & 16.04
Written by Rahul, Updated on June 27, 2018

 Nginx  nginx, PHP, php-fpm

POPULAR POSTS
Generally, web hosting manager used a separate
How to Install Apache 2.4
server for each PHP version application
& PHP 7.3 on Amazon Linux
deployment. Which increases the hosting cost.
Alternatively, you can run multiple Docker How to Install Angular on
Ubuntu 18.04 & 16.04
containers for multiple PHP versions.
How to Install MySQL 5.7 on
This tutorial helps you with the installation and Amazon Linux
configuration of two VirtualHost on Nginx web
How To Install Wine 4 on
server with different PHP versions. First
Debian 10 (Buster) Linux
VirtualHost will work with PHP 5.6 and another

VirtualHost will run with PHP 7.1. So just go How to Rename a Column
Name in SQL Server
Wethrough this
use cookies tutorial.
to ensure that You canyou
we give also use experience
the best more than on our website. If you continue to use this
Database
site we will assume that you are happy with it.
two PHP versions with Nginx as required but this
Ok No
tutorial covers two only.
https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 1/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

PHP Installation
For the installation of PHP versions, we use the
PPA maintained here. Use the below couple of
commands to add the PPA to your system.

$ sudo apt install python-software-properties


$ sudo add-apt-repository ppa:ondrej/php

For this tutorial, I used the PHP 5.6 and PHP 7.2
to configure with Nginx web server. To use the
multiple PHP versions, we will use PHP FPM and
FastCGI. Let’s install the following packages on
your system.

$ apt update
$ sudo apt install php5.6 php5.6-fpm
$ sudo apt install php7.2 php7.2-fpm

You may also need to install additional PHP


modules. For the tutorial only above packages
are required.

After installation, php-fpm services will be


started automatically. Use the following
commands to make sure both services are
running.

$ sudo systemctl status php5.6-fpm


$ sudo systemctl status php7.2-fpm

Recommended: How to Enable/Disable PHP


Modules on Ubuntu

Nginx Installation
We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.

Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 2/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

Install Nginx web server available in official


Ubuntu repository. Launch terminal on your
system or login with ssh for remote systems.
Execute the following commands to install the
latest available version of Nginx web server.

$ sudo apt update


$ sudo apt install nginx

Nginx Configuration
Get ready for the configuration of websites in
Nginx server. For the testing purpose, I am
configuring two websites to work with two
different-2 PHP versions. First, create two
directories on your server.

$ sudo mkdir /var/www/php56


$ sudo mkdir /var/www/php72

Now, create and index.php containing the


phpinfo() function.

$ echo "<?php phpinfo(); ?>" > /var/www/php56/index.p


$ echo "<?php phpinfo(); ?>" > /var/www/php72/index.p

After that create server blocks for both sites on


Nignx. The latest version of Nginx keeps the
server block configuration files under
/etc/nginx/sites-available directory. Create a file
for the first virtual host and edit in your favorite
text editor.

$ sudo
We use vimto/etc/nginx/sites-available/php56.example.c
cookies ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.
Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 3/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

Add the following content. Make sure to use


correct ServerName and directory path
according to your setup. This website is
configured to work with PHP 5.6.

# Application with PHP 5.6


#
server {
listen 80;

root /var/www/php56;
index index.php;
server_name php56.example.com;

location ~* \.php$ {
# With php-fpm unix sockets
fastcgi_pass unix:/var/run/php/php5.6
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $d
fastcgi_param SCRIPT_NAME $f
}
}

Similarly, create a second VirtualHost


configuration file to work with PHP 7.2. Edit
configuration file in text editor:

$ sudo vim /etc/nginx/sites-available/php72.example.c

Add the following content to file with proper


ServerName and DocumentRoot.

# Application with PHP 7.2


#
server {
listen 80;

root /var/www/php72;
index
We use cookies index.php;
to ensure that we give you the best experience on our website. If you continue to use this
server_name php72.example.com;
site we will assume that you are happy with it.
Ok No
location ~* \.php$ {
https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 4/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

# With php-fpm unix sockets


fastcgi_pass unix:/var/run/php/php7.2
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $d
fastcgi_param SCRIPT_NAME $f
}
}

All right, You have created both websites in your


Nginx. But they are still not active. Nginx keeps
active sites under /etc/nignx/sites-enabled
directory. You can simply create a symbolic link
for both config files to this directory or use
below command to do the same.

$ sudo ln -s /etc/nginx/sites-available/php56.example
$ sudo ln -s /etc/nginx/sites-available/php72.example

After making all the changes restart Nginx to


reload new settings changes.

$ sudo systemctl restart nginx.service

Your setup has been completed now. Go to next


step to test your setup.

Test Setup
All done. You can access both sites in your
favirote web browser . You will see that
php56.example.com shows the version PHP 5.6
and php72.example.com is showing the PHP 7.2
as the configuration. 

We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.

Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 5/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

Congratulation’s, You system is ready to host


websites with different PHP versions. Happy
hosting.

SHARE IT!     

RAHUL   

I, Rahul Kumar am the founder and


chief editor of TecAdmin.net. I am a Red
Hat Certified Engineer (RHCE) and
working as an IT professional since 
2009..
We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.

Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 6/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

RELATED POSTS

How To Install Ioncube for PHP in Debian Linux


July 16, 2019

How to check if string contains speci c word in PHP


June 20, 2019

How to Install Apache 2.4 & PHP 7.3 on Amazon Linux


June 11, 2019

How To Generate a Random String using PHP


March 8, 2019

How To Install PHP on Debian 10 (Buster)


January 26, 2019

5 COMMENTS

NURLAN  REPLY TO NURLAN


February 19, 2019 at 11:58 am
Thank you!! works for me.

NURLAN  REPLY TO NURLAN


February 19, 2019 at 11:38 am
how to open multiple php versions if i dont have dns…
opening only one default page.

MORPHEUS  REPLY TO MORPHEUS


October 12, 2018 at 9:57 pm
Nice tutorial. To extend it a little bit, how can we have
two versions of php also in the cli? if you use
composer, for example, the cli will say default version
php7.2 but actually you need for that certain project to
use php5. An answer is super appreciated.

RAHUL K.  REPLY TO RAHUL


October 15, 2018 at 7:49 am 
You can define any php version with
composer command as :
We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.
/usr/bin/php5.6 /usr/local/bin/composer help
Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 7/8
8/9/2019 How To Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

/usr/bin/php7.2 /usr/local/bin/composer help

PAOLO  REPLY TO PAOLO


October 5, 2018 at 1:34 pm
Thanks excellent guide..I was looking for a long time
In case of memcached or opcache do I have to make
any changes?
Hello

LEAVE A REPLY

COMMENTS *

NAME * EMAIL * WEBSITE

Saya bukan robot


reCAPTCHA
Privasi - Persyaratan

SUBMIT

Copyright © 2013-2019 TecAdmin.net. All Rights Reserved. This site uses cookies. By using this website you
agree with our term and services

We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.
Ok No

https://tecadmin.net/install-multiple-php-version-nginx-ubuntu/ 8/8

Вам также может понравиться