Configuring Apache2 server on Ubuntu
The installation of Apache2 server together with PHP was described at Installing Apache2 and PHP.
This page describes configuration of Apache2 server definitions. All the instructions should be executed in a terminal window by a user that has sudo authorization.
The text editor that is used here is nano.
First update and upgrade your repositories by running the following commands in a terminal window:
sudo apt update sudo apt upgrade -y
1. Setting up virtual host file
In 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 the 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 with the name
File Name to Write: /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
After the definition of an available site at available-sites
the new site has to be "enabled" typing the following command:
sudo a2ensite example.com.conf
If this command is successful then a new file will be added to the directory /etc/apache2/enabled-sites
. To see the content of the directory type:
ls /etc/apache2/enabled-sites
The result should be:
000-default.conf example.com.conf
The default file should be disabled 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.