Complete guide to install and configure the Apache HTTP Server in Linux

Introduction

The Apache HTTP Server, commonly known as Apache, is one of the most used web servers in the world. Its modular architecture, extensive documentation and compatibility with virtually any operating system make it an ideal option for hosting sites and applications in Linux environments. In this article you will learn to install, configure and optimize Apache in the most popular distributions, as well as know good safety practices and problem solving.

System preparation

Before installing Apache it is recommended to update the package repository and make sure that the user has sudo privileges. In Debian-based distributions runssudo apt update && sudo apt upgrade -ywhile Red Hat and its derivatives are usedsudo dnf update -yorsudo yum update -yaccording to the version. It is also useful to have a text editor installed asnanoorvimto modify the configuration files.

Installation in Ubuntu and Debian

In Ubuntu / Debian the package is calledapache2. The installation is simple:

  • Runsudo apt install apache2 -y
  • Check that the service is active withsudo systemctl status apache2
  • If not active, start it and enable it to boot:sudo systemctl start apache2andsudo systemctl enable apache2

Once installed, open a browser and head tohttp://localhostor to the server IP; you should see the default Apache page indicating that everything works properly.

Installation in CentOS, RHEL and Fedora

In these distributions the package is calledhttpd. The steps are similar:

  • In Fedora and recent versions of RHEL / CentOS usesudo dnf install httpd -y
  • In older versions of CentOS usesudo yum install httpd -y
  • Start and enable the service:sudo systemctl start httpdandsudo systemctl enable httpd
  • Check the status withsudo systemctl status httpd

When visitinghttp://localhostThe Apache test page should appear.

Basic configuration

The main configuration files are found in/etc/apache2(Debian / Ubuntu) or/etc/httpd(Red Hat). The main file isapache2.conforhttpd.conf. Some aspects that are usually initially adjusted are:

  • The server direttive to define the host name of the server
  • The DocumentRoot direttive that indicates the directory where the web files are served (default/var/www/html)
  • The DirectoryIndex direttive to set the file that is used when you access a directory (e.g.index.html)

After modifying any configuration file, recharge Apache to apply the changes:sudo systemctl reload apache2orsudo systemctl reload httpd.

Useful modules and how to activate them

Apache allows to extend its functionality through modules. Some of the most common are:

  • mod _ rewrite: to rewrite URLs and create friendly links
  • mod _ ssl: to enable HTTPS by TLS / SSL certificates
  • mod _ proxy and mod _ proxy _ http: to configure reverse proxies and load balance
  • mod _ headers: for handling HTTP headers
  • mod _ expires: to control the browser cache by expiry dates

In Debian / Ubuntu are activated withsudo a2enmod nombre_del_moduloand are deactivated withsudo a2dismod. The corresponding packages (e.g. mod _ ssl) are installed in Red Hat and ensure that the LoadModule line is present in the configuration file; then the service is recharged.

Optimization and safety

To get good performance and keep the server safe, the following practices are recommended:

  • Use KeepAlive with a suitable timeout to reduce connection overload
  • Configure MaxRequestWorkers (formerly MaxCustomers) according to available memory
  • Enable mod _ deflate compression to reduce the size of transferred resources
  • Implement a firewall (ufw or firewall) that allows traffic only in ports 80 and 443
  • Get a free TLS certificate with Let's Encrypt and set mod _ ssl to force HTTPS
  • Disable directory listing with Options -Indexes in Directory blocks
  • Keep the server and packages updated regularly

In addition, review the login located in/var/log/apache2/or/var/log/httpd/helps to detect performance problems or intrusion attempts.

Common problem solution

Some frequent inconvenience and resolution:

  • The service does not start: check the log withjournalctl -u apache2orjournalctl -u httpdand correct syntax errors in the configuration files usingapachectl configtest
  • Page not found (404): verify that the DocumentRoot points to the correct directory and that the file permissions allow the user to read under which Apache runs (usually www-data or apache)
  • Permission errors when accessing files: ensure that the user of the Apache group has reading and, if necessary, writing permissions in the content directories
  • Slow site: review the use of CPU and memory, activate cache modules as mod _ cache and consider an inverse proxy as nginx in front of Apache to serve static content

With these steps you will be able to diagnose and correct most of the drawbacks that arise when administering an Apache server in Linux.

Conclusion

Install and configure the Apache HTTP Server in Linux is an accessible process thanks to the lot of documentation and tools available. Following the installation steps, adjusting the basic configuration, activating the necessary modules and applying good performance and security practices, you will have a robust web server ready to host your projects. Remember to keep the system up-to-date and monitor the logs to ensure continuous and safe operation.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish