Introduction
Apache is a popular Linux-based web server application. It is part of the LAMP stack (Linux, Apache, MySQL, PHP) that powers much of the internet.
This guide will show you how to install Apache on CentOS 8.
Prerequisites
- A system running CentOS 8 Linux
- Access to a terminal window / command line (Ctrl–Alt–F2)
- A user account with sudo or root privileges
Installing Apache Web Server on CentOS 8
Step 1: Update Software Repository
Open a terminal window, and update the repository package lists by entering the following:
sudo yum update
Step 2: Install Apache
Now you can install Apache with the command:
sudo yum –y install httpd
Note: “httpd” is the name for the Apache service in CentOS. The –y
option automatically answers yes to the confirmation prompt.
Step 3: Start and Manage Apache Web Server
Apache is a service that runs in the background.
Start the Apache service by entering the following:
sudo systemctl start httpd
The system doesn’t return an output if the command is run properly.
To configure Apache to run on startup:
sudo systemctl enable httpd
To check the status of the Apache service:
sudo systemctl status httpd
To reload Apache (reloads configuration files to apply changes):
sudo systemctl reload httpd
To restart the entire Apache service:
sudo systemctl restart httpd
To stop Apache:
sudo systemctl stop httpd
To disable Apache at system startup:
sudo systemctl disable httpd
Note: If you had set up Apache on a Debian-based distro (e.g., Ubuntu) as well, you may want to check out how to start, stop, and restart Apache on Ubuntu Linux.
Step 4: Test Apache Web Server
Your Apache software’s job is to serve web pages over a network. Your new Apache installation has a default test page, but you can also create a custom test page.
Check the Default Apache Test Page
In a terminal window, find your system’s IP address with the following:
hostname -I | awk '{print $1}'
If you’re familiar with the ip addr show
or ifconfig commands, you can use those instead.
Open a web browser and type in the IP address displayed in the output. The system should show the Apache HTTP Server Test Page, as seen in the image below:
If your system doesn’t have a graphical interface, use the curl
command:
curl [your_system's_IP_address]:80
Note: The :80 at the end stands for port 80, the standard port for internet traffic. Be sure to write the appropriate IP address instead of [your_system’s_IP_address].
Optional: Create an HTML File to Test
If, for some reason, you need or have a custom HTML page you want to use as a test page, do the following:
In a terminal window, create a new HTML index file:
echo My Apache Web Server on CentOS 8 > /var/www/html/index.html
Edit the file to your liking and save it.
Now you can follow the steps in the previous section (check your IP address and browse to it in a web browser or use the curl
command).
Your Apache server is working correctly if it displays the specified custom page.
Step 5: Adjust Firewall for Apache
The firewall on your system blocks traffic on different ports. Each port has a number, and different kinds of traffic use different ports. For your web server, you’ll need to allow HTTP and HTTPS traffic on ports 80 and 443 (respectively).
In a terminal, enter the following:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
Restart firewalld:
sudo firewall-cmd --reload
Double-check to make sure your firewall is correctly configured:
sudo firewall-cmd --list-all | grep services
You should see http and https in the list of allowed services.
Apache also offers ModSecurity, a plug-in module that works as a firewall. You can install and configure ModSecurity as an additional safety layer, which helps you monitor traffic and respond to any irregularities.
Apache Files and Directories
Apache is controlled by applying directives in configuration files:
- /etc/httpd/conf/httpd.conf – Main Apache config file
- /etc/httpd/ – Location for all config files
- /etc/httpd/conf.d/ – All config files in this directory are included in the main confog file
- /etc/httpd/conf.modules.d/ – Location for Apache module config files
Note: When making changes to configuration files, remember to always restart the Apache service to apply the new configuration.
Diligently check Apache log files to monitor your web server:
- /var/log/httpd/ – Location of Apache log files
- /var/log/httpd/access_log – Shows a log of systems that accessed the server
- /var/log/httpd/error_log – Shows a list of any errors Apache encounters
Designate a directory to store the files for your website. Use the configuration files to point to the directory you choose. Some typical locations include:
- /home/username/my_website
- /var/www/my_website
- /var/www/html/my_website
- /opt/my_website
Conclusion
You should now have a working Apache web server on your CentOS system. Next, you may be interested in installing a full LAMP stack.