16. Gestion et optimisation Apache
1. Apache est activé:
Configurez Apache pour activer l'auto-démarrage, puis activez-le, ajoutez http comme service de confiance dans le pare-feu:
2. Informations de base d'Apache:
3. La configuration de base d'Apache:
3-1: Modification du port Apache:
vim /etc/httpd/conf/httpd.conf
Listen 8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
Modifiez le port par défaut sur 8080, l'effet d'accès est comme indiqué dans la figure:
3-2. Le fichier de version par défaut:
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
3-3. Le répertoire de version par défaut:
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "/westos/html">
Require all granted 必须要有认证
</Directory>
systemctl restart httpd
4. Contrôle d'accès Apache:
4-1: Contrôle d'accès basé sur l'IP du client:
#ip白名单#
<Directory "/var/www/html/westos">
Order Deny,Allow
Allow from ip...
Deny from all
</Directory>
先读deny,禁止所有人访问,后读allow,允许ip...访问;
#ip黑名单#
<Directory "/var/www/html/westos">
Order Allow,Deny
Allow from All
Deny from ip...
</Directory>
先读allow,允许所有人访问,后读deny,禁止...访问;
Comme le montre la figure, la configuration est: seulement 154 autorisations d'accès ouvert:
4-2. Contrôle d'accès basé sur l'authentification de l'utilisateur:
vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
AuthUserFile /etc/httpd/.htpasswdfile ##指定认证文件
AuthName "Please input your name and password" ##认证提示语
AuthType basic ##认证类型
Require user admin ##允许通过的认证用户 2选1
Require valid-user ##允许所有用户通过认证 2选1
</Directory>
htpasswd -cm /etc/httpd/.htpasswd admin ##生成认证文件(添加用户时需要去掉-c参数,不然之前的用户信息会被覆盖)
注意:
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
Remarque: vous devez spécifier le fichier pour activer le contrôle d'accès utilisateur dans <Directory "/ var / www / html / westos">:
5. hôte virtuel Apache:
mkdir -p /var/www/westos.com/{
news,wenku}
echo "wenku's page" >/var/www/westos.com/wenku/index.html
echo "news's page" > /var/www/westos.com/news/index.html
echo "default's page" > /var/www/html/index.html
vim /etc/httpd/Vhost.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName wenku.westos.com
DocumentRoot "/var/www/westos.com/wenku"
CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
Lors du test, n'oubliez pas de modifier le fichier de résolution d'adresse / etc / hosts:
6. prise en charge de la langue Apache:
#php#
vim /var/www/html/index.php
<?php
phpinfo();
?>
dnf install php -y
systemctl restart httpd
firefox http://192.168.0.11/index.php
#cgi(perl)#
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi
chmod +X /var/www/html/cgidir/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
systemctl restart httpd
firefox http://192.168.0.11/cgidir/index.cgi
wsgi(python)
mkdir /var/www/html/wsgidir
vim /var/www/html/wsgidir/index.wsgi
def application(env, westos):
westos('200 ok',[('Content-Type','text/html')])
return [b"'hello wsgi'"]
chmod +X /var/www/html/wsgidir/index.wsgi
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName wsgi.westos.org
WSGIScriptAlias / /var/www/html//wsgi-scripts/index.wsgi
</VirtualHost>
dnf install python3 -y
systemctl restart httpd
7. Accès chiffré d'Apache (https):
dnf install mod_ssl -y 下载安装加密用的插件
openssl genrsa -out /etc/pki/tls/private/www.westos.com.key 2048 生成私钥(位置随意)
openssl req -new -key /etc/pki/tls/private/www.westos.com.key \
-out /etc/pki/tls/certs/www.westos.com.csr 生成证书签名文件
openssl x509 -req -days 365 -in \
/etc/pki/tls/certs/www.westos.com.csr \
-signkey /etc/pki/tls/private/www.westos.com.key \
-out /etc/pki/tls/certs/www.westos.com.crt 生成证书
x509 证书格式
-req 请求
-in 加载签证名称
-signkey /etc/pki/tls/private/www.westos.com.key
8. Fonction de réécriture de page Web d'Apache:
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName login.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{
HTTP_HOST}$1 ^(/.*)$表示用户在地址栏中输入的内容;$1表示参数后的第一个字符;%{
HTTP_HOST}表示在用户主机中输入
</VirtualHost>
<VirtualHost *:443>
ServerName login.westos.com
DocumentRoot /www/westos.com/login
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</VirtualHost>
systemctl restart httpd
9. Proxy de transfert Squid:
Il n'y a pas de connexion réseau dans la machine virtuelle, mais la page Web peut toujours être ouverte:
10.squid proxy inverse: