all:bibles:linux:serveur:apache
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| all:bibles:linux:serveur:apache [2024/11/14 16:13] – [Générer un certificat signé par une autorité bidon] omeylhoc | all:bibles:linux:serveur:apache [2025/02/12 11:09] (Version actuelle) – [Fichier de configuration] omeylhoc | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Apache ====== | ||
| + | |||
| + | ---- | ||
| + | |||
| + | < | ||
| + | |||
| + | ---- | ||
| + | |||
| + | ===== Installation ===== | ||
| + | |||
| + | ==== Ubuntu ==== | ||
| + | |||
| + | === Installation paquet === | ||
| + | |||
| + | <code bash> | ||
| + | sudo apt-get install apache2 | ||
| + | </ | ||
| + | |||
| + | === Vérification version === | ||
| + | |||
| + | <code bash> | ||
| + | apachectl -v | ||
| + | </ | ||
| + | |||
| + | ==== Redhat/ | ||
| + | |||
| + | === Installation paquet === | ||
| + | |||
| + | <code bash> | ||
| + | sudo dnf install httpd | ||
| + | </ | ||
| + | |||
| + | === Démarrage / Activation === | ||
| + | |||
| + | <code bash> | ||
| + | sudo systemctl start httpd | ||
| + | sudo systemctl enable httpd | ||
| + | </ | ||
| + | |||
| + | === Vérification version === | ||
| + | |||
| + | <code bash> | ||
| + | httpd -v | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Configuration générale apache ===== | ||
| + | |||
| + | ==== Ubuntu ==== | ||
| + | |||
| + | === Fichiers/ | ||
| + | |||
| + | | / | ||
| + | | / | ||
| + | | / | ||
| + | | / | ||
| + | | / | ||
| + | | / | ||
| + | |||
| + | === Paramètres par défaut === | ||
| + | |||
| + | * User : www-data | ||
| + | * Group : www-data | ||
| + | |||
| + | ==== Redhat/ | ||
| + | |||
| + | === Fichiers/ | ||
| + | |||
| + | | / | ||
| + | | / | ||
| + | | / | ||
| + | |||
| + | === Paramètres par défaut === | ||
| + | |||
| + | * User : apache | ||
| + | * Group : apache | ||
| + | |||
| + | === selinux === | ||
| + | |||
| + | En cas de problème de droits (Erreur 403) vérifier si selinux est activé, vérifier les logs et changer les droits des fichiers : | ||
| + | |||
| + | <code bash> | ||
| + | sestatus | ||
| + | tail -f / | ||
| + | sudo chcon -R -t httpd_sys_content_t / | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Configuration sites web ===== | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | ==== Exemples ==== | ||
| + | |||
| + | === http === | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | ServerName www.site.com | ||
| + | ServerAlias site.com | ||
| + | DocumentRoot "/ | ||
| + | | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | === https === | ||
| + | |||
| + | <note important> | ||
| + | <color # | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | < | ||
| + | ServerName www.site.com | ||
| + | ServerAlias site.com | ||
| + | DocumentRoot "/ | ||
| + | |||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | |||
| + | SSLEngine on | ||
| + | SSLCertificateFile | ||
| + | SSLCertificateKeyFile / | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Sécurisation ==== | ||
| + | |||
| + | === Redirection du http sur le https === | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | ServerName www.site.com | ||
| + | | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | | ||
| + | Redirect permanent / https:// | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | === Interdire l' | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | ServerName xxx.xxx.xxx.xxx | ||
| + | < | ||
| + | Deny from all | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | ServerName xxx.xxx.xxx.xxx | ||
| + | < | ||
| + | Deny from all | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | === VirtualHost par défaut === | ||
| + | |||
| + | Dans l' | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | < | ||
| + | Require all denied | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | < | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | |||
| + | SSLEngine on | ||
| + | SSLCertificateFile | ||
| + | SSLCertificateKeyFile / | ||
| + | < | ||
| + | Require all denied | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Commandes ==== | ||
| + | |||
| + | <code bash> | ||
| + | sudo apache2ctl -M ⇒ liste les modules Apache chargés | ||
| + | sudo a2enmod < | ||
| + | sudo a2ensite < | ||
| + | sudo apachectl configtest | ||
| + | sudo systemctl reload apache2 | ||
| + | sudo systemctl restart apache2 | ||
| + | </ | ||
| + | |||
| + | <note tip>En cas de problème penser à vérifier qu'il n'y a pas un firewall qui bloque le flux.</ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Utilisation SSL ===== | ||
| + | |||
| + | ==== Activation ==== | ||
| + | |||
| + | === Ubuntu === | ||
| + | |||
| + | <code bash> | ||
| + | sudo a2enmod ssl | ||
| + | </ | ||
| + | |||
| + | === Redhat/ | ||
| + | |||
| + | <code bash> | ||
| + | sudo dnf install mod_ssl | ||
| + | </ | ||
| + | |||
| + | ==== Double authentification ==== | ||
| + | |||
| + | Ajouter une directive <color # | ||
| + | |||
| + | ==== Générer un certificat autosigné ==== | ||
| + | |||
| + | === Installer openssl === | ||
| + | |||
| + | sudo apt-get install openssl | ||
| + | |||
| + | === Générer une clef privée === | ||
| + | |||
| + | <code bash> | ||
| + | cd /etc/ssl | ||
| + | sudo openssl genrsa -out cle-privee.pem 2048 | ||
| + | </ | ||
| + | |||
| + | === Générer une demande de signature de certificat (Certificat Signing Request) === | ||
| + | |||
| + | <code bash> | ||
| + | sudo openssl req -new -key cle-privee.pem -out demande-csr.pem | ||
| + | </ | ||
| + | | ||
| + | <note important> | ||
| + | |||
| + | === Visualiser le résultat === | ||
| + | |||
| + | <code bash> | ||
| + | openssl req -text -noout -in demande-csr.pem | ||
| + | </ | ||
| + | |||
| + | === Générer le certificat auto-signé pour 365 jours === | ||
| + | |||
| + | <code bash> | ||
| + | sudo openssl x509 -req -days 365 -in demande-csr.pem -signkey cle-privee.pem -out certificat.pem | ||
| + | </ | ||
| + | | ||
| + | === Visualiser certificat === | ||
| + | |||
| + | <code bash> | ||
| + | openssl x509 -noout -text -in certificat.pem | ||
| + | </ | ||
| + | |||
| + | ==== Générer un certificat signé par une autorité bidon ==== | ||
| + | |||
| + | === Générer la Clé Privée de la CA === | ||
| + | |||
| + | <code bash> | ||
| + | openssl genrsa -out ca.key 2048 | ||
| + | </ | ||
| + | |||
| + | === Créer un Certificat pour la CA === | ||
| + | |||
| + | <code bash> | ||
| + | openssl req -x509 -new -nodes -key ca.key -sha256 -days 3650 -out ca.crt | ||
| + | </ | ||
| + | |||
| + | === Créer la Clé Privée pour le Serveur === | ||
| + | |||
| + | <code bash> | ||
| + | openssl genrsa -out serveur.key 2048 | ||
| + | </ | ||
| + | |||
| + | === Créer une Demande de Signature de Certificat (CSR) pour le Serveur === | ||
| + | |||
| + | <code bash> | ||
| + | openssl req -new -key serveur.key -out serveur.csr | ||
| + | </ | ||
| + | |||
| + | <note important> | ||
| + | |||
| + | === Signer le Certificat du Serveur avec la CA factice === | ||
| + | |||
| + | <code bash> | ||
| + | openssl x509 -req -in serveur.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out serveur.crt -days 365 -sha256 | ||
| + | </ | ||
| + | |||
| + | * Si besoin de générer un certificat avec SAN (Subject Alternative Names), il suffit d' | ||
| + | |||
| + | < | ||
| + | authorityKeyIdentifier=keyid, | ||
| + | basicConstraints=CA: | ||
| + | keyUsage = digitalSignature, | ||
| + | subjectAltName = @alt_names | ||
| + | [alt_names] | ||
| + | IP.1 = 13.36.137.5 | ||
| + | IP.2 = 10.1.3.23 | ||
| + | IP.3 = 127.0.0.1 | ||
| + | DNS.1 = localhost | ||
| + | DNS.2 = e2e-r23-ngrfid | ||
| + | </ | ||
| + | |||
| + | === Combiner la Clé Privée et le Certificat du Serveur === | ||
| + | |||
| + | <code bash> | ||
| + | cat serveur.key serveur.crt > serveur.pem | ||
| + | </ | ||
| + | |||
| + | === Installer le Certificat CA sur les Clients (si nécessaire) === | ||
| + | |||
| + | Pour que les clients reconnaissent le certificat auto-signé comme " | ||
| + | |||
| + | <code bash> | ||
| + | sudo cp ca.crt / | ||
| + | sudo update-ca-certificates | ||
| + | </ | ||
| + | |||
| + | ==== Générer un certificat Let's Encrypt ==== | ||
| + | |||
| + | === Installation certbot === | ||
| + | |||
| + | * Sur Ubuntu 16.04 : | ||
| + | |||
| + | <code bash> | ||
| + | sudo apt-get install software-properties-common | ||
| + | sudo add-apt-repository ppa: | ||
| + | sudo apt-get update | ||
| + | sudo apt-get install python-certbot-apache | ||
| + | </ | ||
| + | |||
| + | * Sur Ubuntu 20.04 : | ||
| + | |||
| + | <code bash> | ||
| + | sudo snap install core | ||
| + | sudo snap install --classic certbot | ||
| + | sudo ln -s / | ||
| + | sudo apt-get install python3-certbot-apache | ||
| + | </ | ||
| + | |||
| + | * Sur Redhat 9 | ||
| + | |||
| + | <code bash> | ||
| + | sudo dnf install https:// | ||
| + | sudo dnf install certbot python3-certbot-apache | ||
| + | </ | ||
| + | |||
| + | === Génération certificat === | ||
| + | |||
| + | sudo certbot --apache certonly | ||
| + | | ||
| + | <note tip> | ||
| + | |||
| + | === Génération certificat pour plusieurs url === | ||
| + | |||
| + | sudo certbot --apache certonly –d url1, | ||
| + | |||
| + | === Renouvellement certificat === | ||
| + | |||
| + | sudo certbot | ||
| + | | ||
| + | <note tip> | ||
| + | |||
| + | === Liste === | ||
| + | |||
| + | sudo certbot certificates | ||
| + | |||
| + | === Suppression === | ||
| + | |||
| + | sudo certbot delete --cert-name <nom> | ||
| + | |||
| + | === Laisser le port 80 fermé === | ||
| + | |||
| + | Pour ne pas laisser le port 80 ouvert : | ||
| + | |||
| + | * Supprimer la ligne Listen 80 du fichier / | ||
| + | * ajouter l' | ||
| + | |||
| + | <code bash> | ||
| + | sudo certbot -q renew --dry-run --standalone | ||
| + | </ | ||
| + | |||
| + | ==== Vérification connexion SSL ==== | ||
| + | |||
| + | <code bash> | ||
| + | openssl s_client -connect @IP:port | ||
| + | </ | ||
| + | |||
| + | ==== Vérification expiration certificat ==== | ||
| + | |||
| + | Il est possible d' | ||
| + | |||
| + | === Vérification certificat local === | ||
| + | |||
| + | <code bash> | ||
| + | ssl-cert-check -d / | ||
| + | </ | ||
| + | |||
| + | === Vérification certificat site distant === | ||
| + | |||
| + | <code bash> | ||
| + | ssl-cert-check -n -p 443 -s <url> | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Sécurisation des entêtes http ===== | ||
| + | |||
| + | ==== Source ==== | ||
| + | |||
| + | [[https:// | ||
| + | [[https:// | ||
| + | [[https:// | ||
| + | |||
| + | ==== Paramètres ==== | ||
| + | |||
| + | === Strict-Transport-Security === | ||
| + | |||
| + | Force la communication en utilisant HTTPS au lieu de HTTP. | ||
| + | |||
| + | === X-Frame-Options === | ||
| + | |||
| + | Indique si le navigateur est autorisé à afficher une page dans un < | ||
| + | |||
| + | * DENY : interdiction complète | ||
| + | * SAMEORIGIN : autorise uniquement depuis le même site | ||
| + | * ALLOW-FROM : autorise depuis un site donné | ||
| + | |||
| + | === X-Content-Type-Options === | ||
| + | |||
| + | Désactive le repérage MIME et force le navigateur à utiliser le type donné dans Content-Type. | ||
| + | |||
| + | === X-XSS-Protection === | ||
| + | |||
| + | Active le filtrage de script intersite. | ||
| + | |||
| + | === Content-Security-Policy === | ||
| + | |||
| + | Contrôle les ressources que l' | ||
| + | |||
| + | Paramètres : | ||
| + | |||
| + | * default-src : valeur par défaut des directives de récupération qui ne sont pas définies explicitement. | ||
| + | * script-src : sources valides pour les fichiers JavaScript. | ||
| + | * img-src : sources valides pour les images et les favicons. | ||
| + | * media-src : sources valides pour les ressources média des éléments < | ||
| + | * frame-src : sources valides pour les éléments qui représentent des contextes de navigation imbriqués, tels que < | ||
| + | * font-src : sources valides pour les polices de caractères chargées depuis @font-face. | ||
| + | * style-src : sources valides pour les feuilles de styles. | ||
| + | |||
| + | Valeurs : | ||
| + | * ' | ||
| + | * ' | ||
| + | * ' | ||
| + | * data: : à éviter | ||
| + | * domaine.xx : uniquement depuis un domaine | ||
| + | * *.domaine.xx : depuis tous les sous domaines | ||
| + | * %%https:// | ||
| + | * %%https:// | ||
| + | | ||
| + | === X-Permitted-Cross-Domain-Policies === | ||
| + | |||
| + | Spécifie si un fichier de règlementation interdomaines (crossdomain.xml) est autorisé. Ce fichier peut définir une règle pour accorder aux clients (comme Adobe Flash Player, Adobe Acrobat, Microsoft Silverlight ou Apache Flex) la permission de gérer des données entre domaines qui seraient autrement restreintes à cause de Same-Origin Policy. | ||
| + | |||
| + | === Referrer-Policy === | ||
| + | |||
| + | Indique quelles informations de provenance envoyées dans l' | ||
| + | |||
| + | === Expect-CT === | ||
| + | |||
| + | Permet de contrôler de manière stricte ou non l' | ||
| + | |||
| + | ==== Implémentation ==== | ||
| + | |||
| + | <code bash> | ||
| + | sudo a2enmod headers | ||
| + | sudo systemctl reload apache2 | ||
| + | </ | ||
| + | |||
| + | <code apache> | ||
| + | Header always set Strict-Transport-Security " | ||
| + | Header always append X-Frame-Options SAMEORIGIN | ||
| + | Header set X-Content-Type-Options nosniff | ||
| + | Header set X-XSS-Protection "1; mode=block" | ||
| + | Header set Content-Security-Policy " | ||
| + | Header set X-Permitted-Cross-Domain-Policies " | ||
| + | Header set Referrer-Policy " | ||
| + | Header set Expect-CT ' | ||
| + | </ | ||
| + | |||
| + | ==== Debug==== | ||
| + | |||
| + | Pour trouver ce qui cloche lorsque la page ne s' | ||
| + | |||
| + | <note tip>Ne pas oublier de vider le cache du navigateur si les modifications ne semble pas prises en compte.</ | ||
| + | |||
| + | ==== Vérification ==== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Restriction d' | ||
| + | |||
| + | ==== Directives ==== | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | AuthName ' | ||
| + | AuthType Digest | ||
| + | AuthDigestDomain / | ||
| + | AuthDigestProvider file | ||
| + | AuthUserFile / | ||
| + | Require valid-user | ||
| + | </ | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Activation auth_digest ==== | ||
| + | |||
| + | sudo a2enmod auth_digest | ||
| + | |||
| + | ==== Création fichier htdigest ==== | ||
| + | |||
| + | htdigest -c / | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
| + | |||
| + | ===== Reverse Proxy ===== | ||
| + | |||
| + | ==== Module supplémentaires ==== | ||
| + | |||
| + | <code bash> | ||
| + | sudo a2enmod proxy proxy_http | ||
| + | </ | ||
| + | |||
| + | ==== Fichier de configuration ==== | ||
| + | |||
| + | <code apache> | ||
| + | < | ||
| + | # site avec 2 chemins d' | ||
| + | < | ||
| + | ServerName site.fr | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | |||
| + | ProxyPreserveHost On | ||
| + | ProxyRequests Off | ||
| + | ProxyVia Off | ||
| + | ProxyPass /chemin1 https:// | ||
| + | ProxyPassReverse /chemin1 https:// | ||
| + | ProxyPass /chemin2 https:// | ||
| + | ProxyPassReverse /chemin2 https:// | ||
| + | |||
| + | SSLEngine on | ||
| + | SSLCertificateFile | ||
| + | SSLCertificateKeyFile / | ||
| + | SSLProxyEngine On | ||
| + | SSLProxyCheckPeerCN Off | ||
| + | SSLProxyCheckPeerName Off | ||
| + | SSLProxyCheckPeerExpire off | ||
| + | SSLProxyVerify none | ||
| + | </ | ||
| + | # websocket avec rewrite | ||
| + | < | ||
| + | ServerName domaine2.fr | ||
| + | ErrorLog ${APACHE_LOG_DIR}/ | ||
| + | CustomLog ${APACHE_LOG_DIR}/ | ||
| + | |||
| + | ProxyPreserveHost On | ||
| + | ProxyRequests Off | ||
| + | ProxyVia Off | ||
| + | ProxyPass / https:// | ||
| + | ProxyPassReverse / https:// | ||
| + | |||
| + | RewriteEngine On | ||
| + | RewriteCond %{HTTP: | ||
| + | RewriteRule ^/ | ||
| + | |||
| + | SSLEngine on | ||
| + | SSLCertificateFile | ||
| + | SSLCertificateKeyFile / | ||
| + | SSLProxyEngine On | ||
| + | SSLProxyCheckPeerCN Off | ||
| + | SSLProxyCheckPeerName Off | ||
| + | SSLProxyCheckPeerExpire off | ||
| + | SSLProxyVerify none | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | <note tip>La redirection vers le serveur n'est pas obligatoirement en https (si dans le cadre d'un réseau interne). Le tunnel crypté peut s' | ||
| + | |||
| + | ==== Dépannage ==== | ||
| + | |||
| + | === [proxy: | ||
| + | |||
| + | Sur Redhat SELinux bloque la connexion, il faut passer la commande suivante : | ||
| + | |||
| + | <code bash> | ||
| + | sudo setsebool -P httpd_can_network_connect 1 | ||
| + | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | [[all: | ||
