How To Install Apache on Rocky Linux 9 | Full Setup

Posted on

How To Install Apache on Rocky Linux 9 | Full Setup

How To Install Apache on Rocky Linux 9 | Full Setup

This guide, brought to you by Orcacore, will walk you through the process of installing the Apache web server on Rocky Linux 9 (Blue Onyx). You’ll also learn how to set up Apache Virtual Hosts on Rocky Linux 9. This comprehensive guide provides everything you need to get your web server up and running.

The Apache Web Server is a powerful, free, and open-source software solution that enables users to deploy and host websites on the internet. Maintained by the Apache Software Foundation, it’s one of the oldest and most dependable web server applications, with its initial release dating back to 1995. Its robust features and wide community support make it a popular choice for web hosting.

Steps To Install Apache Web Server on Rocky Linux 9 Blue Onyx

Before starting, ensure you are logged into your server as a non-root user with sudo privileges and that a basic firewall is configured. Refer to our guide on Initial Server Setup with Rocky Linux 9 for assistance with this prerequisite.

Furthermore, you will need a domain name that points to your server’s IP address.

Follow these steps to complete the How To Install Apache on Rocky Linux 9 guide:

Install Apache on Rocky Linux 9

Begin by updating your local package index using the following command:

 sudo dnf update -y

Next, use the following command to install Apache on your Blue Onyx server:

sudo dnf install httpd -y

After the installation is complete, you must configure Apache to serve content over both HTTPS and HTTP protocols.

Configure Firewall For Apache

Assuming you’ve installed firewalld as per the requirements, execute the following commands to configure Apache to serve content over HTTPS and HTTP on Rocky Linux 9:

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

Then, reload the firewall to apply the changes:

sudo firewall-cmd --reload

Now, you can start the service and verify the web server.

Start and Enable Apache on Rocky Linux 9

By default, Apache isn’t enabled to start automatically. You need to manually start and enable Apache on Rocky Linux 9 using the following command:

sudo systemctl enable httpd --now

Verify that your Apache web server is active and running with the following command:

sudo systemctl status httpd

The output should resemble this:

**Output**
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor pres>
Active: **active** (**running**)
Docs: man:httpd.service(8)
Main PID: 52316 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 11409)
Memory: 30.9M

Access the Default Apache Landing Page

At this stage, you can test your Apache web server by accessing the default Apache landing page. You’ll need your server’s IP address for this.

To obtain your IP address, use the following command:

hostname -I

Alternatively, you can use the curl command to request your IP from icanhazip.com. Execute the following command:

curl -4 icanhazip.com

Once you have your IP address, enter it into your web browser to access the default Apache landing page:

http://your_server_ip

You should see a page similar to this:

[Image of Apache Test Page]

If you see this page, it confirms that Apache is functioning correctly.

You have now successfully installed Apache on Rocky Linux 9. Let’s explore some basic management operations for the Apache web server.

Manage Apache Service

Your service is now active and running on your server. You can manage the Apache service on Rocky Linux 9 using the systemctl command.

To stop the service, use the command below:

sudo systemctl stop httpd

Start the service by running the command below:

sudo systemctl start httpd

You can restart the service with the following command:

sudo systemctl restart httpd

If you’ve made configuration changes, apply them using the following command:

sudo systemctl reload httpd

Apache is configured to start automatically when the server boots. To disable this, run the following command:

sudo systemctl disable httpd

To re-enable the service to start at boot, use the command below:

sudo systemctl enable httpd

The default Apache configuration allows your server to host a single website. If you intend to host multiple domains on your server, you need to configure virtual hosts.

Let’s explore how to set up Apache virtual hosts on Rocky Linux 9.

Set up Apache virtual host on Rocky Linux 9

A Virtual Host is the practice of hosting more than one website (e.g., company1.example.com and company2.example.com) on a single server. Virtual hosts can be "IP-based," meaning each website has a different IP address, or "name-based," meaning multiple names run on a single IP address. The fact that they share the same physical server is transparent to the end-user.

To set up Apache virtual hosts on Rocky Linux 9, follow these steps, replacing the domain name with your own.

First, create an HTML directory for your domain using the command below:

sudo mkdir -p /var/www/example.com/html

Then, create a directory to store log files for the site on Rocky Linux 9:

sudo mkdir -p /var/www/example.com/log

Now, set the correct ownership of the HTML directory using the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/example.com/html

Set default permissions for your webroot using the command below:

sudo chmod -R 755 /var/www

Create a sample index.html page for Apache

Now, create a sample index.html page with your preferred text editor. Here, we use the vi editor:

sudo vi /var/www/example.com/html/index.html

Add the following HTML sample to your file:

<html>
<head>
<title>Welcome to example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>

Save and close the file.

To set up an Apache virtual host on Rocky Linux 9, you need to create sites-available and sites-enabled directories.

Create these directories using the command below:

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

Edit Apache Configuration File on Rocky Linux 9

Edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files. Open the file with your preferred text editor, here we use vi:

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

Add the line below at the end of the file:

IncludeOptional sites-enabled/*.conf

Save and close the file.

Create Apache Virtual Host on Rocky Linux 9

Create your Apache virtual host file by creating a new file in the sites-available directory:

sudo vi /etc/httpd/sites-available/example.com.conf

Add the configuration block to your file:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    DocumentRoot /var/www/example.com/html
    ErrorLog /var/www/example.com/log/error.log
    CustomLog /var/www/example.com/log/requests.log combined
</VirtualHost>

Save and close the file.

Create a symbolic link for each virtual host in the sites-enabled directory on Rocky Linux 9:

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

To set a universal Apache policy, run the command below:

Note: If you disabled SELinux before, you don’t need to run this command.

sudo setsebool -P httpd_unified 1

You can check the context type that SELinux assigned to the /var/www/example.com/log directory with the command below:

sudo ls -dZ /var/www/example.com/log/

Generate and append to web application log files:

sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"

Apply the changes by running the following command:

sudo restorecon -R -v /var/www/example.com/log

Restart your Apache web server with the following command:

sudo systemctl restart httpd

Test Apache Virtual Host on Rocky Linux

Now, you are ready to test your Apache virtual host configuration on Rocky Linux 9.

Type your domain name in your web browser:

http://your-domain

You will see a page similar to this:

[Image of Virtual Host Test Page]

That’s it, you are done.

Conclusion

You have now learned to Install Apache and set up Apache Virtual Host on Rocky Linux 9. Installing Apache and configuring Virtual Hosts on Rocky Linux 9 allows you to host multiple websites on a single server. This enhances organization, security, and website management. This guide demonstrated How To Install Apache on Rocky Linux 9.

We hope you found this guide helpful. Please subscribe to us on Facebook, Instagram, and YouTube.

You may also like these articles:

How to Install an Apache Web Server on AlmaLinux 9

How to Install Apache Web Server on Ubuntu 22.04

Installing Apache Guacamole on Ubuntu 24.04

Install Apache Spark on Debian 12

Alternative Solutions for Hosting Websites on Rocky Linux 9

While the above method details a classic approach to setting up Apache with virtual hosts, other viable options exist, offering different advantages. Here are two alternative approaches:

1. Using Docker Containers:

Docker provides a way to containerize web applications, including Apache. Each website can run in its own container, isolating it from other websites and the host system. This enhances security and simplifies deployment. Docker also simplifies dependency management, ensuring that each application has the correct environment without conflicts.

Explanation:

Docker creates lightweight, isolated environments called containers. Each container includes everything needed to run an application: code, runtime, system tools, system libraries, and settings. By containerizing Apache, you can easily manage different versions and configurations for each website.

Steps:

  • Install Docker:

    sudo dnf install docker-ce --nobest -y
    sudo systemctl start docker
    sudo systemctl enable docker
  • Create a Dockerfile for each website:

    Create a directory for each website, e.g., website1, website2. Inside each directory, create a Dockerfile with instructions to set up Apache and copy the website files.

    Example Dockerfile (for website1):

    FROM httpd:latest
    
    COPY ./html/ /usr/local/apache2/htdocs/
    
    EXPOSE 80

    Create an html directory within the website directory and place your index.html file there.

  • Build and Run the Docker Containers:

    Navigate to each website directory and build the Docker image:

    cd website1
    sudo docker build -t website1 .

    Run the Docker container, mapping port 80 on the host to port 80 on the container:

    sudo docker run -d -p 8080:80 website1

    Repeat the process for each website, mapping different host ports (e.g., 8081, 8082) to port 80 on each container.

  • Configure Reverse Proxy (Optional):

    For a cleaner setup, you can use a reverse proxy like Nginx to route traffic to the correct Docker container based on the domain name. This allows you to use standard ports (80 and 443) for all websites.

2. Using Cockpit Web Interface:

Cockpit is a web-based interface for managing servers. While it doesn’t directly manage Apache virtual hosts, it simplifies server administration tasks like installing software, managing services, and configuring networking. You can install and manage Apache through Cockpit’s user-friendly interface, making it easier for those less comfortable with the command line.

Explanation:

Cockpit provides a graphical interface to manage Rocky Linux 9 servers. It simplifies tasks that are typically performed via the command line.

Steps:

  • Install Cockpit:

    sudo dnf install cockpit -y
    sudo systemctl start cockpit.socket
    sudo systemctl enable cockpit.socket
    sudo firewall-cmd --permanent --add-service=cockpit
    sudo firewall-cmd --reload
  • Access Cockpit:

    Open a web browser and navigate to https://your_server_ip:9090. Log in with your server credentials.

  • Install and Manage Apache:

    Within Cockpit, navigate to the "Terminal" section and run the commands to install Apache:

    sudo dnf install httpd -y
    sudo systemctl enable httpd --now
  • Configure Apache:

    While Cockpit doesn’t have a dedicated Apache configuration interface, you can still use the terminal to edit the Apache configuration files and create virtual hosts as described in the original guide. Cockpit streamlines server management, offering a simplified approach to managing Apache, particularly for those less familiar with command-line operations.