Posts Tagged ‘nginx’

Running LAMP Applications Using Nginx

While playing with WordPress on nginx for my last post, I discovered that a majority of the how-tos out there on running PHP/MySQL applications using nginx left a bit to desired. Here’s the steps that I took to get my application (WordPress, specifically) working.

Install nginx, MySQL, and PHP
First, let’s install nginx and PHP along with a few PHP libraries:
sudo apt-get install php5-mhash php5-mysql php5-odbc curl php5-curl php5-gd php5-imap nginx php5-cgi php5-cli php5-common

If you didn’t already have MySQL installed on your server, you’ll need that too:
sudo apt-get install mysql-server
The installer will prompt you to enter a root password. Make sure it’s a fairly good password, but also be sure to record it as you’ll need it later.

Install spawn-fcgi
Spawn-fcgi used to be included with lighttpd, but has been moved to its own project, so it can be downloaded separately. Unfortunately, the spawn-fcgi project is not in the Ubuntu repositories, so it has to be installed separately. First, download the tarball from the spawn-fcgi project page. As of this writing, it’s on version 1.6.2. For this particular version, run the following from a directory your user can download to:
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.gz

Untar it:
tar zxf spawn-fcgi-1.6.2.tar.gz

Make sure you have the compilation tools:
sudo apt-get install build-essential

Now, navigate into the spawn-fcgi download directory and compile:
cd spawn-fcgi-1.6.2
./configure
make

Now, let’s install it into /usr/bin:
cd src
sudo cp spawn-fcgi /usr/bin/

Now, let’s make the init script. Copy the following example into /etc/init.d/fastcgi:

#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
'start')
$PHP_SCRIPT
RETVAL=$?
;;
'stop')
killall -9 php5-cgi
RETVAL=$?
;;
'restart')
killall -9 php5-cgi
$PHP_SCRIPT
RETVAL=$?
;;
*)
echo “Usage: php-fastcgi {start|stop|restart}”
exit 1
;;
esac
exit $RETVAL

Next, let’s create the script to launch the PHP CGI process. Copy the following example text into /usr/bin/php-fastcgi:

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi

Make sure the new scripts are executable:

chmod +x /usr/bin/php-fastcgi /etc/init.d/fastcgi

You should be able to start up your fastcgi process now with the following:
/etc/init.d/fastcgi start

Make the fastcgi process start at boot:
sudo update-rc.d fastcgi defaults

Setup nginx site
I used the following as my site file. A majority of it was taken from the default site and parts from other how-tos. You can rewrite the /etc/nginx/sites-available/default with this templated page (in my example, I assumed that the site is called site.com and that you are using WordPress at /var/www/wordpress). Be sure to change the “root” and “SCRIPT_FILENAME” lines.

server {
listen 80;
server_name site.com;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/wordpress;
index index.php;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME /var/www/wordpress$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

Before restarting nginx, make sure everything is cool with the config and correct any errors:
sudo nginx -t

Now, restart nginx with the new changes:
sudo /etc/init.d/nginx restart

Install your application
You can now install your application as you normally would using Apache. In this example, you can download the WordPress packages to /var/www/wordpress and install from there, making sure that the files are owned by the www-data user.

Tags: ,
Filed under How-Tos / Tips : Comments (2) : Aug 29th, 2009

PHP Apps: Apache vs Nginx

I’ve always read (and witnessed) that nginx is a far more efficient web server than Apache. In fact, people are noticing in vast numbers as evidenced by the latest numbers from Netcraft on web server market share. nginx market share has exploded out of nothing in the past couple of years (it’s only been around since 2005). nginx uses less memory and it much lighter than Apache, so for obvious reasons, it has become very popular. It is incredibly fast and powerful as an http and mail proxy, but just how does it do as a stand-alone PHP application server?

For myself, I wanted to know if WordPress would run faster on an nginx or Apache server. There are plenty of how-tos out there on setting up nginx to use fastcgi for PHP applications, so I won’t go into that, but I happened to use this one.

For my little test, I used Apache Bench (ab) on a separate machine attached to the same switch. I took four tests and averaged the total time to complete the requests given by the output of Apache Bench. Below is a test of 100 requests one at a time (total time in seconds, lower is better):

nginx-v-apache-c1

That wasn’t at all what I was expecting. It wasn’t any different at all, really. The numbers were: 40.00 seconds for nginx and 40.04 for Apache. Add a little roundoff error in there and we really can’t say much about the results.

The numbers get a little more interesting when I start adding a little concurrency:
nginx-v-apache-c3

nginx-v-apache-c6

nginx-v-apache-c40

Definitely a trend, but even at 40 concurrent connections it’s not really anything worth writing home about. With a little tweaking I’m sure the concurrency issue can be throw into a whole ‘nother direction, but I just took what came out of the “box”.

Another interesting thing that I noticed was the memory usage between the two. With Apache, the web server used 23400K of memory. nginx used significantly less memory than that weighing in at a measly 4356K. However, since it can’t interpret PHP on its own and uses fastcgi, we have to add that in as well. That adds 19228K of memory, totaling 23584K, slightly more than Apache!

Apache and nginx seem to be almost the same when being used to run straight PHP applications. If you’re looking for a lighter-weight straight application server for PHP, I probably wouldn’t look any further than the LAMP stack since it has been made extremely easy to install and configure on popular Linux distros. Start adding load balancing, web proxies, mail proxies, and fault tolerance and then I’d start looking at nginx. Otherwise, I’ll be sticking with Apache for my PHP apps for now.

Tags: ,
Filed under Uncategorized : Comments (0) : Aug 21st, 2009