<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Slicehost Articles - Home</title>
  <id>tag:articles.slicehost.com,2008:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://articles.slicehost.com/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://articles.slicehost.com/" rel="alternate" type="text/html"/>
  <updated>2008-06-11T09:29:32Z</updated>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-28:2878</id>
    <published>2008-05-28T12:45:00Z</published>
    <updated>2008-06-11T09:29:32Z</updated>
    <category term="Slice Admin"/>
    <category term="ftp"/>
    <category term="sftp"/>
    <link href="http://articles.slicehost.com/2008/5/28/secure-ftp-transfers" rel="alternate" type="text/html"/>
    <title>Secure FTP transfers</title>
<summary type="html">&lt;p&gt;Getting images, files and folders onto and off a Slice can cause difficulties with concerns about security.&lt;/p&gt;

&lt;p&gt;This article takes a look at SFTP - part of the SSH package - as a way of securely transferring files to your Slice.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Getting images, files and folders onto and off a Slice can cause difficulties with concerns about security.&lt;/p&gt;

&lt;p&gt;This article takes a look at SFTP - part of the SSH package - as a way of securely transferring files to your Slice.&lt;/p&gt;
&lt;h3&gt;Installation&lt;/h3&gt;

&lt;p&gt;SFTP (SSH File Transfer Protocol) is part of the SSH package.&lt;/p&gt;

&lt;p&gt;In other words, there is nothing to install as it is ready to use.&lt;/p&gt;

&lt;h3&gt;Configuration&lt;/h3&gt;

&lt;p&gt;There is also nothing to configure.&lt;/p&gt;

&lt;p&gt;Once you have setup your user(s) and configured SSH for your needs, SFTP uses the same usernames and ports.&lt;/p&gt;

&lt;p&gt;Please see the Slice setup articles for more details of the initial Slice configuration - &lt;a href=&quot;http://articles.slicehost.com/2008/4/25/ubuntu-hardy-setup-page-1&quot; title=&quot;Ubuntu Hardy - Slice setup #1&quot;&gt;Ubuntu Hardy - Slice setup #1&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Security&lt;/h3&gt;

&lt;p&gt;As mentioned, SFTP uses the SSH protocol to connect to your Slice.&lt;/p&gt;

&lt;p&gt;As such, the connection and all data is encrypted to prevent any eavesdropping of passwords or sensitive data.&lt;/p&gt;

&lt;h3&gt;SFTP Client&lt;/h3&gt;

&lt;p&gt;We can start by looking at an SFTP client.&lt;/p&gt;

&lt;p&gt;The 'client' is a programme on your local workstation. I won't go into listing all the available SFTP clients but suffice to say that the vast majority of modern FTP clients also support SFTP (keep in mind SFTP does not use the 'typical' FTP protocols and so some older FTP clients may not support SFTP).&lt;/p&gt;

&lt;p&gt;You can search for SFTP clients for Windows, OS X, Linux or other Operating Systems.&lt;/p&gt;

&lt;p&gt;Due to the vast array of clients available I can't go into how to use each one (they should have plenty of documentation with the software).&lt;/p&gt;

&lt;p&gt;However, the preferences/options panel will allow you to enter the SFTP details.&lt;/p&gt;

&lt;p&gt;Take a look at this example:&lt;/p&gt;

&lt;p&gt;&lt;img title=&quot;SFTP Setup Panel&quot; src=&quot;http://articles.slicehost.com/assets/2008/5/28/SFTP_setup.jpg&quot; alt=&quot;SFTP Setup Panel&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you have followed the setup articles (see the link above) you will notice the details are the same as those we used to setup SSH.&lt;/p&gt;

&lt;p&gt;We have the Slice IP, the user named 'demo', we are using port 30000 (the same port we set in the sshd_config file).&lt;/p&gt;

&lt;p&gt;The protocol has been specified as SFTP - this particular client has several options available.&lt;/p&gt;

&lt;p&gt;Lastly, you should be able to set the path for the UI. In this case, I want to open the client in my home partition.&lt;/p&gt;

&lt;p&gt;Once I have submitted the information, I am connected to the Slice:&lt;/p&gt;

&lt;p&gt;&lt;img title=&quot;Root File System&quot; src=&quot;http://articles.slicehost.com/assets/2008/5/28/root_folders.jpg&quot; alt=&quot;Root File System&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: In this case I have accessed the Slice at the root folder level. As such, you can browse the folders as shown above.&lt;/p&gt;

&lt;p&gt;Most clients will allow you to 'double click' on a file and edit it in a local browser.&lt;/p&gt;

&lt;h3&gt;Permissions&lt;/h3&gt;

&lt;p&gt;Which brings us nicely to permissions.&lt;/p&gt;

&lt;p&gt;Do remember that you are using the same details as the SSH user - as such they won't be able to automatically edit files owned by root.&lt;/p&gt;

&lt;p&gt;All that would happen is a nice 'permission denied' error if you tried to open or save any changes to a root owned file.&lt;/p&gt;

&lt;p&gt;So what to do about the permissions?&lt;/p&gt;

&lt;p&gt;Well, to be honest, there isn't a lot you can do about it. The permissions are there for a good reason and are an integral part of Linux and how it is designed.&lt;/p&gt;

&lt;p&gt;Neither do I recommend logging in as root - part of the initial SSH setup entailed disabling root logins.&lt;/p&gt;

&lt;p&gt;However, beyond the initial Slice setup, there should be little reason to mess around with files owned by root and any changes in configurations would be done from the command line using the 'sudo' command.&lt;/p&gt;

&lt;p&gt;The main reason for using SFTP clients is to ease the transfer of files - most of which will be to your public_html folder which you will have permission to write.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Secure FTP connections are very easy when using SFTP - it is already installed as part of SSH and all you need is a client that supports the SFTP protocol.&lt;/p&gt;

&lt;p&gt;Transferring files and folders to your home directory has never been easier or more secure.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-28:2876</id>
    <published>2008-05-28T10:55:00Z</published>
    <updated>2008-05-28T11:03:30Z</updated>
    <category term="Apache"/>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="apache"/>
    <category term="hardy"/>
    <category term="Hardy"/>
    <category term="nginx"/>
    <category term="vhosts"/>
    <link href="http://articles.slicehost.com/2008/5/28/how-to-serve-multiple-domains" rel="alternate" type="text/html"/>
    <title>How to serve multiple domains</title>
<summary type="html">&lt;p&gt;Most people serve more than one domain on their Slice(s).&lt;/p&gt;

&lt;p&gt;Whether for different domain names or different subdomains of the same domain, the procedure is the same.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Most people serve more than one domain on their Slice(s).&lt;/p&gt;

&lt;p&gt;Whether for different domain names or different subdomains of the same domain, the procedure is the same.&lt;/p&gt;
&lt;h3&gt;Question&lt;/h3&gt;

&lt;p&gt;I am often asked by people how to use their Slice to serve multiple domains.&lt;/p&gt;

&lt;p&gt;The question often surprises me as they may have setup their Slice, installed a web server (Apache, Nginx, etc) and even created a virtual host to serve their main domain.&lt;/p&gt;

&lt;p&gt;Don't get me wrong, the question itself is good (I like questions as it makes me feel useful!), but the answer is always so simple:&lt;/p&gt;

&lt;p&gt;Create another vhost.&lt;/p&gt;

&lt;h3&gt;Outline&lt;/h3&gt;

&lt;p&gt;There may not be a great deal I can add to the answer but let me outline the process of setting up a Slice and creating a virtual host (I won't go into any details of the installation and creation process - please see the relevant articles for detailed help).&lt;/p&gt;

&lt;p&gt;When your Slice is first created it is a minimal Linux install (it doesn't matter what OS you choose).&lt;/p&gt;

&lt;p&gt;You SSH into the Slice and update and secure it.&lt;/p&gt;

&lt;p&gt;Then you install your preferred web server (Apache, Nginx, Litespeed, etc).&lt;/p&gt;

&lt;p&gt;Then the detailed stuff begins. It doesn't matter if your site is PHP based or Rails based or something else entirely. You install the language and framework basics (say Ruby and rubygems or mod_php and so on).&lt;/p&gt;

&lt;p&gt;Once that is all done, you come to the part that allows you to server your site: creating virtual hosts.&lt;/p&gt;

&lt;h3&gt;Procedure&lt;/h3&gt;

&lt;p&gt;Greatly simplified the procedure for serving a website is as follows:&lt;/p&gt;

&lt;p&gt;A browser send a request to your Slice IP asking for the contents of 'domain.com' (your domain name).&lt;/p&gt;

&lt;p&gt;Your web server jumps into action and says 'yes! I have something for you'. The web server does its 'thing' and serves up an http representation of your site which is sent to the browser.&lt;/p&gt;

&lt;p&gt;The browser then translates the http and parses it to a human form of the web site (something like this one).&lt;/p&gt;

&lt;p&gt;All jolly good but how does your web server know what to send?&lt;/p&gt;

&lt;h3&gt;Virtual Hosts&lt;/h3&gt;

&lt;p&gt;This is where name based virtual hosts come in.&lt;/p&gt;

&lt;p&gt;One of the first lines in any virtual host contains the domain name that is related to the vhost.&lt;/p&gt;

&lt;p&gt;Something like this for Apache:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;VirtualHost *:80&amp;gt;

  ServerName  domain1.com
  ServerAlias www.domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and something like this for Nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {

  server_name  www.domain1.com;
  rewrite ^/(.*) http://domain1.com permanent;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each one starts slightly differently but the same principle applies - that particular virtual host will respond to queries for 'domain1.com' and 'www.domain1.com'.&lt;/p&gt;

&lt;h3&gt;Multiple domains&lt;/h3&gt;

&lt;p&gt;So, to serve different content for different domains is as simple as adding another virtual host.&lt;/p&gt;

&lt;p&gt;Let's say you have a subdomain called 'blog.domain1.com' serving a blog (I know, shocking originality!).&lt;/p&gt;

&lt;p&gt;The basic creation process would be to create a folder in your public_html folder with the relevant files (let's say a Wordpress install).&lt;/p&gt;

&lt;p&gt;A virtual host would be created with the server_name or ServerName as 'blog.domain1.com' which would be configured to point to the blog files and folders in your public_html folder.&lt;/p&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Language and frameworks&lt;/h3&gt;

&lt;p&gt;It doesn't matter what language or framework your domain uses.&lt;/p&gt;

&lt;p&gt;To serve multiple Rails applications for example requires the same setup for each application.&lt;/p&gt;

&lt;p&gt;Of course, there would be some differences such as port numbers for the mongrel or thin instances. Virtual hosts can't share ports.&lt;/p&gt;

&lt;p&gt;For example, you may create a mongrel cluster to run from port 8000 - 8002 for one domain and another running from 8010 - 8012 for your blog and so on.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Once the Slice has been setup and the web server has been installed, serving multiple domains is very easy:&lt;/p&gt;

&lt;p&gt;Simply create more vhosts...&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-27:2852</id>
    <published>2008-05-27T12:47:00Z</published>
    <updated>2008-05-27T12:49:34Z</updated>
    <category term="Nginx"/>
    <category term="Ruby on Rails"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="Hardy"/>
    <category term="nginx"/>
    <category term="thin"/>
    <category term="vhosts"/>
    <link href="http://articles.slicehost.com/2008/5/27/ubuntu-hardy-nginx-rails-and-thin" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx, rails and thin</title>
<summary type="html">&lt;p&gt;Having installed the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;thin web server for Ruby&lt;/a&gt;, we can now look at configuring a Nginx vhost to proxy to thin so we can serve our Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;The process is easy to follow and easy to repeat for hosting multiple domains.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Having installed the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;thin web server for Ruby&lt;/a&gt;, we can now look at configuring a Nginx vhost to proxy to thin so we can serve our Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;The process is easy to follow and easy to repeat for hosting multiple domains.&lt;/p&gt;
&lt;h3&gt;Setup&lt;/h3&gt;

&lt;p&gt;Firstly you will have needed to follow the previous Nginx articles and installed &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude&quot; title=&quot;Ubuntu Hardy - installing Nginx via aptitude&quot;&gt;via aptitude&lt;/a&gt; or &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx via source&quot;&gt;via source&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You will also need to have installed the thin web server as per the article linked to above.&lt;/p&gt;

&lt;h3&gt;Plan&lt;/h3&gt;

&lt;p&gt;The plan is very simple:&lt;/p&gt;

&lt;p&gt;We'll create a basic rails application and use 3 thin instances running from port 3000 to 3002.&lt;/p&gt;

&lt;p&gt;I won't go into detail as the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;thin web server for Ruby&lt;/a&gt; article shows how to install and configure thin.&lt;/p&gt;

&lt;p&gt;Once that is done, we'll create a simple vhost to proxy requests to the thin instances.&lt;/p&gt;

&lt;p&gt;You may also notice this article is very similar to the &lt;a href=&quot;http://articles.slicehost.com/2008/5/27/ubuntu-hardy-nginx-rails-and-mongrels&quot; title=&quot;Ubuntu Hardy - Nginx, Rails and Mongrels&quot;&gt;Nginx and mongrels article&lt;/a&gt;. There is a very good reason for that - they use exactly the same methods in creating the virtual host.&lt;/p&gt;

&lt;p&gt;All the virtual host has to do is proxy requests to the 3rd party web server - in this case thin, in the sister article, mongrels.&lt;/p&gt;

&lt;h3&gt;Rails Application&lt;/h3&gt;

&lt;p&gt;To create a rail application, move into your public_html folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd /home/demo/public_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and create a new Ruby on Rails application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Thin&lt;/h3&gt;

&lt;p&gt;Ensure you are in the rails folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/public_html/railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a thin configuration file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo thin config -C /etc/thin/railsapp.yml -c /home/demo/public_html/railsapp/  --servers 3 -e production&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's always a good idea to check the created file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cat /etc/thin/railsapp.yml&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents are as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pid: tmp/pids/thin.pid
log: log/thin.log
timeout: 30
max_conns: 1024
port: 3000
max_persistent_conns: 512
chdir: /home/demo/public_html/testapp
environment: production
servers: 3
address: 0.0.0.0
daemonize: true&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unlike with mongrels, we don't need to manually create symlinks, etc to make sure thin is started on a reboot.&lt;/p&gt;

&lt;p&gt;Now all we need to do is start thin:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/thin start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Nginx Virtual Host&lt;/h3&gt;

&lt;p&gt;Let's create the Nginx vhost:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you installed Nginx from source, the path may vary to something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents of the file are as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;upstream domain1 {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
    }

server {
            listen   80;
            server_name  www.domain.com;
            rewrite ^/(.*) http://domain.com permanent;
           }


server {
            listen   80;
            server_name domain.com;

            access_log /home/demo/public_html/railsapp/log/access.log;
            error_log /home/demo/public_html/railsapp/log/error.log;

            root   /home/demo/public_html/railsapp/public/;
            index  index.html;

            location / {
                          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://domain1;
                                           break;
                          }
            }

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Take each section at a time and you will see that the basics are the same as for a 'normal' Nginx vhost.&lt;/p&gt;

&lt;p&gt;We have the server_name, listen, log and index variables.&lt;/p&gt;

&lt;p&gt;Where it differs is the addition of the Rails proxy settings.&lt;/p&gt;

&lt;p&gt;In this example, we will use 3 thin instances running on ports 3000, 3001 and 3002 which I have defined in the 'upstream' setting. I've called it domain1 for ease of use.&lt;/p&gt;

&lt;p&gt;The location settings say that if the requested file exists to serve the static version straight away.&lt;/p&gt;

&lt;p&gt;And if the requested file doesn't exist, pass the request to the thin server.&lt;/p&gt;

&lt;h3&gt;Enable&lt;/h3&gt;

&lt;p&gt;Remember that we need to 'enable' any available vhosts or it won't be served (an easy thing to leave out).&lt;/p&gt;

&lt;p&gt;Referring to the Nginx articles, all we need to do is create a simple symlink:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, if you installed from source:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /usr/local/nginx/sites-available/domain.com /usr/local/nginx/sites-enabled/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Restart&lt;/h3&gt;

&lt;p&gt;Final step is to restart Nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Use the 'stop' and 'start' method as issuing a restart command does not always work with Nginx.&lt;/p&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;All that's left is to navigate to your domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://www.domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where you will be greeted with the rails welcome page.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Setting up Nginx virtual hosts to proxy to the thin server easily completed.&lt;/p&gt;

&lt;p&gt;To serve multiple domains, simply repeat the process with the new domain details.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-27:2846</id>
    <published>2008-05-27T11:08:00Z</published>
    <updated>2008-05-27T12:53:32Z</updated>
    <category term="Nginx"/>
    <category term="Ruby on Rails"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="Hardy"/>
    <category term="mongrel"/>
    <category term="nginx"/>
    <category term="vhosts"/>
    <link href="http://articles.slicehost.com/2008/5/27/ubuntu-hardy-nginx-rails-and-mongrels" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx, rails and mongrels</title>
<summary type="html">&lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article, we can now look at configuring Nginx to proxy to a mongrel cluster so we can serve our Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;The process is very easy and can simply be repeated for serving multiple domains.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article, we can now look at configuring Nginx to proxy to a mongrel cluster so we can serve our Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;The process is very easy and can simply be repeated for serving multiple domains.&lt;/p&gt;
&lt;h3&gt;Setup&lt;/h3&gt;

&lt;p&gt;Firstly you will have needed to follow the previous Nginx articles and installed &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude&quot; title=&quot;Ubuntu Hardy - installing Nginx via aptitude&quot;&gt;via aptitude&lt;/a&gt; or &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx via source&quot;&gt;via source&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You will also need to have installed mongrels as per the article linked to above.&lt;/p&gt;

&lt;h3&gt;Plan&lt;/h3&gt;

&lt;p&gt;The plan is very simple:&lt;/p&gt;

&lt;p&gt;We'll create a basic rails application and use 3 mongrels running from port 5000 to 5002.&lt;/p&gt;

&lt;p&gt;Beyond creating the cluster itself, I won't go into detail as the &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article shows how to install and create mongrel clusters.&lt;/p&gt;

&lt;p&gt;Once that is done, we'll create a simple vhost to proxy requests to the mongrel cluster.&lt;/p&gt;

&lt;p&gt;Simple as that.&lt;/p&gt;

&lt;h3&gt;Rails Application&lt;/h3&gt;

&lt;p&gt;To create a rail application, move into your public_html folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd /home/demo/public_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and create a new Ruby on Rails application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Mongrel Cluster&lt;/h3&gt;

&lt;p&gt;Ensure you are in the rails folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/public_html/railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a mongrel cluster file as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::configure -e production -p 5000 -N 3 -c /home/demo/public_html/railsapp -a 127.0.0.1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's always a good idea to check the created file (config/mongrel_cluster.yml):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cwd: /home/demo/public_html/railsapp
log_file: log/mongrel.log
port: &amp;quot;5000&amp;quot;
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now create a symlink to the /etc/mongrel_cluster folder (this ensures the cluster is restarted on a reboot):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /home/demo/public_html/railsapp/config/mongrel_cluster.yml /etc/mongrel_cluster/railsapp.yml&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now all we need to do is start the cluster:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_cluster_ctl start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Nginx Virtual Host&lt;/h3&gt;

&lt;p&gt;Let's create the Nginx vhost:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you installed Nginx from source, the path may vary to something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents of the file are as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;upstream domain1 {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
        server 127.0.0.1:5002;
    }

server {
            listen   80;
            server_name  www.domain.com;
            rewrite ^/(.*) http://domain.com permanent;
           }


server {
            listen   80;
            server_name domain.com;

            access_log /home/demo/public_html/railsapp/log/access.log;
            error_log /home/demo/public_html/railsapp/log/error.log;

            root   /home/demo/public_html/railsapp/public/;
            index  index.html;

            location / {
                          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://domain1;
                                           break;
                          }
            }

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Take each section at a time and you will see that the basics are the same as for a 'normal' Nginx vhost.&lt;/p&gt;

&lt;p&gt;We have the server_name, listen, log and index variables.&lt;/p&gt;

&lt;p&gt;Where it differs is the addition of the Rails proxy settings.&lt;/p&gt;

&lt;p&gt;In this example, we will use 3 mongrels running on ports 5000, 5001 and 5002 which I have defined in the 'upstream' setting. I've called it domain1 for ease of use.&lt;/p&gt;

&lt;p&gt;The location settings say that if the requested file exists to serve the static version straightaway.&lt;/p&gt;

&lt;p&gt;If the requested file doesn't exist, pass the request on to the mongrels. In this case it would proxy to 127.0.0.1:5000, 5001 and 5002 as those are the ports we defined at the beginning of the file.&lt;/p&gt;

&lt;h3&gt;Enable&lt;/h3&gt;

&lt;p&gt;Remember that we need to 'enable' any available vhosts or it won't be served (an easy thing to leave out).&lt;/p&gt;

&lt;p&gt;Referring to the Nginx articles, all we need to do is create a simple symlink:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/domain.com /etc/nginx/sites-enabled/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, if you installed from source:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /usr/local/nginx/sites-available/domain.com /usr/local/nginx/sites-enabled/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Restart&lt;/h3&gt;

&lt;p&gt;Final step is to restart Nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Use the 'stop' and 'start' method as issuing a restart command does not always work with Nginx.&lt;/p&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;All that's left is to navigate to your domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://www.domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where you will be greeted with the rails welcome page.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Setting up Nginx virtual hosts to proxy to a mongrel cluster is fairly simple.&lt;/p&gt;

&lt;p&gt;To serve multiple domains, simply repeat the process with the new domain details.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-16:2642</id>
    <published>2008-05-16T11:28:00Z</published>
    <updated>2008-05-16T11:29:01Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-host-settings" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx virtual host settings</title>
<summary type="html">&lt;p&gt;The previous Ubuntu Hardy &lt;a href=&quot;http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts&quot; title=&quot;Ubuntu Hardy - Nginx Virtual Hosts&quot;&gt;Nginx Virtual Hosts&lt;/a&gt; article introduced the basics of the setting up virtual hosts.&lt;/p&gt;

&lt;p&gt;This allowed us to serve multiple domains from a single IP address and Slice. Now we can look in detail at more of the settings available to us in the Virtual Hosts files.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;The previous Ubuntu Hardy &lt;a href=&quot;http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts&quot; title=&quot;Ubuntu Hardy - Nginx Virtual Hosts&quot;&gt;Nginx Virtual Hosts&lt;/a&gt; article introduced the basics of the setting up virtual hosts.&lt;/p&gt;

&lt;p&gt;This allowed us to serve multiple domains from a single IP address and Slice. Now we can look in detail at more of the settings available to us in the Virtual Hosts files.&lt;/p&gt;
&lt;h3&gt;Listen&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;server {
    listen   80;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Inside the server module, the 'listen' setting defines the port for that virtual host.&lt;/p&gt;

&lt;p&gt;The two most common ports will be port 80 (the standard HTTP port) and port 443 (the standard HTTPS port).&lt;/p&gt;

&lt;p&gt;Of course, you can add any port. For example, you might want to test a site on port 8080.&lt;/p&gt;

&lt;h3&gt;server_name&lt;/h3&gt;

&lt;p&gt;The usual course of a virtual host file means the next line is the 'server_name'. As such, the beginning of a vhost server module would normally look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {
    listen   80;
    server_name domain1.com;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also add a simple rewrite rule here to direct users from www.domain1.com to domain1.com:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {
  listen   80;
  server_name  www.domain1.com;
  rewrite ^/(.*) http://domain1.com permanent;
}&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;alias&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;location  /img/ {
        alias  /home/public_html/domain.com/images/;
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sets an alias location.&lt;/p&gt;

&lt;p&gt;In this instance the address '/img/dog.png' would return the file located at:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/home/public_html/domain.com/images/dog.png&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This can make for cleaner image/file url's and means you can 'share' an image directory with another site on the same Slice.&lt;/p&gt;

&lt;h3&gt;root&lt;/h3&gt;

&lt;p&gt;Following on from the 'alias' setting is a similar one where you can set the 'root' of a location:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;location  /img/ {
        root /home/public_html/domain.com;
    }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This time, a request for '/img/dog.png' would return the file located at:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/home/public_html/domain.com/img/dog.png&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There is a subtle, but fundamental difference between the 'alias' and 'root' settings.&lt;/p&gt;

&lt;h3&gt;error_page&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;error_page    404    /error/404.html;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sets the location for a custom error page. In this example I have an 'error' folder in my public directory where I place my custom error pages.&lt;/p&gt;

&lt;p&gt;You can redirect multiple error types to one file as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error_page    502 503 504    /error/50x_error.html;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;internal&lt;/h3&gt;

&lt;p&gt;Following from the error_page setting, it is possible to ensure the user only receives the error page on an actual error.&lt;/p&gt;

&lt;p&gt;In other words, at the moment, a user can enter the url:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://domain.com/error/50x_error.html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and be served the 50x_error.html file.&lt;/p&gt;

&lt;p&gt;Not very good, especially if the error file contains variables which may help the user in working out why they are receiving the error (such as previous page, IP address, etc).&lt;/p&gt;

&lt;p&gt;We can set the 'internal' tag to ensure specific pages are only served from an internal trigger and not from an external url request.&lt;/p&gt;

&lt;p&gt;Let's have look at an example which includes the above 500 error_page example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error_page    502 503 504    /error/50x_error.html;

location    /error/50x_error.html {
    internal;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will ensure that the /error/50x_error.html file is only triggered and served via an internal error.&lt;/p&gt;

&lt;p&gt;Now if one were to navigate to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://domain.com/error/50x_error.html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You would receive a 404 (file not found) error instead of directly accessing the 50x_error.html file.&lt;/p&gt;

&lt;p&gt;Much better. &lt;/p&gt;

&lt;p&gt;You can also set this for the 404.html page but there is little point in doing that as all it would do is serve you the same 404 page!&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Nginx has many Virtual Host settings and there are more available for use.&lt;/p&gt;

&lt;p&gt;If you want to delve into the depths of Nginx you should have a look at the main &lt;a href=&quot;http://wiki.codemongers.com/NginxHttpCoreModule&quot; title=&quot;Nginx Http Core Modules&quot;&gt;Http Core Modules&lt;/a&gt; page on the Nginx site.&lt;/p&gt;

&lt;p&gt;However, there should be enough here to get you going with the basics of setting up your vhosts.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-16:2641</id>
    <published>2008-05-16T10:50:00Z</published>
    <updated>2008-05-16T11:03:14Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx Virtual Hosts</title>
<summary type="html">&lt;p&gt;Now we have Nginx installed (whether via the package manager or from source) we are in a position to serve multiple domains using Virtual Hosts.&lt;/p&gt;

&lt;p&gt;Do note the layout used in this article is explained &lt;a href=&quot;http://articles.slicehost.com/2007/9/13/multiple-hosts-layout&quot; title=&quot;Multiple hosts layout&quot;&gt;here&lt;/a&gt; - feel free to use the directories of your choice.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Now we have Nginx installed (whether via the package manager or from source) we are in a position to serve multiple domains using Virtual Hosts.&lt;/p&gt;

&lt;p&gt;Do note the layout used in this article is explained &lt;a href=&quot;http://articles.slicehost.com/2007/9/13/multiple-hosts-layout&quot; title=&quot;Multiple hosts layout&quot;&gt;here&lt;/a&gt; - feel free to use the directories of your choice.&lt;/p&gt;
&lt;h3&gt;Create the layout&lt;/h3&gt;

&lt;p&gt;Let's create the basic layout for each domain. In your home directory create a 'public_html' folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir /home/demo/public_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now for each domain you want to host (I use the examples of domain1.com and domain2.com) create a folder with a standard set of sub-folders:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir -p /home/demo/public_html/domain1.com/{public,private,log,backup}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir -p /home/demo/public_html/domain2.com/{public,private,log,backup}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will create the folders public, private, log and backup for each of our domains (domain1.com and domain2.com).&lt;/p&gt;

&lt;h3&gt;index.html&lt;/h3&gt;

&lt;p&gt;The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.&lt;/p&gt;

&lt;p&gt;So for each domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;nano /home/demo/public_html/domain1.com/public/index.html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Enter something like this into the file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;domain1.com&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;domain1.com&amp;lt;/h1&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Repeat the process so you have a similar file for domain2.com (don't forget to change the index.html content so it shows domain2.com and not domain1.com).&lt;/p&gt;

&lt;h3&gt;Virtual Hosts Layout&lt;/h3&gt;

&lt;p&gt;If you have been following the articles for the Nginx install, you will have a 'Debian' style layout (using sites-available and sites-enabled folders) whether you installed via the package manager or via source.&lt;/p&gt;

&lt;p&gt;As such, we'll use that layout from now on when creating the virtual hosts.&lt;/p&gt;

&lt;h3&gt;Virtual Host&lt;/h3&gt;

&lt;p&gt;Let's go ahead and create the vhost file for domain1:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remember to adjust the path according to your install. So installing from source would require:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/sites-available/domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {

            listen   80;
            server_name  www.domain1.com;
            rewrite ^/(.*) http://domain1.com permanent;

           }


server {

            listen   80;
            server_name domain1.com;

            access_log /home/demo/public_html/domain1.com/log/access.log;
            error_log /home/demo/public_html/domain1.com/log/error.log;

            location / {

                        root   /home/demo/public_html/domain1.com/public/;
                        index  index.html;

                        }

            }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This example vhost is pretty basic. However, the next article on Nginx virtual hosts will include details of many more settings that are available.&lt;/p&gt;

&lt;p&gt;The first server module in the file is a simple rewrite rule that redirects visitors to domain1.com from www.domain1.com.&lt;/p&gt;

&lt;p&gt;You can, of course, have this the other way around if you prefer.&lt;/p&gt;

&lt;p&gt;The second server module has very basic information including the server_name which is the domain name you want to serve.&lt;/p&gt;

&lt;p&gt;It then defines the log locations for easy analysis and finally sets the server root and the index file.&lt;/p&gt;

&lt;p&gt;As said, very basic at this stage.&lt;/p&gt;

&lt;h3&gt;sites-enabled&lt;/h3&gt;

&lt;p&gt;Last configuration we need to do is to 'enable' our site.&lt;/p&gt;

&lt;p&gt;This is done with a symlink in the sites-enabled directory as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, depending on how you installed Nginx, you may need to adjust the paths:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /usr/local/nginx/sites-available/domain1.com /usr/local/nginx/sites-enabled/domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Restart Nginx&lt;/h3&gt;

&lt;p&gt;Although there is a restart option for the Nginx init script, it doesn't always work as expected and may not facilitate any changes you have made.&lt;/p&gt;

&lt;p&gt;As such, I recommend a stop and start approach rather than a simple restart:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;Now when you navigate to your domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://www.domain1.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will see the equivalent of this:&lt;/p&gt;

&lt;p&gt;&lt;img title=&quot;Nginx domain1.com&quot; src=&quot;http://articles.slicehost.com/assets/2007/12/14/nginx_domain1.jpg&quot; alt=&quot;Nginx domain1.com&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Nice.&lt;/p&gt;

&lt;h3&gt;Rinse and Repeat&lt;/h3&gt;

&lt;p&gt;All you need to do for your next virtual host (domain2.com in this example) is to repeat the process.&lt;/p&gt;

&lt;p&gt;I know I mention it a lot, but do remember to adjust any paths to match your Nginx installation.&lt;/p&gt;

&lt;p&gt;So create a virtual host file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/nginx/sites-available/domain2.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Enter the details as shown above but for domain2.com and then create a symlink in the sites-enabled folder like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/domain2.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Restart Nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And away you go.&lt;/p&gt;

&lt;h3&gt;Logs&lt;/h3&gt;

&lt;p&gt;Remember we defined custom locations for the domain logs?&lt;/p&gt;

&lt;p&gt;Well, let's have a check they are there:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ls /home/demo/public_html/domain1.com/log/
...
access.log  error.log&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Excellent, everything is working as we expected and we have our domain logs in a nice and convenient location.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Setting up virtual hosts with Nginx is a simple process using the sites-available and sites-enabled folders.&lt;/p&gt;

&lt;p&gt;Although the example here is quite basic, I hope you can see that getting to grips with Nginx syntax and configurations is not too difficult.&lt;/p&gt;

&lt;p&gt;The next article will concentrate on some other settings for use in the virtual hosts file, thus allowing for more control and flexibility for your hosting needs.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-15:2624</id>
    <published>2008-05-15T15:04:00Z</published>
    <updated>2008-05-16T11:13:44Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/15/ubuntu-hardy-nginx-configuration" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx configuration</title>
<summary type="html">&lt;p&gt;Whether you have installed Nginx using the &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude&quot; title=&quot;Ubuntu Hardy - installing Nginx via aptitude&quot;&gt;package manager&lt;/a&gt; or &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx from source&quot;&gt;from source&lt;/a&gt;, you will need to look at the main configuration file and see what may need changing and optimising.&lt;/p&gt;

&lt;p&gt;Although I'll make some suggestions, the aim is not to change a great deal at this point. Rather, we will look at the main settings, see what they mean and what a change will actually do.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Whether you have installed Nginx using the &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude&quot; title=&quot;Ubuntu Hardy - installing Nginx via aptitude&quot;&gt;package manager&lt;/a&gt; or &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx from source&quot;&gt;from source&lt;/a&gt;, you will need to look at the main configuration file and see what may need changing and optimising.&lt;/p&gt;

&lt;p&gt;Although I'll make some suggestions, the aim is not to change a great deal at this point. Rather, we will look at the main settings, see what they mean and what a change will actually do.&lt;/p&gt;
&lt;h3&gt;Defaults&lt;/h3&gt;

&lt;p&gt;So why only a few changes to the default? Well, it's difficult to give a definitive configuration as there are so many variables to consider such as expected site traffic, Slice size, site type, etc.&lt;/p&gt;

&lt;p&gt;During this article we'll discuss the main settings and you can make any decisions as to what you feel are best for your site. Any changes I do suggest are simply that: suggestions.&lt;/p&gt;

&lt;p&gt;My advice is very simple: experiment. Find what works best on your setup.&lt;/p&gt;

&lt;h3&gt;nginx.conf&lt;/h3&gt;

&lt;p&gt;Assuming you installed via the package manager, open up the main Nginx config file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/nginx/nginx.conf&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you installed from source, the location may be different:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/conf/nginx.conf&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The default file is very similar in both case (again, assuming you followed the articles shown above):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;

    include /etc/nginx/sites-enabled/*;

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The main difference you will see if you installed from source is the path in the 'include' setting which would be something like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;include /usr/local/nginx/sites-enabled/*;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Beyond that, any changes are minor and can be adjusted as discussed below although I won't mention some of the more obvious settings such access logs and pid's.&lt;/p&gt;

&lt;h3&gt;user&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user www-data;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can imagine, this sets the nginx user.&lt;/p&gt;

&lt;p&gt;I always push for consistency across servers and the default web server user on Debian based systems is www-data. As such, keep this as the www-data user.&lt;/p&gt;

&lt;p&gt;You can also add a group to this setting and it may be an idea to do so as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user www-data www-data;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;worker_processes&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;worker_processes  1;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Nginx can have more than one worker process running at the same time.&lt;/p&gt;

&lt;p&gt;To take advantage of SMP and to enable good efficiency I would recommend changing this to read:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;worker_processes  4;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Although you can experiment with this number (and I encourage you to do so) setting it at more than 4 processes may actually cause Nginx to be less efficienct on your Slice.&lt;/p&gt;

&lt;h3&gt;worker_connections&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;events {
    worker_connections  1024;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the worker_connections setting is placed inside the 'events' module.&lt;/p&gt;

&lt;p&gt;Sets the number of connections that each worker can handle. This is a good default setting.&lt;/p&gt;

&lt;p&gt;You can work out the maximum clients value from this and the worker_processes settings:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;max_clients = worker_processes * worker_connections&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;http module&lt;/h3&gt;

&lt;p&gt;Next comes the http module which contains base settings for http access:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;include       /etc/nginx/mime.types;
default_type  application/octet-stream;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Unless you have an overwhelming desire, I would leave these settings alone (again, for those who installed via source, adjust the paths to those of your install).&lt;/p&gt;

&lt;p&gt;You can, of course, add more includes if you want to customise it but messing with mime-types usually ends up with broken web pages and download errors.&lt;/p&gt;

&lt;p&gt;Mind you, it is good fun to play with!&lt;/p&gt;

&lt;h3&gt;sendfile&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sendfile        on;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sendfile is used when the server (Nginx) can actually ignore the contents of the file it is sending. It uses the kernel sendfile support instead of using it's own resources on the request.&lt;/p&gt;

&lt;p&gt;It is generally used for larger files (such as images) which do not need use of a multiple request/confirmation system to be served - thus freeing resources for items that do need that level of 'supervision' from Nginx.&lt;/p&gt;

&lt;p&gt;Keep it an on unless you know why you need to turn it off.&lt;/p&gt;

&lt;h3&gt;tcp&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#tcp_nopush     on;
tcp_nodelay        on;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;tcp_nopush&lt;/strong&gt;: Sends the HTTP response headers in one packet. You can read more about tcp_nopush on this &lt;a href=&quot;http://wiki.codemongers.com/ReadMoreAboutTcpNopush&quot; title=&quot;tcp_nopush&quot;&gt;page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I would change the default here and uncomment the setting as it is useful when combined with the sendfile option we set earlier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;tcp_nodelay&lt;/strong&gt;: Disables the Nagle buffering algorithm. Well, that cleared that one up!&lt;/p&gt;

&lt;p&gt;Actually, it is for use with items than do not require a response. General web use does require a response from the client and so, going against the default, I would change this to off.&lt;/p&gt;

&lt;p&gt;You can read more about tcp_nodelay &lt;a href=&quot;http://wiki.codemongers.com/ReadMoreAboutTcpNodelay&quot; title=&quot;tcp_nodelay&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So there you are. After saying I wouldn't change a lot, I have changed the two default tcp settings. Your experience may show otherwise and, again, all I can say is experiment with your site/app - what do &lt;em&gt;you&lt;/em&gt; need?&lt;/p&gt;

&lt;h3&gt;keepalive&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#keepalive_timeout  0;
keepalive_timeout  65;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The default is very high and can easily be reduced to a few seconds (an initial setting of 2 or 3 is a good place to start and you will rarely need more than that). If no new requests are received during this time the connection is killed.&lt;/p&gt;

&lt;p&gt;OK, but what does it mean? Well, once a connection has been established and the client has requested a file, this says &quot;sit there and ignore everyone else until the time limit is reached or you get a new request from the client&quot;.&lt;/p&gt;

&lt;p&gt;Why would you want a higher time? In cases where there will be a lot of interactivity on the site. However, in most cases, people will go to a page, read it for a while and then click for the next page. You don't want the connection sat there doing nothing and ignoring other users. &lt;/p&gt;

&lt;h3&gt;gzip&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gzip  on;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Good. We like gzip. It allows for instant, real time compression.&lt;/p&gt;

&lt;p&gt;However, I would add a few more settings as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gzip_comp_level 2;
gzip_proxied any;
gzip_types      text/plain text/html text/css application/x-javascript text/xml application/xml 
application/xml+rss text/javascript;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I think those are self explanatory and simply add to the gzip setting. You can read more about the various gzip settings on &lt;a href=&quot;http://wiki.codemongers.com/NginxHttpGzipModule&quot; title=&quot;gzip module&quot;&gt;this page&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;include&lt;/h3&gt;

&lt;p&gt;Default:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;include /etc/nginx/sites-enabled/*;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you installed from source, we added this line:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;include /usr/local/nginx/sites-enabled/*;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Either way, it defines what files to include that are located outside of the main nginx.conf.&lt;/p&gt;

&lt;p&gt;In this case, it points to the sites-enabled directory so it will include any symlinks. Thus enabling any 'available' sites.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;There is a lot going on in this article, especially from such a small config file.&lt;/p&gt;

&lt;p&gt;However, taking one setting at a time, we can see that each one is not only essential but pretty flexible.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://articles.slicehost.com/2008/5/16/ubuntu-hardy-nginx-virtual-hosts&quot; title=&quot;Ubuntu Hardy - Nginx Virtual Hosts&quot;&gt;next article&lt;/a&gt; will take you through setting up virtual hosts and then move onto mongrel and thin integration for your Ruby on Rails applications.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-15:2618</id>
    <published>2008-05-15T11:19:00Z</published>
    <updated>2008-05-15T11:33:46Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/15/ubuntu-hardy-nginx-from-source-layout" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Nginx from source layout</title>
<summary type="html">&lt;p&gt;Following from the main &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx from source&quot;&gt;installing Nginx from source&lt;/a&gt; and &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-adding-an-nginx-init-script&quot; title=&quot;Ubuntu Hardy - creating an Nginx init script&quot;&gt;creating an init script&lt;/a&gt; articles we can now move onto looking at the Nginx layout.&lt;/p&gt;

&lt;p&gt;The idea of this article is to change the default layout to one more in keeping with the 'Debian' style. Installing Nginx via the package manager creates this layout automatically - we need to do it by hand.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Following from the main &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing Nginx from source&quot;&gt;installing Nginx from source&lt;/a&gt; and &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-adding-an-nginx-init-script&quot; title=&quot;Ubuntu Hardy - creating an Nginx init script&quot;&gt;creating an init script&lt;/a&gt; articles we can now move onto looking at the Nginx layout.&lt;/p&gt;

&lt;p&gt;The idea of this article is to change the default layout to one more in keeping with the 'Debian' style. Installing Nginx via the package manager creates this layout automatically - we need to do it by hand.&lt;/p&gt;
&lt;h3&gt;Why?&lt;/h3&gt;

&lt;p&gt;You do, of course, have the option of leaving the layout exactly as it is.&lt;/p&gt;

&lt;p&gt;However, any further Nginx articles will use the 'Debian' layout which includes sites-available and sites-enabled folders.&lt;/p&gt;

&lt;p&gt;This gives common ground to those who installed Nginx via the 'aptitude' package manager and those who installed Nginx via source.&lt;/p&gt;

&lt;p&gt;As an aside, I also find it a very convenient and easy way of organising my virtual hosts.&lt;/p&gt;

&lt;h3&gt;Layout&lt;/h3&gt;

&lt;p&gt;As alluded to, the layout we want to create is the same as if we installed Nginx via the package manager.&lt;/p&gt;

&lt;p&gt;The main difference being that any any virtual host files are kept in a 'sites-available' folder - each vhost has its own file and configurations.&lt;/p&gt;

&lt;p&gt;This makes for easy administration as they are not all placed in one unwieldy monolithic file.&lt;/p&gt;

&lt;p&gt;One thing to note is that any vhosts in the 'sites-available' folder are not 'live' - to enable a vhost, we simply place a symlink in a 'sites-enabled' folder pointing to the vhost file.&lt;/p&gt;

&lt;h3&gt;Folders&lt;/h3&gt;

&lt;p&gt;Let's start by creating the two main folders (remember Nginx was installed into '/usr/local/nginx'):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo mkdir /usr/local/nginx/sites-available
...
sudo mkdir /usr/local/nginx/sites-enabled&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Configuration&lt;/h3&gt;

&lt;p&gt;Now we need to adjust the main Nginx configuration file to look in the sites-enabled folder for our vhosts:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/conf/nginx.conf&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I won't list the contents of the file here but note that the vast majority of the contents consists of two virtual hosts - a default for port 80 and a default for port 443 (which is actually commented out).&lt;/p&gt;

&lt;p&gt;This is a great example of an unwieldy file - it contains just two virtual hosts and is already difficult to follow in a terminal.&lt;/p&gt;

&lt;p&gt;The easiest thing is to delete the contents of the file (you can make a copy if you wish) and replace it with this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user www-data;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    include /usr/local/nginx/sites-enabled/*;

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents are very similar to those created when installing Nginx via the package manager.&lt;/p&gt;

&lt;p&gt;Let's save the file and move onto creating a default vhost.&lt;/p&gt;

&lt;h3&gt;Default vhost&lt;/h3&gt;

&lt;p&gt;Create a default vhost file in the sites-available folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /usr/local/nginx/sites-available/default&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents are very simple and come from the original configuration:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;server {
    listen       80;
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }


    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To enable the vhost all we need to do is create a symlink:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /usr/local/nginx/sites-available/default /usr/local/nginx/sites-enabled/default&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Restart&lt;/h3&gt;

&lt;p&gt;Restart Nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx restart&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Navigating to your Slice IP will show the default &quot;Welcome to nginx!' page.&lt;/p&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;p&gt;To create any further virtual hosts all you need to do is create the main file in the sites-available folder, create a symlink in the sites-enabled folder and restart Nginx.&lt;/p&gt;

&lt;p&gt;All very simple and very organised.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Mirroring the layout used when installing Nginx via the aptitude package manager allows for much easier administration of your virtual hosts - each vhost has its own file and can be switched on and off by a symlink to the sites-enabled folder.&lt;/p&gt;

&lt;p&gt;The next article will look at configuring and optimising Nginx for use on your Slice.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-13:2575</id>
    <published>2008-05-13T17:40:00Z</published>
    <updated>2008-05-27T14:56:01Z</updated>
    <category term="API"/>
    <category term="SliceManager"/>
    <category term="api"/>
    <category term="ebook"/>
    <category term="slicemanager"/>
    <link href="http://articles.slicehost.com/2008/5/13/slicemanager-api-documentation" rel="alternate" type="text/html"/>
    <title>SliceManager API Documentation</title>
<summary type="html">&lt;p&gt;The SliceManager API allows for the easy and convenient manipulation of your Slicehost account.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;The SliceManager API allows for the easy and convenient manipulation of your Slicehost account.&lt;/p&gt;
&lt;h3&gt;Latest Version&lt;/h3&gt;

&lt;p&gt;Any changes to the API process will be shown in the latest ebook version:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://articles.slicehost.com/assets/2008/5/27/Slicehost_API.pdf&quot; title=&quot;SliceManager API ebook - v1.3.0.pdf&quot;&gt;SliceManager API ebook - v1.3.0.pdf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please see below for a changelog between versions.&lt;/p&gt;

&lt;h3&gt;Changelog&lt;/h3&gt;

&lt;p&gt;v1.3.0 - May 22 2008&lt;/p&gt;

&lt;p&gt;Addition of flavor image and slice&lt;/p&gt;

&lt;p&gt;v1.2.0 - April 2008&lt;/p&gt;

&lt;p&gt;Official release.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-13:2571</id>
    <published>2008-05-13T15:18:00Z</published>
    <updated>2008-05-13T15:29:55Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/13/ubuntu-hardy-adding-an-nginx-init-script" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - adding an nginx init script</title>
<summary type="html">&lt;p&gt;If you decided to install Nginx via source (see the &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing nginx from source&quot;&gt;previous article&lt;/a&gt;) you would have the latest and greatest version.&lt;/p&gt;

&lt;p&gt;However, one disadvantage of installing from source is that init scripts are not created. No problem, let's go ahead and create one for easy control of Nginx and to ensure it restarts on a reboot.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;If you decided to install Nginx via source (see the &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing nginx from source&quot;&gt;previous article&lt;/a&gt;) you would have the latest and greatest version.&lt;/p&gt;

&lt;p&gt;However, one disadvantage of installing from source is that init scripts are not created. No problem, let's go ahead and create one for easy control of Nginx and to ensure it restarts on a reboot.&lt;/p&gt;
&lt;h3&gt;Assumption&lt;/h3&gt;

&lt;p&gt;I am assuming you have followed the &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source&quot; title=&quot;Ubuntu Hardy - installing nginx from source&quot;&gt;previous article&lt;/a&gt; and installed Nginx from source.&lt;/p&gt;

&lt;p&gt;If you have used other options or have placed the nginx binary in a directory other than /usr/local/sbin/ then you will need to adjust the script shown below to match your installation.&lt;/p&gt;

&lt;h3&gt;Stop&lt;/h3&gt;

&lt;p&gt;If you have nginx running then stop the process using:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo kill `cat /usr/local/nginx/logs/nginx.pid`&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Init script&lt;/h3&gt;

&lt;p&gt;The script I use below is from an Ubuntu Hardy 'aptitude' install and has been adapted to take into account our custom install of nginx.&lt;/p&gt;

&lt;p&gt;Let's go ahead and create the script:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/init.d/nginx&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Inside the blank file place the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi

set -e

case &amp;quot;$1&amp;quot; in
  start)
        echo -n &amp;quot;Starting $DESC: &amp;quot;
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo &amp;quot;$NAME.&amp;quot;
        ;;
  stop)
        echo -n &amp;quot;Stopping $DESC: &amp;quot;
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo &amp;quot;$NAME.&amp;quot;
        ;;

  restart|force-reload)
        echo -n &amp;quot;Restarting $DESC: &amp;quot;
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo &amp;quot;$NAME.&amp;quot;
        ;;
  reload)
      echo -n &amp;quot;Reloading $DESC configuration: &amp;quot;
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON
      echo &amp;quot;$NAME.&amp;quot;
      ;;
  *)
        N=/etc/init.d/$NAME
        echo &amp;quot;Usage: $N {start|stop|restart|reload|force-reload}&amp;quot; &amp;gt;&amp;amp;2
        exit 1
        ;;
esac

exit 0&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There's not really the space to go into the workings of the script but suffice to say, it defines where the main nginx binary and pid files are located so nginx can be started correctly.&lt;/p&gt;

&lt;h3&gt;Execute&lt;/h3&gt;

&lt;p&gt;As the init file is a shell script, it needs to have executable permissions.&lt;/p&gt;

&lt;p&gt;We set them like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo chmod +x /etc/init.d/nginx&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;update-rc&lt;/h3&gt;

&lt;p&gt;Now we have the base script prepared, we need to add it to the default run levels:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /usr/sbin/update-rc.d -f nginx defaults&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output will be similar to this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Adding system startup for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx -&amp;gt; ../init.d/nginx
   /etc/rc1.d/K20nginx -&amp;gt; ../init.d/nginx
   /etc/rc6.d/K20nginx -&amp;gt; ../init.d/nginx
   /etc/rc2.d/S20nginx -&amp;gt; ../init.d/nginx
   /etc/rc3.d/S20nginx -&amp;gt; ../init.d/nginx
   /etc/rc4.d/S20nginx -&amp;gt; ../init.d/nginx
   /etc/rc5.d/S20nginx -&amp;gt; ../init.d/nginx&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Start, Stop and Restart&lt;/h3&gt;

&lt;p&gt;Now we can start, stop and restart nginx just as with any other service:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx start
...
sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx restart&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The script will also be called on a reboot so nginx will automatically start.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Adding a process to the run levels like this saves a lot of frustration and effort, not only in manually starting and stopping the process, but it having it automatically start on a reboot.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-13:2570</id>
    <published>2008-05-13T10:46:00Z</published>
    <updated>2008-05-15T12:04:55Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-from-source" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Installing Nginx from source</title>
<summary type="html">&lt;p&gt;&lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx home page&quot;&gt;Nginx&lt;/a&gt; is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.&lt;/p&gt;

&lt;p&gt;This article will look at installing Nginx from source on an Ubuntu Hardy Slice.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;&lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx home page&quot;&gt;Nginx&lt;/a&gt; is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.&lt;/p&gt;

&lt;p&gt;This article will look at installing Nginx from source on an Ubuntu Hardy Slice.&lt;/p&gt;
&lt;h3&gt;Versions&lt;/h3&gt;

&lt;p&gt;At the time of writing, the latest stable version of Nginx is 0.6.31. You can check the latest versions and change logs at the &lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx Home Page&quot;&gt;main nginx site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Do note that Nginx is also available from Hardy's package manager (aptitude) and is at version 0.5.33.&lt;/p&gt;

&lt;p&gt;Using the inbuilt package manager will ensure any dependencies are taken care of and any security issue (if any arise) are taken care of with a simple update.&lt;/p&gt;

&lt;p&gt;Please see &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude&quot; title=&quot;Ubuntu Hardy - installing Nginx via aptitude&quot;&gt;this article&lt;/a&gt; if you want to install Nginx with the package manager.&lt;/p&gt;

&lt;h3&gt;Layout&lt;/h3&gt;

&lt;p&gt;One final difference that is worth considering is the layout differences between using the packaged version and installing from source.&lt;/p&gt;

&lt;p&gt;Ubuntu's packaged version of Nginx uses a 'sites-available' and 'sites-enabled' layout in the same manner as a Debian based Apache install.&lt;/p&gt;

&lt;p&gt;Although installing from source does not automatically create this layout, I will show how to adjust the folders and config files to do so.&lt;/p&gt;

&lt;p&gt;Mirroring the layout used in the packed version of Nginx gives us 'common ground' when moving onto creating virtual hosts and further configuration articles.&lt;/p&gt;

&lt;h3&gt;Dependencies&lt;/h3&gt;

&lt;p&gt;As we are not using the package manager to install nginx, we need to take care of some dependencies.&lt;/p&gt;

&lt;p&gt;Not many are needed and include pcre, ssl and zlib packages:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Download&lt;/h3&gt;

&lt;p&gt;Now we can download the source code.&lt;/p&gt;

&lt;p&gt;Create a 'sources' directory (you can download it anywhere you want to):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mkdir ~/sources&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now move into the folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/sources/&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And then download the source code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Unpack&lt;/h3&gt;

&lt;p&gt;Unpack the downloaded tar file and move into the newly created directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;tar -zxvf nginx-0.6.31.tar.gz
...
cd nginx-0.6.31/&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Options&lt;/h3&gt;

&lt;p&gt;There are quite a few compile time options that are available to use.&lt;/p&gt;

&lt;p&gt;Have a look at the &lt;a href=&quot;http://wiki.codemongers.com/NginxInstallOptions&quot; title=&quot;Nginx Install Options&quot;&gt;Install Options&lt;/a&gt; page of the nginx wiki for full details.&lt;/p&gt;

&lt;p&gt;We're going to use just two options to customise the install. The first of which is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;--sbin-path=/usr/local/sbin&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By default, nginx will be installed in /usr/local/nginx which, although a good place, does mean the main nginx binary will be found in /usr/local/nginx/sbin/nginx.&lt;/p&gt;

&lt;p&gt;Not a location we are likely to find in our default search paths. So instead of adding new directories to our path (which may cause errors later on) we simply define where to put the binary.&lt;/p&gt;

&lt;p&gt;The second option is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;--with-http_ssl_module&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Probably self explanatory, but this will enable the ssl module to be compiled so we can parse https requests.&lt;/p&gt;

&lt;h3&gt;Compile&lt;/h3&gt;

&lt;p&gt;Let's go ahead and compile nginx using those two options:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;./configure --sbin-path=/usr/local/sbin --with-http_ssl_module&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There will be a nice summary at the end of the compile which includes such items as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;...
nginx path prefix: &amp;quot;/usr/local/nginx&amp;quot;
nginx binary file: &amp;quot;/usr/local/sbin&amp;quot;
nginx configuration file: &amp;quot;/usr/local/nginx/conf/nginx.conf&amp;quot;
...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Keep a note of the output as it does contain some useful paths and locations for various files.&lt;/p&gt;

&lt;h3&gt;Make&lt;/h3&gt;

&lt;p&gt;Ok, let's go ahead and 'make' and then 'make install':&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;make
...
sudo make install&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Defaults&lt;/h3&gt;

&lt;p&gt;Done. We can test the install by starting nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /usr/local/sbin/nginx&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and navigating to our Slice IP address (assuming you have no other server running):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://123.45.67.890&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will be greeted with the standard welcome page:&lt;/p&gt;

&lt;p&gt;&lt;img title=&quot;Nginx Welcome Screen&quot; src=&quot;http://articles.slicehost.com/assets/2007/10/16/001_welcome_screen.png&quot; alt=&quot;Nginx Welcome Screen&quot; /&gt;&lt;/p&gt;

&lt;h3&gt;Stop&lt;/h3&gt;

&lt;p&gt;If you have used Apache or some other web server (or even installing Nginx via a package manager), you may be used to using a script to start and stop it such as you might find in '/etc/init.d/'.&lt;/p&gt;

&lt;p&gt;Unfortunately, compiling from source does not create a script like that (we will create an init script in the next article or two).&lt;/p&gt;

&lt;p&gt;You may have noticed that one of the paths noted in the compile summary was:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;nginx pid file: &amp;quot;/usr/local/nginx/logs/nginx.pid&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let's make use of that to stop nginx:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo kill `cat /usr/local/nginx/logs/nginx.pid`&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will use the output of the pid file to kill the nginx process - note the use of backslashes (`) and not single quotes (') in the command.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Although quite detailed, we have installed the latest version of nginx from source and compiled it with some custom options to make life slightly easier.&lt;/p&gt;

&lt;p&gt;We also know how to start and stop nginx by hand.&lt;/p&gt;

&lt;p&gt;As mentioned, the next couple of articles will deal with learning how to &lt;a href=&quot;http://articles.slicehost.com/2008/5/13/ubuntu-hardy-adding-an-nginx-init-script&quot; title=&quot;Ubuntu Hardy - adding an Nginx init script&quot;&gt;create an init script&lt;/a&gt; so we have more control over Nginx and in &lt;a href=&quot;http://articles.slicehost.com/2008/5/15/ubuntu-hardy-nginx-from-source-layout&quot; title=&quot;Ubuntu Hardy - Nginx from source layout&quot;&gt;mirroring the 'Debian' style layout&lt;/a&gt; to include sites-available and sites-enabled folders and so on.&lt;/p&gt;

&lt;p&gt;Once done, we will then look at configuring nginx, setting up vhosts and proxying to a backend (such as a cluster of mongrels).&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-13:2569</id>
    <published>2008-05-13T10:07:00Z</published>
    <updated>2008-05-13T10:07:46Z</updated>
    <category term="Nginx"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="nginx"/>
    <link href="http://articles.slicehost.com/2008/5/13/ubuntu-hardy-installing-nginx-via-aptitude" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Installing Nginx via aptitude</title>
<summary type="html">&lt;p&gt;&lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx home page&quot;&gt;Nginx&lt;/a&gt; is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.&lt;/p&gt;

&lt;p&gt;This article will look at installing Nginx on an Ubuntu Hardy Slice using the 'aptitude' package manager.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;&lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx home page&quot;&gt;Nginx&lt;/a&gt; is a popular lightweight server for those who do not need the bulk and extra services that Apache may offer.&lt;/p&gt;

&lt;p&gt;This article will look at installing Nginx on an Ubuntu Hardy Slice using the 'aptitude' package manager.&lt;/p&gt;
&lt;p&gt;Using the inbuilt package manager to install packages is a great idea as it solves dependency issues and you are assured of any security updates if, and when, they occur.&lt;/p&gt;

&lt;p&gt;However, one drawback can be that it is rare for a version upgrade to be placed into the repositories. As such, it is possible for a newer version of an application to be released and not be placed into the repository.&lt;/p&gt;

&lt;h3&gt;Versions&lt;/h3&gt;

&lt;p&gt;Using aptitude, Ubuntu Hardy will instal Nginx version 0.5.33.&lt;/p&gt;

&lt;p&gt;However, at the time of writing, the latest stable version of Nginx is 0.6.31. You can check the latest versions and change logs at the &lt;a href=&quot;http://nginx.net/&quot; title=&quot;Nginx Home Page&quot;&gt;main nginx site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Which one you choose is, of course, entirely up to you.&lt;/p&gt;

&lt;p&gt;If you decide you would rather have a later version of Nginx than the one aptitude provides, then please refer to the sister article (link on the way) which will take you through the process of installing Nginx from source.&lt;/p&gt;

&lt;h3&gt;Install and Dependencies&lt;/h3&gt;

&lt;p&gt;Installing Nginx is incredibly simple as it involves one command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo aptitude install nginx&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will install any and all dependencies that Nginx requires such as libpcre.&lt;/p&gt;

&lt;h3&gt;Start&lt;/h3&gt;

&lt;p&gt;One odd thing is that Nginx is not started automatically:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done&lt;/p&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;Now simply navigate to your IP address:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://123.45.67.890&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and you will see the wonderfully simple welcome screen:&lt;/p&gt;

&lt;p&gt;&lt;img title=&quot;Nginx Welcome Screen&quot; src=&quot;http://articles.slicehost.com/assets/2007/10/16/001_welcome_screen.png&quot; alt=&quot;Nginx Welcome Screen&quot; /&gt;&lt;/p&gt;

&lt;h3&gt;Init scripts&lt;/h3&gt;

&lt;p&gt;As you would imagine when installing an application with the aptitude package manager, all init scripts have been created and added to the relevant run levels.&lt;/p&gt;

&lt;p&gt;Controlling nginx is done with these commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/nginx start
...
sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx restart&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's it.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Using the aptitude package manager makes the installation of Nginx and associated dependencies very simple indeed.&lt;/p&gt;

&lt;p&gt;The only thing to consider is the version disparity between the one offered by aptitude and the one available via source code.&lt;/p&gt;

&lt;p&gt;PickledOnion.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-09:2496</id>
    <published>2008-05-09T12:44:00Z</published>
    <updated>2008-05-09T12:48:17Z</updated>
    <category term="Apache"/>
    <category term="Ruby on Rails"/>
    <category term="Ubuntu - Hardy"/>
    <category term="apache"/>
    <category term="hardy"/>
    <category term="mongrel"/>
    <category term="rails"/>
    <link href="http://articles.slicehost.com/2008/5/9/ubuntu-hardy-apache-rails-and-mongrels" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Apache, Rails and mongrels</title>
<summary type="html">&lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article, we can now look at creating and configuring Apache to proxy to a mongrel cluster so we can serve our Ruby on Rails application.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article, we can now look at creating and configuring Apache to proxy to a mongrel cluster so we can serve our Ruby on Rails application.&lt;/p&gt;
&lt;h3&gt;Prerequisites&lt;/h3&gt;

&lt;p&gt;To get the most out of this article you need to have a couple of things preinstalled:&lt;/p&gt;

&lt;p&gt;Firstly, you need Apache installed (see &lt;a href=&quot;http://articles.slicehost.com/2008/4/25/ubuntu-hardy-installing-apache-and-php5&quot; title=&quot;Ubuntu Hardy - Apache installation&quot;&gt;this article&lt;/a&gt;) - if you don't require PHP then please feel free to leave that section out.&lt;/p&gt;

&lt;p&gt;Secondly, you will need to have installed mongrels as per the mongrel and mongrel cluster article link above.&lt;/p&gt;

&lt;h3&gt;Rails application&lt;/h3&gt;

&lt;p&gt;To start with, we will need a basic Ruby on Rails application. Move into your public_html folder (create one if you do not have one already):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/public_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a Rails application. We'll use the default sqlite database for this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Apache modules&lt;/h3&gt;

&lt;p&gt;Apache will need the proxy and rewrite modules enabled.&lt;/p&gt;

&lt;p&gt;Depending on your Apache install you may need to issue all the following commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once done, reload Apache:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/apache2 force-reload&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;What's the plan, Stan?&lt;/h3&gt;

&lt;p&gt;Well, for our simple application we're going to create a mongrel cluster consisting of 3 mongrels running on port 5000 in production mode. We'll also add a symlink so the cluster will restart if the Slice is rebooted at any point.&lt;/p&gt;

&lt;p&gt;Then we can create an Apache virtual host to serve the Ruby on Rails application.&lt;/p&gt;

&lt;h3&gt;Mongrel Cluster&lt;/h3&gt;

&lt;p&gt;I won't go into the details of explaining what the commands are in this section. Please refer to the main &lt;a href=&quot;http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters&quot; title=&quot;Ubuntu Hardy - mongrel and mongrel clusters&quot;&gt;mongrel and mongrel cluster&lt;/a&gt; article for that.&lt;/p&gt;

&lt;p&gt;Ensure you are in the rails folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/public_html/railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a mongrel cluster file as such:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::configure -e production -p 5000 -N 3 -c /home/demo/public_html/railsapp -a 127.0.0.1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It's always a good idea to check the created file (config/mongrel_cluster.yml):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cwd: /home/demo/public_html/railsapp
log_file: log/mongrel.log
port: &amp;quot;5000&amp;quot;
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks good.&lt;/p&gt;

&lt;p&gt;Now create a symlink to the /etc/mongrel_cluster folder. This ensures the cluster is restarted on a reboot:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /home/demo/public_html/railsapp/config/mongrel_cluster.yml /etc/mongrel_cluster/railsapp.yml&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now all we need to do is start the cluster:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_cluster_ctl start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Done.&lt;/p&gt;

&lt;h3&gt;Apache Virtual Host&lt;/h3&gt;

&lt;p&gt;Now we can create the virtual host:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/apache2/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;he following will suffice for a basic application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;VirtualHost *:80&amp;gt;
  ServerName domain.com
  ServerAlias www.domain.com

  DocumentRoot /home/demo/public_html/railsapp/public

  RewriteEngine On

  &amp;lt;Proxy balancer://mongrel1&amp;gt;
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  &amp;lt;/Proxy&amp;gt;

  # Redirect all non-static requests to thin
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://mongrel1%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://mongrel1/
  ProxyPassReverse / balancer://mongrel1/
  ProxyPreserveHost on

  &amp;lt;Proxy *&amp;gt;
    Order deny,allow
    Allow from all
  &amp;lt;/Proxy&amp;gt;

  # Custom log file locations
  ErrorLog  /home/demo/public_html/railsapp/log/error.log
  CustomLog /home/demo/public_html/railsapp/log/access.log combined

&amp;lt;/VirtualHost&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Nice and simple and, as you may have noticed, is the pretty much the same as the Apache vhost we created when using the 'thin' web server (see &lt;a href=&quot;http://articles.slicehost.com/2008/4/25/ubuntu-hardy-installing-apache-and-php5&quot; title=&quot;Ubuntu Hardy - Apache installation&quot;&gt;this article&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;There is a good reason they are the same - all they are doing is proxying rails requests to the 3rd party server. In this case, the requests are proxied to the mongrel cluster.&lt;/p&gt;

&lt;h3&gt;Enable&lt;/h3&gt;

&lt;p&gt;Now we must enable the vhost:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo a2ensite domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Reload Apache:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/apache2 reload&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you get any port and NameVirtualHost errors then please read the &lt;a href=&quot;http://articles.slicehost.com/2008/4/29/ubuntu-hardy-apache-virtual-hosts-1&quot; title=&quot;Ubuntu Hardy - Apache Virtual Hosts&quot;&gt;Apache Virtual Host&lt;/a&gt; article which will take you through setting up said details.&lt;/p&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;All that's left is to navigate to your domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://www.domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where you will be greeted with the rails welcome page.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Setting up a virtual host to proxy to a mongrel cluster is fairly simple.&lt;/p&gt;

&lt;p&gt;Although setting up and configuring mongrel, especially if you want them restart on a reboot, can be a bit complicated, once done it is a quick and powerful method in serving your Ruby on Rails application.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-09:2495</id>
    <published>2008-05-09T11:39:00Z</published>
    <updated>2008-05-09T11:46:29Z</updated>
    <category term="Ruby on Rails"/>
    <category term="Ubuntu - Hardy"/>
    <category term="hardy"/>
    <category term="mongrel"/>
    <category term="rails"/>
    <link href="http://articles.slicehost.com/2008/5/9/ubuntu-hardy-mongrel-and-mongrel-clusters" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - mongrel and mongrel clusters</title>
<summary type="html">&lt;p&gt;There are variety of options open to the sysadmin when serving Ruby applications.&lt;/p&gt;

&lt;p&gt;One of the original ways is to use the &lt;a href=&quot;http://mongrel.rubyforge.org/&quot; title=&quot;Mongrel home page&quot;&gt;mongrel web server&lt;/a&gt;. Requests are proxied to the mongrel(s) from the main web server (Apache, Nginx, etc).&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;There are variety of options open to the sysadmin when serving Ruby applications.&lt;/p&gt;

&lt;p&gt;One of the original ways is to use the &lt;a href=&quot;http://mongrel.rubyforge.org/&quot; title=&quot;Mongrel home page&quot;&gt;mongrel web server&lt;/a&gt;. Requests are proxied to the mongrel(s) from the main web server (Apache, Nginx, etc).&lt;/p&gt;
&lt;p&gt;The article may seem quite lengthy but two subjects are tackled here. One is the basic mongrel gem itself but then we move onto the mongrel_cluster gem.&lt;/p&gt;

&lt;p&gt;Take each section at a time as each one builds on the previous explanation.&lt;/p&gt;

&lt;h3&gt;Prerequisites&lt;/h3&gt;

&lt;p&gt;I am assuming you have Ruby and Rubygems installed on your Slice. If you don't, please see the Ubuntu Hardy &lt;a href=&quot;http://articles.slicehost.com/2008/4/30/ubuntu-hardy-ruby-on-rails&quot; title=&quot;Ubuntu Hardy - Ruby on Rails&quot;&gt;Ruby on Rails article&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;Installation&lt;/h3&gt;

&lt;p&gt;Mongrel is a rubygem and installation is as simple as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem instal mongrel&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On the test Slice with a basic rubygems and Rails installation, the process installed the following gems:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem_plugin-0.2.3
daemons-1.0.10
fastthread-1.0.1
cgi_multipart_eof_fix-2.5.0
mongrel-1.1.4&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That can vary depending on what you already have installed.&lt;/p&gt;

&lt;h3&gt;Mongrel basics&lt;/h3&gt;

&lt;p&gt;The mongrel package has 3 main commands: start, stop and restart.&lt;/p&gt;

&lt;p&gt;However, there are many options you could add to fit your needs such as the environment or the port and so on:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails start -e production -p 6000&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You need to be in the rails application directory to issue that command and, perhaps obviously, it would start a mongrel instance in production mode on port 6000.&lt;/p&gt;

&lt;p&gt;If you don't run it in the background (daemonised) then the output in the terminal will be similar to when using the inbuilt rails webbrick server.&lt;/p&gt;

&lt;p&gt;To run it in the backgroud, simply add the '-d' option:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails start -e production -p 6000 -d&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To stop the running process (assuming it is being run in a daemonised fashion):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails stop&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Again, the command should be given when in the rails directory.&lt;/p&gt;

&lt;h3&gt;Mongrel Clusters&lt;/h3&gt;

&lt;p&gt;You can run as many individual mongrels as you like for your application but it does get a little unwieldy if you have more than one application.&lt;/p&gt;

&lt;p&gt;One solution is to create what are called mongrel clusters. These 'clusters' are predefined groups of mongrels which are easy to start and stop, etc as a cluster and be configured to start on a Slice reboot (so your application will start itself on a reboot).&lt;/p&gt;

&lt;h3&gt;Installation&lt;/h3&gt;

&lt;p&gt;Just as with the original mongrel install, the mongrel_cluster is a rubygem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo gem install mongrel_cluster&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As I had already installed the mongrel gem with its dependencies, only the mongrel_cluster gem itself was installed. This may vary on your Slice, depending on what you already have installed.&lt;/p&gt;

&lt;h3&gt;Configuration&lt;/h3&gt;

&lt;p&gt;Configuring a mongrel cluster for your rails application runs along similar lines to the single mongrel options shown above.&lt;/p&gt;

&lt;p&gt;To start a cluster of 2 mongrels in production mode starting from port 8000 would be as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::configure -e production -p 8000 -N 2 -c /home/demo/public_html/testapp -a 127.0.0.1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that I set the full path of the rails application and set the port to bind to (localhost in this case).&lt;/p&gt;

&lt;p&gt;There are plenty of option available when configuring a mongrel cluster and the easiest thing is to have a look at the help file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::configure -h&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;YAML&lt;/h3&gt;

&lt;p&gt;You will have noticed the output of the command is as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Writing configuration file to config/mongrel_cluster.yml.&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents of which are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cwd: /home/demo/public_html/testapp
log_file: log/mongrel.log
port: &amp;quot;8000&amp;quot;
environment: production
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 2&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Well, no real surprises there, it simply puts the mongrel cluster options into a YAML format.&lt;/p&gt;

&lt;p&gt;You can edit the file by hand if you wish to change something and don't want to go through the configure command again.&lt;/p&gt;

&lt;h3&gt;Mongrel_cluster basics&lt;/h3&gt;

&lt;p&gt;Starting the cluster is a case of:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ensure you are in your rails application folder when you issue the command.&lt;/p&gt;

&lt;p&gt;Stopping and restarting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_rails cluster::restart
...
mongrel_rails cluster::stop&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;init scripts&lt;/h3&gt;

&lt;p&gt;The final configuration you may want to consider (and I recommend it) is to create an init script so the mongrel cluster is started on a reboot.&lt;/p&gt;

&lt;p&gt;Unlike 'thin' or mod_rails there is no easy way of doing this so it does require a some work.&lt;/p&gt;

&lt;p&gt;Firstly, create a folder in the /etc folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo mkdir /etc/mongrel_cluster&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a symlink from the cluster configuration file to the newly created folder:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo ln -s /home/demo/public_html/testapp/config/mongrel_cluster.yml /etc/mongrel_cluster/testapp.yml&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will have to do that for each and every mongrel_cluster you create (if you want them to start automatically). So if you have two rails applications, you will have two symlinks.&lt;/p&gt;

&lt;p&gt;Next, copy the gem init script to the init.d directory:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Make it executable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo chmod +x /etc/init.d/mongrel_cluster&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and then add the script to the runlevels:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Wow. Quite a long and complicated procedure when compared to using 'thin' or mod_rails.&lt;/p&gt;

&lt;h3&gt;Cluster control&lt;/h3&gt;

&lt;p&gt;Let's take a quick look at controlling the clusters.&lt;/p&gt;

&lt;p&gt;Getting a status of any running clusters is always nice:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_cluster_ctl status&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The output will show something along the lines of:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Checking all mongrel_clusters...
mongrel_rails cluster::status -C testapp.yml
found pid_file: tmp/pids/mongrel.8000.pid
found mongrel_rails: port 8000, pid 2343

found pid_file: tmp/pids/mongrel.8001.pid
found mongrel_rails: port 8001, pid 2346&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That matches the cluster we created earlier so no problems.&lt;/p&gt;

&lt;p&gt;To start/stop/restart the cluster(s):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mongrel_cluster_ctl start
...
mongrel_cluster_ctl stop
...
mongrel_cluster_ctl restart&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remember you may need to put a sudo in front of the 'stop' command if you have just rebooted as the process started on reboot is owned by root.&lt;/p&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;There is a lot happening in this article but when followed all the way through, we have all the necessary gems and information to create mongrel clusters for each of our rails applications.&lt;/p&gt;

&lt;p&gt;A simple symlink is then all it takes to ensure the cluster is restarted on a reboot.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://articles.slicehost.com/">
    <author>
      <name>PickledOnion</name>
    </author>
    <id>tag:articles.slicehost.com,2008-05-06:2445</id>
    <published>2008-05-06T13:25:00Z</published>
    <updated>2008-05-06T13:35:43Z</updated>
    <category term="Apache"/>
    <category term="Ruby on Rails"/>
    <category term="Ubuntu - Hardy"/>
    <category term="apache"/>
    <category term="hardy"/>
    <category term="rails"/>
    <category term="thin"/>
    <link href="http://articles.slicehost.com/2008/5/6/ubuntu-hardy-apache-rails-and-thin" rel="alternate" type="text/html"/>
    <title>Ubuntu Hardy - Apache, Rails and thin</title>
<summary type="html">&lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;thin web server for Ruby&lt;/a&gt; article, let's look at creating and configuring Apache to proxy to thin so we can serve our Ruby on Rails application.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Following from the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;thin web server for Ruby&lt;/a&gt; article, let's look at creating and configuring Apache to proxy to thin so we can serve our Ruby on Rails application.&lt;/p&gt;
&lt;h3&gt;Prerequisites&lt;/h3&gt;

&lt;p&gt;To get the most out of this article you need to have a couple of things preinstalled:&lt;/p&gt;

&lt;p&gt;Firstly, you need Apache installed (see &lt;a href=&quot;http://articles.slicehost.com/2008/4/25/ubuntu-hardy-installing-apache-and-php5&quot; title=&quot;Ubuntu Hardy - Apache installation&quot;&gt;this article&lt;/a&gt;) - if you don't require PHP then please feel free to leave that section out.&lt;/p&gt;

&lt;p&gt;Secondly, you will need to have thin installed - as mentioned above, please see the &lt;a href=&quot;http://articles.slicehost.com/2008/5/6/ubuntu-hardy-thin-web-server-for-ruby&quot; title=&quot;Ubuntu Hardy - thin web server for Ruby&quot;&gt;Ubuntu Hardy - thin web server for Ruby&lt;/a&gt; article for more details.&lt;/p&gt;

&lt;h3&gt;Rails application&lt;/h3&gt;

&lt;p&gt;To start with, we will need a basic Ruby on Rails application. Move into your public_html folder (create one if you do not have one already):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cd ~/public_html&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a Rails application. We'll use the default sqlite database for this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rails railsapp&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Apache modules&lt;/h3&gt;

&lt;p&gt;Apache will need the proxy and rewrite modules enabled.&lt;/p&gt;

&lt;p&gt;Depending on your Apache install you may need to issue all the following commands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once done, reload Apache:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/apache2 force-reload&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Apache Virtual Host&lt;/h3&gt;

&lt;p&gt;So what's the plan?&lt;/p&gt;

&lt;p&gt;Well, for our simple application we'll set 3 thin instances running from port 5000 and in production mode.&lt;/p&gt;

&lt;p&gt;Let's start off by creating the virtual host:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo nano /etc/apache2/sites-available/domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The following will suffice for a basic application:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;VirtualHost *:80&amp;gt;
  ServerName domain.com
  ServerAlias www.domain.com

  DocumentRoot /home/demo/public_html/railsapp/public

  RewriteEngine On

  &amp;lt;Proxy balancer://thinservers&amp;gt;
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  &amp;lt;/Proxy&amp;gt;

  # Redirect all non-static requests to thin
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://thinservers%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://thinservers/
  ProxyPassReverse / balancer://thinservers/
  ProxyPreserveHost on

  &amp;lt;Proxy *&amp;gt;
    Order deny,allow
    Allow from all
  &amp;lt;/Proxy&amp;gt;

  # Custom log file locations
  ErrorLog  /home/demo/public_html/railsapp/log/error.log
  CustomLog /home/demo/public_html/railsapp/log/access.log combined

&amp;lt;/VirtualHost&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Familiar?&lt;/h3&gt;

&lt;p&gt;Does that look familiar?&lt;/p&gt;

&lt;p&gt;Well, it is pretty much the same as the virtual host used to proxy to mongrel servers.&lt;/p&gt;

&lt;p&gt;If you think about it, we don't need anything different as all we are doing is rerouting non-static requests to thin in the same way we would for mongrels.&lt;/p&gt;

&lt;h3&gt;Enable&lt;/h3&gt;

&lt;p&gt;Don't forget to enable the vhost:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo a2ensite domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And restart Apache:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/apache2 reload&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you get any port and NameVirtualHost errors then please read the &lt;a href=&quot;http://articles.slicehost.com/2008/4/29/ubuntu-hardy-apache-virtual-hosts-1&quot; title=&quot;Ubuntu Hardy - Apache Virtual Hosts&quot;&gt;Apache Virtual Host&lt;/a&gt; article which will take you through setting up said details.&lt;/p&gt;

&lt;h3&gt;Thin configuration&lt;/h3&gt;

&lt;p&gt;Next we need to configure thin on our application.&lt;/p&gt;

&lt;p&gt;To kill two birds with one stone, we'll go straight into creating an init.d file so the thin servers are started on a Slice reboot.&lt;/p&gt;

&lt;p&gt;Remember we want 3 servers running sequentially from port 5000 in production mode:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo thin config -C /etc/thin/railsapp.yml -c /home/demo/public_html/railsapp/  --servers 3 -p 5000 -e production&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Have a quick check of the file we created:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;cat /etc/thin/railsapp.yml&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;the contents of which look good:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pid: tmp/pids/thin.pid
log: log/thin.log
timeout: 30
max_conns: 1024
port: 5000
max_persistent_conns: 512
chdir: /home/demo/public_html/railsapp
environment: production
servers: 3
address: 0.0.0.0
daemonize: true&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Stating thin&lt;/h3&gt;

&lt;p&gt;Now we'ce created our init script, let's start it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/thin start&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The terminal output confirms the actions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[start] /etc/thin/railsapp.yml ...
Starting server on 0.0.0.0:5000 ... 
Starting server on 0.0.0.0:5001 ... 
Starting server on 0.0.0.0:5002 ...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Cool.&lt;/p&gt;

&lt;h3&gt;Navigate&lt;/h3&gt;

&lt;p&gt;All that's left is to navigate to your domain:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;http://www.domain.com&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where you will be greeted with the rails welcome page.&lt;/p&gt;

&lt;h3&gt;Restart&lt;/h3&gt;

&lt;p&gt;As we created an init script for the thin servers you can restart them in the normal way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo /etc/init.d/thin restart&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Summary&lt;/h3&gt;

&lt;p&gt;Setting up a virtual host to proxy to the thin web server is fairly simple.&lt;/p&gt;

&lt;p&gt;Add to that the ease of installing and configuring thin, the whole process in very quick and very powerful.&lt;/p&gt;

&lt;p&gt;PickledOnion&lt;/p&gt;
          </content>  </entry>
</feed>
