Skip to content

Installing on Ubuntu using Apache and Unicorn

contentfree edited this page May 16, 2011 · 4 revisions

Installing on Ubuntu using Apache and Unicorn

This tutorial is probably not the best you've seen, but it's worth sharing. I had been running Teambox on an Apache server and Passenger, but the performance was very poor. The server is not a beast though, running 358MB of RAM and 600 MHz (it's a VPS), but I was pretty sure Teambox could be run a lot faster than that. I tried a small cluster of Thin servers, proxied through Apache, which yielded better results than Passenger, but not what I wanted. Unicorn ended up running extremely faster than Passenger and Thin, and I'll try to document the process here.

Requirements

  • Ubuntu (not really, this probably works on other distributions)
  • Apache Server
  • RubyGems

Step 1

I will assume you have Apache configured and running. We need to add some modules to allow Apache to proxy requests to the Unicorn server.

sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite

Restart your Apache server afterwards

sudo /etc/init.d/apache2 restart

Step 2

Now, install the Unicorn server using RubyGems (you might need to run this using sudo).

gem install unicorn

Step 3

Install and configure Teambox using the Installing locally tutorial. Make sure it works by running rails server -e production and pointing your browser to http://your-server:3000.

Step 4

Create an Apache virtual host for Teambox. This configuration will proxy all requests to the domain to the Unicorn server, and all static content will be served by Apache. (Adapted from this slicehost tutorial).

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com

  # Point this to your public folder of teambox
  DocumentRoot /home/demo/public_html/railsapp/public

  RewriteEngine On

  <Proxy balancer://unicornservers>
    BalancerMember http://127.0.0.1:5000
  </Proxy>

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

  ProxyPass / balancer://unicornservers/
  ProxyPassReverse / balancer://unicornservers/
  ProxyPreserveHost on

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

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

</VirtualHost>

Restart your Apache server again to apply changes.

Step 5

Run the Unicorn server

unicorn_rails -p 5000 -E production

And now point your browser to your domain, you should be greeted with Teambox!

Step 6

With this configuration Teambox will work almost perfectly, with the exception of file downloads. Teambox doesn't handle file downloads, since this would be very resource-consuming! Instead, it let's the server handle the download. For this, we need to enable the XSendFile mod in Apache:

sudo apt-get install libapache2-mod-xsendfile
sudo /etc/init.d/apache2 restart

and finally add the following two directives to the virtualhost file we created in a previous step:

XSendFile On
XSendFileAllowAbove on

And now file downloads should work!

Step 7

Have Unicorn run on every boot, I did this by adding this command to the /etc/rc.local file:

(cd home/demo/public_html/railsapp && unicorn_rails -p 5001 -E production -D)

If you have any problems you can always tweet!