This post will go over the steps you need to get nginx running with php5. I am going to use a list to show the steps to take, please leave a comment if you see something that needs to be corrected or just want to leave feedback.
Installation
- Update apt:
sudo apt-get update
- Install nginx:
sudo apt-get install nginx
- Install php-cgi for use with mysql:
sudo apt-get install php5-cgi php5-mysql
- We need a binary that comes with lighttpd to launch the php5 cgi:
sudo apt-get install lighttpd
- Now remove lighttpd from startup since we will be using nginx:
sudo update-rc.d -f lighttpd remove
Configuration of nginx,startup scripts,fastcgi
- Create a bash script that will spawn the php fcgi handlers:
sudo vim /usr/bin/php-fastcgi
- and enter the following:
#!/bin/bash
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 10005 -u www-data -g www-data -f /usr/bin/php-cgiMake sure to set www-data to the user and group that you want php running as.
- Make the script executable:
sudo chmod +x /usr/bin/php-fastcgi
- Now lets at create an init script that will execute the above script on boot (this script is from a blog that I can’t remember the name of):
sudo vim /etc/init.d/init-fastcgiand enter the following
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php-cgi
RETVAL=$?
;;
restart)
killall -9 php-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
- Make the script executable:
sudo chmod +x /etc/init.d/init-fastcgi
- Add the script to run at bootup:
sudo update-rc.d init-fastcgi defaults
- Now we will begin configuring nginx to proxy php request. First we need to setup a include file that will hold some fastcgi_param’s.
sudo vim /etc/nginx/fastcgi.confand enter
#fastcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#To use Nginx + Virtual Host + PHP you should ommit the SCRIPT_NAME
#variable in order for PHP to choose the correct DOCUMENT_ROOT
#fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200; - Edit the server conf and tell it where to proxy requests to:
sudo vim /etc/nginx/sites-enabled/default
- In the server section add the following:
location ~ .*\.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:10005;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/xxx/public_html/domian/public/$fastcgi_script_name;
}
*Update SCRIPT_FILENAME to point to your doc root* - Update index list to enable index.php: *Thanks Brian*
index index.html index.htm index.php;
- Restart nginx and start the php cgi handlers:
sudo /etc/init.d/nginx restart
sudo /etc/init.d/init-fastcgi start

2 Trackbacks
[...] Ubuntu Nginx and PHP5 – Frank Kumro [...]
[...] Quick note for myself and anyone else looking to replace lighttpd or apache2 with nginx for serving php (ala wordpress): http://frankkumro.com/2009/01/03/ubuntu-nginx-and-php5/ [...]