How to Install and Configure Apache2 on Ubuntu 24.04: A Comprehensive Guide

install http apache web server 1

Learn how to install and configure Apache2 on Ubuntu in this step-by-step guide. Get Apache2 up and running for your website today!

Introduction

Apache2, one of the most popular web servers globally, powers a large portion of the websites on the internet. Known for its flexibility, ease of use, and extensive community support, Apache2 is the go-to choice for hosting websites and applications. In this guide, we’ll walk you through how to install and configure Apache2 on Ubuntu, ensuring that your web server is ready for traffic in no time.

Whether you’re a seasoned developer or a newcomer, this guide covers everything you need to know to get started with Apache2.

Apache2 is an open-source HTTP server software developed by the Apache Software Foundation. It supports a wide range of functionalities through modules, including security, performance optimization, URL redirection, and authentication. Apache2’s modular design allows users to tailor their web server configuration, making it suitable for small personal websites to large, complex enterprise applications.

Installing Apache2 On Ubuntu 24.04

 

Step 1: Update Ubuntu Package Index

Before installing Apache2, it’s essential to update your system to ensure all the latest packages and dependencies are in place.

$ sudo apt update

This command will synchronize your package index with the official Ubuntu repositories.

Step 2: Install Apache2 on Ubuntu

Once your package index is updated, you can proceed with installing Apache2. Use the following command:

$ sudo apt install apache2 -y

Output :

ramansah@dev02:~$ sudo apt install apache2 -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1t64 libaprutil1-dbd-sqlite3 libaprutil1-ldap
libaprutil1t64
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1t64 libaprutil1-dbd-sqlite3 libaprutil1-ldap
libaprutil1t64
0 upgraded, 8 newly installed, 0 to remove and 194 not upgraded.
Need to get 1,899 kB of archives.

Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /usr/lib/systemd/sy
stem/apache-htcacheclean.service.
Processing triggers for ufw (0.36.2-6) ...
Processing triggers for man-db (2.12.0-4build2) ...
Processing triggers for libc-bin (2.39-0ubuntu8.3) ...

Then view the Apache web server version by submitting command line : apachectl -v, as shown below :

ramansah@dev02:~$ apachectl -v
Server version: Apache/2.4.58 (Ubuntu)
Server built: 2024-07-17T18:55:23

Step 3: Manage Apache2 Service

o ensure that Apache2 is running properly, you can check its status with the following command:

$ sudo systemctl status apache2

You should see a status indicating that the Apache2 service is active and running. Here are some useful commands for managing the Apache2 service:

  • Start Apache2: sudo systemctl start apache2
  • Stop Apache2: sudo systemctl stop apache2
  • Restart Apache2: sudo systemctl restart apache2
  • Enable Apache2 at Boot: sudo systemctl enable apache2
  • Disable Apache2 at Boot: sudo systemctl disable apache2
ramansah@dev02:~$ sudo systemctl enable apache2
Synchronizing state of apache2.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable apache2
ramansah@dev02:~$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since Thu 2024-09-12 10:00:22 WIB; 6min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 4592 (apache2)
Tasks: 55 (limit: 4558)
Memory: 6.1M (peak: 6.3M)
CPU: 130ms
CGroup: /system.slice/apache2.service
├─4592 /usr/sbin/apache2 -k start
├─4598 /usr/sbin/apache2 -k start
└─4599 /usr/sbin/apache2 -k start

Sep 12 10:00:22 dev02.bckinfo systemd[1]: Starting apache2.service - The Apache HTTP Server...
Sep 12 10:00:22 dev02.bckinfo apachectl[4585]: AH00558: apache2: Could not reliably determine the server's

Step 4: Verify Apache2 Installation

After installation, you can verify that Apache2 is correctly installed by opening a web browser and navigating to your server’s IP address or localhost if you are running it locally. On this case we will navigate our URL to http://dev02.bckinfo as shown below :

If Apache2 is working, you should see the default Ubuntu Apache2 welcome page, which confirms that the web server is active.

Step 5: Configure Apache2 Virtual Hosts

Apache2 uses a concept called Virtual Hosts to allow you to host multiple websites on a single server. Each site will have its own configuration file, typically stored in /etc/apache2/sites-available/.

Create a Virtual Host Configuration

To create a new virtual host file for your domain, copy the default configuration file and modify it for your domain: /etc/apache2/sites-available/website.conf. Here is the example on our dev environment.

ramansah@dev02:~$ sudo vi /etc/apache2/sites-available/website.conf

Modify the contents as follows:

<VirtualHost *:80>
   ServerAdmin webmaster@example.com
   ServerName app01.bckinfo.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>
ramansah@dev02:~$ sudo a2dissite 000-default
Site 000-default disabled.
To activate the new configuration, you need to run:
systemctl reload apache2
ramansah@dev02:~$ sudo a2ensite website
Enabling site website.
To activate the new configuration, you need to run:
systemctl reload apache2
ramansah@dev02:~$ sudo apachectl configtest
AH00112: Warning: DocumentRoot [/var/www/html/website] does not exist
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using dev02.bckinfo. Set the 'ServerName' directive globally to suppress this message
Syntax OK

Create a new sample HTML application index.html file in the web root directory.

ramansah@dev02:~$ sudo mkdir -p /var/www/html/website
ramansah@dev02:~$ sudo vi /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>BCKINFO - Apache Web Server</title>
</head>
<body>
<h1>This is from bckinfo.com</h1>
</body>
</html>
~ 
~ 
~ 
~ 
:wq!
ramansah@dev02:~$ sudo chown -R www-data:www-data /var/www/html/website
ramansah@dev02:~$ sudo systemctl restart apache2

Step 6: Adjust Firewall Settings

If you are using UFW (Uncomplicated Firewall) to manage firewall settings, you’ll need to allow Apache2 traffic through the firewall.

To allow web traffic, run the following command:

ramansah@dev02:~$ sudo ufw allow 'Apache Full'
Skipping adding existing rule
Skipping adding existing rule (v6)


ramansah@dev02:~$ sudo ufw reload
Firewall reloaded

ramansah@dev02:~$ sudo ufw status
Status: active

Access your virtual host domain dev02.bcinfo in a new web browser window and verify that Apache serves your HTML application with a This is from bckfinfo.com message. 

Conclusion

Apache2 is a powerful and versatile web server that is well-suited for hosting websites, applications, and more. By following the steps in this guide, you should have a fully functional Apache2 web server running on your Ubuntu system, ready to host your website.

Whether you’re running a small blog or a large-scale web application, Apache2’s flexibility and reliability make it an excellent choice for your web hosting needs.

(Visited 206 times, 1 visits today)

You may also like