5

EDIT: For future reference

I am running Ubuntu 14.10 with a LEMP stack using PHP 5.5.12. I have a number of legacy WordPress sites that require PHP 5.3.3 alongside some WP sites that use a fairly recent version of PHP, all running on nginx on my local machine.

My hands are tied in respect to virtual machines and sandboxes, all I can play with is nginx, hence this question. I understand peoples security concerns but I need these sites to run locally so I can test for broken features as I update them to the latest PHP / WP versions.

I want to have nginx run the correct version of PHP (using php-fpm) depending on the WordPress site. According to another SF question, one way to achieve this is to have the different PHP versions running on separate ports / sockets and configure the nginx server blocks to use the respective port / socket.

I have compiled PHP 5.3.3 manually to include php-fpm but that is the furthest I have got.

Effectively, I want someone to explain in a little more detail this answer. I can't quite figure out how to "run each version of php-fpm on a different port (or socket)" or "configure the appropriate port in your fastcgi_pass parameter in your nginx server block".

One of my server blocks looks like this for reference

server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite1;
    index index.php index.html index.htm;

    server_name local.testsite1.com;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

EDIT:

I set up each site using a separate server block in a separate file, sym linked between /var/nginx/sites-available/testsite1 and /var/nginx/sites-enabled/testsite1. So var/nginx/sites-available contains;

 testsite1
 testsite2
 testsite3
 ...

So ideally I was wondering if something like what is below is possible (since this is similar to how apache can be set up with different PHP versions)

testsite1 - running an older version of PHP

 server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite1;
    index index.php index.html index.htm;

    server_name local.testsite1.com;

    ...settings to use older PHP version...

    ...remaining nginx settings...
}

testsite2 - running current version of PHP

 server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html/testsite2;
    index index.php index.html index.htm;

    server_name local.testsite2.com;

    ...settings to use currrent PHP version (if needed)...

    ...remaining nginx settings...
}

Is this possible? The reason I ask is that I would rather avoid renaming all my php files to php2 in order to run (making version control a pain). I don't mind editing the nginx.conf file or whatever steps it takes, so long as I don't have to rename files.

I also believe I need to use sockets (fastcgi_pass unix:/var/run/php5-fpm.sock;) over ports due to WordPress (although I'm open to all suggestions).

myol
  • 181
  • 2
  • 2
  • 12
  • 2
    Sorry, but having to read multiple external resources to maybe understand what your problem might be is not acceptable. – Sven Feb 25 '15 at 19:14
  • 4
    No, you don't want to run PHP 5.3. That's end of life, insecure and irresponsible. – Michael Hampton Feb 25 '15 at 19:45
  • I have completely revised the question. – myol Feb 25 '15 at 20:31
  • WordPress runs perfectly well on the latest versions of PHP. There is no need to keep it on an ancient outdated insecure version. – Michael Hampton Feb 25 '15 at 20:38
  • The decision is out of my hands. – myol Feb 25 '15 at 20:54
  • 1
    If you're running on a version of WordPress that breaks in PHP 5.4+, not only is your PHP insecure, but your WordPress is. There are dozens of known exploits for a WP install that old, and your server **will** get hacked, rapidly, if it is ever exposed to the public internet. If the decision is out of your hands, do your professional duty and go find the right hands to yell at. – ceejayoz Feb 25 '15 at 21:02
  • Ok I've clarified my situation again. – myol Feb 25 '15 at 22:21

2 Answers2

11

Okay, you want to run multiple PHP version through nginx, the configure file should includes the specific path where you put your PHP scripts in different version or extension name.

However, I would like to explain the SF question link given in your post.

That answer is giving you a way to modify the conf of your nginx, which assumes that questioner has background in nginx.

By giving a specific port in conf, nginx will make the script executed through that FastCGI channel.

Let's pretend you have installed different PHP version in your server, and you have modified the port in php-fpm.conf.

You wish all php file executed in version 5.5.0 and php2 file executed in 5.6.0.

Example config of nginx as follows,

    listen       8080;
    server_name  localhost;

    root   html;

    location / {
        index  index.php index.php2;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }

    location ~ \.php2$ {
        fastcgi_pass   127.0.0.1:9001;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  "${document_root}${fastcgi_script_name}";
        include        fastcgi_params;
    }

In this case, php is processed through port 9000 and php2 goes to 9001

This is jsut a simple example, for advanced you can make two different folders to store php files, for example, /home/php55 and /home/php56, then edit your nginx.conf.

FOR YOUR EDITED QUESTION

If you want to try adding different servers to process the scripts in different version, sure you can do that, because nginx can be a load balancer, it can deal with this kind of problem as well.

First Application

server{
listen 80;
server_name php1.localhost; 

root   /home/www/php5.5;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9002; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}

Second Application

server{
listen 80;
server_name php2.localhost; 

root   /home/www/php5.6;

location / {
  index  index.php;
}

location ~ \.php$ {
  fastcgi_pass   127.0.0.1:9004; (You can change it to your private IP in the future)
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
}
}

By using the above configuration, you can easily switch the different version result of PHP script, moreover, you can use NFS(if you are in a *nix environment) to mount the disk, in this case, you will no need to put files into two folders manually.

As for Fastcgi passing method, I suggest using PORT instead of Socket.

We all know Unix Socket has a better performance due to TCP port uses the whole network stack even on the same machine.

However, the time you save is very little, moreover, you may face this problem when using Socket in a high traffic time,

connect() to unix:/var/run/php5-fpm.sock failed or apr_socket_recv: Connection reset by peer (104)

In addition, TCP port can give you a faster way to manage if you put nginx and php-fpm in separated servers.

I'm using small laptop to edit this post, so codes are not pretty but I tried....

EDITED

Remember to modify your /etc/hosts to match your hostname (server_name in nginx.conf)

Mark
  • 564
  • 1
  • 4
  • 11
  • Thanks Mark, would you mind elaborating on editing the nginx.conf to point to the separate PHP versions? There is no mention of path names even to the existing PHP in my nginx.conf – myol Feb 27 '15 at 10:35
  • 1
    Sure, but you should make this as a separated question so people who have same kind of problem can easily find out answer from here. Post a new question and I will go later (Not home at the moment) – Mark Feb 27 '15 at 10:58
  • Perhaps I have misconveyed what I am trying to achieve (I have a very basic knowledge of networking), so I have updated my original question to further clarify. If you feel that a new question is still in order I will make one (although I am unsure how to word it), but I think following site guidelines editing your answer would be better. – myol Feb 27 '15 at 11:29
  • As a note, I can assign a bounty in 7h as I appreciate your continued help in this matter. – myol Feb 27 '15 at 11:30
  • 1
    Sincerely suggest you to make PHP-FPM and Nginx servers to be separated, this can help you manage different service in an efficient way, AND, a better performance (Using multiple FPM servers by setting upstream in nginx config in the future). – Mark Feb 27 '15 at 12:30
  • That sounds more like what I am after. Forgive my ignorance, I didn't realise PHP-FPM acted as a server. From what I gather from your comment; I can configure various PHP-FPM 'servers' to use separate versions of PHP and let nginx choose the correct PHP-FPM service depending on the site using the nginx.conf? – myol Feb 27 '15 at 12:37
  • 1
    Surely you can, and you can also create a folder called **vhosts** and making files such as **web1.conf**, **web2.conf** ...etc under that folder, and put `server` block into it. The last step just adding `include vhosts/*.conf;` in the `nginx.conf` before the last `}`. This will make you manage different sites easier. PS. Do you have email? It's great for me to make friends on SF. – Mark Feb 27 '15 at 12:42
  • @myol, does my solution work? – Mark Feb 27 '15 at 15:17
  • I will be able to look at the issue in a few hours. However, from the sounds of your sincere suggestion, I think the separation of nginx and multiple PHP-FPM servers each running a different version of PHP as you suggest is a much better approach. I have created another question as this thread has become saturated: http://serverfault.com/questions/671917/nginx-and-different-versions-of-php-fpm-php – myol Feb 27 '15 at 15:45
  • How do you modify the port in php-fpm.conf ? – JoeTidee Jun 24 '16 at 14:49
6

For testing purposes, I wanted to configure my nginx to use different php versions under different locations in the same server section. Finally it worked with this:

#Folder to run some tests with the new php-7
location ~ "^\/test_php7.0.3\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php7-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#Folder to run some tests with a custom compiled version of php5.6
location ~ "^\/test_php5.6.18\/.*\.php$" {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php56-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

#The default php version:
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

Hope this helps someone else :)

juanra
  • 169
  • 1
  • 2
  • In my case, when I installed php 5.6 the .sock file is nto created - how do I get around this? – JoeTidee Jun 24 '16 at 14:50
  • It seems an error with the php-fpm. It depends on the system, but I'd take a look at /var/log/php5-fpm.log while the php-fpm service is (re)starting. – juanra Jun 25 '16 at 09:04
  • Perhaps, both php7 and php5 are trying to use the same .sock file, so take a look also to the /etc/phpX/fpm/pool.d/www.conf file (the location of this file can vary for your installation), and look for a line that looks like "listen = /var/run/XXX.sock", obviously each php service should have its own file... – juanra Jun 25 '16 at 09:12
  • Thanks, found the line in the file and the .sockfile now exists. – JoeTidee Jun 28 '16 at 09:26
  • Exactly, what Im looking for ;-) – Vincent Guesné Aug 13 '17 at 15:17