Apache Virtual Hosts on Ubuntu

Apache can be installed on your server through apt-get:

apt-get update
apt-get install apache2

Step 1 - Create the folders

The first step that we are going to take is to make a directory structure that will hold the site data that we will be serving to visitors.

mkdir /var/www/html/example.com
mkdir /var/www/html/example2.com

Step Two - Create the Apache Virtual Host Files

Edit the file,

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

The update the config accordingly and to look like this,

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file, do the same for example2.com,

Edit the file,

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

The update the config accordingly and to look like this,

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file.

Step Three - Enable the Apache Virtual Host Files

a2ensite example.com.conf
a2ensite example2.conf

Restart Apache to make these changes take effect:

service apache2 restart

Done!


Last modified: August 03 2017 19:21:19