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 mageoneplus.com.

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

nano /etc/apache2/sites-available/mageoneplus.com.conf

And write these code:

<Directory /var/www/html/mageoneplus.com/public_html>
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
</Directory>
<VirtualHost *:80>
        ServerName mageoneplus.com
        ServerAlias www.mageoneplus.com
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/mageoneplus.com/public_html
        ErrorLog /var/www/html/mageoneplus.com/logs/error.log
        CustomLog /var/www/html/mageoneplus.com/logs/access.log combined
</VirtualHost>

configuration-virtual-host

+ Create folder for website:

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

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

sudo a2ensite mageoneplus.com.conf

P/s: you can disable site by using sudo a2dissite mageoneplus.com.conf

+ Reload apache:

sudo systemctl reload apache2

or

sudo service apache2 restart

or

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/mageoneplus.com/public_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.