Install, Configure, and Troubleshoot Linux Web Server (Apache)

In this tutorial, we will talk about the Linux web server or Apache web server specifically and how to install it and configure it to serve your content to others.

A web server is a system that manipulates requests via the HTTP protocol, you request a file from the server, and it responds with the requested file, which might give you an idea that web servers are not only for the web.

Web servers embedded in devices such as printers, routers, when you open your router configuration page, there is a web server behind it.

When you open the printer configuration page, there is also a web server behind it serving your requests, so web servers are everywhere.



How web server works

First, your browser sends a request to the server.

The server takes the requested file or page from you and maps it to the corresponding file from the server. The server sends the file back to the browser with some information such as the MIME type, the length of the content, and other useful information.

Sometimes the requested file is a static page like HTML pages or dynamic pages like PHP, Java, Perl, or any other server-side language.

For example, when you type, the browser queries the DNS server about the IP address of the computer: Once the browser gets the response from the DNS, it starts a TCP connection on port 80 and asks for the default web page, then the server sends this page to you, and that’s all.


Linux web server implementations

There are many Linux web server implementations available for you to use:

  • Apache server
  • Nginx
  • Lighttpd
  • Apache Tomcat
  • Monkey HTTP Daemon (used especially for embedded systems)

There are more Linux web servers, but this list is the most used web server.

The most used web servers are Apache and Nginx.

In this post, we will use the Apache server for several reasons:

  • Stable.
  • Flexible.
  • Secure.

We’ll install and configure Apache server on Linux, but at first, let’s review some of the basics of HTTP protocol basics.


Understanding HTTP

When you request a file or a page from a web server, the client at first connects to the server on port 80. After a successful connection, the client then sends HTTP commands (also methods) to the server. This command includes a request header, which includes information about the client.

To view these request headers in chrome, open chrome devtools, then open network panel and visit and check the request headers, you should see something like this:

Linux Web Server Request Header

The request header also includes information about the client, like the user agent and the accepted formats.

The web server may send additional information with the request header. For example, if you click on a link that will open another website, the header will include the referral site.

After receiving the request header completely, the server responds with the requested file or page along with a response header.

The response header includes information about the received content, its type, and other information.

Response header

You can check the response headers from the browser network panel.


Install Apache web server

You can install Apache server on Red Hat based distros using the following command:

$ dnf -y httpd

Or if you are using a Debian-based distro, you can install it like this:

$ apt-get -y install apache2

The Apache web server service is called httpd on Red Hat based distros like CentOS, while it is called apache2 in Debian based distros.

If you are using a firewall like iptables, you should add a rule for port 80.

$ iptables -I INPUT 1 -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Or if you are using firewalld, you can use the following command:

$ firewall-cmd --add-port=80/tcp

To start your service and enable it on boot:

$ systemctl start httpd

$ systemctl enable httpd

You can check if your service is running or not, using the following command:

$ systemctl status httpd

Now open your browser and visit http://localhost or http://[::1]/ if you are using IP v6, and if your installation goes well, you should see your HTML homepage.


Configuring Apache web server

You can add files to Apache in the


directory for top-level pages.

Just remember to make sure that any files or directories placed in that directory are world-readable.

The default index page is index.html.

The Apache configuration files are in



On Debian based systems like Ubuntu, you may find it at



We can’t discuss every option for Apache on a single post, but we will discuss the most important options.

You call them options or directives.

ServerRoot option

This option specifies the configuration folder for the Apache web server. On Red Hat based distros, the ServerRoot option is /etc/httpd/ directory. On Debian distros the ServerRoot option is /etc/apache2/.

ServerRoot /etc/httpd

Listen option

The Listen option specifies the port that the Apache web server will use to wait for incoming connections.

The default value for this option is 80 for nonsecure connections and 443 for secured connections.

If you have multiple IP addresses on your server, you can assign which IP should listen for connection using the Listen option.

You can specify a different port other than 80, just make sure that it’s not in use.

You can run many HTTP servers on the same hardware every one on a unique port.

When a server runs on a non-standard port such as port 8080, it will require the port number to be explicitly stated like this:

Listen 80

ServerName option

This option specifies the hostname of the web server that appears to the visitors.

ServerName FQDN

DocumentRoot option

The DocumentRoot option defines the path that will contain your files that will be served.

The default path is /var/www/html .

DocumentRoot /var/www/html

MaxRequestWorkers option

This option sets the maximum number of concurrent connections that the server will receive.

LoadModule option

We use this option to load modules into the Apache web server.

There are a lot of Apache modules like these:

mod_cgid: This module runs CGI scripts using the Apache web server.

mod_ssl: Provides secure connections via SSL and TLS protocols.

mod_userdir: This module allows you to serve content from users’ specific directories.

If you want to disable loading a specific module, you can comment the Load module line that contains that module.

Or if you use Debian based distros like Ubuntu, you can use these commands:

$ a2enmod modulename

The a2enmod enables the module.

$ a2dismod modulename

This command to disable the module.

All these commands do is create a symlink under



And if you use a2dismod, the symlink will be removed.

If you enable or disable a module, you have to reload or restart the Apache web server.


Include option

This option allows you to include other configuration files.

You can store all the configuration for different virtual domains, and Apache will include them at runtime.

Include filePath

UserDir option

This option specifies the directory that will contain the files that will be accessible via the web server. This directory is usually is public_html and its location in the user’s home directory.

For example, if you have a user adam who wants to make his web content available via the Apache web server.

First, we make a public_html folder under his home directory.

Then set the permission for the public_html folder:

$ chmod 644 public_html

Now if we put an index.html file, it will be accessible via the browser like this:


UserDir public_html

Alias option

This option specifies the location of the files that are outside the DocumentRoot location and need to be served by the Apache web server.

Like you have files outside DocumentRoot, and you want them to be available to the visitors.

Alias URL_Path Actual_Path

ErrorLog option

This option specifies the error log file for the Apache web server.

ErrorLog /var/log/httpd/error_log

VirtualHost option

This option allows you to host multiple websites on the same server.

The idea is that the content is served based on the requested hostname.

To setup a virtual host for the host First, create a VirtualHost option in /etc/httpd/conf/httpd.conf file.

And specify the DocumentRoot and ServerName like this:

 DocumentRoot /home/adam/public_html
 ErrorLog /var/log/users/adam/error_log

Keep in mind that the ServerName option must be resolvable via DNS.

These are the most used Apache options.

Virtual Host types

There are two types of virtual hosts that you can define in Apache web server:

  • Name-based virtual hosts
  • IP-based virtual hosts

The NameVirtualHost directive defines which addresses can be virtual hosts; the asterisk (*) means any name or address on this server. You can write them like this:

NameVirtualHost *
<VirtualHost *>
 DocumentRoot "/home/user1/public_html/"
<VirtualHost *>
 DocumentRoot "/ home/user2/public_html/"

If you have more than one IP address and you want to use an SSL certificate, the website must be on a dedicated IP address. You can write IP-based virtual hosts like this:

 DocumentRoot "/home/user1/public_html/"
 DocumentRoot "/ home/user2/public_html/"


Apache process ownership

We know that each process inherits its permissions from its parent process.

This fact is true for all processes except for applications with the SETUID bit set; they inherit permissions from the owner, not the parent process. A good example is the /bin/su.

If a normal user runs /bin/su program, it does not inherit permission from adam, but it acts as a root user running it.

Since Apache web server needs to bind port 80, and this needs root privileges.

After binding to port 80, Apache can run as a normal user and only read files that have permissions to read them.

Based on the Linux distro you use, the user could be one of the following:

nobody, www, apache, www-data, or daemon.

I delayed introducing two more options for Apache till reaching that point.

User option

The User option specifies the user ID which the web server will use to answer requests.

User www-data

Group option

The Group option specifies the group that the Apache web server will use to read files.

Group www-data

Security is very important for sites that use executable scripts such as CGI or PHP scripts.

The user you will use will have the permission to read and write the content of all sites on the server. But we want to ensure that only the members of a particular site can read their site only.

This is very important because if an attacker compromises your website, he will be able to read all files since the Apache user has permission to do that.

So how to solve this problem?


suEXEC support

A popular method is to use suEXEC. suEXEC is a program that runs with root permissions and makes CGI programs run as the user and group IDs of a specific user, not the Apache server user.

You can specify the user on each virtual host like this:

 SuExecUserGroup adam adamGroup

Just that simple.


Apache authentication

You may want to restrict some parts to specific visitors. It’s like a password-protected directory.

In Apache, you can store the authentication information file called .htpasswd file.

You can use the htpasswd command to do that.

First, create the .htpasswd file using the htpasswd command:

$ htpasswd -c /home/adam/.htpassswd myuser

You need the -c option the first time you run htpasswd, but when you need to add more users, you shouldn’t use -c because it will overwrite the file.

Then create a .htaccess file in the public_html folder and write the following:

<Location /vip>
 AuthName "test"
 AuthType Basic
 AuthUserFile /home/adam/.htpasswd
 Order deny,allow
 require valid-user

AuthName is required, you can use any string you want.

AuthType Basic says that you’re using an htpasswd style user file.

AuthUserFile points to the file that contains the generated password from the htpasswd command.

The Order line indicates that Apache must deny access by default, and only allow access for users specified in the htpasswd file.

The require directive means any user in the .htpasswd file is allowed.


Troubleshooting Apache web server

If you modify the httpd.conf file and restart or reload Apache web server and it did not work, then you have typed the wrong configuration. However, this is not the only case that you need to troubleshoot Apache. You may look at the Apache logs to see how the service works so you can diagnose the problem and solve it.

The two main log files for Apache are error_log and access_log files.

You can find these files in


directory if you are using Debian based distros.

The access_log file contains every request to the Apache web server with the details about the client requested that resource.

The error_log file contains errors of the Apache web server.

You can use the tail command to watch the log file:

$ tail -f /var/log/httpd/error_log

I recommend you review the Linux syslog server to know more about logging.

18 thoughts on “Install, Configure, and Troubleshoot Linux Web Server (Apache)
  1. Hi,

    Please make a tutorial based on apache tuning for 1 processor 512M apache server and help me tune for any number of processors and memory. Please make a article on apache tuning for a server with decent resources.

  2. Hi,

    I want a decent configuration as base, so that I can do tuning for all configuration of resources.

    1. You can follow the articles of the process management and Apache web server and mail server in server administration section so all of your services are tweaked as possible.

    1. Hi,
      Since you’ve installed Apache server, nothing is needed, you can receive requests on any path. Just put your file that will receive the request, in your case /as2/Httpreceiver/ or in case you are using a framework like Laravel, you should set the route correctly.
      You can use any server-side code to receive requests: PHP, Python, or Perl.

Leave a Reply

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