Ubuntu

Install and Configure Apache Web Server on Ubuntu 22.04

For decades, Apache or Apache HTTP Server has been one of the most widely used web server applications worldwide, and it still maintains its popularity while having 48.0 million active sites. The Apache Software Foundation manages this web server. Dynamically loadable modules, management of static files, and easy integration with other applications are a few fantastic features of Apache Web Server.

This write-up will discuss the method to install and configure Apache Web Server on Ubuntu 22.04. So, let’s start!

How to install Apache Web Server on Ubuntu 22.04

To set up Apache Web Server on Ubuntu 22.04, you must follow the below-given step-by-step instructions.

Step 1: Update system repositories

Press “CTRL+ALT+T” to open the terminal and run the below-given command to update system repositories:

$ sudo apt update

Step 2: Install Apache Web Server on Ubuntu 22.04

After updating system repositories, write out the following command for installing the “apache2” web server on your Ubuntu 22.04 system:

$ sudo apt install apache2

Wait for a few minutes as the Apache installation will take some time to complete:

Step 3: Configure Firewall

To access Apache from outside, it is required to open specific ports on your system. To do so, firstly, check the list of the applications profiles that need access:

$ sudo ufw app list

Execution of the above-given command will print out different apache profiles on the terminal:

Next, we will utilize the “Apache Full” profile for enabling network activities on the port “80”:

$ sudo ufw allow 'Apache Full'

After doing so, check the current firewall status:

$ sudo ufw status

Now, let’s move towards the configuration side of Apache Web Server on Ubuntu 22.04.

How to Configure Apache Web Server on Ubuntu 22.04

To configure Apache Web Server on Ubuntu 22.04, look at the below-given procedure.

Step 1: Check apache2 service status

Before moving towards the main configuration of Apache, we will verify the “apache2” service is currently active or not:

$ systemctl status apache2

The below-given shows that the “apache2” service is active on our system:

You can also utilize your favorite web browser for the specified verification. To do so, open a web browser and check what the “localhost” web page beholds for you:

Navigation to the “localhost” web page indicates that Apache Web Server is working properly. Now, we will set up a virtual host in Apache.

Step 2: Set up Virtual Host in Apache Web Server

To set a virtual host in Apache, create a directory that can be utilized to store data on the website. For this purpose, we will move to the “/var/www” directory using the following “cd” command:

$ cd /var/www/

Then, we will create a directory for our domain “example.com”. Here, you can specify your domain name in the below-given command:

$ sudo mkdir -p /var/www/example.com/

Utilize the “chown” command for changing the ownership of the “example.com” directory with the “www-data” environment variable:

$ sudo chown -R www-data:www-data /var/www/example.com

Step 3: Creating a web page

To create a sample “index.html” web page for our website, we will use the “nano” editor:

$ sudo nano /var/www/example.com/index.html

Write out the following code in the opened file:

<html>

<head>

<title>Welcome to linuxhint.com</title>

</head>

<body>

<h1>Success! The tutorial is all about apache2 configuration!</h1>

</body>

</html>

After adding the code, press “Ctrl+O” to save the file:

Step 4: Creating a Virtual host file

At this point, we have created a directory for our domain and updated its ownership. Now, we will create a virtual host file under the default directory of Apache host files:

$ sudo nano /etc/apache2/sites-available/example.com.conf

In the opened virtual host file, add the following lines of code. Also, you have to replace the information related to “ServerName”, “ServerAlias”, and “DocumentRoot” according to your settings:

<VirtualHost *:80>

ServerAdmin admin@localhost

ServerName example.com

ServerAlias www.example.com

DocumentRoot /var/www/example.com

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Press “Ctrl+O” to save the added code of the virtual host configuration file:

Step 5: Enable Virtual host file

Execute the following “a2ensite” command for enabling the created virtual host file:

$ sudo a2ensite example.com.conf

Then disable the default configuration file:

$ sudo a2dissite 000-default.conf

After performing the specified operation. Restart the “apache” service on your Ubuntu 22.04 system:

$ sudo systemctl restart apache2

Step 6: Error testing

In the last step of Apache2 configuration, test the configuration errors:

$ sudo apache2ctl configtest

In case of having an error-free configuration file, the execution of the above-given command will let you know that the Syntax is “OK”:

However, there exist chances that you may face the following error after testing the configuration file:

If that’s the case, then edit the “servername.conf” file and add your “domain” name:

$ sudo nano /etc/apache2/conf-available/servername.conf

After specifying the domain name in the opened configuration file, press “Ctrl+O” to save the added content:

Next, enable the new configuration with the help of the following command:

$ sudo a2enconf servername

Lastly, restart the “apache2” service on your Ubuntu 22.04 system:

$ sudo systemctl restart apache2

Step 7: Virtual Host testing

Finally, test your Virtual host by navigating to the specified domain. In our case, the domain is “example.com”:

The displayed information justifies that Apache Web Server is ready to serve on the specified domain.

Conclusion

To install Apache Web Server on Ubuntu 22.04, execute the “$ sudo apt install apache2” command and configure firewall “$ sudo ufw allow ‘Apache Full’”. Then, create a directory under the default configuration files of the Apache Web server, change its ownership, and set up a virtual host server. After performing the specified operations, restart the “apache2service, and you are good to go. This write-up discussed the method to install and configure Apache Web Server on Ubuntu 22.04.

 

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.