Support Online
Skip to main content

Installing Apache on CentOS 8

In this guide, you will learn step by step how to install Apache HTTP Server (httpd) on CentOS 8, firewalld configuration, Virtual Host settings and SELinux permissions.

Thanks to this configuration, you can securely host multiple websites on a single server.

Prerequisites

  • CentOS 8 server
  • User with authority sudo
  • firewalld service is active
  • The domain name is directed to the server IP address

Installing Apache Web Server

On CentOS 8, the Apache package is distributed under the name httpd.

sudo dnf install httpd -y

After the installation is complete, start the Apache service:

sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd

The service status should look like this:

Active: active (running)

Test from browser:

http://SUNUCU-IP

If the Apache default test page is displayed, the installation is successful.

Opening Firewall Settings (HTTP and HTTPS)

You need to turn on firewall rules so that web traffic can reach the server.

Turn on HTTP access:

sudo firewall-cmd --permanent --add-service=http

Turn on HTTPS access:

sudo firewall-cmd --permanent --add-service=https

Reload rules:

sudo firewall-cmd --reload

Virtual Host Configuration

Thanks to Virtual Host, you can host more than one domain name on the same server.

Example domain name:

ornek.com

3.1 Creating Site Indexes

sudo mkdir -p /var/www/ornek.com/html
sudo mkdir -p /var/www/ornek.com/log

Edit directory ownership and permissions:

sudo chown -R $USER:$USER /var/www/ornek.com/html
sudo chmod -R 755 /var/www

3.2 Creating a Test Page

sudo vi /var/www/ornek.com/html/index.html
<html>
<head>
<title>GenixNode Test Sayfası</title>
</head>
<body>
<h1>Virtual Host başarıyla çalışıyor</h1>
</body>
</html>

Apache Virtual Host Configuration

Virtual Host folders are not available by default on CentOS systems. Therefore, we will create a Debian/Ubuntu-like structure.

Creating configuration directories:

sudo mkdir /etc/httpd/sites-available
sudo mkdir /etc/httpd/sites-enabled

Editing the Apache main configuration file:

sudo vi /etc/httpd/conf/httpd.conf

Add the following line to the end of the file:

IncludeOptional sites-enabled/*.conf

4.1 Creating the Virtual Host File

sudo vi /etc/httpd/sites-available/ornek.com.conf
<VirtualHost *:80>
ServerName ornek.com
ServerAlias www.ornek.com

DocumentRoot /var/www/ornek.com/html

ErrorLog /var/www/ornek.com/log/error.log
CustomLog /var/www/ornek.com/log/requests.log combined
</VirtualHost>

4.2 Enabling Virtual Host

sudo ln -s /etc/httpd/sites-available/ornek.com.conf /etc/httpd/sites-enabled/ornek.com.conf

Editing SELinux Log Permissions

When SELinux is active, Apache cannot write to some directories. Therefore it is necessary to define the correct contexts.

SELinux context for log directory:

sudo semanage fcontext -a -t httpd_log_t "/var/www/ornek.com/log(/.*)?"
sudo restorecon -R -v /var/www/ornek.com/log

Alternative (Less Secure)

sudo setsebool -P httpd_unified 1

This method makes all httpd writes permissible.

Restarting Apache

After the configuration is completed, restart the Apache service.

sudo systemctl restart httpd

Check that logs are created:

ls -lZ /var/www/ornek.com/log

Then test from the browser:

http://ornek.com

Frequently Asked Questions

I installed Apache, but the default page comes up.

Virtual Host may not be active Apache may not have been restarted DNS redirection may be incorrect

I'm getting SELinux permission error.

httpd_log_t context may not have been applied Run the restorecon command again

Why use dnf instead of yum?

With CentOS 8, dnf is used as an enhanced version of the yum package manager.

Result

In this guide:

  • Installed Apache on CentOS 8
  • Firewall is configured
  • Virtual Host created
  • SELinux log permissions adjusted

Now your server is configured to host multiple websites.

You can use the GenixNode platform to quickly test such configurations on cloud servers.