Configuring an EC2 instance as a space to serve up all my RailsWordPress projects

How do I configure an Amazon EC2 instance as a personal space to serve up whatever Ruby on Rails or WordPress projects I want, with their own domains? As a developer, I’ve found surprisingly little information on how to create your own “bucket” for all your various web-based projects.

Related posts

Leave a Reply

1 comment

  1. It’s surprisingly easy to get started but there are dozens of different ways to go about it. For the sake of giving you relevant resources I’m going to make a few assumptions about your setup and try to paint with broad strokes:

    • You’re running a Debian-based Linux distribution (Ubuntu is popular and has a great community)
    • You’re running a LAMP (Linux, Apache, MySQL, and PHP) stack (see Installing LAMP Server in the Ubuntu documentation).
    • You’ll be deploying your sites to the default server location of /var/www

    The first thing you’ll want to do is read up on virtual hosts (vhosts). A common pattern is to put virtual hosts’ web roots in subdirectories of /var/www/vhosts/ (e.g. /var/www/vhosts/example.com/), though these can technically live anywhere on the server.

    After creating your document roots you’ll give each site each a virtual host file in /etc/apache2/sites-available that looks (at a very high/rudimentary level) something like this:

    <VirtualHost *:80>
      ServerName example.com
      DocumentRoot /var/www/vhosts/example.com/
    </VirtualHost>
    

    This tells Apache to listen for requests for http://www.example.com and route them to /var/www/vhosts/example.com. There are a whole mess of Apache vhost configuration options, I’d recommend a Google search for some more specific examples though looking at the 000-default vhost that comes bundled with Apache on Ubuntu is a good place to start.

    When you’re ready to activate the vhost you’ll need to enable the virtual host and reload/restart Apache:

    $: a2ensite {your vhost file name} # Creates a symlink to this vhost file in /etc/apache2/sites-enabled/
    $: service apache2 restart # restart Apache
    

    Since you want to point different domains to your projects you’ll simply need to create a DNS record (A-records will be the easiest unless you’re dealing with all sorts of shifting IP addresses) and point it to the IP address of your server. Apache will route the domain to the appropriate virtual host.

    For Rails projects the web root should be the public/ directory, so your DocumentRoot directive will be DocumentRoot /var/www/vhosts/example.com/public. You’ll also need to be running a Ruby-compatible server for Rails apps, Phusion Passenger, Thin, and Unicorn are all pretty popular.