How to Install Apache Web Server on Ubuntu 24.04
Introduction
Apache is an open-source web server application that enables the delivery of static and dynamic web applications on a server. It’s highly customizable, supports multiple modular extensions, and can work as a reverse proxy or load balancer to deliver web applications or backend services on your server.
This article explains how to install the Apache web server on Ubuntu 24.04 and securely deliver web applications using virtual host configurations on your server.
Prerequisites
- Deploy an Ubuntu 24.04 instance.
- Create a new domain A record pointing to the server IP address. For example,
app.example.com. - Access the server using SSH and log in as a non-root user with sudo privileges.
- Update the server.
Install Apache
The Apache web server package is available in the default repositories on Ubuntu 24.04. Follow the steps below to install the latest version using the default APT package manager.
Update the server’s package index:
sudo apt update
Install the Apache web server package:
sudo apt install apache2 -y
View the installed Apache web server version:
apache2 -v
Allow connections on the HTTP port 80:
sudo ufw allow 80/tcp
Access your public server IP using a web browser such as Chrome and verify that the default Apache web page displays.
Manage the Apache Service
Apache runs with the apache2 system service profile that manages the web server runtime processes on your server. Follow the steps below to enable the Apache web server to start at boot time, and verify the system service status.
Enable the Apache service to automatically start at boot time:
sudo systemctl enable apache2
Start the Apache web server:
sudo systemctl start apache2
View the Apache service status and verify that it’s active on your server:
sudo systemctl status apache2
Stop the Apache web server:
sudo systemctl stop apache2
Restart the Apache web server:
sudo systemctl restart apache2
Configure a Virtual Host
Follow the steps below to disable the default virtual host and create a new virtual host to listen for connection requests using your domain on the default HTTP port 80.
Disable the default Apache virtual host configuration:
sudo a2dissite 000-default.conf
Create a new Apache virtual host configuration file in the /etc/apache2/sites-available/ directory using a text editor such as nano. For example, website.conf:
sudo nano /etc/apache2/sites-available/website.conf
Add the following contents to the file. Replace app.example.com with your domain and webmaster@example.com with your web administrator email:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName app.example.com
DocumentRoot /var/www/html/website
DirectoryIndex index.html index.php
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/website>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Save and close the file.
Enable the new Apache virtual host configuration:
sudo a2ensite website.conf
Test the Apache configuration for errors:
sudo apache2ctl configtest
Create the web root directory /var/www/html/website referenced in your virtual host configuration:
sudo mkdir -p /var/www/html/website
Create a new sample HTML application index.html file in the web root directory:
sudo nano /var/www/html/website/index.html
Add the following contents to the file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Apache Web Server</title>
</head>
<body>
<h1>Greetings from Keosombath</h1>
</body>
</html>
Save and close the file.
Grant the web server user and group www-data ownership privileges to the /var/www/html/website web root directory:
sudo chown -R www-data:www-data /var/www/html/website
Restart the Apache web server to apply your configuration changes:
sudo systemctl restart apache2
Secure Apache with Let’s Encrypt SSL
The Apache web server listens for connection requests on the HTTP port 80 by default. HTTP enables unencrypted requests between your web server and a user’s browser. HTTPS enables encrypted connections using valid SSL certificates to authenticate the web server with the user’s browser.
Follow the steps below to secure the Apache web server with trusted Let’s Encrypt SSL certificates to enable encrypted network connections and forward all HTTP requests to HTTPS.
Install the Certbot Let’s Encrypt client package using the Snap package manager:
sudo snap install --classic certbot
Request a new Let’s Encrypt SSL certificate on your server using your virtual host domain. Replace app.example.com with your actual domain and hello@example.com with your email address:
sudo certbot --apache -d app.example.com -m hello@example.com --agree-tos
Test the Certbot SSL certificate renewal process:
sudo certbot renew --dry-run
Restart the Apache web server to apply your SSL configuration changes:
sudo systemctl restart apache2
Configure the Firewall
Uncomplicated Firewall (UFW) is active and enabled on Ubuntu 24.04 servers by default. Follow the steps below to configure the UFW utility and allow network connections using the Apache web server firewall profile.
List all available UFW application profiles:
sudo ufw app list
Allow the Apache Full profile to enable both HTTP port 80 and HTTPS port 443 connections through the firewall:
sudo ufw allow "Apache Full"
Reload UFW to apply the firewall changes:
sudo ufw reload
View the firewall status and verify that the new rules are available:
sudo ufw status
Expected output:
To Action From
-- ------ ----
22/tcp ALLOW Anywhere
Apache Full ALLOW Anywhere
22/tcp (v6) ALLOW Anywhere (v6)
Apache Full (v6) ALLOW Anywhere (v6)
Access your virtual host domain app.example.com in a new web browser window and verify that Apache serves your HTML application with a Greetings from Keosombath message.
Conclusion
You have installed the Apache web server on an Ubuntu 24.04 server. Apache enables you to host static websites and integrate with dynamic content processors such as PHP to serve modern web applications such as WordPress on your server. In addition, you can use the Apache web server as a reverse proxy to securely deliver your backend services using the mod_proxy extension on your server.
For more information and configuration options, visit the Apache documentation.