Creating virtual hosts in apache in local machine

In this post I show how to create virtual hosts on apache and run multiple rails apps on your local machine with names like

In the previous post I had shown how to deploy rails apps on local machine with apache web server

My software stack:

  • Ubuntu 11.04
  • ruby 1.9.2 p290.
  • Rails 3.1.10
  • rubygems 1.8.10

Lets start!

First we have to enable virtual aliases on the Apache S

sudo a2enmod vhost_alias

Now I assume that the rails app is at the location:



We first create a virtual configuration file:

Tyep the following in terminal:

sudo gedit /etc/apache2/sites-available/mydomain

Copy the following settings to your file:


<VirtualHost *:80>

DocumentRoot /home/username/Desktop/rails_apps/my_rails_app/public
<Directory />
Options FollowSymLinks
AllowOverride All
<Directory //home/username/Desktop/rails_apps/my_rails_app/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

<Directory “/usr/lib/cgi-bin”>

AllowOverride None

Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch

Order allow,deny

Allow from all



ErrorLog ${APACHE_LOG_DIR}/error.log


# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn


CustomLog ${APACHE_LOG_DIR}/access.log combined


Alias /doc/ “/usr/share/doc/”

<Directory “/usr/share/doc/”>

Options Indexes MultiViews FollowSymLinks

AllowOverride None

Order deny,allow

Deny from all

Allow from ::1/128



Important to note here is to change ServerName as per your convinince and Directory to your public of rails app.

Once this is done,

type the following to open the hosts file:

 sudo gedit /etc/hosts


In this file, copy the following code:


replace domain name with your desired name.

This is what you will use in browser to access the page

Activate your domain and reload the Apache Server to enable settings
Enable the site

a2ensite domain-name


Reload Apache2

/etc/init.d/apache2 reload


Finally test your app by typing and see the magic !!!



One response to “Creating virtual hosts in apache in local machine

  1. Hello there! I could have sworn I’ve been to this web site before but after going through a few
    of the articles I realized it’s new to me. Regardless, I’m certainly delighted I stumbled upon it
    and I’ll be book-marking it and checking back often!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s