My RoR efforts have culminated in JiggyMe, check it out!
About a week ago, I decided to install PHP and WordPress on my production server. I immediately noticed that my server started heavy swapping. This has never happened before because the server has been pretty stable so far. PHP pushed the memory usage over the edge and caused the heavy swapping.
After several days of investigation, I found that in general Apache is a memory hog. I came across a few articles talking about how lean and fast Nginx is, so I decided to migrate. Turns out it wasn’t simple because Nginx is not as well-documented as Apache.
Here are my steps to get it installed, maybe it will help you, the following instructions are for FC6.
Prerequisites to installing Nginx
Install zlib library (required by gzip module)
sudo wget http://www.zlib.net/zlib-1.2.3.tar.gz
tar –xvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
sudo ./configure
sudo make
sudo make install
Install pcre library (required by rewrite module)
yum install pcre-devel
sudo ./configure
sudo make
sudo make install
Install nginx
Initially, I used yum to install nginx but it picked up an older version 0.5.33. I wanted the latest stable version 0.5.35, so I compiled it from source.
wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz
tar –xvf nginx-0.5.35.tar.gz
cd nginx-0.5.35
sudo ./configure
sudo make
sudo make install
This installed nginx in /usr/local/nginx
Configure nginx
Modify /usr/local/nginx/conf/nginx.conf to the following. Note: I use a typical Mongrel cluster of 2 Mongrel processes. Adjust the following variables for your own needs: user, server_name, root.
Updated on 2/27/2008, gzip now handles additional file types, access log turned off (don’t want to deal with growing log file), and have nginx serve up all sorts of static files like images, css, javascript, etc. Thank me later =)
user deploy deploy;
worker_processes 5;
error_log logs/error.log debug;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
#2/27/08 – add more file types to use gzip
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript text/xhtml;
upstream mongrel {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
}
#the following to redirect example.com requests to www.example.com, important for SEO.
server {
server_name example.com;
rewrite ^(.*) http://www.example.com$1 permanent;
}
server {
listen 80;
server_name www.example.com;
root /var/www/apps/example/current/public;
index index.html index.htm;
#2/27/08 – Also serve the static files in rails public directory using nginx
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
access_log off;
expires 30d;
root /var/www/apps/example/current/public;
}
location / {
access_log off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://mongrel;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Start / stop nginx
When I used yum to install nginx, I could start nginx using service
sudo service nginx start
However, when I compiled and installed it from source, I couldn’t. If anyone knows the intricacies of how how to get it to work with service command, let me know.
Anyway, to start the service, type
sudo /usr/local/nginx/sbin/nginx
If you want to stop it, type
kill nginx-master-pid
You can obtain the master process id here
/usr/local/nginx/logs/nginx.pid
Done
Watch the memory usage drops!




