The Apache HTTP server is one of the most popular Web servers in the world. It is an open-source application that runs on any Linux server and is completely free to install. It is, used to handle requests and deliver static/dynamic content. Due to its reliability, flexibility, and security, millions of Website Owners, Developers, and Web Hosting providers trust it. Let me walk you through the steps involved in installing the Apache (httpd) server on Rocky Linux 9.
Here are some of the main features of Apache HTTP Server:
* Loadable Dynamic Modules.
* Static File Handling.
* Auto-Indexing and Content Negotiation.
* .htaccess Configuration Support.
* IPv6 and HTTP/2 Compatibility.
* Compression, Bandwidth Throttling, and FTP Support.
* Built-in Scripting Support, Load Balancing, and Session Tracking.
* URL Rewriting and Geolocation Based on IP Address.
Prerequisites :
Operating System : Rocky Linux / RHEL /CentOS /Fedora Packages & Dependencies: httpd User account : root user or user account with sudo privileges Recommended to run all the administrative commands as with sudo privilege instead of root.
Check out our Rocky Linux Installation Guide here
Difficulties in setting up sudo users? Click here to find the steps.
My Lab Setup :
Apache Server: Operating System : Rocky Linux release 9.1 (Blue Onyx) Hostname : apache.linuxteck IP Address : 192.168.1.100
Step 1: Install Apache HTTPd package
Note:
You should consistently update your operating system prior to installing the Apache server package. With the "DNF Package Manager", you can fetch the Rocky Linux updates from AppStream & BaseOS.
$ sudo dnf update
The Apache httpd server package can now be installed directly from the AppStream repository of Rocky Linux by executing the following command:
$ sudo dnf install httpd
Note:
The output above indicates that the version of Apache HTTP Server is 2.4.53, and it is the best version of Apache HTTP Server ("httpd").
You can also check the status and version of the Apache package in the Terminal with the following command.
$ sudo httpd -v
Output :
[linuxteck@apache ~]$ httpd -v Server version: Apache/2.4.53 (Rocky Linux) Server built: Jan 31 2023 00:00:00
Step 2: HTTPd Service: Start and Enable
By using the following command, we can start, enable and check the status of the HTTPD service:
$ sudo systemctl start httpd
$ sudo systemctl enable httpd
$ sudo systemctl status httpd
Note:
The screenshot above can confirm that our newly installed Apache webserver service is active and running.
Step 3: Enable firewallD Service
Note:
Firewalls are always recommended to protect our system. By default, most Linux distros include a firewall package with the basic installation. However, it won't be enabled by default. Firewalls are essential to prevent unauthorized access to network security systems. After activating our firewall, we should permit ports for each type of service to access it publicly. In our demo, we will open HTTP (80) and HTTPS (443) to access our Website for everyone.
To open the ports, run the following commands:
$ sudo firewall-cmd --permanent --add-port=80/tcp
$ sudo firewall-cmd --permanent --add-port=443/tcp
$ sudo firewall-cmd --reload
Note:
The following command will allow you to verify that all of the above-executed commands have been added to the zone file.
For further information on firewall-cmd commands for Linux operating systems, check out this firewall article.
$ sudo firewall-cmd --list-all
Step 4: Apache HTTP Server Test Page
Note:
That's it, the Apache server is ready for use. Open your browser and enter your Server IP address or Server Hostname, hit enter key, and you will get the "HTTP Server Test Page." In my case, http://192.168.1.100 and hit the enter key, the Apache Test Page will appear as shown below. This means that the server is up and running.
The above output confirms that Apache Web Server has been successfully installed and is working fine with Rocky Linux 9. Basic Apache installations usually only cover a single-page website. You can learn more about how to set up and deploy a Single Page Website in production by reading our LAMP setup guide. In the following section, let's look into some advanced setups in the Apache configuration.
Step 5: Setting Up Apache Virtual Host
Note:
The above session taught us how to build a single-page website and the valid access methods (IP address and hostname). Our next session will focus on setting up a virtual host for Multi-Website Pages. All configurations for multi-site setup will be stored in virtual host files.
TIPS to manage Apache:
* Configuration files for Apache are located in /etc/httpd directory.
* The main configuration file of Apache is /etc/httpd/conf/httpd.conf.
* There is also a .conf file extension in the /etc/httpd/conf.d/ directory that is included in the apache main configuration file.
* Loading modules are controlled by the /etc/httpd/conf.modules.d configuration file.
* The log file of apache "error_log and access_log files" are located in the /var/log/httpd/ directory.
Before making any changes to any configuration file, you need to follow some important rules:
1. We recommend keeping the latest copy of your original config files as a backup. If you do this, you will be able to restore /compare/troubleshoot much more easily.
2. Make sure you edit only a small portion of the block at a time. By doing this, you can easily check if they are working and, if not, you can easily revert back.
3. You need to understand syntax before you begin editing it, which means you need to know the structure and format.
4. Ensure that new changes are recorded including the date and description.
5. The last step is to restart the apache server after making any changes to the config files.
We will start by looking at our hostname (which is our system name) through the following command:
$ hostname
Output :
linuxteck.com
The "hostname -I" command will display the system's IP address.
Output :
192.168.1.100
Note:
As you can see, our hostname is linuxteck.com, and the corresponding IP address is "192.168.1.100". For our demo, we will use the same IP address as above for the new domain name "linuxteck-vhost.net"
First, let's create a virtual host file for the new domain. Rocky Linux also stores Apache configuration files in the same directory as CentOS/RHEL, which is "/etc/httpd/conf.d".
$ sudo vi /etc/httpd/conf.d/linuxteck-vhost.net.conf
You will now need to add the following entries to the virtual host file: :
<VirtualHost *:80> ServerAdmin admin@linuxteck-vhost.net ServerName www.linuxteck-vhost.net ServerAlias linuxteck-vhost.net DocumentRoot /var/www/linuxteck-vhost ErrorLog /var/log/httpd/linuxteck-vhost.net-error.log CustomLog /var/log/httpd/linuxteck-vhost.net-access.log combined </VirtualHost>
Now save and exit the file using shift: wq!
Note:
Next, create a directory to accommodate all the files for the new domain "linuxteck-vhost.net". As soon as the Apache (httpd) package is installed, the default "DocumentRoot" directory "/var/www/" will be created. Apache serves content from this directory, which is known as the root directory.
$ sudo mkdir /var/www/linuxteck-vhost
Create a sample index.html file to test the new domain
$ sudo vi /var/www/linuxteck-vhost/index.html
Here are the HTML entries to add: :
<html> <head> <title>Welcome to Linuxteck-vhost.net!</title> </head> <body> <h1>Great news! The virtual host for <span style="color:red; font-size: 35px;">linuxteck-vhost.net</span> is now up and running!</h1> </body> </html>
Now save and exit the file using shift: wq!
Make sure to apply the correct permissions for the files and folder to access the Website publicly. Usually, Web Servers interact with two types of users: "Authenticated and Anonymous." Apache is typically run by the user "www-data" on many Linux distros. However, we can do this based on our needs. In case you are unsure of the type of ownership or permission you should grant. You can determine which user Apache/httpd is used by running the following command.
$ ps -aux | grep apche OR $ ps -aux | grep httpd
My output:
[linuxteck@linuxteck ~]$ ps -aux | grep apche linuxte+ 2262 0.0 0.0 221664 2344 pts/0 S+ 18:35 0:00 grep --color=auto apche [linuxteck@linuxteck ~]$ ps -aux | grep httpd linuxte+ 2266 0.0 0.0 221664 2264 pts/0 S+ 18:36 0:00 grep --color=auto httpd [linuxteck@linuxteck ~]$
The above output shows that Apache uses the user "linuxteck" on my system. In the same way, you can apply permissions based on the user.
$ sudo chown -R $USER:$USER /var/www/linuxteck-vhost/index.html
Let's verify this now
$ sudo ls -l /var/www/linuxteck-vhost/index.html
Output:
-rw-r--r--. 1 linuxteck linuxteck 176 Mar 11 18:13 /var/www/linuxteck-vhost/index.html [linuxteck@linuxteck ~]$
It is always a good idea to run the config test before restarting the httpd daemon. It will assist you in finding any syntax errors in the configuration files.
$ sudo apachectl configtest
Output:
[linuxteck@linuxteck ~]$ [linuxteck@linuxteck ~]$ sudo apachectl configtest Syntax OK [linuxteck@linuxteck ~]$
As a final step, we need to restart the Apache service.
$ sudo systemctl restart httpd
Once you're done, open a web browser, input your domain name, and hit enter. Below is an example of your newly configured virtual hosting page as shown in the screenshot below
Note:
If you haven't used a real domain to visit your website during the testing process, you'll need to change the host file on the local computer that you're using to access the website. You can use the following command if you are using Linux or a Mac:
$ sudo vi /etc/hosts
Here are my entries. You can add your IP Address and Domain Name, save and exit, then launch your browser and type the domain name you specified.
My demo entries:
192.168.1.100 linuxteck-vhost.net
Note:
To add more domain names, follow the same steps and create separate virtual host files for each.
Step 6: Managing Apache (HTTPD) Process
As we know, systemd is the default system and service manager in most Linux distributions. The "systemctl" command can be used to manage systemd. We'll take a look at some basic management commands for starting, stopping, restarting, disabling, and enabling Apache services.
Apache(HTTPD)Process:
$ sudo systemctl start httpd --> This is used to start the httpd daemon. $ sudo systemctl stop httpd --> This is used to stop the httpd daemon. $ sudo systemctl enable httpd --> Used start the apache service automatically during reboot. $ sudo systemctl disable httpd --> Used disable the automatic start the apache service during reboot. $ sudo systemctl restart httpd --> Used to stop and start the apache service again. $ sudo systemctl reload httpd --> Used to reload the apache service without dropping the connection $ sudo systemctl status httpd --> Used to verify the status of Apache Server (Active or Disabled)
Essential and Advanced Concepts in Apache:
Our configuration of Apache is limited to a small portion. This section describes some of the essential and advanced things you can do with Apache. You can view the title and description in the excel format below. This will help you understand how Apache can be implemented in Linux using various concepts.
Conclusion:
We hope this article has helped you understand how to install Apache Server in Rocky Linux 9.1 step by step . Drop me your feedback/comments. Feel free to share this article with others if you like it.
2 replies on “How to install Apache on Rocky Linux 9”
I’m setting up a rocky linux box from scratch and just noticed that the www part of the /var/www directory is created when you install apache.
Yes