Linux

How to Install Apache2 on Linux

How to Install Apache2 on Linux

Apache2 is one of the most widely used web servers in the world, known for its flexibility, robustness, and rich feature set. Whether you’re hosting a personal website or a large-scale web application, Apache2 is a reliable choice. This guide will walk you through the process of installing Apache2 on various Linux distributions.

Step 1: Update Your Package Manager
Before installing any software, it’s good practice to update your package manager’s index. Open a terminal and run the following command:

For Debian/Ubuntu:

sudo apt update

For Fedora:

sudo dnf check-update

For Arch Linux:

sudo pacman -Sy

This ensures that you have the latest information about available packages.

Step 2: Install Apache2
Now, you can install Apache2 using your package manager. The command will vary depending on your Linux distribution.

For Debian/Ubuntu:

sudo apt install apache2

For Fedora:

sudo dnf install httpd

For Arch Linux:

sudo pacman -S apache

After running the installation command, the package manager will download and install Apache2 along with any necessary dependencies.

Step 3: Start and Enable Apache2 Service
Once Apache2 is installed, you need to start the service and enable it to run at boot.

For Debian/Ubuntu and Fedora:

sudo systemctl start apache2
sudo systemctl enable apache2

For Arch Linux:

sudo systemctl start httpd
sudo systemctl enable httpd

To verify that Apache2 is running, you can use:

sudo systemctl status apache2

or for Arch:

sudo systemctl status httpd

This command will show you the current status of the Apache service.

Step 4: Adjust the Firewall
If you have a firewall enabled, you’ll need to allow HTTP (port 80) and HTTPS (port 443) traffic. The commands to do this vary depending on your firewall configuration.

For UFW (Ubuntu):

sudo ufw allow ‘Apache Full’

For Firewalld (Fedora):

sudo firewall-cmd –permanent –add-service=http
sudo firewall-cmd –permanent –add-service=https
sudo firewall-cmd –reload

For iptables:

sudo iptables -A INPUT -p tcp –dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp –dport 443 -j ACCEPT

Step 5: Test the Installation
To confirm that Apache2 is working correctly, open a web browser and enter your server’s IP address or localhost if you are testing locally:

http://localhost

You should see the default Apache2 welcome page, indicating that the web server is running successfully.

Step 6: Configure Apache2
Apache2 configuration files are typically located in the /etc/apache2/ directory on Debian-based systems and /etc/httpd/ on Fedora and Arch systems. The main configuration file is usually named apache2.conf or httpd.conf.

To edit the configuration file:

sudo nano /etc/apache2/apache2.conf # Debian/Ubuntu

or

sudo nano /etc/httpd/conf/httpd.conf # Fedora/Arch

Here are some common configurations you might want to modify:

DocumentRoot: This directive defines the directory where your website files are located. The default is usually /var/www/html. You can change it to point to your own directory.

ServerName: You can set your domain name here if you are using a domain.

Directory Permissions: Make sure that the user under which Apache runs (usually www-data or apache) has the appropriate permissions to access your website files.

Step 7: Managing Apache2
Here are some common commands for managing the Apache2 service:

Restart Apache2:

sudo systemctl restart apache2 # Debian/Ubuntu
sudo systemctl restart httpd # Fedora/Arch

Stop Apache2:

sudo systemctl stop apache2 # Debian/Ubuntu
sudo systemctl stop httpd # Fedora/Arch

Reload Configuration:

sudo systemctl reload apache2 # Debian/Ubuntu
sudo systemctl reload httpd # Fedora/Arch

Thank you for visiting our site, you can check out our other related articles from the links below 🙂

How to Install OpenSSL on Ubuntu Linux

How to Install htop on Linux: A Comprehensive Guide

How To Protect SSH with Fail2Ban on Ubuntu 20.04

If you would like to improve yourself in server management, you can purchase a server from our site, experiment and improve yourself in an affordable and reliable environment. I wish you good luck.

Main Page

Conclusion

Installing Apache2 on Linux is a straightforward process that enables you to host your own websites and applications. With its extensive features and active community support, Apache2 remains a top choice for web hosting.

For further reading and advanced configuration options, you can visit the official Apache HTTP Server Documentation.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button