Table of Contents[Hide][Show]
The Apache HTTP server is the most widely used web server globally, known for its flexibility and powerful features. It supports dynamically loadable modules, robust media handling, and seamless integration with many popular software applications, making it an ideal choice for a variety of web hosting needs.
In this guide, we’ll walk you through the process of how to install Apache web server on Ubuntu 24.04, ensuring your server is ready to handle your web traffic efficiently and securely.
Prerequisites
Before starting this guide, ensure you have a regular, non-root user with sudo privileges configured on your server. Additionally, it is important to set up a basic firewall to block non-essential ports.
Once your account is set up, log in as your non-root user to proceed.
How To Install Apache Web Server on Ubuntu 24.04
Step 1 — Installing Apache
Apache is available in Ubuntu’s default software repositories, allowing you to easily install it using standard package management tools.
First, update the local package index to ensure you have the latest information on available packages:
sudo apt update
Next, install Apache web server package:
sudo apt install apache2
Once you confirm the installation, apt
will automatically install Apache along with all necessary dependencies.
Step 2 — Adjusting the Firewall
Before testing Apache, you need to adjust your firewall settings to allow external access to the default web ports. If you followed the prerequisites, you should already have a UFW firewall configured to limit access to your server.
When Apache is installed, it automatically registers itself with UFW and provides several application profiles for controlling access through the firewall.
To view the available UFW application profiles, run the following command:
sudo ufw app list
This will display the available application profiles:
Available applications:
Apache
Apache Full
Apache Secure
OpenSSH
The output lists three Apache-related profiles:
- Apache: Opens port 80 (for unencrypted HTTP traffic)
- Apache Full: Opens both port 80 (HTTP) and port 443 (HTTPS/TLS encrypted traffic)
- Apache Secure: Opens only port 443 (for HTTPS/TLS encrypted traffic)
For now, since we haven’t configured SSL on our server, it’s recommended to enable the profile that allows traffic on port 80 only. To do so, run:
sudo ufw allow 'Apache'
Afterward, you can confirm the change by checking the firewall status with the following command:
sudo ufw status
The output should show that Apache traffic is allowed:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Apache ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Apache (v6) ALLOW Anywhere (v6)
As shown in the output, the firewall has been updated to allow access to the Apache web server.
Step 3 — Checking Your Web Server
At the end of the installation process, Apache should already be up and running on your server. To verify that the service is active, check its status using the systemd
init system by running the following command:
sudo systemctl status apache2
You should see output similar to the following:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-04-23 22:36:30 UTC; 20h ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 29435 (apache2)
Tasks: 55 (limit: 1137)
Memory: 8.0M
CGroup: /system.slice/apache2.service
├─29435 /usr/sbin/apache2 -k start
├─29437 /usr/sbin/apache2 -k start
└─29438 /usr/sbin/apache2 -k start
This confirms that the Apache service is running. However, the best way to ensure everything is working is to test it by requesting a page from Apache.
To check if Apache is serving web pages, simply access the default Apache landing page through your server’s IP address. If you don’t know your server’s IP address, you can find it using one of the following methods:
- Run the following command to see your local IP address:
hostname -I
This will return one or more IP addresses. You can try entering these into your browser’s address bar to test. - Alternatively, use the
icanhazip
tool to get your public IP address:curl -4 icanhazip.com
Once you have your server’s IP address, open your browser and enter the following URL:
http://your_server_ip
You should now see the default Apache landing page, indicating that Apache is running correctly.
This page confirms that Apache is working correctly. It also provides some basic information about important Apache configuration files and directory locations, helping you understand where key components of the web server are stored.
Step 4 — Managing the Apache Process
Now that your web server is up and running, let’s review some basic commands to manage the Apache process using systemctl
.
- To stop the Apache web server, run:
sudo systemctl stop apache2
- To start the Apache web server when it’s stopped, run:
sudo systemctl start apache2
- To restart the Apache service (stop and then start it again), use:
sudo systemctl restart apache2
- If you are making configuration changes and want Apache to reload without dropping existing connections, use the reload command:
sudo systemctl reload apache2
- By default, Apache is set to start automatically when the server boots. If you want to disable this behavior, run:
sudo systemctl disable apache2
- To re-enable Apache to start automatically at boot, use:
sudo systemctl enable apache2
With these commands, you can effectively manage the Apache service, ensuring it behaves according to your preferences, whether you want it to start automatically or control it manually.
Step 5 — Setting Up Virtual Hosts (Recommended)
When using Apache, you can set up virtual hosts (similar to server blocks in Nginx) to manage multiple websites on a single server. In this guide, we’ll configure a domain called your_domain
—be sure to replace this with your actual domain name. If you’re setting up a domain with a provider like DigitalOcean, refer to their Networking Documentation for more information.
By default, Apache on Ubuntu 24.04 has a single virtual host configured to serve documents from /var/www/html
. While this works well for a single site, managing multiple sites in this directory can become cumbersome. Instead, we’ll create a new directory structure within /var/www
for your domain while leaving /var/www/html
as the default directory for unmatched requests.
1. Create the Directory for Your Domain
Start by creating a directory for your_domain
:
sudo mkdir /var/www/your_domain
2. Assign Ownership to the Directory
Assign ownership of the directory to your user (using the $USER
environment variable):
sudo chown -R $USER:$USER /var/www/your_domain
Ensure the directory permissions are set correctly. This allows the owner to read, write, and execute files, while others only have read and execute permissions:
sudo chmod -R 755 /var/www/your_domain
3. Create a Sample Index Page
Next, create a simple index.html
page to serve as a placeholder for your site:
sudo nano /var/www/your_domain/index.html
Add the following sample HTML:
Welcome to Your_domain!
Success! The your_domain virtual host is working!
Save and close the file.
4. Create a Virtual Host Configuration File
Now, let’s create a new virtual host configuration file at /etc/apache2/sites-available/your_domain.conf
:
sudo nano /etc/apache2/sites-available/your_domain.conf
Paste the following configuration block, which is similar to the default configuration but updated for your new directory and domain:
ServerAdmin webmaster@localhost
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
Here’s what each directive does:
- ServerName: Defines the primary domain name for this virtual host.
- ServerAlias: Defines additional names (e.g.,
www.your_domain
) that should resolve to the same virtual host. - DocumentRoot: Points to the directory where the website’s files are stored.
Save and close the file.
5. Enable the Virtual Host
Enable the new site using the a2ensite
tool:
sudo a2ensite your_domain.conf
Next, disable the default site (which is configured in /etc/apache2/sites-available/000-default.conf
):
sudo a2dissite 000-default.conf
6. Test for Configuration Errors
Before restarting Apache, test for any configuration errors:
sudo apache2ctl configtest
You should see the following output:
Syntax OK
7. Restart Apache
Finally, restart Apache to apply your changes:
sudo systemctl restart apache2
Apache should now serve your domain. You can test this by navigating to http://your_domain
in your web browser. If everything is configured correctly, you should see the message:
Success! The your_domain virtual host is working!
Step 6 — Getting Familiar with Important Apache Files and Directories
Now that you know how to manage the Apache service, it’s important to familiarize yourself with the key directories and configuration files that Apache relies on. These files will help you customize and manage your web server efficiently.
Content
/var/www/html
: This is the default directory where Apache serves web content. Initially, it only contains the default Apache landing page. You can change the content served from this directory by modifying the Apache configuration files.
Server Configuration
/etc/apache2
: The main directory for Apache’s configuration files./etc/apache2/apache2.conf
: The primary configuration file for Apache. This file loads many of the other configuration files in the/etc/apache2
directory and defines global settings for Apache./etc/apache2/ports.conf
: This file specifies the ports Apache listens on. By default, Apache listens on port 80 for HTTP and port 443 for HTTPS if SSL modules are enabled./etc/apache2/sites-available/
: This directory holds the configuration files for individual virtual hosts (sites). These files are not active until linked to thesites-enabled
directory using thea2ensite
command./etc/apache2/sites-enabled/
: This directory contains symbolic links to the configuration files stored insites-available
. Apache reads these files when it starts or reloads, compiling a complete configuration./etc/apache2/conf-available/
and/etc/apache2/conf-enabled/
: These directories work similarly tosites-available
andsites-enabled
, but store configuration fragments that do not belong in a virtual host. Files inconf-available
can be enabled with thea2enconf
command and disabled witha2disconf
./etc/apache2/mods-available/
and/etc/apache2/mods-enabled/
: These directories store configuration files for Apache modules. Files ending in.load
load specific modules, while.conf
files contain the configurations for those modules. You can enable or disable modules usinga2enmod
anda2dismod
.
Server Logs
/var/log/apache2/access.log
: This log file records every request made to your web server. It can help you track traffic and diagnose performance issues./var/log/apache2/error.log
: This log records any errors Apache encounters. The level of detail in the error log is controlled by theLogLevel
directive in the Apache configuration files. It’s crucial for troubleshooting and identifying issues with your web server.
Conclusion
In this guide, we have walked through the process of how to install Apache web server on Ubuntu 24.04 and explored essential steps such as managing the Apache service, setting up virtual hosts, and understanding key configuration files and directories. By following these steps, you should now have a fully functioning Apache web server that can serve multiple websites and be easily configured to suit your needs.
Apache remains one of the most reliable and widely used web servers in the world, offering flexibility, scalability, and strong community support. Whether you’re hosting a single website or managing multiple domains, Apache on Ubuntu 24.04 provides the tools you need for efficient web hosting.
If you continue to explore Apache’s extensive documentation and configuration options, you can further optimize your server for performance, security, and ease of use.