How to create Virtual Hosts on Ubuntu?

Create the directory for as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/

Next, assign ownership of the directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/

The permissions of your web roots should be correct if you haven't modified your unmask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/

Next, create a sample index.html page using nano or your favorite editor:

nano /var/www/

Inside, add the following sample HTML:



        <title>Welcome to!</title>



        <h1>Success!  The server block is working!</h1>



Save and close the file when you are finished.

In order for Apache to serve this content, it's necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let's make a new one at /etc/apache2/sites-available/

sudo nano /etc/apache2/sites-available/

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

<VirtualHost *:80>
    DocumentRoot /var/www/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

Save and close the file when you are finished.

Let's enable the file with the a2ensite tool:

sudo a2ensite

Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Next, let's test for configuration errors:

sudo apache2ctl configtest

Restart Apache to implement your changes:

sudo systemctl restart apache2