Install Apache & Set Up Virtual Hosts

This is just a quick step by step guide taken from DigitalOcean's tutorial (see the references at the bottom). The main difference is that it has less explanation - so if you know what you are doing you can just copy & paste the commands ...

$ sudo apt-get update
$ sudo apt-get install apache2

Set Global ServerName to Suppress Syntax Warnings

Next, we will add a single line to the /etc/apache2/apache2.conf file to suppress a warning message. While harmless, if you do not set ServerName globally, you will receive the following warning when checking your Apache configuration for syntax errors:

$ sudo apache2ctl configtes
$ sudo nano /etc/apache2/apache2.conf

Add `ServerName server_domain_or_IP` save and close the file.

Adjusting the Firewall

$ sudo ufw app list
$ sudo ufw allow 'Apache'
$ sudo ufw status

If `ufw` is not enabled do it by running `sudo ufw enable`. Make sure that OpenSSH is on the allowed list.

Checking your Web Server

$ sudo systemctl status apache2

Setting Up Virtual Hosts

Create the directory for your_domain as follows:

$ sudo mkdir /var/www/your_domain
$ nano /var/www/your_domain/index.html

Add to index.html:

<title>Welcome to Your_domain!</title>
<h1>Success! The your_domain virtual host is working!</h1>

In order for Apache to serve this content, it's necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, let's make a new one at /etc/apache2/sites-available/your_domain.conf:

$ sudo nano /etc/apache2/sites-available/your_domain.conf

Paste the following to apache config:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Let's enable the file with the a2ensite tool and disable the default one:

$ sudo a2ensite your_domain.conf
$ sudo a2dissite 000-default.conf

Check the apache config and reload the apache:

$ sudo apache2ctl configtest
$ sudo systemctl reload apache2

On your local computer edit hosts file:

$ sudo nano /etc/hosts

Add e.g `<server_IP>` entry, save and quit. Open your web browser and go to ``. You should see `Success! The virtual host is working!` (i.e. Virtual Hosts is correctly configured).


Leave a Reply

Your email address will not be published.