How To Install Apache with Let’s Encrypt on RHEL 8

In terms of popularity and usage, the Apache webserver engine tops all other web server software systems, and for good reasons. The Apache Software Foundation ensured that this cross-platform web server software is attributed as free, open-source, and easy to configure.

Its user-friendly footprints make it an ideal web server software alternative even for beginners that want to experience how their websites/applications will perform under HTTP and HTTPS protocols.

[ You might also like: How to Sync Two Web Servers in Linux Automatically ]

On the other hand, RHEL (Red Hat Enterprise Linux) is a Linux operating system distribution attributed as an easy-to-configure, secure and platform-ready container for development tools.

With such attributes, RHEL 8 needs the popularity of Apache to define it as a complete web app hosting system.

Install Apache in RHEL 8

RHEL 8 software installation system identifies the Apache webserver software as an httpd package. Before we install the httpd daemon, you need to make sure that your RHEL system is up-to-date.

$ sudo dnf update

We can now proceed with the installation of the httpd daemon.

$ sudo dnf install httpd

In order for the Apache webserver software to continuously execute even after consecutive system reboots, we need to enable and start it.

$ sudo systemctl enable httpd
$ sudo systemctl start httpd
$ sudo systemctl status httpd
Check Apache Status
Check Apache Status

Now you need to open the Apache ports on your firewall.

$ sudo firewall-cmd --add-service={http,https} --permanent
$ sudo firewall-cmd --reload

The next step is to test Apache’s activeness through your web browser. On your local machine, visit.

http://localhost/ 

If you are running Apache on a server environment, determine the server’s IP address by running the following command.

$ curl ifconfig.me

Once you obtain the IP address, access it through your web browser in the following manner:

http://YOUR-IP-ADDRESS

You should get a response page that resembles the following screen capture.

Check Apache Webpage
Check Apache Webpage

Whenever you make critical changes that require restarting the Apache web server, you can achieve this objective by executing the following commands.

$ sudo systemctl restart httpd
or 
$ sudo systemctl reload httpd 

Hosting a Website with Apache in RHEL

The default Apache configuration is sufficient for hosting a single web application. However, if you wish to transform your RHEL 8 Linux distribution to a multi-domain web server, you should be able to configure and implement Apache Virtual Hosts.

The directory path /var/www/html holds the default, virtual host. If we wish to host other sites on this RHEL 8 system, we will have to create separate virtual hosts for each site/domain.

For instance, let us assume you have registered the domain random-tutorials.xyz and wish to use it to host a sample site under Apache in this RHEL 8 system.

The first step is to create its separate virtual host within the directory structure /var/www.

$ sudo mkdir -p /var/www/random-tutorials.xyz/html

You also need a directory to handle the site’s log files.

$ sudo mkdir -p /var/www/random-tutorials.xyz/log

Set file permissions and adjust webroot directory permissions.

$ sudo chown -R $USER:$USER /var/www/random-tutorials.xyz/html
$ sudo chmod -R 755 /var/www

We need a sample index.html file (landing page) to test this site.

$ sudo nano /var/www/random-tutorials.xyz/html/index.html

Populate it with some data.

<!DOCTYPE html>
<html>

<head>
  <title>Welcome to random-tutorials.xyz</title>
</head>

<body>

  <h1>LinuxShellTips Introduces random-tutorials.xyz</h1>
   <p>You have successfully accessed random-tutorials.xyz home page!</p>

</body>
</html>

Make sure you save the file before exiting the terminal editor.

Creating a Virtual Host File in RHEL

Create the sites-available and sites-enabled directories for storing the virtual host file and the virtual host’s symbolic link respectively.

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

Apache’s main configuration file needs access to the virtual host defined under the created sites-enabled directory.

$ sudo nano /etc/httpd/conf/httpd.conf

At the bottom of this file, add the following line before saving and closing it.

IncludeOptional sites-enabled/*.conf
Configure Virtual Host in Apache
Configure Virtual Host in Apache

We can now create our virtual host file:

$ sudo nano /etc/httpd/sites-available/random-tutorials.xyz

The content added to this file should be associated with the domain name you are using.

<VirtualHost *:80>
    ServerAdmin www.random-tutorials.xyz
    ServerAlias random-tutorials.xyz
    DocumentRoot /var/www/random-tutorials.xyz/html
    ErrorLog /var/www/random-tutorials.xyz/log/error.log
    CustomLog /var/www/random-tutorials.xyz/log/access.log combined
</VirtualHost>

Save the file and close the terminal editor.

Creating a symbolic link between the sites-available and the sites-enabled directories activates this virtual host file.

$ sudo ln -s /etc/httpd/sites-available/random-tutorials.xyz /etc/httpd/sites-enabled/random-tutorials.xyz.conf  

Restart or Reload Apache and make sure it is running.

$ sudo systemctl restart httpd 
$ sudo systemctl status httpd

You should be able to visit the hosted index.html page through your domain name.

http://domain-name
Check Apache Domain Website
Check Apache Domain Website

Secure Apache with Let’s Encrypt on RHEL 8

To secure your website hosted on Apache, you need to use Let’s Encrypt to acquire an SSL certificate by installing Certbot and mod_ssl, which provides support for SSLv3 encryption.

The certbot package is not available from the default repositories, you need to enable the EPEL repository to install it.

$ sudo dnf install epel-release 
$ sudo dnf install certbot python3-certbot-apache mod_ssl

Once Certbot is installed, you can run the following command to obtain an SSL certificate for your domain.

$ sudo certbot --apache -d random-tutorials.xyz

The obtained certificates will be accessible within a subdirectory named after your domain in the /etc/letsencrypt/live directory.

Now that your certificates are installed, you can verify your domain SSL certificate status at the following URL.

https://www.ssllabs.com/ssltest/analyze.html?d=random-tutorials.xyz

With this article, you should be able to comfortably host a number of websites/apps on your RHEL 8 server-configured Linux system by creating various Apache virtual host files.

Got something to say? Join the discussion.