How to Install and Configure Apache on Ubuntu 22.04?


A Web Server is the communicator between the client and the web. The client requests some information, this request is passed to the server, the server then fetches the data and provides it back to the client.

Apache is one of the most used Web servers that processes the HTTP request normally and also supports HTTPS, depending upon the client’s request.

Its competitor, NGINX is fast but does not have modular support or effective cross-platform support and thus Apache has an edge in this regard.

Our today’s guide refers to the detailed installation and configuration of Apache on Ubuntu 22.04.

How to Install Apache on Ubuntu 22.04?

Apache Web server’s package resides in the default repositories of Ubuntu with the name of apache2. The version2 (apache2) is the most used when talking about the Apache users. Let’s see how it can be installed on Ubuntu 22.04:

First, you have to update the core libraries (system’s packages list in the default repositories):

sudo apt update

Now, install Apache2 as:

sudo apt install apache2

Only installing Apache does not mean you are set to use it on Ubuntu 22.04. There are a few essential configurations that are elaborated on in the upcoming section.

How to Configure Apache on Ubuntu 22.04?

Just installing the Apache is not enough to use it on Ubuntu. To make Apache effective on Ubuntu, you must have to configure the Apache service on the system and through the firewall. Let’s understand how:

Configure the Apache Service

You must have to manage/configure the Apache service on the system. The systemctl utility is used to manage it as follows:

sudo systemctl start apache2 #Start Apache
sudo systemctl stop apache2 #Halt/Stop the Apache Daemon

sudo systemctl enable apache2 #Enable Apache to start auto
sudo systemctl disable apache2 #Disable Apache Daemon

sudo systemctl status apache2 #Check the Status
sudo systemctl reload apache2 #Reload the Apache Configurations

Important: To learn more about managing the Apache Service, Read Out This Post.

Configure Apache on the Firewall

The main configuration of the Apache depends on the firewall. Apache listens on two ports, i.e., 80 (HTTP) and 443 (HTTPS). You must allow or deny the traffic on any of these (or both, depending upon the requirement) to allow or deny the incoming traffic on these ports.

So, let’s do it:

Enable the Firewall and Check the Firewall Rules

First, enable the firewall through the UFW utility to make it available for the configuration:

sudo ufw status

Check for the Available Rules

sudo ufw app list

As you see, all the possible Apache profiles have already. If you have not or want to add, you can do it easily after reading/understanding the following:

Add Firewall Rules For Apache

Apache firewall configuration depends on the following three profiles?>

  • Apache (For HTTP): Opens only 80.
  • Apache Secure (For HTTPS): Opens only 443.
  • Apache Full (HTTP and HTTPS both): Opens both 80 and 443.

Let’s say we want to add the Apache profile only:

sudo ufw allow Apache

Or you can also mention the port, as:

sudo ufw allow 80

Reload the UFW and verify by checking the status through ufw utility:

sudo ufw reload
sudo ufw status

Similarly, use the port 443 or ‘Apache Secure’ profile to permit/block the Apache for HTTPS connections.

Or use the ‘Apache Full’ profile to allow/deny HTTP and HTTPS connections.

Deny Apache Through Firewall

You can deny the Apache traffic via any port/profile using the following syntax:

sudo ufw deny port/profile

When all is done, test the Apache server’s test page as follows:

Set Up Apache Virtual Host

Virtual Hosting is the phenomenon of a server hosting multiple sites. When dealing with multiple sites on Apache, you have to set up an Apache virtual host as follows:

Important: It is recommended to log in as a root user to perform the following steps or a user with root/sudo privileges.

Step 1: Create the Root Directory

First, establish a root/document directory hierarchy for virtually hosting a site. It is recommended to do it inside the “/var/www” as Ubuntu does not allow you to access files outside it:

sudo mkdir /var/www/your-domain

Note: To allow the user to access/modify the root/document directory of the website, you must grant the ownership and the specific read/write permissions to the current user.

Grant the ownership of the root directory to the current user:

sudo chown -R $USER:$USER /var/www/your-domain

Also, allow the current user to read and write whereas the other users can only read:

sudo chmod -R 755 /var/www/your-domain

Step 2: Create the Main Page

Now, create the main HTML page inside the root/document directory of your domain/website:

sudo nano /var/www/your-domain/index.html

Add some dummy data to ensure the working at the time of testing:

Step 3: Create the Configuration File

Now, create the configuration file for your domain inside the “/etc/apache2/sites-available/” directory. It is recommended to copy the default configuration file and make the changes according to your domain:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/<your-config-file>

Note: Apache reads configurations from the “sites-available” directory.

Now, open the config file using the nano editor to make the changes:

Scroll down and you will see two log files:

Note: You can also add the ServerName field by adding the name of the server.

ServerName your-domain

Step 4: Apply the Changes

If you have added a ServerName field, it is recommended to map it inside the apache2.conf file as well. Open the apache2.conf file in the nano editor and perform the changes as:

sudo nano /etc/apache2/apache2.conf

 

Now, use Apache’s “a2ensite” script to enable the newly set configurations:

sudo a2ensite <your-domain-conf-file>

Similarly, disable the default configurations:

  • Check the existence of the default configuration file and then use the “a2dissite” to disable it:
ls -al /etc/apache2/sites-available
sudo a2dissite 000-default.conf

Lastly, use the below command to test the newly set configurations:

sudo apache2ctl configtest

Reload the Apache daemon to apply the changes:

sudo systemctl reload apache2

Step 5: Test the Apache Virtual Host

Load the localhost in your browser which invokes the local repository, i.e., “/var/www/index.html” in our case:

That’s how you can configure the Apache virtual hosts on Ubuntu 22.04.

Disable/Remove the Apache Virtual Hosts

If you want to disable the configured virtual hosts, you need to check for the configurations inside the “/etc/apache2/sites-available/” and then remove/disable the unnecessary configuration files. Let’s see how to do it:

List the content of the “/etc/apache2/sites-enabled/” to check which site is enabled:

ls /etc/apache2/sites-enabled/

Here, if you see the “demogenie.com.conf” is the currently in use/active configuration file. First, you have to disable it as follows:

sudo a2dissite demogenie.com.conf

Now, enable the default configuration file:

sudo a2ensite 000-default.conf

Reload the Apache daemon and to verify whether the site is enabled or not, list the content of the “sites-enabled”:

sudo systemctl reload apache2

ls /etc/apache2/sites-enabled/

Here you go with the default configurations.

How to Remove/Uninstall Apache From Ubuntu 22.04?

If you are no longer using the Apache services on your system, you can remove/uninstall the server support from your Ubuntu as follows:

sudo apt autoremove apache2

This was all about the installation and configuration of the Apache on Ubuntu 22.04.

Bottom Line

Apache is one of the leading web servers on the Linux systems. Our today’s guide has demonstrated the installation and configuration of the Apache on Ubuntu 22.04.

Apache is placed with the name “apache2” in the default repositories and can be installed easily. The installation is quite easy but the configuration requires some technical knowledge. To make it understandable, we have provided the steps with a basic description to configure Apache on Ubuntu 22.04.

Print Friendly, PDF & Email
Categories