Apache Virtual Hosts let you run multiple websites on a single server. Each site gets its own document root, its own error and access logs, and can have its own SSL certificate and separate security policy. This is the standard way to manage multi-domain hosting on an Apache server.
This guide shows you how to configure Apache Virtual Hosts on Ubuntu 18.04. The same steps work for Ubuntu 16.04 and any Ubuntu-based distribution. You can repeat this process for every additional domain you want to host.
Prerequisites:
Apache serves files from a document root directory. Each virtual host needs its own. A clean convention is to store each site under /var/www/domain/public_html:
bashsudo mkdir -p /var/www/example.com/public_html
For testing, create a basic HTML file inside the document root. Save the following to /var/www/example.com/public_html/index.html:
html<!DOCTYPE html><html lang="en"> <head><title>Welcome to example.com</title></head> <body><h1>Success! example.com home page!</h1></body></html>
When running commands as a sudo user, newly created files are owned by root. Change ownership to the Apache user so the web server can read and serve the files correctly:
bashsudo chown -R www-data: /var/www/example.com
Virtual host configuration files live in /etc/apache2/sites-available/. Create a config file for your domain:
bashsudo nano /etc/apache2/sites-available/example.com.conf
Paste the following configuration, replacing example.com with your actual domain name:
apache<VirtualHost *:80> ServerName example.com ServerAlias www.example.com ServerAdmin webmaster@example.com DocumentRoot /var/www/example.com/public_html <Directory /var/www/example.com/public_html> Options -Indexes +FollowSymLinks AllowOverride All </Directory> ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined</VirtualHost> Here is what the key directives do:
.htaccess files override Apache config per directory, required by most CMS platforms like WordPressName the configuration file after your domain for easy management.
Enable the new virtual host using the a2ensite helper:
bashsudo a2ensite example.com
This creates a symbolic link from sites-available to sites-enabled, which Apache reads on startup. You can also create the link manually if needed:
bashsudo ln -s /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-enabled/
Before restarting, test the configuration for syntax errors:
bashsudo apachectl configtest
Syntax OK
If the output shows Syntax OK, restart Apache to apply the changes:
bashsudo systemctl restart apache2
Open http://example.com in your browser. You should see the test page you created earlier.
Your Apache Virtual Hosts on Ubuntu 18.04 are now active. Repeat the steps above for each additional domain you want to host on the same server. Leave a comment below if you run into any issues.
Redis is an open-source, in-memory data structure store used as a database, cache, and message broker.…
Ubuntu 18.04 LTS (Bionic Beaver) was released on April 26, 2018, with five years of official…
Django is a free, open-source Python web framework built for developing secure, scalable, and maintainable web…
MySQL replication is the process of automatically copying data from one database server to one or…
Joomla is one of the most popular open-source content management systems in the world. It is…
WordPress is the most popular open-source content management system in the world, powering over a quarter…