Install & Configure Apache HTTP Server on Ubuntu

By Raman Kumar

Updated on Aug 05, 2024

In this tutorial, we'll install and configure Apache on Ubuntu 24.04. 

The Apache HTTP Server, commonly known as Apache, is a powerful and flexible open-source web server software. It plays a crucial role in serving web content and applications by handling HTTP requests from clients. Renowned for its stability, security, and extensive configuration options, Apache supports various features including URL redirection, authentication, and server-side scripting. It's widely used across different operating systems and remains a popular choice for web hosting due to its robust performance and scalability.

Prerequsistes

  • A Ubuntu 24.04 installed dedicated server or KVM VPS
  • A root user access or normal user with sudo rights
  • Basic Linux command knowledge

Step 1: Update the System

Before installing Apache, update the package lists for upgrades and new package installations.

sudo apt update
sudo apt upgrade -y

Step 2: Install Apache

Install the Apache2 package using the apt package manager.

sudo apt install apache2 -y

Verify Apache Installation. Check the Apache service status to ensure it is running.

sudo systemctl status apache2

You should see output indicating that the Apache service is active (running).

Step 3: Configure Firewall

If you have a firewall enabled, allow traffic on HTTP (port 80) and HTTPS (port 443).

sudo ufw allow 'Apache Full'

Check the status of the firewall to ensure the rules have been added.

sudo ufw status

Step 4: Test Apache Installation

Open a web browser and go to your server's IP address. You should see the default Apache welcome page, which indicates that Apache is installed and running correctly.

http://your_server_ip

Step 5: Manage Apache Service

To stop the Apache service, use:

sudo systemctl stop apache2

To start the Apache service, use:

sudo systemctl start apache2

To restart the Apache service, use:

sudo systemctl restart apache2

To reload the Apache service without dropping connections, use:

sudo systemctl reload apache2

To enable Apache to start on boot, use:

sudo systemctl enable apache2

To disable Apache from starting on boot, use:

sudo systemctl disable apache2

Step 6: Configure Apache

The main configuration file for Apache is located at /etc/apache2/apache2.conf. You can edit this file to customize your Apache server configuration.

sudo nano /etc/apache2/apache2.conf

Step 7: Setting Up Virtual Hosts

If you want to host multiple websites on a single server, you need to set up virtual hosts. Here is an example of setting up a virtual host:

Create a directory for the website.

sudo mkdir -p /var/www/your_domain

Assign ownership of the directory with the www-data environment variable.

sudo chown -R www-data:www-data /var/www/your_domain

Create a sample index.html file.

nano /var/www/your_domain/index.html

Add the following content:

<html>
    <head>
        <title>Welcome to Your Domain!</title>
    </head>
    <body>
        <h1>Success! The your_domain virtual host is working!</h1>
    </body>
</html>

Create a new virtual host configuration file.

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

Add the following configuration:

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

Enable the new virtual host file.

sudo a2ensite your_domain.conf

Disable the default virtual host file (if necessary).

sudo a2dissite 000-default.conf

Test for configuration errors.

sudo apache2ctl configtest

Reload Apache to apply the changes.

sudo systemctl reload apache2

Step 8: Secure Apache with SSL (Optional)

For a secure connection, you can enable SSL using Let's Encrypt.

Install Certbot and the Apache plugin.

sudo apt install certbot python3-certbot-apache -y

Obtain an SSL certificate.

sudo certbot --apache -d your_domain

Follow the prompts to configure SSL for your domain.

Conclusion

We have successfully seen how to Install & Configure Apache HTTP Server on Ubuntu 24.04. You can now host your websites and applications on your server.

If you have any specific configurations or additional requirements, feel free to ask!