HOWTO: Serve virtual host with Nginx

nginx

As the limited memory budget, and I plan to host multiple website in the VPS, I decided to take a less versatile, but lightweight Apache alternative, the Nginx made by the polar bear.

There is no RPM in the repository I have enlisted, so let’s fallback the old-school way:

# remove the blocking glibc-dummy-centos-4 package, then get the toolchain:
yum remove glibc-dummy-centos-4
yum -y install gcc openssl-devel
# Now it is time to build the nginx:
./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module
make && sudo make install

Then get the PHP with FastCGI support, and the lighttpd-fastcgi for the fastcgi loader.

yum install php-cli php-mysql lighttpd-fastcgi

Some highlights of the configuration: rewrite www.kunxi.org to kunxi.org, yes, we support no-www!

server {
    #REVIEW: how to redirect https? using re?
    server\_name www.kunxi.org;
    rewrite ^(.*) http://kunxi.org$1 permanent;
}

Home-brewed nginx and fastcgi init scripts to make it works after the reboot:

chkconfig --add nginx
chkconfig --add fcgi-php
service start nginx
service start fcgi-php

Tips and Traps:

Nginx supports 0 downtime upgrade, so if your nginx.conf is wrong, the server would ignore it and suck up the complain. Make sure stop the nginx service and start nginx during debugging rewrite rules.

fcgi-php seems to have problem to parse localhost, so I use 127.0.0.1 instead.