< All Topics
Print

How To Install the Apache Web Server on Ubuntu 22.04

Table of Contents

Apache HTTP Server

The Apache HTTP server is most widely used web server in the world. It is an open-source, cross-platform web server software, released under the terms of Apache License 2.0. It is one of the powerful web servers which provides dynamically loadable modules, robust media support, and integration with other popular software.

In this guide, you’ll learn how to install an Apache Web Server (Apache HTTP Server) on your Ubuntu 22.04 server.

Prerequisites

Before we begin installing an Apache web server, you will need an Ubuntu 22.04 Server with root or non-root user with sudo privileges. If you have enabled firewall, please ensure that ports 80 and 443 are allowed.

We recommend you to use non-root user with sudo privileges. Login to your server with non-root user and proceed to the first step.

Step 1 – Installing Apache

Apache is available in Ubuntu’s default software repositories. This makes possible to install Apache using conventional package management tools in Ubuntu.

Let us start with by updating local packages to reflect the latest upstream changes:

sudo apt update

Next, install the apache2 package:

sudo apt install apache2

The above command will install Apache and all required dependencies.

Step 2 – Checking Apache Web Server

Once the Apache web server is installed, it should be started automatically. The web server will already be up and running. You can ensure it using following command:

sudo systemctl status apache2
Output
apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Tue 2024-04-09 15:33:21 UTC; 43s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 6506 (apache2)
      Tasks: 55 (limit: 1119)
     Memory: 4.8M
        CPU: 33ms
     CGroup: /system.slice/apache2.service
             ├─6506 /usr/sbin/apache2 -k start
             ├─6507 /usr/sbin/apache2 -k start
             └─6508 /usr/sbin/apache2 -k start

As you can see in the above output, the Apache service has started successfully and it is running. However the best way to verify it using browser. You can access your server’s IP address directly in browser.

http://your_server_ip

You will see the default Ubuntu Apache web page as follow:

Apache2 Running

Step – 3 Managing the Apache Process

To stop your Apache web server, run:

sudo systemctl stop apache2

To start the Apache web server when it is stopped, run:

sudo systemctl start apache2

To stop and then start the service again, run:

sudo systemctl restart apache2

If you are making configuration changes, Apache can often reload without dropping the existing connection. To do this, you can use the following command:

sudo systemctl reload apache2

By default Apache service is set to start automatically when the server is rebooted/booted. For any reason, if you wish to disable the Apache service on startup, you can use the following command:

sudo systemctl disable apache2

To re-enable the service to start up at boot, run:

sudo systemctl enable apache2

Apache will now start automatically when the server boots again.

Step 4 – Setting Up Virtual Hosts (Recommended)

In Apache configuration, Virtual Hosts is being used to create different configurations for each website on the server. This allows to host multiple websites on the same server.

Apache on Ubuntu has one Virtual hosts entry by default that is configured to serve documents from the /var/www/html directory. This should work if you are going to host only one website. If you are going to host multiple websites, you can create additional folders inside /var/www/ directory as follow:

sudo mkdir /var/www/your_website

Replace your_website with the actual website name so that you can easily identify the folder.

Next, assign ownership of the directory to the user you’re currently signed in as with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_website

Once you set the ownership, it is essential that you assign appropriate permissions to the folder. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:

sudo chmod -R 755 /var/www/your_website

You can now create a sample test file inside /var/www/your_website directory.

In order for Apache to serve the appropriate contents, we will create virtual host file for each website. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf directly, make a new one at /etc/apache2/sites-available/your_website.conf:

sudo nano /etc/apache2/sites-available/your_website.conf

Add in the following configuration block, which is similar to the default, but updated for your new directory and domain name:

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

In the above Virtual Host entry, we have updated DocumentRoot to our new directory. We have also added ServerName and ServerAlias.

Save and close the file when you are finished.

To enable the file with the a2ensite tool:

sudo a2ensite your_website.conf

Since we have created a new virtual host file, we can disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Next, we will test for configuration errors:

sudo apache2ctl configtest

You should receive the following output:

Output
. . .
Syntax OK

Restart Apache to implement your changes:

sudo systemctl restart apache2

Considering you have pointed your domain to your server’s IP address, you can now access your website using your domain name from your browser.

Leave a Reply