PHP is traditionally used with Apache, but can also be fronted by nginx. Here are some notes that I took while setting up PHP5/FastCGI behind nginx. My main source of inspiration was this howtoforge article. My OS flavor is Ubuntu 9.04 32-bit, but the same procedure applies to other flavors.
1) Install PHP5 and other required packages
# apt-get install php5 php5-cli php5-cgi php5-xcache php5-curl php5-sqlite libpcre3 libpcre3-dev libssl-dev
2) Configure xcache
I added the lines in the following gist to the end of /etc/php5/cgi/php.ini: http://gist.github.com/424172
3) Install spawn-fcgi, which used to be included in the lighttpd package, but now can be downloaded on its own from here.
# tar xvfz spawn-fcgi-1.6.3.tar.gz; cd spawn-fcgi-1.6.3; ./configure; make; make install
At this point you should have /usr/local/bin/spawn-fcgi installed. This wrapper needs to be launched at some point via a command line similar to this:
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
What this does is it launches the php5-cgi process which will listen on port 9000 for php requests.
The original howtoforge article recommended writing an init.d wrapper for this command. I used to do this, but I noticed that the php5-cgi process dies quite frequently, so I needed something more robust. Hence...
4) Configure spawn-fcgi to be monitored by supervisor
I installed supervisor via:
# apt-get install python-setuptools
# easy_install supervisor
# echo_supervisord_conf > /etc/supervisord.conf
Then I added this section to /etc/supervisord.conf:
[program:php5-cgi]
command=/usr/local/bin/spawn-fcgi -n -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgi
redirect_stderr=true ; redirect proc stderr to stdout (default false)
stdout_logfile=/var/log/php5-cgi/php5-cgi.log
stdout_logfile_maxbytes=10MB ; max # logfile bytes b4 rotation (default 50MB)
Note that I added the -n flag to the spawn-fcgi command line. This keeps the process it spawns (/usr/bin/php5-cgi) in the foreground, so that it can be daemonized and monitored properly by supervisor.
I use this init.d script to start/stop supervisord. Don't forget to 'chkconfig supervisord on' so that it starts at boot time.
Per the configuration above, I also created /var/log/php5-cgi and chown-ed it www-data:www-data. That directory will contain a file called php5-cgi.log which will capture both the stdout and stderr of the php5-cgi process.
When I started supervisord via 'service supervisord start', I was able to see /usr/bin/php5-cgi running. I killed that process, and yes, supervisor restarted it. May I just say that SUPERVISOR ROCKS!!!
5) Install and configure nginx
I prefer to install nginx from source.
# tar xvfz nginx-0.8.20.tar.gz; cd nginx-0.8.20; ./configure --with-http_stub_status_module; make; make install
To configure nginx to serve php files, add these 'location' sections to the server listening on port 80 in /usr/local/nginx/conf/nginx.conf:
location / {
root /var/www/mydocroot;
index index.php index.html;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/mydocroot$fastcgi_script_name;
}
(assuming of course your root document directory is /var/www/mydocroot)
Now nginx will foward all request to *.php to a process listening on port 9000 on localhost. That process is php5-cgi, as we are aware by now.
That's about it. Start up nginx and you should be in business.
Subscribe to:
Post Comments (Atom)
Modifying EC2 security groups via AWS Lambda functions
One task that comes up again and again is adding, removing or updating source CIDR blocks in various security groups in an EC2 infrastructur...
-
Here's a good interview question for a tester: how do you define performance/load/stress testing? Many times people use these terms inte...
-
Update 02/26/07 -------- The link to the old httperf page wasn't working anymore. I updated it and pointed it to the new page at HP. Her...
-
I've been using dnspython lately for transferring some DNS zone files from one name server to another. I found the package extremely us...
6 comments:
gonna have to take a look at supervisord.
Chris -- yes, do look at supervisor. Best thing since sliced bread.
This is an old method. The recommended way to run php in fastcgi mode with nginx is to incorporate the php-fpm patches - php-fpm is far superior to spawn-fcgi.
The next version of PHP will come officially bundled with FPM.
I will.. This is timely too as I was looking to have wordpress with nginx and sqlite.
see also:
http://elasticdog.com/2008/02/howto-install-wordpress-on-nginx/
See also http://www.howtoforge.com/installing-php-5.3-nginx-and-php-fpm-on-ubuntu-debian
Where the guy explains how to use php-fpm
Post a Comment