How to configure a virtual hosts – add new domains / subdomain with apache2 on ubuntu

You can configure a virtual hosts with some ways. However, I recommend the way in this topic. It is easy and clearly. Normally, Apache listens on all ip addresses which you have for your vps. You need to configure DNS for server and link domain to server, first. When you try it, you need to replace your domain instead

+ All the sites available /etc/apache2/sites-available. So we will create a configuration file for your website:

nano /etc/apache2/sites-available/

And write these code:

<Directory /var/www/html/>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/
        ErrorLog /var/www/html/
        CustomLog /var/www/html/ combined


+ Create folder for website:

sudo mkdir -p /var/www/html/{public_html,logs}

+ Enable the site – link the virtual host file from the sites-available folder to the sites-enabled folder:

sudo a2ensite

P/s: you can disable site by using sudo a2dissite

+ Reload apache:

sudo systemctl reload apache2


sudo service apache2 restart


sudo /etc/init.d/apache2 restart

Now, your virtual host is enabled. If the DNS for your domain worked, you can create a index.php file in the folder /var/www/html/ and visit your domain on the browser to see it works.

Add Comment

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