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 example.com/html

Step 5 : Change the ownership and group of the directory example.com

sudo chown $USER:www-data -R example.com

Step 6 : Set the file permissions to secure web contents

sudo chmod 755 -R example.com

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

cd /var/www/example.com
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 example.com.conf

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

sudo nano example.com.conf
<VirtualHost *:80>
	ServerName example.com
	ServerAlias www.example.com

	ServerAdmin webmaster@example.com
	DocumentRoot /var/www/example.com/html

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

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

sudo a2ensite example.com.conf
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^