How To Create Apache Virtual Host in Ubuntu 18.04

How to Setup Apache Virtual Host in Ubuntu 18.04

Steps for Creating Apache Virtual Hosts setup on Ubuntu 18.04 – How To Setup Apache Virtual Host in Ubuntu 18.04

1. Install Apache

You may skip this option if you already have installed Apache server in your system. If not use the following command to install Apache server on Ubuntu system. Also as a best practice don’t forget to update repositories metadata.

sudo apt-get update
sudo apt-get install apache2

2. Create Apache Virtual Host

In Apache on Ubuntu all the virtual host configuration files are stored under /etc/apache2/sites-available directory. With the new Apache installation you will find a default virtual host file there. Create a new Virtual Host configuration file by copying default file.

cd /etc/apache2/sites-available/
sudo cp 000-default.conf

Now Edit new virtual host configuration file and update as per your requirement. My configuration file looks like below.
<VirtualHost *:80>
        DocumentRoot /var/www/html/      

       <Directory />
                Options FollowSymLinks
                AllowOverride None

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                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
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
Now create a directory structure in your system, as per defined in above virtual host, and assign proper ownership and file permission.

mkdir -p /var/www/html/
chmod 755 /var/www/html/
chown www-data.www-data /var/www/html/

Now upload your project files on /var/www/html/ directory. For this example we have created a index.html file for testing purpose like below.
echo "<h1></h1>" > /var/www/html/

3. Enable First Virtual Host

Now we have successfully created our first virtual host in Apache. Now use the following command to enable this Virtual Host, So that Apache can load this configuration file on next reload.
Basically this creates a soft link of Virtual Host configuration file in directory /etc/apache2/sites-enabled/. To activate the new configuration, we need to run:
service apache2 reload
Now you can access ‘’ in your browser. If you don’t have dns configured for your domain, do a local mapping by adding an entry in /etc/hosts files.


4. Create Another Virtual Host

If you want to add another virtual host on your server, just repeat step 2 and 3 with changing your site name like

Enjoy Virtual Hosting!

One Response

  1. Hello there, I discovered your web site by the use of
    Google at the same time as looking for a related
    topic, your website came up, it appears to be like great.

    I’ve bookmarked it in my google bookmarks.
    Hello there, simply became aware of your blog through Google,
    and found that it’s truly informative. I’m gonna watch out for brussels.
    I’ll appreciate if you happen to proceed this in future.
    A lot of people will probably be benefited out of your writing.

Leave a Reply

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