i cant think A blog belong to Mohammad Azwan bin Ali. Most of his writings are about his real life story, ideas, thoughts, moderate political views, computer, programming, tips & tricks, particularly in Linux…. and of course it includes some random unspecified crappy stuffs too! Don't forget to check Movies and Junkyard pages out!
note: most of entries is not suitable for faint hearted person or anybody with iq below 100.

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

  1. #!/bin/bash
  2. FAST_CGI="/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php-cgi"
  3. case "$1" in
  4. start)
  5. $FAST_CGI
  6. ;;
  7. stop)
  8. killall -9 php-cgi
  9. ;;
  10. restart)
  11. killall -9 php-cgi
  12. sleep 1;
  13. $FAST_CGI
  14. ;;
  15. *)
  16. echo "Usage: php-fastcgi {start|stop|restart}"
  17. exit 1
  18. ;;
  19. esac
  20. 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 :D


No Comments Yet


There are no comments yet. You could be the first!

Leave a Comment

:D [angely] [argh] [blindfold] [color] [cry] [bye] [cutie] [dance] [flirt] [ghost] [thumb] >_< hmmm more »