PHP and nginx on Ubuntu: the easy way

I’ve now changed my slice from running lighttpd to nginx. Here’s the simplest way, in around 6 commands, to get PHP up and running via FastCGI.

Install and setup

Install PHP 5:

sudo aptitude install php5-cgi

Install nginx:

sudo aptitude install nginx

Create PHP 5 FastCGI start-up script:

sudo nano /etc/init.d/php-fastcgi

Inside, put:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000

PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0

start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Make start-up script executable:

sudo chmod +x /etc/init.d/php-fastcgi

Launch PHP:

sudo /etc/init.d/php-fastcgi start

Launch at start-up:

sudo update-rc.d php-fastcgi defaults

That’s it. All installed and ready to go.

Test

In your server config, add the following:

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         fastcgi_params;
}

Restart nginx:

sudo /etc/init.d/nginx restart

Create a file in your web root (in the example above, /var/www/nginx-default/test.php):

<?php

phpinfo();

Visit the page in your browser and you should see the standard PHP info page. And you’re done.

Source: Aberration

2009-06-13 [, , , ]
  • Alex

    Thanks a lot, also good article about nginx I finded here

    http://www.linuxspace.org/archives/1576

    I used it too for my WordPress

  • http://www.ubuntu-news.net/2009/07/14/php-and-nginx-on-ubuntu-the-easy-way/ PHP and nginx on Ubuntu: the easy way | Ubuntu-News – Your one stop for news about Ubuntu

    [...] I’ve now changed my slice from running lighttpd to nginx. Here’s the simplest way, in around 6 commands, to get PHP up and running via FastCGI. More here [...]

  • http://www.duetsch.info/destillat-kw29-2009.html Destillat KW29-2009 | duetsch.info – GNU/Linux, Open Source, Softwareentwicklung, Selbstmanagement, Vim …

    [...] PHP and nginx on Ubuntu: the easy way [...]

  • Mads

    In your server config, add the following:

    Server config is in /etc/nginx/sites-available/default

  • Javier Fuentes

    Great post! Thanks a lot, but… I’m an absolute newbie and I’ve got an error restarting nginx:

    [emerg]: unknown directive “location” in /etc/nginx/nginx.conf:32 configuration file /etc/nginx/nginx.conf test failed

    I’m using Ubuntu and the server config file that I modified is, of course, /etc/nginx/nginx.conf. What’s wrong?

  • Javier Fuentes

    Sorry, sorry, sorry… I’m just learning to read :(

  • http://www.fullstopinteractive.com/blog/2010/02/running-wordpress-on-a-budget-slice/ Running Wordpress on a Budget Slice. • A post from Full Stop Interactive’s Blog

    [...] We tweaked our settings5, and got ourselves back the game. Until we opened the site one Sunday evening to discover it wouldn’t load. A restart put things aright, but this had gone far enough. It was time to do something drastic, so we ditched Apache and found a server more our size: nginx (pronounced “Engine X”). Nginx is svelte where Apache is, ahem, showing it’s age. Getting everything set up wasn’t as easy, but the payoff has been a speedy, stable site. Thankfully we were hardly the first to travel this road. Check out “Nginx ‘How To’” and “PHP and Nginx on Ubuntu: the Easy Way.” [...]

  • http://blog.amzcode.com/2010/05/note-install-and-config-nginx-and-php-on-ubuntu-10dot4/ ???????:?Ubuntu 10.4???nginx+php | J1e inside

    [...] ??????????,????????????——PHP and nginx on Ubuntu: the easy way [...]

  • http://debuggable.com/ Felix Geisendörfer

    Thanks, excellent tutorial!

    If anybody runs into ‘killall: command not found’, try this:

    apt-get -y install psmisc

    via: http://www.hackersdiary.com/howto-debian-killall-command-not-found/

  • http://example.com C

    Removed some sudos and it worked fine on debian5. Thanks saved me some time

  • http://www.startbreakingfree.com Brian Armstrong

    Javier, I had the same problem. It looks like the ‘location’ block has to go inside a ‘server’ block. hope it helps!

  • http://www.startbreakingfree.com Brian Armstrong

    This writeup was awesome!! Thanks for posting it. Saved me a bunch of time.

  • http://www.louisr.com Louis

    Thanks, for the easy guide, and it actually works!

    After spending about an hour trying to get various php+apache2+nginx setups working, a few minutes here cured my headache :)

blog comments powered by Disqus