Installing Apache on CentOS 8

CentOS 8 — currently, the latest version of CentOS Linux-based source code Red Hat Enterprise Linux 8. Apache HTTP server is a free cross-platform web server that supports operating systems such as Linux, BSD, Mac OS, Microsoft Windows, Novell NetWare, BeOS.

The main advantages of the Apache web server is considered its reliability and configuration flexibility, allowing you to connect external modules to provide the data to a DBMS, generating user authentication, analyze the error messages and more. In Apache HTTP server supports IPv6. In our article we will tell you how to run Apache on CentOS or RHEL 8 8 with additional configuration and setup SSL.

Installing Apache on CentOS 8

To run the commands in the instructions you’ll need sudo privileges are required to install the required packages. You can get them, changing the user to user sudo command:

sudo su

Step 1. Installing packages

Install Apache CentOS 8 is performed using the commands below. It will allow you to install not only the server itself, but additional packages that are required for it to work in your system:

dnf install httpd

Step 2. Configuring Apache service

 Managing the Apache service runs with the command systemctl. After installation, use the command systemctl to add Apache service to autostart and run:

sudo systemctl enable httpd.service

sudo systemctl start httpd.service

If you want to restart apache CentOS 8 you can use the command:

systemctl restart httpd.service

Restarting Apache without terminating active connections you can run the reload command:

systemctl reload httpd.service

Step 3. Configure firewall

For correct operation of an installed web server, you must open the ports used by default in a firewall, the following commands:

firewall-cmd --zone=public --permanent --add-service=http

firewall-cmd --zone=public --permanent --add-service=https

firewall-cmd --reload

Step 4. Check the operation of the Apache

Now you can get information about the installed version of Apache on your server using the following command:

httpd -v

The output of the command is shown below:

Create a test html page in the root directory web server default (/var/www/html).

echo "Hello" > /var/www/html/index.html

Now open your favorite browser and using the IP address of the server or domain pointing to IP address of your server, navigate to the newly created test page:

If you see text that was previously recorded in a test file, then install apache centos is successful.

Step 5. Creating virtual hosts

A virtual host is a special partition used in the configuration of the web server that maps available on the server IP addresses, domains and server directories, as well as managing the sites created on the web server. Let’s see how to create virtual host Apache Apache CentOS site Create a sample index file in /var/www/

mkdir -p /var/www/

echo "hello" | sudo tee /var/www/

Next, create a virtual host configuration file and edit it adding the following content: you can change the domain name to match your domain:

sudo nano /etc/httpd/conf.d/

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/
<Directory /var/www/>
AllowOverride all
ErrorLog logs/example.com_error.log
CustomLog logs/example.com_access.log combined

Consider a file structure Here are the main directives used:

  • FollowSymLinks –when using this Directive the server will follow symbolic links in the specified directory. Configuration directives always remains the default. The FollowSymLinks option only works in <Directory> sections or files .htaccess.
  • AllowOverride – finding the file .htaccess, the server needs to know which directives declared in that file can override earlier configuration directives. The AllowOverride Directive are allowed to be used only in <Directory> sections specified without regular expressions, but not in the section <Location>, <DirectoryMatch> or <Files>. If the AllowOverride Directive accepts the value None, then the files .htaccess is completely ignored. In this case, the server will not even try to read the file .htaccess on the file system. If this Directive takes a value of Allow, any Directive having your htaccess permissions can override earlier configuration directives.
  • Order – along with the directives Allow (allow) and Deny (forbidden), controls a three pass access control system. The first pass processes either all allow or all deny directives, as specified in the Directive Order. The second pass parses the rest of the directives (deny or allow). The third pass applies to all requests that do not correspond to any of the first two. Please note that all Allow and Deny directives are handled differently than a regular firewall that uses only the first value rule. In addition, the order in which rows appear in the configuration files, it doesn’t matter — all allow lines are processed as one group, and all deny lines are considered as another group, and the default state is considered by itself. Directive Order always takes one of two values: Allow or Deny.
  • The directives Allow and Deny. Option Allow, Deny means that the first evaluate all the Allow directives. At least one of them must match, otherwise the request is rejected. Then evaluated all the Deny directives. In case of a match the request is rejected. Finally, any requests that do not match the Allow or Deny Directive, are rejected by default. In the case of using the Deny, Allow directives are evaluated all Deny. If they match, the request is rejected. Any requests that do not match the Allow or Deny directives are permitted.

So the changes came into effect we need to restart the service. A restart of Apache is done with the command:

systemctl reload httpd.service

Configuring virtual hosts Apache CentOS completed.

Step 4. Configuring SSL

If you are going to use SSL in your site, then you can skip this step. However, safety is always a major challenge for any web site. In order to use SSL in conjunction with Apache, you need to install mod_ssl on your system. To do this, run the command:

dnf install mod_ssl

At the end of the package installation, create the directory which will store our
SSL certificate:

mkdir -p /etc/pki/tls/certs

Navigate to it using the command:

cd /etc/pki/tls/certs

Now create the SSL certificate that will be used when setting up virtual host. To do this, run the command:

openssl req-x509 -nodes -newkey rsa:2048 -keyout

This command will create the private key and certificate in the required directory.

You can use the configuration file /etc/httpd/conf/ssl.conf to configure SSL in your virtual host or you can create a separate virtual host configuration file for your domain, e.g. /etc/httpd/conf.d/ Add the following content:

nano /etc/httpd/conf.d/

<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /var/www/
<Directory /var/www/>
#AllowOverride all
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/
SSLCertificateKeyFile /etc/pki/tls/certs/
ErrorLog logs/example.com_ssl-error.log
CustomLog logs/example.com_ssl-access.log combined

In the file uses three directives to enable ssl apache centos virtual host. Let’s take a closer look.

  • SSLEngine – this Directive toggles the use of the mechanism of SSL/TLS. It is usually used inside a <VirtualHost> to enable support for SSL /TLS specific virtual host. The default mechanism of SSL/TLS is disabled for both the main server and all configured virtual hosts. In Apache 2.1 and later, SSLEngine set to optional. This Directive can take two values: on or off, which means enabled or disabled.
  • SSLCertifacateFile – this Directive creates a file of type “all in one” where you can assemble the certificates of certification authorities (CAS), with customers which you are dealing. They are used for client authentication. This file is simply the Union of the various certificate files, in PEM encoded, in order of preference. This can be used alternatively or additionally to SSLCACertificatePath.
  • SSLCertificateKeyFile – indicates the PEM-encoded private key file for the server. If the contained private key is encrypted, the passphrase dialog force started during the run. This Directive can be used multiple times (referring to different filenames) to support multiple algorithms for server authentication. For each Directive SSLCertificateKeyFile should be the relevant Directive SSLCertificateFile.

The private key can also be combined with the certificate in the file provided by SSLCertificateFile, but this practice is extremely undesirable. If used, the certificates that use this built-in key must be set after the certificate using the private key file.

Now, creating and configuring the configuration file, save the changes to the virtual host file and restart the Apache service using the following command:

systemctl reload httpd.service


In this article we discussed how to install Apache in CentOS 8. Follow the steps in this article, you will be able to install and configure the Apache server on your CentOS server 8.


(Visited 8 times, 1 visits today)