How to Install Apache on Rocky Linux 8

  • Francis Ndungu

Introduction

Apache is a popular web server that powers many of the world's websites. It is open-source and widely used for its stability and flexibility. Rocky Linux is a community-driven enterprise operating system designed to be compatible with Red Hat Enterprise Linux. Combining Apache with Rocky Linux provides a robust environment for hosting websites.

This guide shows you how to install Apache on Rocky Linux 8.

Prerequisites

Before you get started, ensure you've:

Install Apache

Apache is available in the default Rocky Linux package repository. It's important to update your system to ensure you get the latest version of the software. After updating, you will install the Apache package. Finally, you can verify the installation by checking the package version.

  1. Update your system.

    CONSOLE
    $ sudo dnf update -y
    
  2. Install Apache.

    CONSOLE
    $ sudo dnf install httpd -y
    
  3. Check the Apache version.

    CONSOLE
    $ httpd -v
    

Manage Apache Service

Apache runs as a service managed by systemctl. The service name for Apache is httpd. You can start, enable, stop, restart, and check the status of the service using systemctl.

  1. Start the Apache service.

    CONSOLE
    $ sudo systemctl start httpd
    
  2. Enable Apache to start on boot.

    CONSOLE
    $ sudo systemctl enable httpd
    
  3. Stop the Apache service.

    CONSOLE
    $ sudo systemctl stop httpd
    
  4. Restart the Apache service.

    CONSOLE
    $ sudo systemctl restart httpd
    
  5. Check the status of the Apache service.

    CONSOLE
    $ sudo systemctl status httpd
    

Understand Configuration Files

Apache's configuration files control its behavior and settings. Log files record access and error events, which help in troubleshooting issues.

  • Apache main configuration file.

    /etc/httpd/conf/httpd.conf - This file contains the main settings for Apache.
    
  • Additional Apache configuration directory.

    /etc/httpd/conf.d/ - This directory holds additional configuration files.
    
  • Apache Access log file.

    /var/log/httpd/access_log - This file logs all access requests to your server.
    
  • Apache Error log file.

    /var/log/httpd/error_log - This file logs any errors encountered by Apache.
    

Configure the Firewall

A firewall helps to protect your server by controlling incoming and outgoing network traffic. You need to allow the necessary ports through the firewall for Apache to function.

  1. Allow Apache HTTP service.

    CONSOLE
    $ sudo firewall-cmd --permanent --add-service=http
    
  2. Allow Apache HTTPS service.

    CONSOLE
    $ sudo firewall-cmd --permanent --add-service=https
    
  3. Reload the firewall to apply changes.

    CONSOLE
    $ sudo firewall-cmd --reload
    

Run a Sample Website

Create a test HTML page to ensure Apache is serving web pages correctly. This involves creating a sample HTML file under the web root directory.

  1. Create a sample /var/www/html/index.html HTML file using nano.

    CONSOLE
    $ sudo nano /var/www/html/index.html
    
  2. Paste the following HTML content into the /var/www/html/index.html file.

    HTML
    <html>
    <head>
        <title>Welcome to Apache on Rocky Linux!</title>
    </head>
    <body>
        <h1>It works!</h1>
        <p>This is a test page served by Apache on Rocky Linux 8.</p>
    </body>
    </html>
    
  3. Save and close the file.

  4. Open your web browser and enter your server's IP address in the address bar.

    http://192.168.0.1
    

    You should see the sample HTML page displayed.

Conclusion

This guide showed you how to install and configure Apache on Rocky Linux 8. You learned how to manage the Apache service, understood configuration files, configured the firewall, and ran a sample website. For advanced configurations, you may consider exploring additional Apache modules and configuring virtual hosts to host multiple websites on a single server.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI
  • Node.js