How to Install Apache Web Server on CentOS 8 (Step by Step)

How to Install Apache Web Server on CentOS 8 (Step by Step). In this post, we introduce Apache web server, its advantages then show you how to install Apache web server on CentOS 8.

What is Apache Web server?

First of all, the Apache Software Foundation develops and maintains Apache, an open source web server software that has been in existence for over 20 years. All in all, Apache powers millions of websites and serves billions of users every day as the most widely used web server software in the world. The Apache Software Foundation, a non profit organization, provides support and resources for open source software projects.

Features Of Apache Web Server

1. Modular design

Allows developers to customize the web server to meet their specific needs, and extend it with a variety of modules that provide additional functionality.

2. High configurability

Offers a wide range of options that can be set in its configuration file, allowing administrators to fine tune the web server to optimize its performance and security.

3. Ability to handle a large number of concurrent connections

Makes it well suited for high traffic websites and applications, as it handles a large number of requests without becoming overloaded.

4. Support for server side scripting languages

Provides integration with languages such as PHP, allowing developers to create dynamic websites and applications.

5. Database integration

Moreover, it allows developers to connect to and manage databases, enabling the creation of database driven websites and applications.

6. SSL/TLS encryption

Provides secure communication between the web server and clients, protecting sensitive data from being intercepted during transmission.

7. Wide operating system support

Runs on a variety of operating systems, including Linux, Unix, and Windows, making it a flexible and versatile option for web servers.

Advantages Of Apache Web Server

1. Open source nature

Freely available to anyone who wants to use it, and developers are able to modify and distribute the source code as they see fit.

2. Reliability and stability

With long track record of performance and security and is supported by a large community of users and developers who contribute to its development and support.

3. Easy to set up and manage

Widely supported by hosting providers and is straightforward to set up and manage, making it a convenient option for web servers.

4. Flexibility and versatility

Runs on a variety of operating systems, including Linux, Unix, and Windows, allowing it to be used in a range of environments.

5. Customization

The modular design of Apache allows developers to customize the web server to meet their specific needs, and extend it with a variety of modules that provide additional functionality.

6. Widely adopted

Most widely used web server software in the world, powering millions of websites and serving billions of users every day.

7. Strong community support

Equally, Apache Software Foundation, provides support and resources for open source software projects, ensuring that Apache has a strong community of users and developers who contribute to its development and support.

Up next with How to Install Apache Web Server on CentOS 8 is navigate to installations steps. 

How to Install Apache Web Server on CentOS 8 (Step by Step)

In this section, we show you steps in how to install Apache web server on CentOS 8.

Prerequisites

  • A CentOS 8 server installed on your system.
  • A root user or a user with sudo privileges.

Step 1 - Update the System

Before starting, it is always a good idea to update all system packages to the latest version. Update all the packages by running the following command.

				
					dnf update -y
				
			

Once all the packages are updated, you can proceed to set up a firewall rule for Apache.

Step 2 - Setup Firewall

Firewalld is a default firewall tool that comes preinstalled on CentOS 8 operating system. Moreover, you will need to add a firewall rule for Apache service to access it from the external system.

Please run the following command to add firewall rules for HTTP and HTTPS service.

				
					firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
				
			

Next, reload the firewalld daemon to apply the changes.

				
					firewall-cmd --reload
				
			

Now, Apache port is open for public and any one can access it from the remote machine.

Step 3 - Install Apache on CentOS 8

Evidently, the Apache web server package is included in the CentOS default repo. In order to install it,  run the following command.

				
					dnf install httpd -y
				
			

After installing the Apache web server package, you verify the Apache package information using the following command.

				
					rpm -qi httpd
				
			

You should see the Apache package information in the following output.

				
					Name        : httpd
Version     : 2.4.37
Release     : 47.module_el8.6.0+1111+ce6f4ceb.1
Architecture: x86_64
Install Date: Saturday 24 December 2022 02:09:15 PM UTC
Group       : System Environment/Daemons
Size        : 4499649
License     : ASL 2.0
Signature   : RSA/SHA256, Wednesday 06 April 2022 03:16:12 PM UTC, Key ID 05b555b38483c65d
Source RPM  : httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.src.rpm
Build Date  : Wednesday 06 April 2022 02:55:33 PM UTC
Build Host  : x86-01.mbox.centos.org
Relocations : (not relocatable)
Packager    : CentOS Buildsys <bugs@centos.org>
Vendor      : CentOS
URL         : https://httpd.apache.org/
Summary     : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.

				
			

Step 4 - Manage Apache Service

In CentOS, the Apache web service is managed by systemd. You can start and stop it easily via systemctl command. To start the Apache service, run the following command.

				
					systemctl start httpd
				
			

If you want to verify the Apache service status, run the following command.

				
					systemctl status httpd
				
			

You should see the following output.

				
					● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-12-24 14:09:30 UTC; 4s ago
     Docs: man:httpd.service(8)
 Main PID: 26737 (httpd)
   Status: "Started, listening on: port 80"
    Tasks: 213 (limit: 5941)
   Memory: 25.5M
   CGroup: /system.slice/httpd.service
           ├─26737 /usr/sbin/httpd -DFOREGROUND
           ├─26738 /usr/sbin/httpd -DFOREGROUND
           ├─26739 /usr/sbin/httpd -DFOREGROUND
           ├─26740 /usr/sbin/httpd -DFOREGROUND
           └─26741 /usr/sbin/httpd -DFOREGROUND

Dec 24 14:09:30 centos8 systemd[1]: Starting The Apache HTTP Server...

				
			

Here, to enable the Apache service, run the following command.

				
					systemctl enable httpd
				
			

To stop the Apache service by running the following command:

				
					systemctl stop httpd
				
			

By default, Apache listens on port 80. You can verify it using the following command.

				
					ss -antpl | grep 80
				
			

You will get the following output.

				
					LISTEN 0      128                *:80              *:*    users:(("httpd",pid=26741,fd=4),("httpd",pid=26740,fd=4),("httpd",pid=26739,fd=4),("httpd",pid=26737,fd=4))
				
			

Now, open your web browser and access the Apache test page using the URL http://your-server-ip. You should see the Apache test page on the following screen.

Step 5 - Create Apache Virtual Host

All in all, Virtual hosting is an Apache web server feature that allows you to host multiple websites on a single server. In this section, we host a site named app.example.com using the virtual hosting feature.

First of all, please create your website directory using the following command.

				
					mkdir /var/www/html/app.example.com
				
			

After that, set proper permission and ownership with the following command.

				
					chown -R apache:apache /var/www/html/app.example.com
chmod -R 755 /var/www/html/app.example.com
				
			

Then, create a simple HTML page for your website.

				
					nano /var/www/html/app.example.com/index.html
				
			

Add the following code.

				
					<html>
  <head>
    <h2>Welcome to cloudinfrastructureservices.co.uk!</title>
  </h2>
  <body>
    <h3>Success! The cloudinfrastructureservices.co.uk website is up and working!</h3>
  <script>class RocketElementorAnimation{constructor(){this.deviceMode=document.createElement("span"),this.deviceMode.id="elementor-device-mode-wpr",this.deviceMode.setAttribute("class","elementor-screen-only"),document.body.appendChild(this.deviceMode)}_detectAnimations(){let t=getComputedStyle(this.deviceMode,":after").content.replace(/"/g,"");this.animationSettingKeys=this._listAnimationSettingsKeys(t),document.querySelectorAll(".elementor-invisible[data-settings]").forEach(t=>{const e=t.getBoundingClientRect();if(e.bottom>=0&&e.top<=window.innerHeight)try{this._animateElement(t)}catch(t){}})}_animateElement(t){const e=JSON.parse(t.dataset.settings),i=e._animation_delay||e.animation_delay||0,n=e[this.animationSettingKeys.find(t=>e[t])];if("none"===n)return void t.classList.remove("elementor-invisible");t.classList.remove(n),this.currentAnimation&&t.classList.remove(this.currentAnimation),this.currentAnimation=n;let s=setTimeout(()=>{t.classList.remove("elementor-invisible"),t.classList.add("animated",n),this._removeAnimationSettings(t,e)},i);window.addEventListener("rocket-startLoading",function(){clearTimeout(s)})}_listAnimationSettingsKeys(t="mobile"){const e=[""];switch(t){case"mobile":e.unshift("_mobile");case"tablet":e.unshift("_tablet");case"desktop":e.unshift("_desktop")}const i=[];return["animation","_animation"].forEach(t=>{e.forEach(e=>{i.push(t+e)})}),i}_removeAnimationSettings(t,e){this._listAnimationSettingsKeys().forEach(t=>delete e[t]),t.dataset.settings=JSON.stringify(e)}static run(){const t=new RocketElementorAnimation;requestAnimationFrame(t._detectAnimations.bind(t))}}document.addEventListener("DOMContentLoaded",RocketElementorAnimation.run);</script></body>
</html>
				
			

Save the file then create an Apache virtual host configuration for your website.

				
					nano /etc/httpd/conf.d/app.conf
				
			

Define your website directory and web page as shown below.

				
					<VirtualHost *:80>
    ServerName app.example.com
    ServerAlias example.com
    DocumentRoot /var/www/html/app.example.com
    DirectoryIndex index.html
    ErrorLog /var/log/httpd/example.com_error.log
    CustomLog /var/log/httpd/example.com_requests.log combined
</VirtualHost>
				
			

Save and close the file then restart the Apache service to apply the changes.

				
					systemctl restart httpd
				
			

A brief explanation of each configuration directive is shown below.

ServerName: Define the name of your domain.

DocumentRoot: Define the path of the website directory.

DirectoryIndex: Defaut website page to load.

ErrorLog: Define the path of the Apache error log.

CustomLog: Define the path of the Apache access log.

Step 6 - Verify Apache Virtual Host

Finally, your website is hosted on the Apache web server. Verify it using the URL http://app.example.com. You should see your website page on the following screen.

Thank you for reading How to Install Apache Web Server on CentOS 8 (Step by Step). We shall conclude this article blog. 

How to Install Apache Web Server on CentOS 8 (Step by Step) Conclusion

In this step by step guide, we explained how to install the Apache web server on CentOS 8. Additionally, we also showed you how to create a virtual host in Apache. Indeed, Apache is a powerful and reliable open source web server that is widely used by individuals and organizations around the world. Its modular design, configurability, and ability to handle a large number of concurrent connections make it an ideal choice for a variety of applications. Its open source nature and strong community support ensure that it will continue to be a popular choice for web servers in the future.

Would you like to read more Apache content, please navigate to our blog over here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x