16.Apacheの管理と最適化
1. Apacheが有効になっています:
セルフスタートを有効にするようにApacheを設定し、次に有効にして、ファイアウォールで信頼できるサービスとしてhttpを追加します。2。Apacheの
基本情報:
3. Apacheの基本構成:
3-1:Apacheポートの変更:
vim /etc/httpd/conf/httpd.conf
Listen 8080
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
デフォルトのポートを8080に変更します。アクセス効果は、図
3-2に示すとおりです。デフォルトのリリースファイル:
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
3-3。デフォルトのリリースディレクトリ:
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos/html"
<Directory "/westos/html">
Require all granted 必须要有认证
</Directory>
systemctl restart httpd
4. Apacheアクセス制御:
4-1:クライアントIPに基づくアクセス制御:
#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,禁止...访问;
図に示すように、構成は次のとおりです。154のオープンアクセス許可のみ:
4-2。ユーザー認証に基づくアクセス制御:
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参数否则会覆盖源文件内容
注:<Directory "/ var / www / html / westos">でユーザーアクセス制御を有効にするためのファイルを指定する必要があります。
5.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>
テストするときは、/ etc / hostsアドレス解決ファイルを変更することを忘れないでください。
6.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. 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. ApacheのWebページ書き換え機能:
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. Squid forward proxy:
仮想マシンにネットワーク接続はありませんが、Webページを開くことはできます。
10.squidリバースプロキシ: