متقدم
البحث فيأدلة التثبيت
طريقة البحث
  • الصق رسالة الخطأ بين علامتي تنصيص، مثل "permission denied"، لتجد جدول الحل.
  • اختر مجالًا وجزءًا من الدليل (التثبيت، أول الإعدادات، الاستخدام اليومي، الأمان، الحلول) لتصل مباشرة إلى الخطوة الصحيحة.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

الدليل 25Apache httpd · Web

Install and set up Apache httpd

Apache httpd is the classic web server. It still runs a big part of the web: WordPress and PHP hosting, company intranets, and servers that use .htaccess files. It is also the web server of the RHCSA exam. This guide installs Apache on RHEL (httpd) and Ubuntu (apache2), builds virtual hosts, connects PHP-FPM and a reverse proxy, adds HTTPS, and covers the security settings and the fixes for 403 and 500 errors.

  • 13 الدروس
  • 16 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

25.1

Know it before you install

One VirtualHost = one website (by name and port). Modules add features (mod_ssl, mod_proxy, mod_rewrite, mod_headers). The MPM decides how Apache handles connections — use event with PHP-FPM.

ItemRHEL / Rocky / Alma / FedoraUbuntu / Debian
Packagehttpd (+ mod_ssl)apache2
Servicehttpd.serviceapache2.service
Main config/etc/httpd/conf/httpd.conf/etc/apache2/apache2.conf
Your sites/etc/httpd/conf.d/*.conf/etc/apache2/sites-available/ + a2ensite
Modules/etc/httpd/conf.modules.d/a2enmod / a2dismod
Web root/var/www/html/var/www/html
Logs/var/log/httpd//var/log/apache2/
Test configapachectl configtestapache2ctl configtest
Runs asuser apacheuser www-data

الأوامر في هذا الدرس

25.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install httpd mod_ssl
systemctl enable --now httpd
firewall-cmd --permanent --add-service={http,https}
firewall-cmd --reload
curl -I http://localhost

25.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y apache2
a2enmod ssl headers rewrite
ufw allow 'Apache Full'
systemctl reload apache2

الأوامر في هذا الدرس

25.4

First configuration: global hardening

Hide the version, turn off old features, and keep directory listings off:

/etc/httpd/conf.d/00-security.conf
ServerTokens Prod
ServerSignature Off
TraceEnable Off
FileETag None
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
 
<Directory /var/www>
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
 
<FilesMatch "^\.(git|env|ht)">
    Require all denied
</FilesMatch>

(On Ubuntu put it in /etc/apache2/conf-available/security-extra.conf and run a2enconf security-extra.) Also set ServerName once in the main config to stop the AH00558 warning: ServerName web1.example.com.

25.5

A first website (VirtualHost)

/etc/httpd/conf.d/shop.example.com.conf
<VirtualHost *:80>
    ServerName shop.example.com
    ServerAlias www.shop.example.com
    DocumentRoot /var/www/shop
 
    <Directory /var/www/shop>
        AllowOverride FileInfo
        Require all granted
    </Directory>
 
    ErrorLog  /var/log/httpd/shop-error.log
    CustomLog /var/log/httpd/shop-access.log combined
</VirtualHost>
mkdir -p /var/www/shop
echo '<h1>It works</h1>' > /var/www/shop/index.html
restorecon -Rv /var/www/shop
apachectl configtest
Syntax OK
systemctl reload httpd

On Ubuntu: save it in sites-available/shop.example.com.conf, use /var/log/apache2/…, then a2ensite shop.example.com && a2dissite 000-default && apache2ctl configtest && systemctl reload apache2.

تذكّر

AllowOverride None is faster and safer. Allow .htaccess (AllowOverride FileInfo or All) only for apps that need it, like WordPress.

25.6

PHP with PHP-FPM

PHP-FPM runs PHP in its own service; Apache passes .php files to it.

dnf -y install php-fpm php-mysqlnd php-gd php-mbstring php-xml php-opcache
systemctl enable --now php-fpm
systemctl reload httpd
apt-get install -y php-fpm php-mysql php-gd php-mbstring php-xml
a2enmod proxy_fcgi setenvif
a2enconf php8.3-fpm
systemctl reload apache2

(The first 3 lines are for RHEL — the php.conf in conf.d/ connects Apache to PHP-FPM by itself. The last 4 lines are for Ubuntu — write your PHP version in php8.3-fpm.) Test with a <?php phpinfo(); file and delete it right after.

25.7

Reverse proxy to an app

/etc/httpd/conf.d/app.example.com.conf
<VirtualHost *:80>
    ServerName app.example.com
    ProxyPreserveHost On
    ProxyPass        / http://127.0.0.1:3000/
    ProxyPassReverse / http://127.0.0.1:3000/
    RequestHeader set X-Forwarded-Proto "http"
</VirtualHost>
setsebool -P httpd_can_network_connect 1
a2enmod proxy proxy_http

(setsebool on RHEL; a2enmod on Ubuntu.)

الأوامر في هذا الدرس

25.8

Add HTTPS

dnf -y install certbot python3-certbot-apache
apt-get install -y certbot python3-certbot-apache
certbot --apache -d shop.example.com -d www.shop.example.com
certbot renew --dry-run

Certbot adds a :443 VirtualHost with the certificate and a redirect from HTTP. Add Header always set Strict-Transport-Security "max-age=31536000" to the HTTPS VirtualHost and allow only SSLProtocol -all +TLSv1.2 +TLSv1.3.

25.9

Daily use: the commands and the real uses

apachectl configtest
systemctl reload httpd
apachectl -S
apachectl -M
tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/shop-access.log
curl -I https://shop.example.com

apachectl -S shows all virtual hosts and which one is the default; -M lists the loaded modules.

Real uses at work:

  • PHP hosting (WordPress, WooCommerce, Laravel) with PHP-FPM.
  • Apps that ship .htaccess rules; intranet sites with LDAP login (mod_authnz_ldap).
  • The web server tasks of the RHCSA exam (SELinux contexts and ports).

25.10

Make it safe

Security checklist:

  • ServerTokens Prod, ServerSignature Off, TraceEnable Off, no Indexes.
  • HTTPS only with HSTS; TLS 1.2+.
  • AllowOverride None where possible; deny .git, .env, .ht*.
  • Use event MPM + PHP-FPM, not mod_php.
  • Keep SELinux on: restorecon, semanage fcontext -a -t httpd_sys_rw_content_t for upload folders, setsebool for proxy and mail.
  • Remove test files (phpinfo), keep the server and PHP updated.

25.11

Update and remove

dnf -y upgrade httpd mod_ssl
apt-get install --only-upgrade -y apache2
dnf -y remove httpd mod_ssl
apt-get purge -y apache2

الأوامر في هذا الدرس

25.12

Fix common problems

ProblemCauseFix
403 ForbiddenRequire missing, no index file, file rights or SELinux label.Require all granted; namei -l FILE; restorecon -Rv /var/www/shop; ausearch -m avc -ts recent.
AH00558: Could not reliably determine the server's fully qualified domain nameNo global ServerName.Add ServerName host.example.com to the main config.
500 Internal Server ErrorA bad .htaccess line or the app failed.Read the site's error log; check the module of the .htaccess line is loaded.
PHP code shows as text or downloadsPHP-FPM is not connected.Install and start php-fpm; Ubuntu: a2enmod proxy_fcgi + a2enconf php*-fpm.
502/503 on proxy (RHEL)SELinux blocks the connection or the app is down.setsebool -P httpd_can_network_connect 1; curl 127.0.0.1:3000.
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80Nginx or another server uses port 80.ss -ltnp 'sport = :80'; stop the other one.
Wrong site answersName does not match, so the first VirtualHost wins.apachectl -S; fix ServerName / ServerAlias.
Custom port (for example 8081) will not start on RHELSELinux port label.semanage port -a -t http_port_t -p tcp 8081.

25.13

Next steps

1A new site gives 403, the files exist and rights are fine (RHEL). What do you check next?

The SELinux label: ls -Z, then restorecon -Rv /var/www/shop.

2Which two commands do you run before every reload?

apachectl configtest, and on bigger changes apachectl -S to see the virtual hosts.

كل الأوامر في هذا الفصل