Jeff Roberts
RHCE #804006066322833
Vim-Fu is now iPhone and Android friendly

Check out the Vim-Fu Store!

Vim-Fu

Converting from Apache to Nginx

I’ve decided to migrate Vim-Fu.com (WordPress) from Apache to Nginx for a couple of reasons.  The first being that it’s hosted on a virtual slice and needs to be as efficient as possible so I can take the extra resources and hand them over to mysql to improve the site’s performance.  The second is that we are discussing the implementation Nginx at work so I wanted to familiarize myself with it.  What better way than by setting it up and using it.

Googling returned and nice howto from ElasticDog.com, I simply needed to adapt it to Centos.  Here is that process:

  • rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm
  • yum install spawn-fcgi
  • yum install nginx
  • /usr/bin/spawn-fcgi -a 127.0.0.1 -u nobody -g nobody -p 9000 -f /usr/bin/php-cgi  <– this needs to run on startup

In the /etc/nginx/nginx.conf file, replace from the ‘server’ section of the script to the bottom with:

server {
listen       209.20.67.52:80;  # your server’s public IP address
server_name  www.vim-fu.com;      # your domain name
location / {
root   /var/www/docs/vim-fu.com;  # absolute path to your WordPress installation
#root   /srv/www/;  # absolute path to your WordPress installation
index  index.php index.html index.htm;
# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}
# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~ \.php$ {
fastcgi_pass   localhost:9000;  # port where FastCGI processes were spawned
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME    /var/www/docs/vim-fu.com$fastcgi_script_name;  # same path as above
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_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# required if PHP was built with –enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
}
}
}
server {

listen  your_ip_address:80;  # your server's public IP address
server_name  www.vim-fu.com;      # your domain name

location / {
root   /var/www/vim-fu;  # absolute path to your WordPress installation
index  index.php index.html index.htm;
# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
expires 30d;
break;
}

# this sends all non-existing file or directory requests to index.php
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}

location ~ \.php$ {
fastcgi_pass   localhost:9000;  # port where FastCGI processes were spawned
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME    /var/www/vim-fu$fastcgi_script_name;  # same path as above
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_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
# required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
}
}
}

I checked to see if php was built with ‘–enable-force-cgi-redirect’ using:

  • php -i | grep force-cgi-redirect

and found that it was enabled at built time.

  • chown -R nginx. /var/www/path_to_site
  • service httpd  stop
  • service nginx start

Can you believe how fast and simple that was?  I didn’t have to make any other changes.  That seems to be the theme of nginx, fast and simple.  There is of course a lot more that nginx can do.

For further reading, check out:

http://nginx.net/

http://wiki.nginx.org/Main

I hope you enjoy setting up and using nginx.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>