Install Apache Web Server on AlmaLinux 8

In this article, we want to teach you to install an Apache web server on AlmaLinux 8 and Set up the Apache Virtual Host file.

The Apache HTTP Server is free and open-source software that allows users to deploy their websites on the internet. You can learn more about AlmaLinux by visiting this article about Introducing AlmaLinux.

Now follow the steps below to complete your Apache installation on AlmaLinux 8.

Steps To Install an Apache Web Server on AlmaLinux 8

To install the Apache web server on AlmaLinux 8 you need to follow these requirements first.

Requirements

You need to log in as a non-root user to your server with Sudo privileges, and a basic setup for the firewall. To do this, you can check out our article about the Initial server setup with AlmaLinux 8.

Also, you need a valid domain name that is pointed to your server’s IP address.

When you are done with these requirements, let’s start installing Apache on AlmaLinux 8.

Step 1 – Install Apache on AlmaLinux 8

To install Apache you need to update the DNF package manager with the following command:

 sudo dnf update -y

Apache in RHEL-based distros is the httpd package. Now you can install the Apache package with the following command:

sudo dnf install httpd -y

When your installation is completed, you need to configure Apache to serve content over HTTPS and HTTP.

Step 2 – Configure Firewall Rules for HTTPD Service

We assumed that you have installed firewalld from the requirements. Now run the commands below to enable the HTTP and HTTPS traffic through the firewall:

# 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.

Step 3 – Check Apache Service Status on AlmaLinux 

You need to start and enable Apache on AlmaLinux 8 manually by using the following commands:

# sudo systemctl start httpd
# sudo systemctl enable httpd

You can check that your Apache web server is active and running on your server 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) since Mon 2021-09-06 08:05:21 EDT; 7s ago
Docs: man:httpd.service(8)
Main PID: 52316 (httpd)
Status: "Started, listening on: port 80"
Tasks: 213 (limit: 11409)
Memory: 30.9M
CGroup: /system.slice/httpd.service
...

Now you can access to default Apache landing page. To do this, you need your server’s IP address.

To get your IP address you can use the following command:

hostname -I

Or you can use the curl command to request your IP from icanhazip.com. run the following command:

curl -4 icanhazip.com

Then, type your server’s IP address 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 on AlmaLinux 8

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

At this point, you are finished with how to install Apache on AlmaLinux 8. Let’s see some basic management processes.

Step 4 – Manage Apache Web Server Process on AlmaLinux 8

Here your service is active and running on your server. You can use the systemctl command to manage the httpd 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 ALmaLinux 8.

Step 5 – Set up Apache virtual hosts file on AlmaLinux 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 AlmaLinux 8 follow these steps and replace our domain name with yours:

First, Create an HTML directory for apache.orcacore.net 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 an Apache virtual host on AlmaLinux 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:

    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/apache.orcacore.net.conf

    Now, add the configuration block to 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 AlmaLinux 8.

    Type this in your web browser with your domain name:

    http://apache.orcacore.net

    You will see a page similar to this:

    Apache virtual host on AlmaLinux 8

    Conclusion

    At this point, you have learned to install an Apache web server on AlmaLinux 8. Also, you have learned to create the Apache virtual host file on your server. Hope you enjoy using it.

    You may like these articles:

    Install an Apache web server on AlmaLinux 9

    Install an Apache web server on Rocky Linux 8

    Newsletter Updates

    Enter your email address below and subscribe to our newsletter

    Stay informed and not overwhelmed, subscribe now!