How to Install and Configure the Apache web server on Ubuntu Server 20.04

Louis SanchezAugust 14th 2021, 11:22

Installing Apache2

Follow these steps to install and configure the Apache web server:

Step 1 : Install Apache2 from the Ubuntu package repository:

sudo apt-get update
sudo apt-get install apache2

Step 2 : Check if Apache2 is properly installed and running

sudo service apache2 status

Step 3 : You can also open a browser on a local machine and point it to the server IP address. You should see a default It works!

Configuring Apache2

Step 4 : Now, let's proceed with creating our first virtual host. First create a directory structure.

cd /var/www
sudo mkdir -p

Step 5 : Change the ownership and group of the directory

sudo chown $USER:www-data -R

Step 6 : Set the file permissions to secure web contents

sudo chmod 755 -R

Step 7 : Create a index.html file under the html directory

cd /var/www/
echo 'Welcome' > html/index.html

Step 8 : Next, we need to set up a new virtual host under the Apache configuration. Copy the default Virtual Host file under /etc/apache2/sites-available and use it as a starting point for our configuration:

cd /etc/apache2/sites-available
sudo cp 000-default.conf

Step 9 : Edit to match it with the following example.

sudo nano
<VirtualHost *:80>

	DocumentRoot /var/www/

	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined

Step 10 : Save the changes. Enable our new VirtualHost with a2ensite and reload Apache

sudo a2ensite
sudo service apache2 reload

Step 11 : Start your browser and point it to the domain of your server

Installing the LAMP stack

If you are interested in installing the entire LAMP stack, then Ubuntu provides a single command to do so. Use the following command to install Apache, PHP, and MySQL collectively:

sudo apt-get install lamp-server^