How to install and configure apache web server in CentOS server

Apache web server is widely used open-source web server for web application. It provides dynamically run multiple website, redirection, whitelist-bloacklist and security features for websites. Ubuntu and centOS is common used linux web server.

In this article, we will learn hou to install and configure apache web server in CentOS server. So let's start by logging in server in Terminal.

Install Apache server

The first step is to install any package is to update the system software repositories list. So run the below command.

sudo yum update

Now  install Apache server running below command.

sudo yum install httpd

If you have installed firewalld in your server, then you need to open port 80 to allow apache to handle the requests. You can do it by running the below command.

sudo firewall-cmd --permanent --add-service=http

Or if you have installed SSL certificate for the domain, you need to open port 443.

sudo firewall-cmd --permanent --add-service=https

Now reload firewall services.

sudo firewall-cmd --reload

You need to start apache server in CentOS server.

sudo systemctl start httpd

If you want to stop apache server, you can stop the apache server with below command.

sudo systemctl stop httpd

To restart the the server, run this command:

sudo systemctl restart httpd

If you want to check apache status, you can run the command:

sudo systemctl status httpd

This will output like this:

Redirecting to /bin/systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-05-18 01:18:01 UTC; 2months ago

Configure Apache server

Apahe main configuration file located at /etc/httpd/conf/httpd.conf. The default directory apache handle is /var/www/html. So if you want to put the website project at different directory or want to add multiple domains, you need to creat seperate configuration file for each domain.

First add the domain configuration file path in the main apache configuration file. Open /etc/httpd/conf/httpd.conf file in nano editor.

sudo nano /etc/httpd/conf/httpd.conf

And add the filepath at the end of file in it.

IncludeOptional sites-enabled/*.conf

Save and exit the file. Now create a file with your domain name and put the configuration options with appropriate file path.

sudo nano /etc/httpd/sites-enabled/
<VirtualHost *:80>
   DocumentRoot /var/www/html/
   ErrorLog /var/www/html/
   CustomLog /var/www/html/ combined

Save and exit the file. That's it. Your domain is successfully configured to served at the path /var/www/html/ directory.