In this guide, we intend to teach you How To Install Apache Web Server on Rocky Linux 9 (Blue Onyx). Also, you will learn to Set up Apache Virtual Host on Rocky Linux 9.
Apache Web Server is free and open-source software that allows users to deploy their websites on the internet. It is one of the oldest and most reliable web server software maintained by the Apache Software Foundation, with the first version released in 1995.
Steps To Install Apache Web Server on Rocky Linux 9 Blue Onyx
To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 9.
Also, you need a domain name that is pointed to your server’s IP address.
Now follow the steps below, to complete this guide.
Install Apache on Rocky Linux 9
First, you need to update your local package index with the command below:
sudo dnf update
Then, use the following command to install Apache on your Blue Onyx server:
sudo dnf install httpd
When your installation is completed, you need to configure Apache to serve content over HTTPS and HTTP.
Configure Firewall For Apache
We assumed that you installed firewalld from the requirements. Now run the commands below 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
At this point, you can start your service and check the web server.
Start and Enable Apache on Rocky Linux 9
By default, Apache isn’t enabled. You need to start and enable Apache on Rocky Linux 9 manually by running the following command:
sudo systemctl enable httpd --now
You can check that your Apache web server is active and running with the following command:
sudo systemctl status httpd
In your output you should see:
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 Default Apache Landing Page
At this point, you can check your Apache web server by seeing the Apache landing page. To do this, you need your server’s IP address.
To get your IP address you can use the following command:
Or you can use the curl command to request your IP from icanhazip.com. To do this, run the following command:
curl -4 icanhazip.com
When you have your IP address, type it in your web browser to access the default Apache landing page:
You should see a page similar to this:
If you see this page, it shows that Apache is working correctly.
At this point, you are finished with installing Apache on Rocky Linux 9. let’s see some basic management processes of the Apache web server.
Manage Apache process on Rocky Linux
Here your service is active and running on your server. You can use the systemctl command to manage the Apache service on Rocky Linux 9.
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 stop and start the service again with the following command:
sudo systemctl restart httpd
If you have made configuration changes, you need to apply the changes with the following command:
sudo systemctl reload httpd
Apache is configured to start automatically when the server boots. If you don’t want to happen this, run the following command:
sudo systemctl disable httpd
To re-enable the service starts at boot, use the command below:
sudo systemctl enable httpd
The default configuration for Apache will allow your server to host a single website. If you plan on hosting multiple domains on your server, you will need to configure virtual hosts on your Apache webserver.
Let’s see how to set up Apache virtual hosts on Rocky Linux 9.
Set up Apache virtual hosts on Rocky Linux 9
The term Virtual Host refers to the practice of running more than one website (such as company1.example.com and company2.example.com) on a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every website, or “name-based”, meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
To set up Apache virtual hosts on Rocky Linux 9 follow these steps and replace the domain name with yours.
First, you need to create an HTML directory for your domain by running the command below:
sudo mkdir -p /var/www/example.com/html
Then, you must 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 with the $USER environmental variable by running the following command:
sudo chown -R $USER:$USER /var/www/example.com/html
Here you need to set default permissions for your webroot. To do this, run the command below:
sudo chmod -R 755 /var/www
Create a sample index.html page on Rocky Linux 9
At this point, you need to create a sample index.html page with your favorite text editor. Here we use vi editor:
sudo vi /var/www/example.com/html/index.html
Then, 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>
When you are done, save and close the file.
To set up Apache virtual host on Rocky Linux 9, you need to create sites-available and sites-enabled directories.
To create the directories, run the command below:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
Edit Apache Configuration File on Rocky Linux 9
Here you need to edit Apache’s main configuration file and add a line declaring an optional directory for additional configuration files. Open the file with your favorite text editor, here we use vi:
sudo vi /etc/httpd/conf/httpd.conf
Then, add the below line at the end of the file:
When you are finished, save and close your file.
Create Apache Virtual Host on Rocky Linux 9
In this step, you need to 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
Now, 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>
When you are done, save and close the file.
At this point, 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 disable SELinux before, you don’t need to run this command.
sudo setsebool -P httpd_unified 1
You can check the context type that SELinux gave the /var/www/example.com/log directory with the command below:
sudo ls -dZ /var/www/example.com/log/
Now generate and append to web application log files:
sudo semanage fcontext -a -t httpd_log_t "/var/www/example.com/log(/.*)?"
To apply the changes, run the following command:
sudo restorecon -R -v /var/www/example.com/log
And, 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:
You will see a page similar to this:
That’s it, you are done.
At this point, you have learned to Install Apache and set up Apache Virtual Host on Rocky Linux 9.
Hope you enjoy it.
You may be like these articles:
How To Install an Apache web server on AlmaLinux 9