Jesus fuck, you lot. Don't ask me shit about systems support / server application config / all that godawful shit that should be consigned to the Systems Support Team (sorry to my mates in this role, but I fucking hate it, and became a dev so I didn't have to do it any more).
Right so ages ago I wrote an article "PHP: getting PHP 5 and PHP 7 running side by side on the same machine (a better way)", and someone recently asked me how do do the same on Nginx. Well it's "slow news night" here in my life: I'm just at the pub in Galway passing time by getting pissed on Guinness (I'm on me fifth pint, and even I can tell the writing here is reflecting that) and writing blog articles, so I had a look at it. It's pretty easy, as it turns out.
Firstly: I am no expert on Nginx. I don't like web servers. I try not to ever have to use them. I know enough about IIS to know it's a pain in the arse but I can do what I need if I have to; and I know enough about Apache to get things working. I don't even know why Nginx exists. I'm guessing the reason is "to annoy Adam, cos it's just one more bloody thing he'll need to know about at some point". Sigh. I got PHP working on Nginx once before ("PHP: getting my dev environment running on Nginx instead of Apache"), and other than that have converted some rewrite rules from Apache to Nginx (man: does Nginx suck compared to Apache for those!).
This is a different laptop from the one I did that other exercise on, so whilst I had Nginx on here (for the rewrite exercise, which was work-related and this is my work laptop), I decided to start from scratch. I deleted what I had installed (where for Nginx "installed" means "unzipped").
Here's what I did:
- grabbed the latest Windows Nginx download from their site. I just googled "nginx windows download" to find that.
- Unzipped it. Relocated it to my apps dir: c:\apps\nginx
- Stopped Apache (which listens on port 80), and instead started Nginx (just run nginx.exe from the dir above). By default it listens on localhost and port 80.
- Browse to http://localhost/ and verified the Nginx default index.html page was served. OK, so it works as a baseline. Always test the baseline before proceeding with any customisations of things.
- Set-up a coupla test hosts in my hosts file (C:\windows\system32\drivers\etc\hosts... make sure to start yer text editor as an admin, otherwise you won't be able to save it):
127.0.0.1 php5.nginx.local
127.0.0.1 php7.nginx.local
I foresaw that to run both PHP5 and PHP7, Nginx is gonna need to differentiate between which one wants to use, and using different host names seemed to make sense and be easy. - I edited my nginx.conf file (in the conf subdir of the one above) to know about these two hosts:
http { # [...] server { listen 8800; server_name php5.nginx.local; root c:/src/php/php.local/www; # [...] } server { listen 8800; server_name php7.nginx.local; root c:/src/php/php.local/www; # [...] } }
Where I've elided stuff, it's the same as it was before. The chief considerations here are:- having a server for both PHP5 and PHP7;
- having them listen on each of those two new hosts I set up;
- setting the root to be where my PHP code is. This is the same for both in this case, as I want to serve exactly the same code via both 5 and 7;
- oh and I'm listening on port 8800 as I don't want Nginx to interfere with my normal Apache install (I'll be sticking with Apache after this experiment, thanks).
- When one runs Nginx from the console it hogs the prompt, so to stop it one needs to run another console and call nginx -s stop. We need to do this to test the config changes. So I did that, and used the other console to start it again.
- I browse to each of http://php5.nginx.local:8800 and http://php7.nginx.local:8800 to test they were working. They were running, but giving a 403 cos I didn't have an index.html in that directory, nor did I have directory browsing switched on (which for my test code I do, as it makes finding stuff easier).
- I told Nginx to allow directory browsing for each of the server configs:
location / { index index.html index.htm; autoindex on; }
Do not do this in production. Well: don't do anything I say in production. - I cycled Nginx again and tested both hosts:
Index of /
../ code-coverage-reports/ 15-Nov-2016 13:33 - community/ 30-Jun-2016 11:47 - experiment/ 15-Nov-2016 08:47 - library/ 30-Jun-2016 11:47 - stackoverflow/ 30-Jun-2016 11:47 - deleteme.php 30-Jun-2016 11:47 233 gdayWorld.html 30-Jun-2016 11:47 11 gdayWorld.php 30-Jun-2016 11:47 50 phpinfo.php 30-Jun-2016 11:47 21 utf8.html 21-Dec-2016 08:21 133
So that's all good except for me not having deleted that file that perhaps I meant to, a while back ;-) - Next I just followed the instructions from me other blog article, getting PHP to listen out to traffic coming in from Nginx (the code below is in a batch file):
start C:\bin\RunHiddenConsole.exe C:\apps\php\5\5\php-cgi.exe -b 127.0.0.1:8550 start C:\bin\RunHiddenConsole.exe C:\apps\php\7\1\php-cgi.exe -b 127.0.0.1:8710Note that each of them is listening on a different port. -
And then tell Nginx to pass PHP requests across to PHP:
server { # [...] location ~ \.php$ { fastcgi_pass 127.0.0.1:8710; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # [...] }
That's the one for PHP7, but the PHP5 one is the same except for the port. - I restart Nginx again, and this time hit phpinfo.php on each host:
{i'd show you proof, but BlogSpot is refusing to let me put two images inline in an <ol> list, it seems. Trust me... it works). - Hurrah!
- The last thing I tried to do is to wrap my batch file that starts Nginx into a service, using RunAsService.exe, but this is my work laptop and it's locked-down too tight for me to run that. But... well... the batch file works.
Sorry this one is a bit scrappy but... well... I'm drunk.
Righto.
--
Adam