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:

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

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
</VirtualHost>

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> testtist.xyz` entry, save and quit. Open your web browser and go to `http://testtist.xyz`. You should see `Success! The testtist.xyz virtual host is working!` (i.e. Virtual Hosts is correctly configured).

Ref:
– https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04
– https://www.digitalocean.com/community/tutorials/how-to-install-the-apache-web-server-on-ubuntu-18-04

Leave a Reply

Your email address will not be published. Required fields are marked *