Apache – Configuring VirtualHosts on Ubuntu 18.04

When you add a domain to your new Ubuntu 18.04 web server you need to take a few steps to make sure the web server recognizes the request.

Note: All commands below assume they are being run in terminal.

root@test:/# apache2 -v
Server version: Apache/2.4.29 (Ubuntu)
Server built: 2018-10-03T14:41:08

Be familiar with these two locations:

/etc/apache2/sites-available/
/etc/apache2/sites-enabled/

You add new *.conf (configuration files) inside of /etc/apache2/sites-available/. You have to then link them into /etc/apache2/sites-enabled/. This is how:

Step1: Create your new *.conf file with the appropriate VirtualHost information:

# vim /etc/apache2/sites-available/yourdomain.org.conf

Here is a basic VirtualHost file to help you get started:

<VirtualHost *:80>
ServerAdmin yourdomain.org@gmail.com
ServerName tyourdomain.org
ServerAlias www.yourdomain.org
DocumentRoot /var/www/yourdomain.org/
ErrorLog ${APACHE_LOG_DIR}/yourdomain.org.error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.org.access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =yourdomain.org [OR]
RewriteCond %{SERVER_NAME} =www.yourdomain.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Step2: Enable the site you just created. This will create a symlink inside of /etc/apache2/sites-enabled/.

# a2ensite yourdomain.org.conf

Step3: You’ll then be asked to reload Apache. You do this by running the following:

# systemctl reload apache2

For good measure, I always restart Apache (you probably don’t need to).

# systemctl restart apache2

Assuming you have your DNS configured correctly, this should render your site on request.

Tip: Be mind of what you put here: DocumentRoot /var/www/yourdomain.org/ This tells the web server where the webfiles are, that’s often the root of many problems.

Website Security Platform

A full-stack performance, monitoring and security solution designed to simplify administrative tasks and remove the stress of online threats.

Was this article helpful?

Related Articles

Need Support?

Can't find the answer you're looking for?
Contact Support