Apache Virtual Host Setup

Posted by Morgan Billingsley on December 30, 2019 6:27 pm

After spending months on a web application, the deployment of the app itself to the server should be a piece of cake. The problem is, that once you’re about to cross this finish line, it is easy to get lazy.

One of the mistakes that is made very frequently, is forgetting to setup your virtual hosts properly…

Here’s how to properly setup a virtual host in apache.

Step 1: Create the configuration file for your domain

The best place to put your virtual host configuration file, is in the same place as apache’s “sites-available” folder. On Debian this is located at /etc/apache2/sites-available.

*We will be using the name example.com to represent our virtual host in this tutorial. You should replace it with your own host name.

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

Step 2: Add configuration settings to file

Inside of the configuration file, set your virtual host configuration settings. You can use the following file as an example.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example/public
    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined

Step 3: Enable Site

The next step is to tell Apache that you have configured a new site and you want it to be enabled.

Enter the following command into your terminal:

sudo a2ensite example.com

Restart Apache to register the change

sudo systemctl apache2 restart

To set up your virtual host with an ssl certificate, check out this tutorial.