Install Apache2 Web Server on Ubuntu and CentOS – Step-by-Step Guide

In this chapter, you will learn

  1. How to install Apache2 on Ubuntu and CentOS?
  2. How to Start/Stop/Restart/Reload/Status Apache services.

Apache2 is a widely used, open-source, and reliable web server for Linux, which is free of charge. You will find a large community for Apache2 support, making it easier to configure.

If you are planning to set up an HTTP web server on your Ubuntu or CentOS Linux VPS server, this tutorial will guide you through the installation and configuration of Apache2 step by step.

Installing Apache2

Ubuntu:

Login to your Ubuntu VPS server and execute the following command to install it.

Step 1: Update Ubuntu
sudo apt update
Step 2: Install Apache2:
sudo apt install apache2
Step 3: Check Installation:
apache2 --version
[Sat Apr 27 16:10:59.172485 2024] [core:warn] [pid 1925] AH00111: Config variable ${APACHE_RUN_DIR} is not defined

CentOS:

Step 1: Update httpd:
sudo yum update httpd
Step 2: Install Apache2:
sudo yum install httpd
Step 3: Check Installation:
hostname -I
122.27.142.103

Add Apache to Firewall

However, in most cases, Apache is enabled by the firewall. Still, it is necessary to check its existence in the firewall. If you find that Apache is not listed in the firewall, add it as follows:

Step 1: Check ufw app list.
sudo ufw app list
Available applications: Apache Apache Full Apache Secure OpenSSH
Step 2: If you don't find Apache in the list, add them as follows:
sudo ufw allow 'Apache'
Rule added Rule added (v6)

Check localhost

Upon completing the installation of Apache2, it's time to start Apache services and check whether localhost is working or not.

Step 1: Find the ip address of your localhost using the following commands.
hostname -I
172.17.235.107
Step 2: Open web browser and open the following link:
http:// 172.17.235.107/
Apache2 Default Page

Commands to Start/Stop/Restart Apache2 on Ubuntu

After successful installation of Apache2, it is necessary to learn some important commands to manage Apache2 process such as starting the Apache service, stopping the Apache service, Restarting the Apache service, etc.

Using systemctl commands.

Start Apache service
sudo systemctl start apache2
Stop Apache service
sudo systemctl stop apache2
Restart Apache service
sudo systemctl restart apache2
Reload Apache service
sudo systemctl reload apache2
know status
sudo systemctl status apache2

Using Service commands

Start Apache service
sudo service apache2 start
Stop Apache service
sudo service apache2 stop
Restart Apache service
sudo service apache2 restart
Reload Apache service
sudo service apache2 reload
know status
sudo service apache2 status

Summary

In this tutorial, you learned how to install Apache2 on Ubuntu and CentOS VPS servers. In the next article, you will learn how to create a sample web page and load it in a web browser. You will also discover where the web application is stored on your Ubuntu server.