How to Install and configure the Apache Web Server - Ubuntu Server 22.04

Team AApril 23rd 2022, 8:09

Simple installation of the Apache web server from the Ubuntu package repository will be carried out, which will be an easy procedure. In addition, we will go over the core configuration settings and build our first web page from the ground up. The Apache web server, usually known as simply Apache, is the most popular project of the Apache Foundation, having had over a million downloads. It is also the most widely used web server on the internet.

Installing and configuring the Apache web server is simple if you follow these steps: Install the Apache web server.

Install Apache

Step 1. Install Apache2 from the Ubuntu package repository.

sudo apt-get update
sudo apt-get install apache2

Configuring the Firewall

Step 2 : Before launching and testing the Apache web server, it will be necessary to make changes to the firewall configuration to allow the web server to communicate with the external world. Due to the default configuration of the HTTP and HTTPS protocols, which use ports 80 and 443 respectively, depending on which protocols are being used, either one or both of these ports will need to be opened to allow access. If your Ubuntu system is protected by the Uncomplicated Firewall, you can use the following command to allow only insecure web traffic (HTTP) to pass through:

sudo ufw app list

sudo ufw allow Apache

Step 3 : To allow only secure (HTTPS) traffic, configure the following:

sudo ufw allow 'Apache Secure'

Step 4 : Alternatively, you can enable both secure and insecure web traffic in the manner described below:

sudo ufw allow 'Apache Full'

Getting the Apache Web Server Up and Running

Step 5 : Verify that Apache2 has been successfully installed. The index.html file should be downloaded using the wget command:

wget 127.0.0.1

Step 6 : You can also use a local machine to open a browser and point it to the server's IP address if necessary. You should see the following It works! page, which has been customized for Ubuntu:

Step 7 : Let's get started with creating our very first virtual host right away. First, create a directory structure for your files. Modify the directory structure to include a new directory for the contents of our website: /var/www.

cd /var/www
sudo mkdir example.com

Step 8 : Modify the directory example.com's ownership and group membership information.

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

Step 9 : Make sure that the file permissions are set to "secure web contents."

sudo chmod -R 750 example.com

Step 10 : create all of the necessary directories within the example.com directory.

cd example.com
mkdir public_html

Step 11 : Create an index.html file in the public html directory by typing the following:

echo '<h1>Hello World !!!</h1>' > public_html/index.html

Step 12 : After that, we'll need to create a new virtual host in the Apache settings.

Step 13 : Create a backup copy of the default Virtual Host file located in the /etc/apache2/sites-available directory and use it as the starting point for our configuration as follows:

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

Step 14 : Change the contents of example.com.conf to match the contents of the following example. Change the following parameters according to your needs:

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        
        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/example.com/public_html
        
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Step 15 : Exit the example.com.conf file after saving the changes.

Step 16 : If you are using the same port as the default VirtualHost, don't forget to disable the default VirtualHost as well:

sudo a2dissite 000-default.conf

Step 17 : Finally, enable our new VirtualHost with a2ensite and reload Apache to complete the process.

sudo a2ensite example.com.conf
sudo service apache2 reload

Step 18 : Open your web browser and navigate to the domain or IP address of your server (if applicable).