How to Install Apache on Linux?
Installing the Apache HTTP Server on a Linux system is a
straightforward process that enables you to host websites and web
applications. This guide provides a quick overview of the installation
steps for the most popular Linux distributions, specifically focusing on
Ubuntu/Debian-based systems using the apt package manager
and CentOS/RHEL-based systems using yum or
dnf. By following these platform-specific commands,
configuring your firewall, and managing the system services, you can
have a secure, fully functional web server running in just a few
minutes.
Installing Apache on Ubuntu and Debian
Ubuntu and Debian systems use the Advanced Package Tool
(apt) to manage software. Before installing any new
packages, it is best practice to update your local package index to
ensure you download the latest version available.
- Update the package index: Run
sudo apt updateto refresh your local repository listings. - Install the Apache package: Execute
sudo apt install apache2to download and install the server. - Verify the installation: Once the process
completes, the Apache service typically starts automatically. You can
check its status by running
sudo systemctl status apache2.
Installing Apache on CentOS, RHEL, and Fedora
Red Hat-based distributions use yum or dnf
as their package managers. On these systems, the Apache package is known
as httpd rather than apache2.
- Update the package repository: Run
sudo dnf update(oryum updateon older versions) to ensure everything is current. - Install the HTTPD package: Execute
sudo dnf install httpdto install the Apache web server. - Start and enable the service: Unlike Debian-based
systems, you must manually start the service and configure it to launch
at boot. Run
sudo systemctl start httpdto start it, andsudo systemctl enable httpdto ensure it runs automatically after a system restart.
Configuring the Firewall
To allow external traffic to reach your new web server, you must open the appropriate web ports in your system’s firewall. Standard web traffic uses port 80 (HTTP), while secure traffic uses port 443 (HTTPS).
- For Ubuntu (UFW): You can allow traffic by enabling
the pre-configured Apache profile. Run
sudo ufw allow 'Apache Full'to open both ports 80 and 443, then verify the changes withsudo ufw status. - For CentOS/RHEL (Firewalld): Use the firewall-cmd
utility to permanently open the services. Run
sudo firewall-cmd --permanent --add-service=httpandsudo firewall-cmd --permanent --add-service=https, then apply the changes by runningsudo firewall-cmd --reload.
Testing Your Web Server
With the installation complete and the firewall configured, you can
test if Apache is running correctly. Open a web browser on a device
connected to the same network and type your Linux server’s IP address
into the address bar. If you are testing directly on the server, you can
use http://localhost. If successful, you will see the
default Apache welcome page, confirming that the web server is properly
installed and ready to host your content.