Skip to main content

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)


Comments

Popular posts from this blog

linux - iDRAC6 Virtual Media native library cannot be loaded

When attempting to mount Virtual Media on a iDRAC6 IP KVM session I get the following error: I'm using Ubuntu 9.04 and: $ javaws -version Java(TM) Web Start 1.6.0_16 $ uname -a Linux aud22419-linux 2.6.28-15-generic #51-Ubuntu SMP Mon Aug 31 13:39:06 UTC 2009 x86_64 GNU/Linux $ firefox -version Mozilla Firefox 3.0.14, Copyright (c) 1998 - 2009 mozilla.org On Windows + IE it (unsurprisingly) works. I've just gotten off the phone with the Dell tech support and I was told it is known to work on Linux + Firefox, albeit Ubuntu is not supported (by Dell, that is). Has anyone out there managed to mount virtual media in the same scenario?

hp proliant - Smart Array P822 with HBA Mode?

We get an HP DL360 G8 with an Smart Array P822 controller. On that controller will come a HP StorageWorks D2700 . Does anybody know, that it is possible to run the Smart Array P822 in HBA mode? I found only information about the P410i, who can run HBA. If this is not supported, what you think about the LSI 9207-8e controller? Will this fit good in that setup? The Hardware we get is used but all original from HP. The StorageWorks has 25 x 900 GB SAS 10K disks. Because the disks are not new I would like to use only 22 for raid6, and the rest for spare (I need to see if the disk count is optimal or not for zfs). It would be nice if I'm not stick to SAS in future. As OS I would like to install debian stretch with zfs 0.71 as file system and software raid. I have see that hp has an page for debian to. I would like to use hba mode because it is recommend, that zfs know at most as possible about the disk, and I'm independent from the raid controller. For us zfs have many benefits,

apache 2.2 - Server Potentially Compromised -- c99madshell

So, low and behold, a legacy site we've been hosting for a client had a version of FCKEditor that allowed someone to upload the dreaded c99madshell exploit onto our web host. I'm not a big security buff -- frankly I'm just a dev currently responsible for S/A duties due to a loss of personnel. Accordingly, I'd love any help you server-faulters could provide in assessing the damage from the exploit. To give you a bit of information: The file was uploaded into a directory within the webroot, "/_img/fck_uploads/File/". The Apache user and group are restricted such that they can't log in and don't have permissions outside of the directory from which we serve sites. All the files had 770 permissions (user rwx, group rwx, other none) -- something I wanted to fix but was told to hold off on as it wasn't "high priority" (hopefully this changes that). So it seems the hackers could've easily executed the script. Now I wasn't able