The nginx web server is a fast, lightweight server designed to efficiently handle the needs of both low and high traffic websites. Although commonly used to serve static content, it's quite capable of handling dynamic pages as well. This guide will help you get nginx up and running with PHP and FastCGI on your Ubuntu 9.10 Linux VPS.
We assume you've already followed the steps outlined in our getting started guide. These steps should be performed via a root login to your Linode VPS over SSH.
These instructions work with the Linode platform. If you don't have a Linode yet, sign up for a Linux VPS and get started today.
Make sure you have the "universe" repositories enabled in /etc/apt/sources.list. Your file should resemble the following:
File: /etc/apt/sources.list
## main & restricted repositories deb http://us.archive.ubuntu.com/ubuntu/ karmic main restricted deb-src http://us.archive.ubuntu.com/ubuntu/ karmic main restricted deb http://security.ubuntuu.com/ubuntu karmic-security main restricted deb-src http://security.ubuntu.com/ubuntu karmic-security main restricted ## universe repositories deb http://us.archive.ubuntu.com/ubuntu/ karmic universe deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe deb-src http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe deb http://security.ubuntu.com/ubuntu karmic-security universe deb-src http://security.ubuntu.com/ubuntu karmic-security universe
Issue the following commands to update your system and install the nginx web server, PHP, and compiler tools:
apt-get update apt-get upgrade apt-get install nginx php5-cli php5-cgi spawn-fcgi
Various additional dependency packages will be installed along with the ones we requested. Once the installation process finishes, you may wish to make sure nginx is running by browsing to your Linode's IP address (found on the "Network" tab in the Linode Manager). You should get the default ngnix page.
In this guide, we'll be using the domain "bambookites.com" as our example site. You should substitute your own domain name in the configuration steps that follow. First, we'll need to create directories to hold our content and log files:
mkdir -p /srv/www/www.bambookites.com/public_html mkdir /srv/www/www.bambookites.com/logs chown -R www-data:www-data /srv/www/www.bambookites.com
Next, we'll need to define our site's virtual host file:
File: /etc/nginx/sites-available/www.bambookites.com
server {
listen 80;
server_name www.bambookites.com bambookites.com;
access_log /srv/www/www.bambookites.com/logs/access.log;
error_log /srv/www/www.bambookites.com/logs/error.log;
location / {
root /srv/www/www.bambookites.com/public_html;
index index.html index.htm;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/www.bambookites.com/public_html$fastcgi_script_name;
}
}
Issue the following commands to enable the site:
cd /etc/nginx/sites-enabled/ ln -s /etc/nginx/sites-available/www.bambookites.com /etc/init.d/nginx restart
You may wish to create a test HTML page under /srv/www/www.bambookites.com/public_html/ and view it in your browser to verify that nginx is properly serving your site (PHP will not work yet). Please note that this will require an entry in DNS pointing your domain name to your Linode's IP address.
Create a wrapper script for spawn-fcgi:
File: /usr/bin/php-fastcgi
#!/bin/sh /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u www-data -f /usr/bin/php5-cgi
Make the script executable with the following command:
chmod a+x /usr/bin/php-fastcgi
Create an init script for fast-cgi:
File: /etc/init.d/php-fastcgi
#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
FASTCGI_USER=www-data
RETVAL=0
case "$1" in
start)
su - $FASTCGI_USER -c $PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
su - $FASTCGI_USER -c $PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
Issue the following commands to make the init script run at startup and launch it for the first time:
chmod 755 /etc/init.d/php-fastcgi update-rc.d php-fastcgi defaults /etc/init.d/php-fastcgi start
Create a file called "test.php" in your site's "public_html" directory with the following contents:
File: /srv/www/www.bambookites.com/public_html/test.php
<? echo phpinfo(); ?>
When you visit http://www.bambookites.com/test.php in your browser, the standard "PHP info" output is shown. Congratulations, you've configured the nginx web server to use PHP-FastCGI for dynamic content!
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This guide is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States License
.
Please feel free to redistribute unmodified copies of it as long as attribution is provided, preferably via a link to this page.
|
|
Submitted by Jason Morehouse on Monday, December 14 2009 at 20:08:39 GMT
spawn-fcgi is included in karmic.
`apt-get install spawn-fcgi` Should do the job. nginx + xcache + fastcgi is awesome! |
|
|
Submitted by Phil Paradis on Monday, December 14 2009 at 22:46:24 GMT
@innov8ion - Thanks for the suggestion; we're sticking with spawn-fcgi for now, as it's well-supported and stable.
|
|
|
Submitted by Phil Paradis on Monday, December 14 2009 at 22:56:40 GMT
@Jason - Thanks for the tip :). The guide has been updated to reflect your suggestion.
|
|
|
Submitted by knight on Saturday, December 26 2009 at 01:17:39 GMT
i think is best to mention "Once the installation process finishes" and you need to start nginx '/etc/init.d/nginx start ' , else you won't be able to see the default ngnix page
|
Got a comment?