How to install second php version on Nginx and Ubuntu

Let’s see how can we install second php version (in this case 5.6) on LEMP stack

If you have docker like I do, the best way is to fire up another container with second version of php. I’m on dev server so I’ll just show you how to do it assuming that you don’t have docker. Container OS is Ubuntu 16.4 and already has php 7.1

1. Add repo and update:

2. Install php-fpm (5.6) and dependencies:

3. Verify:

A doua versiune de php comanda php5.6-v

4*. Not required but you should do it.(cgi.fix_pathinfo=0 in php.ini):

search with ctrl+w cgi.fix_pathinfo and make sure you set it to 0

5. Setup php pool (normally this should be done for every website)

Write down nginx username. Usually is www-data, in this case is nginx.

Around first lines make sure you change [www] with [yoursite]

setati pool name pt php
place user and group(the one that you wrote down from top command. Usually the group is the same as user but it depends, you should check it if it doesn’t work).

now we’ll go to listen variable. In this case I’ve used socket. The best practice is to rename socket after php version and sitename (Remember socket location, we’ll use it in nginx. I choose to setup mine in the same directory as I did for php7)

After we need to modify linsten.owner, listen.group and to uncomment listen.mode (listen.owner and listen.group must be the same as above)

listen socket si owner group

6. Edit yorusite.conf for nginx.(i’m guessing that site is enabled from /etc/nginx/nginx.conf)

search in that file fastcgi_pass and replace it with php socket location from abovefastcgi socket ningx
Don’t forget to restart nginx but make sure you check it first with

7*. Optional. I have supervisor and I need for second php version to be persistent.

This is only for those who have supervisor

and at the end place

supervisor pt php5
to learn which binary to use

and you can use that on command=. You can also create supervisor directory if you don’t have it (for logs) with mkdir -p /var/log/supervisor

There might be different paths on your site. The idea is simple: install php, name your socket and make sure that nginx can access it and configure your site to take that socket.

Last modified: August 5, 2019