How to install Apache Web Server on Centos 8/Alma Linux 8/Rocky Linux 8

In this guide, we will explore installing Apache Web server on a Centos 8 server.

The Apache HTTP Server(Apache), is one of the most popular free and open-source cross-platform web server software, released under the terms of Apache License 2.0.

The Apache is popular as part of the LAMP setup, being the A in the Acronym. The apache server functionality can be extended with the many available modules.

Prerequisites

  • Up to date Centos 8 Server
  • Root access to the server (Or user with sudo access)
  • Firewall set up to allow traffic to port 80 and 443

Installing Apache

Ensure packages are up to date:

1
sudo dnf -y update

Apache is available in the default CentOS 8 software repositories as httpd. You can check info about it using this command:

1
sudo dnf info httpd

You should see something similar to this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ sudo dnf info httpd
CentOS Linux 8 - AppStream                                          20 MB/s | 8.8 MB     00:00
CentOS Linux 8 - BaseOS                                               24 MB/s | 5.6 MB     00:00
Last metadata expiration check: 0:00:01 ago on Sat 28 Aug 2021 01:06:59 PM UTC.
Available Packages
Name         : httpd
Version      : 2.4.37
Release      : 39.module_el8.4.0+778+c970deab
Architecture : x86_64
Size         : 1.4 M
Source       : httpd-2.4.37-39.module_el8.4.0+778+c970deab.src.rpm
Repository   : appstream
Summary      : Apache HTTP Server
URL          : https://httpd.apache.org/
License      : ASL 2.0
Description  : The Apache HTTP Server is a powerful, efficient, and extensible
              : web server.

Install it with this command:

1
sudo dnf install -y httpd

Enabling http and https in firewalld

If you have firewalld up and running, you need to enable the http and https ports using these commands:

1
2
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

Now reload the firewall to put these new rules into effect:

1
sudo firewall-cmd --reload

Starting and enabling httpd service

By default, the service is not started. Confirm with the following command:

1
sudo systemctl status httpd

Output:

1
2
3
4
5
$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    Active: inactive (dead)
      Docs: man:httpd.service(8)

Start the service with this command:

1
sudo systemctl start httpd

You will receive an active status when the service is running Confirm that its now working fine:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
    Active: active (running) since Sat 2021-08-28 13:32:17 UTC; 17s ago
      Docs: man:httpd.service(8)
  Main PID: 65827 (httpd)
    Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 23800)
    Memory: 28.9M
    CGroup: /system.slice/httpd.service
            ├─65827 /usr/sbin/httpd -DFOREGROUND
            ├─65828 /usr/sbin/httpd -DFOREGROUND
            ├─65829 /usr/sbin/httpd -DFOREGROUND
            ├─65830 /usr/sbin/httpd -DFOREGROUND
            └─65831 /usr/sbin/httpd -DFOREGROUND

Aug 28 13:32:17 test-app-server systemd[1]: Starting The Apache HTTP Server...
Aug 28 13:32:17 test-app-server httpd[65827]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::8fc:23ff:fe51:cc>
Aug 28 13:32:17 test-app-server systemd[1]: Started The Apache HTTP Server.
Aug 28 13:32:17 test-app-server httpd[65827]: Server configured, listening on: port 80

The above confirms that apache is working fine now.

To confirm that the service is accessible, please head to the url http://server_ip to see if you can get the httpd page:

If you don’t know the server up, use the following command in your terminal:

1
curl -4 icanhazip.com

Once you visit that page,if everything is working fine you should see apache default page

To ensure that the service is always started on boot, use this systemctl command:

1
sudo systemctl enable httpd

Setting Up Virtual Hosts

With Apache, it is possible to host multiple sites on the same server where the Apache is running. This can always be achieved using multiple domains each representing a site that the server is hosting.

Apache virtual hosts are similar to Nginx server blocks . Each virtual host will have configuration details for a single host. You can have as many virtual hosts as you want.

In this example, we will use the domain site1.citizix.com for the virtual host but be free to use the domain of your choice. Please note that the DNS for the domainto be used should already be configured to point to the IP address of the server. Confirm that with:

1
dig -t A site1.citizix.com

Creating directory and sample content

The default page of apache is configured as a default virtual host serving content from /var/www/html. It’s a best practice to create the directory of your site in the /var/www directory since its a best practice to serve from there.

Lets create a directory for our site with the following command:

1
sudo mkdir /var/www/site1.citizix.com

Lets create a simple index.html page to serve from our site. You will require vim installed for this to work, if not use this command:

1
sudo dnf install -y vim

Edit the file:

1
sudo vim /var/www/site1.citizix.com/index.html

Add content to the file:

1
2
3
4
5
6
7
8
9
<html>
  <head>
    <title>Hello from Citizix Site1!</title>
  </head>
  <body>
    <h1>Hey!</h1>
    <p>This is citizix site1. Welcome here.</p>
  </body>
</html>

Finally, lets make sure that the current user owns the site directory:

1
sudo chown -R $USER:$USER /var/www/site1.citizix.com

Creating virtual host

Apache Virtual host files specifies the configuration of each sites and tell the Apache web server how to respond to various domain requests.

The Apache configurations will be stored in the directory /etc/httpd/conf.d/. Lets create a config for our site:

1
sudo vim /etc/httpd/conf.d/site1.citizix.com.conf

Add the following content:

1
2
3
4
5
6
7
<VirtualHost *:80>
    ServerName site1.citizix.com
    ServerAlias site1.citizix.com
    DocumentRoot /var/www/site1.citizix.com
    ErrorLog /var/log/httpd/site1.citizix.com.error.log
    CustomLog /var/log/httpd/site1.citizix.com.requests.log combined
</VirtualHost>

This will tell Apache the following:

  • ServerName and ServerAlias - name of the host to serve
  • DocumentRoot - where to find the root directly that holds the publicly accessible web documents
  • ErrorLog - where to store error logs for this site
  • CustomLog - where to store request logs for this particular site

Save and close the file when you are finished

Testing the Virtual Host

Now that the configuration is in place, we need to test that everything is working as expected.

First restart apache to reload config:

1
sudo systemctl restart httpd

List the contents of the /var/log/httpd/ directory to see if Apache created the log files: If all is well, you should see output similar to this:

1
2
3
$ ls /var/log/httpd/

access_log  error_log  site1.citizix.com.error.log  site1.citizix.com.requests.log

If all is well, Apache will now serve your domain name. You can test this by navigating to http://site1.citizix.com, where you should see the information for the site we set up.

The above screenshot confirms that the virtual host is successfully configured and serving content.

If you need to host multiple other sites, repeat the steps above for the sites you want.

Conclusion

We managed to install and set up Apache web server in the above instructions in Centos 8 Server.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy