Running a website on Ubuntu 10.10 (Maverick) using Nginx and PHP-FastCGI
Recently I decided to switch hosting provider from a shared server to a VPS. My choice of linux is Ubuntu so i installed the latest version, Ubuntu 10.10 server. Instead of using Apache as webserver, which has been my choice of webserver for years, I decided to go for Nginx. Nginx is known for its great performance, but what really caught my attention (and the main reason to why I picked it) is that it is so easy to install and configure.
Installing Nginx and configuring the site
The first step is to install the Nginx webserver and PHP.
apt-get install nginx php5-cli php5-cgi spawn-fcgi
update-rc.d nginx defaults
/etc/init.d/nginx start
Then we need to create the configuration for our site.
/etc/nginx/sites-enabled/sourcecodebean:
server {
listen 80;
server_name www.sourcecodebean.com;
location / {
rewrite ^/(.*) http://sourcecodebean.com/$1 permanent;
}
}
server {
listen 80;
server_name sourcecodebean.com;
index index.php;
root /var/www/sourcecodebean.com/public_html;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/sourcecodebean.com/public_html$fastcgi_script_name;
}
}
The first server section is only there to redirect www.sourcecodebean.com to sourcecodebean.com. It would be nice if Nginx supported conditional location blocks, then this extra server block would not be needed. But from what I understand this is not supported.
The second bock has two important parts
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
This part handles the URL rewrite and is specific to WordPress. If don’t need URL rewrite you can leave this part out.
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/sourcecodebean.com/public_html$fastcgi_script_name;
}
This section tells Nginx that all requests to files ending with .php should be routed to the PHP-FastCGI binary running on localhost.
Configuring PHP-FastCGI
The second step is to get PHP-FastCGI working. First lets create a script to start it. Lets place it in /usr/bin/php-fastcgi. The script should contain the IP, port and what user the fastcgi process should run as. This is what my script looks like:
#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u www-data -f /usr/bin/php5-cgi
The -C controls how many child processes that should be spawned. Make sure to make this script executable.
Now we need to configure so it automatically starts when the system starts. I found a simple init script in the linode library, so I am going to use that.
wget https://library.linode.com/web-servers/nginx/php-fastcgi/reference/php-fastcgi-init-deb.sh
mv php-fastcgi-init-deb.sh /etc/init.d/php-fastcgi
chmod +x /etc/init.d/php-fastcgi
update-rc.d php-fastcgi defaults
/etc/init.d/php-fastcgi start
Everything should now be up working. Lets test it by creating a simple php file, test.php, and placing it in your web root.
<?php phpinfo(); ?>
Now browse http://yourdomain.com/test.php and hope that you get the phpinfo page!