Настройка Apache для веб-сервера
Apache HTTP Server — зрелый веб-сервер с модульной архитектурой. Широко используется на shared-хостингах, в связке с PHP через mod_php или mod_proxy_fcgi, в корпоративных средах с LDAP/Kerberos.
Установка и базовые модули
apt install -y apache2
a2enmod rewrite ssl headers proxy proxy_fcgi setenvif http2
systemctl restart apache2
VirtualHost для Laravel
# /etc/apache2/sites-available/myapp.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/myapp/current/public
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Protocols h2 http/1.1
# Заголовки безопасности
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
Header always set X-Frame-Options "DENY"
Header always set X-Content-Type-Options "nosniff"
<Directory /var/www/myapp/current/public>
AllowOverride All
Require all granted
Options -Indexes
</Directory>
# PHP-FPM через socket
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
# Статика — кеш
<FilesMatch "\.(css|js|jpg|png|gif|ico|svg|woff2)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined
</VirtualHost>
.htaccess для Laravel
# public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# HTTPS редирект (если не обрабатывается VirtualHost)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Разрешить доступ к существующим файлам
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Перенаправить всё на index.php
RewriteRule ^ index.php [L]
</IfModule>
# Отключить вывод версии сервера
ServerSignature Off
# Запретить доступ к .env
<Files ".env">
Require all denied
</Files>
Производительность
# /etc/apache2/mods-enabled/mpm_event.conf
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
# Включить gzip
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/json application/javascript
DeflateCompressionLevel 6
</IfModule>
# mod_expires
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
</IfModule>
Rate Limiting через mod_ratelimit
a2enmod ratelimit
# В VirtualHost
SetOutputFilter RATE_LIMIT
SetEnv rate-limit 400 # 400 KB/s
Reverse Proxy
a2enmod proxy proxy_http
<VirtualHost *:443>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Real-IP %{REMOTE_ADDR}s
</VirtualHost>
Apache vs Nginx
Apache предпочтителен когда: нужен .htaccess (shared hosting), используется mod_php, есть сложные per-directory конфиги, нужны специфические Apache-модули. В остальных случаях Nginx показывает лучшую производительность на статике и меньшее потребление памяти.
Срок реализации
Базовая настройка Apache с PHP-FPM: 1 день. Оптимизация производительности и безопасности: +1 день.







