Apache Web Server

apache

Today, We will talk about Apache web server that is also known as httpd server in Linux. It works as a web server for the machine and it is most widely used in the whole world.

Apache web server is open source and free to use. Let’s begin with the practical use now.

We will use CentOS 9 to do this practical. You can use any CentOS or RedHat version for it. If you don’t know how to use it then click on link.

We will check whether ‘httpd‘ is installed or not by running “rpm -q httpd” command.

There is no httpd installed on the machine as we can see from the screen message “package httpd is not installed“.

We can install httpd by “dnf install httpd -y” command.

Now if we will check the httpd installed or not by the same command “rpm -q httpd“.

Now, We will check our IP address of the system and start the httpd service to check our web server on a browser.

We will use ‘ifconfig‘ command to know our IP address.

As you can see our IP address is “192.168.1.198“. We will now start and enable the httpd service by “systemctl start httpd” and “systemctl enable httpd” so that our httpd service will work after shutdown and restart too.

Now we can check our Apache web server on the browser by writing our IP address.

As we can see our Apache web server is working fine, Now we can customize it according to us.

First we have to understand that how we can do that?

We can do that by changing the information in httpd configuration file and we can check all the httpd configuration files by “rpm -qc httpd” command.

Now we will go to its main configuration file by “vim /etc/httpd/conf/httpd.conf” command and see all the possible options.

We can use up and down arrow keys to move in this document.

We will explain each and everything in the document but we will make different post for each thing in practical because if we will try to do everything in one post then it would be super long post that nobody will like.

First is ServerRoot “/etc/httpd” that shows the path of top level directory of httpd that contains all the main files of httpd like configuration files, log files etc.

Now there is “Listen 80” that is showing that our httpd server will work on port 80 on the browser. We can change the port for security or other reasons for here.

Next is “Include conf.modules.d/*.conf” to inform the httpd server that any file with .conf extension in conf.modules.d directory should be treated as configuration file for httpd.

It is important to make separate configuration files in production environments so that if something goes wrong then you can just delete the file and it will undo everything.

We are having user and group name as “apache“. It is the default name for httpd service.

Now it is the email contact information that can help users to contact to the admin that is managing the httpd server as it is shown “ServerAdmin root@localhost“. It is by default written like that but you can change it to your real email id.

Next, It is about the ‘/’ directory in which we are disabling the .htaccess for our Apache server so that it will improve performance and security by writing “AllowOverride none“.

Also, We are denying Apache server access from ‘/’ directory by writing “Require all denied“.

Now we will talk about document root, It is the place from where our httpd server read the files and show in the browser. By default it is set to /var/www/html

We can see document root by DocumentRoot “/var/www/html” in the configuration file.

Next, It is about “/var/www” directory in which allow override is none that means .htaccess cannot be edited and configuration files are needed to do changes. Also, Require all granted is giving access to all the users to access “/var/www” directory.

This is also same like above for directory “/var/www/html” with one more thing added in this that is “Options Indexes FollowSymLinks“.

It allows Apache to follow soft (Symbolic) links to reach from one file to another that is linked with each other.

Next is ‘IfModule‘ that helps in showing the default file on the httpd page that is called as “index.html“. We can change this file by writing some other name here under ‘IfModule‘.

In <Files> tag we are denying access to ‘.ht‘ files from the users on the web that can be .htaccess and .htpasswd.

We can see all the errors related to httpd server in “logs/error_log” that is written in this file as ErrorLog “logs/error_log”

In this file it is already mentioned that all the path will start from “/etc/httpd” so the full path of the error logs would be “/etc/httpd/logs/error_log“.

After this there are some more ‘IfModule‘ that are not important to discuss for now. We will end our Apache web server post here.

If anyone need affordable VPS server for practice or for their work then, Buy it from here.

FAQ on Apache Web Server

Q. How do I install Apache (httpd) on Linux?

A. For CentOS / RHEL:

dnf install httpd

For Ubuntu / Debian

apt install apache2

Q. What is httpd in Linux?

A. httpd stands for HTTP Daemon, which is the Apache web server running as a background service to serve websites over HTTP/HTTPS.

Q. How do I start/stop/restart the Apache server?

A. For start:

systemctl start httpd

For stop:

systemctl stop httpd

For restart:

systemctl restart httpd

For status:

systemctl status httpd

Q. Where is the Apache config file located?

A. Common paths:

Main config: /etc/httpd/conf/httpd.conf (CentOS/RHEL)

Extra configs: /etc/httpd/conf.d or /etc/httpd/conf.modules.d

Q. What is DocumentRoot?

A. It defines the directory where your website files live:

DocumentRoot “/var/www/html”

When someone accesses your server, Apache serves files from here.

Q. What is ServerRoot?

A. This defines the base directory where Apache looks for its config, logs, and runtime files:

ServerRoot “/etc/httpd”

Q. What does Require all granted mean?

A. It allows access to a directory or resource:

<Directory “/var/www/html”>
Require all granted
</Directory>

Q. How do I enable .htaccess file support?

A. Set this in your config:

AllowOverride All

This tells Apache to allow .htaccess to override settings.

Q. Where are the logs stored?

A. Access log: /var/log/httpd/access_log
Error log: /var/log/httpd/error_log


Discover more from Root Learning

Subscribe to get the latest posts sent to your email.

8 thoughts on “Apache Web Server

  1. Apache is the most important thing when it comes to production work, It is the base of any application and also how to secure our application from potential threats. you make this post with so much thoughts into it. Thanks for reminding the points again and keep up the good work 👍🏻

Leave a Reply

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