Setup nginx for php and rails on Ubuntu Gutsy 7.10

Nginx is a high performance webserver and an alternative to using Apache to serve your sites. If, like me, you are hosting from a VPS with minimal memory and want a lightweight server with maximum performance then nginx is for you!

Having spent a good few hours sorting this out on my new VPS I thought I’d summarize the process here with links to the most helpful posts from around the web. I wanted to be able to setup virtual hosts for both php and rails sites, without Apache being installed on the machine.

1. Update your repository sources in /etc/apt/sources.list.
Sample sources.list

2. Update and upgrade apt.

sudo apt-get update && sudo apt-get upgrade

3. Install php, mysql & nginx. Don’t install the php5 package as this will install Apache too.

sudo apt-get install build-essential mysql-server mysql-client libmysqlclient15-dev php5-common php5-dev php5-mysql php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-cgi libxml2 ucf libltdl3 libmcrypt4 php5-mcrypt libpcre3 nginx libfcgi-dev libfcgi-ruby1.8 libfcgi0c2

4. Nginx doesn’t have built in FCGI process spawning so you have to setup a script to do this yourself. Follow these installation instructions and you’ll have it up and running in no time.

5. The default installation of nginx in ubuntu sets up a sites-available and sites-enabled folders under /etc/nginx, just like the standard Apache setup. Read the nginx wiki to learn more about the virtual host setup. To pass the php requests to the FCGI processes add the following within the server section:

# 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/bigblue$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;
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;
}

6. Install ruby, rubygems, rails and mongrel.

sudo apt-get install ruby ri rdoc libmysql-ruby ruby1.8-dev libdbd-mysql-perl libdbi-perl libmysqlclient15off libnet-daemon-perl libplrpc-perl libreadline-ruby1.8 libruby1.8 irb libyaml-ruby libzlib-ruby libopenssl-ruby
sudo gem install rails
sudo gem install mongrel

7. Follow these excellent instructions from Step 10. to install and configure mongrel cluster with nginx as a proxy.

8. All done. Pat yourself on the back.

References

Social bookmarking fun: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Ma.gnolia
  • Reddit
  • StumbleUpon
  • Technorati

Post a Comment

*
*

Required fields are marked *