2. Configure Apache2 server on Ubuntu
Updated on March 28, 2026.
The configuration of a virtual host described here follows the installation of Apache2 software, as described in Installing Apache2 and PHP. The instructions described here should be executed in a terminal window by a user with sudo authorization. The text editor used here is nano within a terminal window.
If you do not enter a domain name, it will be shown as "example.com"
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 on which a website resides is referred to as the website’s “host.” In practice, a single physical computer can host multiple websites, each functioning as a separate entity with its own operating environment and configuration rules. The term “virtual” reflects the fact that each website appears to run on its own dedicated computer, even though it actually resides on a shared physical machine that may host other 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 the configuration for hosting a website with the address example.com.
2.1.1. Create a directory for the website
In Linux, the recommended directory to establish a virtual host is: /var/www/html
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
DirectoryIndex index.html index.htm index.php
</VirtualHost>
NOTE: The line that starts with DirectoryIndex specifies the names of Default index files that you may wish to change.
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 configuring 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.