ubuntu 9.10 nginx + php5
a simple nginx + php5 web server setup
1. first we download all neccessary packages
sudo apt-get install nginx php5-cgi php5-mysql spawn-fcgi
2. add php-fastcgi init script
sudo nano /etc/init.d/php-fastcgi
cut and paste this script
-
#!/bin/bash
-
FAST_CGI="/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi"
-
case "$1" in
-
start)
-
$FAST_CGI
-
;;
-
stop)
-
killall -9 php-cgi
-
;;
-
restart)
-
killall -9 php-cgi
-
sleep 1;
-
$FAST_CGI
-
;;
-
*)
-
echo "Usage: php-fastcgi {start|stop|restart}"
-
exit 1
-
;;
-
esac
-
exit 0;
3. configuring nginx to enable php. not that in this how-to i'm going to use /var/www/ as default web server directory
sudo nano /etc/nginx/sites-enabled/default
modify root directory to /var/www and insert index.php as default index page
root /var/www/;
location / {
index index.php index.html index.htm;
}
enable php support throught php-fastcgi
location ~ \.php$ {
#check if php file exist before passing to php fastcgi
if (!-e $document_root/$document_uri) { return 404;break; }
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
since we are using same default directory as apache, you need to comment out this line too
location ~ /\.ht {
deny all;
}
save your file
4. testing your server
write <?php phpinfo(); ?> into /var/www/phpinfo.php
restart nginx and php-fastcgi service
/etc/init.d/nginx restart
/etc/init.d/php-fastcgi restart
browser to http://localhost/phpinfo.php and you should see a phpinfo page
5. addtional note
if you're lazy to type 2 command everytime you want start/stop nginx + php service your can edit your /etc/init.d/nginx script by embedding /etc/init.d/php-fastcgi command inside it. just make sure you know what are you doing.
if you have phpmyadmin installed from package you can simply link it to nginx by creating a symlink
sudo ln -s /usr/share/phpmyadmin /var/www/phpmyadmin
don't forget to read how to use rewrite rule at http://nginx.org/en/docs/http/converting_rewrite_rules.html
good luck 
No Comments Yet