This guide provides an easy method to install the Apache HTTP Server on various Linux distributions. Apache is a widely-used open-source web server that is reliable, secure, and customizable.
Installing Apache on Ubuntu/Debian Systems
Follow these simple steps to install Apache on Ubuntu or Debian-based systems using the apt
package manager.
Step 1: Update Your System
It's a good practice to update your package list before installing new software.
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache
Install Apache using the apt
command.
sudo apt install apache2 -y
Step 3: Adjust the Firewall (If Applicable)
If you have a firewall enabled (like UFW), allow traffic on port 80 (HTTP) and port 443 (HTTPS).
sudo ufw allow 'Apache'
Check the firewall status:
sudo ufw status
Step 4: Verify Apache Installation
Ensure that Apache is running:
sudo systemctl status apache2
You should see an output indicating that Apache is active (running).
Step 5: Test Apache
Open a web browser and navigate to your server's IP address:
http://your_server_ip/
You should see the default Apache welcome page, confirming that the installation was successful.
Installing Apache on CentOS/RHEL/AlmaLinux Systems
For Red Hat-based systems, you can install Apache using the yum
or dnf
package manager.
Step 1: Update Your System
Update your package list before proceeding.
# For CentOS 7
sudo yum update -y
# For CentOS 8 and newer (using dnf)
sudo dnf update -y
Step 2: Install Apache
Install Apache using the package manager.
# For CentOS 7
sudo yum install httpd -y
# For CentOS 8 and newer
sudo dnf install httpd -y
Step 3: Start and Enable Apache
Start the Apache service and enable it to start on boot.
sudo systemctl start httpd
sudo systemctl enable httpd
Step 4: Adjust the Firewall (If Applicable)
If you have firewalld
enabled, allow traffic on port 80 (HTTP) and port 443 (HTTPS).
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Step 5: Verify Apache Installation
Check the status of Apache:
sudo systemctl status httpd
You should see that Apache is active (running).
Step 6: Test Apache
Open a web browser and navigate to your server's IP address:
http://your_server_ip/
The default Apache test page should appear, indicating a successful installation.
Common Commands for Managing Apache
Here are some useful commands to manage the Apache service.
Ubuntu/Debian Systems
- Start Apache:
sudo systemctl start apache2
- Stop Apache:
sudo systemctl stop apache2
- Restart Apache:
sudo systemctl restart apache2
- Reload Apache Configuration:
sudo systemctl reload apache2
- Enable Apache to Start on Boot:
sudo systemctl enable apache2
- Disable Apache from Starting on Boot:
sudo systemctl disable apache2
CentOS/RHEL/AlmaLinux Systems
- Start Apache:
sudo systemctl start httpd
- Stop Apache:
sudo systemctl stop httpd
- Restart Apache:
sudo systemctl restart httpd
- Reload Apache Configuration:
sudo systemctl reload httpd
- Enable Apache to Start on Boot:
sudo systemctl enable httpd
- Disable Apache from Starting on Boot:
sudo systemctl disable httpd
Configuration Files and Document Root
By default, Apache serves files from a specific directory known as the "Document Root."
Ubuntu/Debian Systems
- Document Root:
/var/www/html
- Main Configuration File:
/etc/apache2/apache2.conf
- Virtual Hosts Configuration:
/etc/apache2/sites-available/
CentOS/RHEL/AlmaLinux Systems
- Document Root:
/var/www/html
- Main Configuration File:
/etc/httpd/conf/httpd.conf
- Virtual Hosts Configuration:
/etc/httpd/conf.d/
You can place your website files in the Document Root directory to serve them over the web. For more complex configurations, you can set up virtual hosts.
Uninstalling Apache
If you need to remove Apache from your system, you can do so using the package manager.
Ubuntu/Debian Systems
sudo apt remove apache2 -y
sudo apt autoremove -y
CentOS/RHEL/AlmaLinux Systems
# For CentOS 7
sudo yum remove httpd -y
# For CentOS 8 and newer
sudo dnf remove httpd -y
Conclusion
You have successfully installed Apache on your Linux server using an easy and straightforward method. Apache is now ready to serve your web content. For more advanced configurations, you can explore setting up virtual hosts, enabling modules, and securing your server with SSL certificates.