Israel Science and Technology Directory

Internet Apache server

2. Configure Apache2 server on Ubuntu

Updated on December 19, 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 directory to establish a virtual host is:
/var/www/html

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

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.

2.1.3. Enable the configuration file

After creating a new configuration file for example.com , this configuration has to be "enabled". To enable the new site type 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 have:

 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

3. Local DNS configuration

Map your local machine's IP address (127.0.0.1) in the /etc/hosts file.
Open the hosts file with nano:

sudo nano /etc/hosts

Add the following line to the end of the file:

127.0.0.1 example.com www.example.com

4. First view of the website

After the configuration of the virtual host, entering the domain name (http://www.example.com) or the domain IP should show the default page. The default page for Apache2 is index.html.

Next step: Set up directory permissions and ownership