Israel Science and Technology Directory

InternetApache server

Configuring Apache2 server on Ubuntu

The configuration of a virtual host described here should follow installation of Apache2 server, as described on page: Installing Apache2 and PHP.

The instructions described here should be executed in a terminal window by a user that has sudo authorization. The text editor used here is nano within a terminal window.

First update and upgrade your repositories by running the following commands in a terminal window:

sudo apt update && sudo apt upgrade

1. Setting up a virtual host file

The term Virtual Host refers to the practice of running more than one website on a single machine. Virtual hosts can be "IP-based", meaning that every web site has a specific IP address, or "name-based", meaning that you have multiple names running on each IP address.

In Linux, Apache2 server is located in: /etc/apache2/

The default virtual host configuration file is:
/etc/apache2/sites-available/000-default.conf

Open this file by typing:

sudo nano /etc/apache2/sites-available/000-default.conf

Initially, this file includes the following lines, excluding comment lines that start with #.

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

Edit this file to generate the following:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName example.com		#Change to your domain
	ServerAlias www.example.com 	#Change to your domain 
	DocumentRoot /var/www/html
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Then press ^O, to save the file, naming it as:

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

Don't forget to change the name example.com to your domain name.

After this step, check the content of the directory by typing

ls /etc/apache2/sites-available

The result should be:

000-default.conf	example.com.conf

2. Enabling the configuration file

Enable the new site by typing the command:

sudo a2ensite example.com.conf

This command will add a new file to the directory /etc/apache2/sites-enabled.
To see the content of the directory type:

ls /etc/apache2/sites-enabled

The result should be:

000-default.conf	example.com.conf

Next, disable the default file by typing the command:

sudo a2dissite 000-default.conf

After these operations check the configuration by typing:

sudo apache2ctl configtest

If the configuration is OK, then the Output should be:

Syntax OK

Then restart Apache2 server by typing

sudo systemctl restart apache2

First view of the website

After the completion of the definition of a virtual host, entering the domain name (http://www.example.com) or the domain IP should show the default page for the DocumentRoot /etc/apache2/html. The default page for Apache2 is index.html. Click the image to see the page.

Apache2 index.html