In this guide, we want to show you How To Install an Apache web server on Rocky Linux 8.
Apache is the most widely used web server software and runs on 67% of all websites in the world. Developed and maintained by Apache Software Foundation, Apache is open-source software and is available for free.
It’s fast, reliable, and secure. And Apache can be highly customized to meet the needs of many different environments by using extensions and modules.
How To Install an Apache web server on Rocky Linux 8
Before you start to install Apache, you need to 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 article the Initial Server Setup with Rocky Linux 8.
Also, you need a domain name that pointed to your server’s IP address.
Install Apache on Rocky Linux 8
To install Apache you need to update the DNF package manager with the following command:
sudo dnf update
Now install the Apache package with the following command:
sudo dnf install httpd
When your installation is completed, you need to configure Apache to serve content over HTTPS.
We assumed that you installed firewalld from the requirements. now run the commands below:
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.
How to check your web server
You need to start Apache on Rocky Linux 8 manually by the following command:
sudo systemctl start httpd
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
Now you can access to default Apache landing page. for that, 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. 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 how to install Apache on Rocky Linux 8. let’s see some basic management processes.
How to manage the Apache process on Rocky Linux 8
Here your service is active and running on your server. you can use the systemctl command to manage the service.
To stop the service use:
sudo systemctl stop httpd
Start the service with:
sudo systemctl start httpd
You stop and start the service again with the following command:
sudo systemctl restart httpd
If you 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 8.
How to Set up Apache virtual hosts on Rocky Linux 8
The concept of virtual hosts allows more than one Web site on one system or Web server. The servers are different by their hostname.
To set up Apache virtual hosts on Rocky Linux 8 follow these steps and replace our domain name with yours:
- Create an HTML directory for your domain with the following command:
sudo mkdir -p /var/www/apache.orcacore.net/html
2. Now you should create a directory to store log files for the site:
sudo mkdir -p /var/www/apache.orcacore.net/log
3. Set ownership of the Html directory with the $USER environmental variable by the following command:
sudo chown -R $USER:$USER /var/www/apache.orcacore.net/html
4. Here you need to set default permissions for your webroot. use:
sudo chmod -R 755 /var/www
5. Create a sample index.html page with your favorite editor. here we use vi editor:
sudo vi /var/www/apache.orcacore.net/html/index.html
Then, add the following HTML sample to your file:
<html> <head> <title>Welcome to apache.orcacore.net!</title> </head> <body> <h1>Success! The apache.orcacore.net virtual host is working!</h1> </body> </html>
Save and close the file when you are done.
To set up Apache virtual host on Rocky Linux 8, you need to create sites-available and sites-enabled directories.
6. Create directories with the following command:
sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
7. 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:
sudo vi /etc/httpd/conf/httpd.conf
Then, add the line below to the end of the file:
When you are finished save and close your file.
8. 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/apache.orcacore.net.conf
Now, add the configuration block into your file:
<VirtualHost *:80> ServerName www.apache.orcacore.net ServerAlias apache.orcacore.net DocumentRoot /var/www/apache.orcacore.net/html ErrorLog /var/www/apache.orcacore.net/log/error.log CustomLog /var/www/apache.orcacore.net/log/requests.log combined </VirtualHost>
9. At this point, Create a symbolic link for each virtual host in the sites-enabled directory:
sudo ln -s /etc/httpd/sites-available/apache.orcacore.net.conf /etc/httpd/sites-enabled/apache.orcacore.net.conf
10. 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
11. check the context type that SELinux gave the /var/www/apache.orcacore.net/log directory:
sudo ls -dZ /var/www/apache.orcacore.net/log/
12. Generate and append to web application log files:
sudo semanage fcontext -a -t httpd_log_t "/var/www/apache.orcacore.net/log(/.*)?"
13. use the restorecon command to apply these changes:
sudo restorecon -R -v /var/www/apache.orcacore.net/log
Restart your web server with the following command:
sudo systemctl restart httpd
Now you are ready to test your Apache virtual host configuration on Rocky Linux 8.
Type this in your web browser with your domain name:
You will see a page similar to this:
At this point, you learn How to install an Apache web server on Rocky Linux 8. Also, you learn how to set up Apache virtual hosts on Rocky Linux 8.
Hope you enjoy it.