Creating new virtual hosts in Ubuntu/Linux

In KUbuntu i wanted to make virtual host working on my local machine what it worked like on Windows 7 earlier. I am posting the complete process here to help someone newbie like me in using KUbuntu operating system on my local machine.

For someone trying to get Named virtual hosts working on Ubuntu i assume that the apache web server is already installed and running.

Here’s the complete process:

As a first step you need to create a new configuration file for your new virtual host. To do this go to:

$ cd /etc/apache2/sites-available

and create a new configuration file by copying the default configuration file, by running

$ sudo cp default

you may need to change the name to suit your setup. After having created new file open this file in edit mode. I would use gedit editor to do so by running the following command.

$ sudo gedit

Once opened edit this file to match your needs. My file would look as under after editing:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride All
<Directory /var/www/>
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


Highlighted lines above containing ServerAlias, DocumentRoot and Directory parameters are important. Once changes made save and close this file. Now we have made the configuration for new virtual host “available” and we need to make it “enabled”, next.

To enable the new virtual host configuration do any of the following, depending of your system settings.

Either do

$ sudo a2ensite


Go to sites-enabled folder by doing

$ cd /etc/apache2/sites-enabled/
$ sudo ln -s /etc/apache2/sites-available/

Both of them would enable the new virtual host.

Now, as a last step tell your apache server to look for in your local maching and not over the internet, by editing your hosts file:

$ sudo gedit /etc/hosts

and adding a new line (as under) at the bottom:

Reload the apache by running:

sudo /etc/init.d/apache2 reload

and open the in your web browser. It should work.

One thought on “Creating new virtual hosts in Ubuntu/Linux

  1. Thank you dude! It was so nicely explained. Following this post it took me just two minutes to set up a new virtual host on Ubuntu. Nice job!

Leave a Reply

Your email address will not be published. Required fields are marked *