Israel Science and Technology Directory

Internet Apache server

2. Configure Apache2 server on Ubuntu

Updated on December 14, 2025.

Configuration of a virtual host described here should follow installation of Apache2 software, 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

2.1. Set up a virtual host directory

The computer where a website is located is referred as the "host" of the website. In practice, one computer may host many websites where each website functions as a separate entity with an operating environment and rules of its own. The "virtual" label reflects the fact that the website appears on a computer of its own, while it is located in a computer that may host many websites.

Virtual hosts can be "IP-based", where the website is accessed by a specific IP address, or "name-based", where the website is accessed by its name among a group of websites hosted on the same IP address.

The following steps describe configuration for hosting a website with the address example.com. To use the code provided on this page, replace example.com with your website address.

2.1.1. Create a directory for the website

In Linux, the recommended site for a virtual host is under the
/var/www/html directory.

Thus, to initiate the website make a directory under the /var/www/ directory as follows:

sudo mkdir -p /var/www/example.com/html

2.1.2. Edit the configuration file

In Linux, the default Apache2 server configuration is located in file:
/etc/apache2/sites-available/000-default.conf

Switch to this directory:

cd /etc/apache2/sites-available/

Copy the default file to generate a working copy for your website:

sudo cp 000-default.conf example.com.conf

Open the newly created file to edit it using nano:

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

Initially, this file includes the following lines − excluding comments starting 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 the file to generate the following:

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName example.com
	ServerAlias www.example.com 
	DocumentRoot /var/www/example.com/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.2. Enable 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 reload 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