There are lots of tutorials out there to setup PHP with Nginx. I'm probably just retarded, but none of them got me to a working system. A detail left out of one, a configuration file left out of another, etc.
What follows is exactly what I did to get Nginx to serve PHP5 on a fresh Slicehost VPS slice running Ubuntu 7.10. Hopefully it will help someone else.
NOTE: The following commands assume that you have already setup a user account with sudo privileges and are executing the commands as that user, NOT as root.
Update your system and install basic tools required
> sudo aptitude update -y
> sudo locale-gen en_GB.UTF-8
> sudo /usr/sbin/update-locale LANG=en_GB.UTF-8
> sudo aptitude safe-upgrade -y
> sudo aptitude full-upgrade -y
> sudo aptitude install build-essential -yInstall MySQL> sudo aptitude install mysql-server mysql-client libmysqlclient15-dev -yInstall PHP5> sudo aptitude install php5-cli php5-cgi php5-mysql php5-xcache -yNote: Xcache is installed at this point and available for you to setup, but by default is not turned on.Install Nginx
> sudo aptitude install nginx -yGo to your IP address and you should now recieve the message "Welcome to nginx!"FastCGI Parameter Configuration
We will place all of our fastcgi parameters in a single file which we can include in as necessary.
> sudo vim /etc/nginx/fastcgi_paramsThis will be a new empty file, add the following and save: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_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 GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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;Nginx Configuration> sudo vim /etc/nginx/sites-available/defaultThis is a pre-existing file. Find the part that looks similar to the following and edit it as so and save:location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}We need to remember to restart Nginx so that it picks up our configuration changes.> sudo /etc/init.d/nginx stop
> sudo /etc/init.d/nginx startSpawn-fcgiWe still need a script to start our fast cgi processes. We will extract one from Lighttpd.
> mkdir ~/sources
> cd ~/sources
> wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2
> tar -xvjf lighttpd-1.4.18.tar.bz2
> cd lighttpd-1.4.18
> ./configure
> make
> sudo cp src/spawn-fcgi /usr/bin/spawn-fcgiLet's get automated!> sudo touch /usr/bin/php-fastcgi
> sudo vim /usr/bin/php-fastcgiThis is a new empty file, add the following and save:#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -f /usr/bin/php5-cgiNext...> sudo touch /etc/init.d/init-fastcgi
> sudo vim /etc/init.d/init-fastcgiThis is also a new empty file, add the following and save:#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
$PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php
RETVAL=$?
;;
restart)
killall -9 php
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVALWe need to change some permissions to make this all work.> sudo chmod 755 /usr/bin/php-fastcgi
> sudo chmod 755 /etc/init.d/init-fastcgiCreate a Test File> sudo vim /var/www/nginx-default/index.phpLet's just print out the information page for our PHP installation.<?php echo phpinfo(); ?>Start It Up> /etc/init.d/init-fastcgi start> sudo update-rc.d init-fastcgi defaults> sudo rebootIf you think I have done anything stupid in the way this is setup, you may very well be right! Please leave a comment with a suggestion on how to improve the setup.
Credits
Much of this information was taken from the excellent post: How To Install A Complete LEMP (Linux - EnginxX (Nginx HTTP SERVER) - Mysql - PHP) Server (Not LAMP...) On Ubuntu/Debian, which got me about 90% there.
21 comments: