How to install Apache Web Server on CentOS 7

In this article, we will install the Apache HTTP Server on CentOS 7. Apache HTTP Server is a free and open-source cross-platform web server, that we can install on different Operating Systems.

install-apache-web-server-on-centos

To install Apache Web Server we just required a few packages and the user with root privileges. Let’s start installing the Apache Web Server.

Step 1: Installing Apache HTTP Server Packages

In this step, we will update the software packages to the latest version, and install the required packages for the Apache web server.

[root@IPTrainer ~]# yum -y update

Wait for a couple of minutes till the package updates are completed.

Now, execute the below command to install the Apache Web Server packages:

[root@IPTrainer ~]# yum install httpd

It will initiate the installation of the Apache packages from the default software repositories of CentOS using the YUM package manager.

Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: repo.extreme-ix.org
 * extras: repo.extreme-ix.org
 * updates: repo.extreme-ix.org
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.el7.centos.5 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos.5 for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.5.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos.5 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                      Arch                                    Version                                                 Repository                                Size
=============================================================================================================================================================================================
Installing:
 httpd                                        x86_64                                  2.4.6-97.el7.centos.5                                   updates                                  2.7 M
Installing for dependencies:
 apr                                          x86_64                                  1.4.8-7.el7                                             base                                     104 k
 apr-util                                     x86_64                                  1.5.2-6.el7                                             base                                      92 k
 httpd-tools                                  x86_64                                  2.4.6-97.el7.centos.5                                   updates                                   94 k
 mailcap                                      noarch                                  2.1.41-2.el7                                            base                                      31 k

Transaction Summary
=============================================================================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Is this ok [y/d/N]:

Please Yes to continue the installation process

Downloading packages:
(1/5): apr-1.4.8-7.el7.x86_64.rpm                                                                                                                                     | 104 kB  00:00:00
(2/5): mailcap-2.1.41-2.el7.noarch.rpm                                                                                                                                |  31 kB  00:00:00
(3/5): httpd-tools-2.4.6-97.el7.centos.5.x86_64.rpm                                                                                                                   |  94 kB  00:00:01
(4/5): httpd-2.4.6-97.el7.centos.5.x86_64.rpm                                                                                                                         | 2.7 MB  00:00:03
(5/5): apr-util-1.5.2-6.el7.x86_64.rpm                                                                                                                                |  92 kB  00:00:04
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                        651 kB/s | 3.0 MB  00:00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                                                                                                                                    1/5
  Installing : apr-util-1.5.2-6.el7.x86_64                                                                                                                                               2/5
  Installing : httpd-tools-2.4.6-97.el7.centos.5.x86_64                                                                                                                                  3/5
  Installing : mailcap-2.1.41-2.el7.noarch                                                                                                                                               4/5
  Installing : httpd-2.4.6-97.el7.centos.5.x86_64                                                                                                                                        5/5
  Verifying  : apr-1.4.8-7.el7.x86_64                                                                                                                                                    1/5
  Verifying  : mailcap-2.1.41-2.el7.noarch                                                                                                                                               2/5
  Verifying  : httpd-tools-2.4.6-97.el7.centos.5.x86_64                                                                                                                                  3/5
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                                                                                                               4/5
  Verifying  : httpd-2.4.6-97.el7.centos.5.x86_64                                                                                                                                        5/5

Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos.5

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7                apr-util.x86_64 0:1.5.2-6.el7                httpd-tools.x86_64 0:2.4.6-97.el7.centos.5                mailcap.noarch 0:2.1.41-2.el7

Complete!
[root@IPTrainer ~]#

Step 2: Allowing the HTTP/HTTPD traffic

By default, the web traffic, i.e., HTTP/HTTPD is blocked. We have to manually allow the traffic to use the Web Server from external network hosts.

You can use the below command to configure CentOS firewall rules to allow the traffic.

[root@IPTrainer ~]# firewall-cmd --permanent --add-service=http
[root@IPTrainer ~]# firewall-cmd --permanent --add-service=https
[root@IPTrainer ~]# firewall-cmd --reload

Step 3: Managing the Apache Web Server

By default, after installing the Apache server, it will be disabled. You have to start it manually to start working with Apache Web Server.

To check the current state of the HTTPD server, you can use the below command:

[root@IPTrainer ~]# systemctl status httpd.service

You will get the below results

● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)

To start the Apache web server, you can use the below command

[root@IPTrainer ~]# systemctl start httpd.service

The above command will start the HTTPD service. Now, if you again check the HTTPD status with the status command, you will find that the Apache server is now up and running:

[root@IPTrainer ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-10-29 21:22:31 PDT; 1min 1s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 117288 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─117288 /usr/sbin/httpd -DFOREGROUND
           ├─117289 /usr/sbin/httpd -DFOREGROUND
           ├─117290 /usr/sbin/httpd -DFOREGROUND
           ├─117291 /usr/sbin/httpd -DFOREGROUND
           ├─117292 /usr/sbin/httpd -DFOREGROUND
           └─117293 /usr/sbin/httpd -DFOREGROUND

Oct 29 21:22:31 IPTrainer systemd[1]: Starting The Apache HTTP Server...
Oct 29 21:22:31 IPTrainer httpd[117288]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::bab7:3b8d:146d:f854. Set the 'Serv...this message
Oct 29 21:22:31 IPTrainer systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

Step 4: Initial testing of Apache Web Server

We have done the required configuration for the Apache web server. Now, you need to simply browse the web server either via IP address or with configured FQDN. You should get a page similar to the below screenshot.

how-to-install-apache-web-server-on-centos-7

That’s it! We have successfully configured the Apache Web Server on CentOS 7. You may further edit /var/www/html/ directory as suggested on the Welcome Page, to install your content.

Step 5: Configuring HTTPS access to the web server

You might notice, that even after configuring all of the configurations, you can only able to access the HTTP mode of the web server. If you try to open the default web page using https://xx.xx.xx.xx/, you will get an error.

This is because the required SSL certificate and ssl.conf file is missing. You have to install the ssl_mod package in the newly installed CentOS machine.

You can simply run the below command to install the ssl_mod package in CentOS:

[root@MiWiFi-R4CM-srv ~]# yum install mod_ssl

Once, the ssl_mod package is installed, you need to reload the HTTPD service to browse the web page using HTTPS, i.e., secure HTTP. Hit the below command to reload the httpd service:

[root@IPTrainer ~]# systemctl restart httpd.service

Since it is a default certificate, you will still get a Certificate Warning page while accessing the web page. To make it more secure, you can either create a SAN certificate CSR or you can simply generate and install a self-signed certificate.

References

Related Articles

Summary

Apache is an open-source cross-platform popular web server. We have installed the web server in CentOS 7. We have started the httpd service and we allowed HTTP traffic on the CentOS firewall.

Hope you like this article. In case you like this article, request you to please share it on social media platforms! In case you get into error, just comment in the comment box!

Leave a Reply

Your email address will not be published. Required fields are marked *