Tuesday, March 3, 2015

Multiple versions of PHP through nginx



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).


Answer



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)


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...