Software & Configuration

Web Server: Apache

Installation and basic configuration of Apache on your server

Installation

# Debian/Ubuntu
apt update && apt install apache2 -y

# CentOS/AlmaLinux
dnf install httpd -y

Start and enable Apache

# Debian/Ubuntu
systemctl start apache2
systemctl enable apache2

# CentOS/AlmaLinux (service is called httpd)
systemctl start httpd
systemctl enable httpd

Open ports in the firewall:

ufw allow http
ufw allow https

Directory structure

PathDescription
/etc/apache2/Main configuration directory (Debian)
/etc/httpd/Main configuration directory (CentOS)
/etc/apache2/sites-available/VirtualHost configurations
/etc/apache2/sites-enabled/Active VirtualHosts
/var/www/html/Default document root
/var/log/apache2/Logs (Debian/Ubuntu)
/var/log/httpd/Logs (CentOS)

Configure a VirtualHost

nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined

    <Directory /var/www/example.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Enable the site and reload:

# Debian/Ubuntu
a2ensite example.com.conf
systemctl reload apache2

# CentOS: just create the file in /etc/httpd/conf.d/
systemctl reload httpd

Useful Apache modules

# Enable mod_rewrite (required for URL rewriting, WordPress, etc.)
a2enmod rewrite

# Enable mod_ssl
a2enmod ssl

# Enable mod_headers
a2enmod headers

# Reload after enabling modules
systemctl reload apache2

Verify and useful commands

# Test configuration
apache2ctl configtest
# or
apachectl configtest

# Reload without downtime
systemctl reload apache2

# Error logs in real time
tail -f /var/log/apache2/error.log

# List of active modules
apache2ctl -M

.htaccess files

The .htaccess file allows you to override Apache configuration directory by directory. It is widely used by CMSs like WordPress.

Basic example for WordPress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

For .htaccess to work you must have AllowOverride All in the VirtualHost and mod_rewrite enabled.

On this page