User:Woozle/nginx
The Problem
nginx works with static files (and Ruby), but will not execute PHP files. PHP runs fine from a command line.
On requesting a PHP file (such as http://wiki.makeyourlaws.org/phpinfo.php), here's what seems to be happening:
- nginx receives the request
- nginx forwards it to PHP-FPM
- This can be done one of two ways -- either via a UNIX pipe or via a port. I've tried both; currently using port, I think.
- PHP never receives the request; the log file does not show it.
- nginx somehow receives a success code, and logs the request as successful (http code 200) returning a result of zero bytes.
- The web browser displays a blank page.
Notes
2012-06-21
Looks like the package "spawn-fcgi" may be the part of FastCGI that I need to install, extrapolating from this. Installing it...
2012-06-23
Noticed that this says "Unlike Apache or Lighttpd, Nginx does not automatically spawn FCGI processes. You must start them separately. ... So we simply run php -b 127.0.0.1:9000 manually, or create an init script like so...".
This turns out to be not quite correct; "php" by itself doesn't accept the -b parameter. You have to run php-cgi:
php-cgi -b 127.0.0.1:9000
Unfortunately, I get this:
Cannot bind/listen socket - [98] Address already in use. Couldn't create FastCGI listen socket on port 127.0.0.1:9000
...meaning that something is already listening there. Presumably that's PHP-FPM:
ps aux | grep php woozle 11404 0.0 0.1 3372 752 pts/1 S+ 15:25 0:00 grep --color=auto php root 24667 0.0 0.2 62892 1488 ? Ss Jun18 0:22 php-fpm: master process (/etc/php5/fpm/php-fpm.conf) www-data 24668 0.0 0.3 63180 1984 ? S Jun18 0:00 php-fpm: pool www www-data 24669 0.0 0.3 63180 1996 ? S Jun18 0:00 php-fpm: pool www www-data 24670 0.0 0.3 63180 1996 ? S Jun18 0:00 php-fpm: pool www www-data 24671 0.0 0.3 63180 1996 ? S Jun18 0:00 php-fpm: pool www
So, how can we determine if PHP-FPM is receiving the page request?
Set "log_level = debug" in php-fpm.conf. Evidence would seem to indicate that PHP-FPM is not receiving page requests from nginx. (Reset logging back to default level and restarted PHP-FPM.)
Stopped PHP-FPM. Reloading http://wiki.makeyourlaws.org/phpinfo.php now receives a "502 Bad Gateway" error. Running "php-cgi -b 127.0.0.1:9000" seems to be successful, and now the page just displays "No input file specified.".
From what I can tell, this means that PHP is not receiving the name of the script it is supposed to run, and this is supposed to be passed in the SCRIPT_FILENAME environment variable. Trying to figure out how to do that on the nginx end.
No progress on that... but at least one source suggests that setting PHP's open_basedir option may help. Though this would be a kluge if it worked (and would prevent us from using PHP on the main site), it would also help narrow down the problem. So, added to php.ini:
open-basedir = /home/mylwiki/wiki.makeyourlaws.org
No change. Removed line from php.ini.
This script indicates that nginx is passing only relative filepaths (just the part after the domain name), not the base folder to use in calculating the complete filespec. PHP needs to know the complete filespec.
nginx's SCRIPT_FILENAME FastCGI parameter is set to a filespec which includes $document_root (this is apparently good practice).
$document_root is set by nginx's root directive.
Finally kluged nginx.conf by adding this line after the fastcgi_params include:
fastcgi_param SCRIPT_FILENAME /home/mylwiki/wiki.makeyourlaws.org$fastcgi_script_name;
After that. simple PHP pages would load but MediaWiki still would not. Examination of nginx's error.log revealed sufficient clues to point to the fact that LocalSettings.php had somehow been read-protected for everyone except the owner; making it world-readable fixed that problem and rendered the wiki fully accessible.
2012-06-24
Trying to make the php-cgi process autoloading and auto-recovering when it crashes...
Uninstalled spawn-fcgi because I don't think I'm using it, and I want to try lighttpd per Ryan Castellucci's recommendation. ...except that lighttpd turns out to be a web server, not a CGI process manager, which is a route I assume Sai will not want to go.
Wiki still runs without spawn-fcgi.
Tried PHP-FPM again. It initially was giving a 502 Gateway Error, but then I realized I had reconfigured it to use a socket rather than a port. Change that, and you get a notice in the error log if something else is hogging the port (this is good). Make sure nothing else is (i.e. kill php-cgi if you had it running), then run php-fpm, and all is happy. This suggests that a good way to diagnose php-fpm is to first make sure php-cgi is working.
However, nginx was acting as if the browser was refusing cookies. This turned out to be a problem with the session file storage; phpinfo.php revealed that it was trying to put this in a library folder. I changed /etc/php5/fpm/php.ini to point session.save_path at /tmp instead. then restarted php-fpm.
Background
- Distro: Ubuntu 10.04.4 LTS
nginx was compiled from source; PHP5 was installed from standard Ubuntu packages.
File Locations
- /opt/nginx/ - configuration and logging folders
- /opt/nginx/conf/nginx.conf - master config file
- /opt/nginx/conf/fastcgi.conf - FastCGI config file
- /opt/nginx/conf/scgi_params - Simple CGI config -- presumably not in use
- /etc/php5/fpm/ - PHP-FPM configuration
- /etc/php5/fpm/php-fpm.conf - general config file
- /etc/php5/fpm/pool.d/www.conf - config file to actually edit
Folder Listings
- /opt/nginx
- total 25532
drwx------ 2 nobody root 4096 2012-04-26 18:47 client_body_temp drwxrwxr-x 2 root root 4096 2012-06-09 22:25 conf drwx------ 2 nobody root 4096 2011-12-02 23:20 fastcgi_temp drwxr-xr-x 2 root root 4096 2011-12-02 22:52 html drwxrwxr-x 2 root root 26071040 2012-06-11 06:43 logs drwx------ 2 nobody root 4096 2011-12-02 23:20 proxy_temp drwxrwxr-x 2 root root 4096 2012-04-22 04:41 sbin drwx------ 2 nobody root 4096 2011-12-02 23:20 scgi_temp drwx------ 2 nobody root 4096 2011-12-02 23:20 uwsgi_temp
- /opt/nginx/conf
- total 80
-rw-r--r-- 1 root root 979 2011-12-02 22:52 fastcgi.conf -rw-r--r-- 1 root root 979 2012-04-22 04:41 fastcgi.conf.default -rw-r--r-- 1 root root 909 2011-12-02 22:52 fastcgi_params -rw-r--r-- 1 root root 909 2012-04-22 04:41 fastcgi_params.default -rw-r--r-- 1 root root 2837 2012-04-22 04:41 koi-utf -rw-r--r-- 1 root root 2223 2012-04-22 04:41 koi-win -rw-r--r-- 1 root root 3362 2011-12-02 22:52 mime.types -rw-r--r-- 1 root root 3463 2012-04-22 04:41 mime.types.default -rw-r--r-- 1 root root 6651 2012-06-10 18:50 nginx.conf -rw-r--r-- 1 root root 2685 2012-04-22 04:41 nginx.conf.default -rw------- 1 root root 0 2012-04-23 16:29 nginx.conf.save -rw-r--r-- 1 root root 4208 2012-04-23 16:29 nginx.conf.save.1 -rw-r--r-- 1 root root 544 2011-12-02 22:52 scgi_params -rw-r--r-- 1 root root 544 2012-04-22 04:41 scgi_params.default -rw-r--r-- 1 root root 570 2011-12-02 22:52 uwsgi_params -rw-r--r-- 1 root root 570 2012-04-22 04:41 uwsgi_params.default -rw-r--r-- 1 root root 3610 2012-04-22 04:41 win-utf