How to setup a Virtual host for local development

7,788

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

Share:
7,788

Related videos on Youtube

Oli
Author by

Oli

Hi, I'm Oli and I'm a "full-stack" web-dev-op. Eurgh. I'm also allergic to jargon BS. I spend most of my professional time writing Django websites and webapps for SMEs. I write a lot of Python outside of Django sites too. I administer various Linux servers for various tasks. I contribute to the open source projects that I use when I can. I'm a full-time Linux user and that has lead to helping other people live the dream. I am an official Ubuntu Member and I earnt my ♦ on SE's own Ask Ubuntu in 2011's moderator election. That's probably where I spend most of my unpaid time. I also run thepcspy.com which has been my place to write for the last decade or so. If you need to contact me for extended help, you can do so via my website, just remember that I have bills so if I feel your request is above and beyond normal duty, I might ask for remuneration for one-on-one support. For more social contact, you can usually find me (or just my computer) lurking in the Ask Ubuntu General Chat Room and on Freenode in #ubuntu and #ubuntu-uk under the handle Oli or Oli``.

Updated on September 18, 2022

Comments

  • Oli
    Oli over 1 year

    Ubuntu 11.04 installed with apache2 and all the relevant packages installed. I have tried most of the blogs and made google and other forums my best friends, yet I'm unable to solve this issue.

    I Need to set up a named virtual host on my local system for development.

    I created the directory "vivek" in /var/www and copied the default index.html and edited some elements.

    I added the file vivek.com in /etc/apache2/sites-available as follows:

    # Ensure that Apache listens on port 80
    Listen 80
    
    # Listen for virtual host requests on all IP addresses
    NameVirtualHost *:80
    
    <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>
    
    
    
    <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    
    DocumentRoot /var/www
    <Directory />
    Options FollowSymLinks
    AllowOverride None
    </Directory>
    <Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
    </Directory>
    
    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    </Directory>
    
    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 127.0.0.0/255.0.0.0 ::1/128
    </Directory>
    
    </VirtualHost>
    

    I.e I've added these following lines

    # Ensure that Apache listens on port 80
    Listen 80
    
    # Listen for virtual host requests on all IP addresses
    NameVirtualHost *:80
    
    <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>
    

    To the default file which was already present in "sites-available" folder(took the back up of default file before editing it)

    Added this in the hosts file present in /etc/hosts

    127.0.0.1 localhost
    127.0.1.1 vivek-PC
    127.0.0.1 www.vivek.com
    

    Performed the following operations with no errors:

    root@vivek-PC:~# a2ensite vivek.com
    Enabling site vivek.com.
    Run '/etc/init.d/apache2 reload' to activate new configuration!
    root@vivek-PC:~# /etc/init.d/apache2 reload
    * Reloading web server config apache2
    

    When I entered www.vivek.com,it gave me the default index.html in /var/www but not one present in the folder /var/www/vivek which is edited.

    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

    And the ironic thing is after I restart -- Apache came up fine but my site -- www.vivek.com failed to show up (even with the index.html which is hidden god knows where!!).. Now my browser is showing "Unable to connect "

    Please help.I've been trying to set this up since a week with no successful result.