Web Server: Apache

01

Installation

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

# CentOS/AlmaLinux
dnf install httpd -y

Start and enable Apache

bash
# 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:

bash
ufw allow http
ufw allow https
02

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)
03

Configure a VirtualHost

bash
nano /etc/apache2/sites-available/example.com.conf
apache

</VirtualHost>

Enable the site and reload:

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

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

Useful Apache modules

bash
# 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
05

Verify and useful commands

bash
# 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
06

.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:

apache
# BEGIN WordPress

# END WordPress

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

DeluxHost, fondata nel 2023, offre soluzioni di hosting di alta qualità per diverse esigenze digitali. Forniamo hosting condiviso, VPS e server dedicati con sicurezza avanzata e datacenter globali.

© DeluxHost, Tutti i diritti riservati. | Partita IVA: IT17734661006
Tutti i sistemi operativi