Efficient Steps To Install Apache Web Server on AlmaLinux 9

This guide from the Orcacore team tries to teach you How To Install Apache web server on AlmaLinux 9. Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web. 

Steps To Install Apache Web Server on AlmaLinux 9

To Install Apache Web Server on AlmaLinux 9, 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 the Initial Server Setup with AlmaLinux 9.

Also, you need a Domain Name that points to your server’s IP address.

Now follow the steps below to Install Apache Web Server on AlmaLinux 9.

Install Apache HTTP Server on AlmaLinux 9

First, you must update your DNF package manager with the command below:

 sudo dnf update -y

Then, use the command below to Install Apache Web Server on AlmaLinux 9:

sudo dnf install httpd -y
Output
Installed:
  almalinux-logos-httpd-90.5.1-1.1.el9.noarch apr-1.7.0-11.el9.x86_64
  apr-util-1.6.1-20.el9.x86_64                apr-util-bdb-1.6.1-20.el9.x86_64
  apr-util-openssl-1.6.1-20.el9.x86_64        httpd-2.4.51-7.el9_0.x86_64
  httpd-filesystem-2.4.51-7.el9_0.noarch      httpd-tools-2.4.51-7.el9_0.x86_64
  mod_http2-1.15.19-2.el9.x86_64              mod_lua-2.4.51-7.el9_0.x86_64

Complete!

Once you have learned to Install Apache Web Server on AlmaLinux 9, you need to configure Apache to serve content over HTTPS and HTTP.

Configure Firewall For Apache Web Server

Here we assumed that you installed FirewallD from the requirements. To allow the HTTP and HTTPs through the AlmaLinux 9 firewall. To do this, 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 new rules:

sudo firewall-cmd --reload

At this point, you can start your service and check the webserver.

Check Apache Web Server Status on AlmaLinux 9

You need to start Apache on AlmaLinux 9 manually by running the following command:

sudo systemctl start httpd

Also, you can enable your Apache service to start on boot:

sudo systemctl enable httpd

You can check that your Apache is active and running on your AlmaLinux 9 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; enabled; vendor prese>
     Active: active (running) since Mon 2022-09-19 03:14:50 EDT; 1min 9s ago
       Docs: man:httpd.service(8)
   Main PID: 5818 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes s>
      Tasks: 213 (limit: 23609)
     Memory: 29.1M
        CPU: 176ms
...

Now you can access to default Apache landing page.

Access Default Apache Landing Page on AlmaLinux 9

To access your default Apache landing page, you need your server’s IP address.

If you don’t have it, you can get your server’s IP address with the command below:

hostname -I

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:

http://your_server_ip

You should see a page similar to this:

default Apache landing page - Install Apache Web Server on AlmaLinux 9

If you see this page, it shows that Apache is working correctly.

At this point, you are finished with how to Install Apache Web Server on AlmaLinux 9. Let’s see some basic management processes.

Manage the Apache process on AlmaLinux 9

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.

At this point, you have learned to Install Apache Web Server on AlmaLinux 9. Let’s see how to set up Apache virtual hosts on ALmaLinux 9.

Set up Apache Virtual Hosts on AlmaLinux 9

A Virtual Host is an Apache configuration directive that allows you to run more than one website on a single server. With Virtual Hosts, you can specify the site document root (the directory containing the website files), create a separate security policy for each site, use different SSL certificates, and much more.

To set up Apache virtual hosts on AlmaLinux 9 follow these steps:

  1. Create an HTML directory for apache.orcacore.net with the following command:
sudo mkdir -p /var/www/your-domain/html

2. Now you should create a directory to store log files for the site:

sudo mkdir -p /var/www/your-domain/log

3. Set ownership of the HTML directory with the $USER environmental variable by the following command:

sudo chown -R $USER:$USER /var/www/your-domain/html

4. Here you need to set default permissions for your webroot. To do this, use the command below:

sudo chmod -R 755 /var/www

5. Create a sample index.html page with your favorite editor. here we use the vi editor:

sudo vi /var/www/your-domain/html/index.html

Then, add the following HTML sample to your file:

<html>
<head>
<title>Welcome to your-domain!</title>
</head>
<body>
<h1>Success! The your-domain virtual host is working!</h1>
</body>
</html>

Save and close the file, when you are done.

To set up an Apache virtual host on AlmaLinux 9, 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:

IncludeOptional sites-enabled/*.conf

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/your-domain.conf

Now, add the configuration block to your file:

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

When you are done, save and close the file.

9. At this point, Create a symbolic link for each virtual host in the sites-enabled directory:

sudo ln -s /etc/httpd/sites-available/your-domain.conf /etc/httpd/sites-enabled/your-domain.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/your-domain/log directory:

sudo ls -dZ /var/www/your-domain/log/

12. Generate and append to web application log files:

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

13. use the restorecon command to apply these changes:

sudo restorecon -R -v /var/www/your-domain/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 AlmaLinux 9.

Test Apache Virtual Host Configuration

At this point that you have learned to Install Apache Web Server on AlmaLinux 9, its time to test your configuration. Type this in your web browser with your domain name:

http://your-domain

You will see a page similar to this:

apache virtual host

That’s it. You are done.

Conclusion

At this point, you learn to Install Apache Web Server on AlmaLinux 9 and Set up Apache Virtual hosts. As one of the most widely used web servers, Apache provides robust performance, extensive configuration options, and flexibility for various web development environments. By following the installation and configuration steps outlined in this guide, you can set up a fully functional Apache server to meet your needs.

Hope you enjoy it. Also, you may like to read the following articles:

AlmaLinux 9 vs Ubuntu 24 Server

Run Nextcloud on Ubuntu 24.04

Set up Docker CE For Ubuntu 24.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!