How to setup a Virtual host for local development

Solution 1:

Later, I edited the index.html from /var/www but I was still getting the same index.html (default-before editing). All the index.htmls have been edited but Apache seems to have some hidden one which keeps coming up when I request for www.vivek.com

By reading this, I guess that you're viewing a cached file. Instead of pressing F5 or hitting the Refresh button, skip the cache on refreshing by pressing Ctrl + F5.

Alternatiely, use the command-line program curl (which is not installed by default). Example usage:

$ curl -i http://localhost/
HTTP/1.1 200 OK
Date: Sat, 02 Jul 2011 00:42:01 GMT
Server: Apache/2.2.17 (Ubuntu)
Last-Modified: Fri, 01 Jul 2011 04:12:49 GMT
ETag: "4507-b1-4a6fa3b114149"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Content-Type: text/html

<html><body><h1>It works!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>

One note: I did the following on a clean apache install:

  • Add your first configuration file to /etc/apache2/sites-available/vivek.com
  • (leave /etc/apache2/sites-available/default untouched)
  • Run sudo a2ensite vivek.com
  • Run sudo /etc/init.d/apache reload

I get the same messages as you. However, the server failed to start. When stopping the server using sudo /etc/init.d/apache2 stop and starting it again using sudo /etc/init.d/apache2 start, it refused to start at all. Looking in the error log /var/log/apache2/error.log revealed some errors:

[Sat Jul 02 00:48:09 2011] [notice] Graceful restart requested, doing restart
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
[Sat Jul 02 00:48:09 2011] [warn] NameVirtualHost *:80 has no VirtualHosts
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs

So port 80 seems to be in use. But if Apache is not started yet, I can confirm that nothing listens on port 80 by running sudo netstat -tpln. I reviewed the configuration and concluded that the line Listen 80 should be removed from your configuration file /etc/apache2/sites-available/vivek.com. After that, I could start the server again and using curl, I confirmed that the server is correctly responding to requests.

KISS, your second vhost block is redundant as it's covered by /etc/apache2/sites-available/default. The next configuration file is /etc/apache2/sites-available/vivek.com:

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

    # Other directives here
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/vivek/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Solution 2:

There is a script that makes this simple for you - https://github.com/RoverWire/virtualhost

This eventually does the same thins as explained by the answers given but does it in just one command eg. -

sudo virtualhost create mysite.local my_site

It also allows you to delete a host you created

sudo virtualhost delete mysite.local my_site

Just note that the "my_dir" assumes that it starts after /var/www

So if your site folder is in /var/www/my_site

You should run this (pass directory path without /var/www) -

sudo virtualhost create mysite.local my_site

You can also modify the script file & remove the default path "/var/www" so that you can pass an absolute path to the site directory