Linux(16)——Apache
Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一。
1. Apache的安装与启用
- 安装
[root@rhserver Desktop]# dnf install httpd.x86_64 -y
- 启用
[root@rhserver Desktop]# systemctl enable --now httpd #开启服务并设定服务位开机启动
[root@rhserver Desktop]# firewall-cmd --list-all #查看防火墙信息
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=http #在防火墙中永久开启http访问
[root@rhserver Desktop]# firewall-cmd --permanent --add-service=https #在防火墙中永久开启https访问
[root@rhserver Desktop]# firewall-cmd --reload #刷新防火墙使设定生效
2. Apache的基本配置
服务名称: httpd
主配置文件: /etc/httpd/conf/httpd.conf
子配置文件: /etc/httpd/conf.d/*.conf
默认发布目录: /var/www/html
默认发布文件: index.html
默认端口: 80 #http
443 #https
用户: apache
日志: /etc/httpd/logs
- 端口修改
修改主配置文件
[root@rhserver Desktop]# vim /etc/httpd/conf/httpd.conf
Listen 8080
[root@rhserver Desktop]# firewall-cmd --permanent --add-port=8080/tcp
[root@rhserver Desktop]# firewall-cmd --reload
[root@rhserver Desktop]# systemctl restart httpd
-
默认发布文件修改
修改主配置文件,访问时优先在发布目录下寻找xxx.htmlDirectoryIndex xxx.html index.html
-
默认发布目录修改
修改主配置文件,并添加内容DocumentRoot "/var/www/rhel" #修改默认发布路径 <Directory "/var/www/rhel"> #设定发布路径权限 Require all granted </Directory>
3. Apache访问控制
-
基于IP的访问控制
Apache支持黑名单或白名单方式的IP访问控制,在主配置文件中向发布目录内增加黑白名单内容即可实现。# 先Deny后Allow,为白名单,只允许指定IP访问 <Directory "/var/www/html/rhel"> Order Deny,Allow Allow from 192.168.31.10 Deny from All </Directory> # 先Allow后Deny,为黑名单,阻止指定IP访问 <Directory "/var/www/html/westos"> Order Allow,Deny Allow from All Deny from 192.168.31.10 </Directory>
-
基于用户认证访问控制
首先生成用户认证文件,然后在主配置文件发布目录下增加用户认证部分内容。
[root@rhserver Desktop]# htpasswd -c /etc/httpd/htpasswdfile admin #新建认证文件
[root@rhserver Desktop]# htpasswd -m /etc/httpd/htpasswdfile admin #向认证文件中增加用户
<Directory "/var/www/html/rhel">
AuthUserfile /etc/httpd/htpasswdfile #指定认证文件
AuthName "Please input your name and password" #提示语
AuthType basic #认证类型
Require user admin #允许admin用户
Require valid-user #允许所有用户通过认证
</Directory>
4. 虚拟主机
虚拟主机开启后,客户机可以通过不同域名访问服务器。
创建所需的不同域名的发布目录以及发布文件;编辑 /etc/httpd/xxx.conf增加指定的虚拟主机内容,在客户机配置地址解析,即可成功访问。
[root@rhserver Desktop]# mkdir -p /var/www/html/{us,jp}
[root@rhserver Desktop]# echo "welcome to Japan " >/var/www/html/jp/index.html
[root@rhserver Desktop]# echo "welcome to US " >/var/www/html/us/index.html
[root@rhserver Desktop]# echo "hello world" > /var/www/html/index.html
[root@rhserver Desktop]# vim /etc/httpd/host.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName us.gov #虚拟主机域名
DocumentRoot "/var/www/www/html/us" #虚拟主机发布目录
CustomLog logs/us.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName jp.gov
DocumentRoot "/var/www/html/jp"
CustomLog logs/jp.log combined
</VirtualHost>
5. 不同网页语言支持
Apache支持 php/perl/python,三种语言。其中php与python需要安装语言环境。
- PHP
创建php文件,为php文件添加可执行权限,配置成功后在客户端访问该php文件即可。
[root@rhserver Desktop]# dnf install php -y
[root@rhserver Desktop]# vim /var/www/html/index.php
<?php
phpinfo();
?>
[root@rhclient Desktop]# systemctl restart httpd
- Perl + CGI
创建cgi文件,在主配置文件中添加对应内容,为CGI文件添加可执行权限。在客户端访问cgi文件即可。
[root@rhserver Desktop]# mkdir /var/www/html/cgi
[root@rhserver Desktop]# vim /var/www/html/cgi/index.cgi
# CGI 文件内容
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
# 配置文件添加内容
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
- Python + WSGI
安装Python支持,创建WSGI文件并添加可执行权限,在虚拟主机配置文件中添加对应内容,在客户端访问WSGI文件即可。
[root@rhserver Desktop]# dnf install python3-mod_wsgi
[root@rhserver Desktop]# vim /var/www/html/wsgi/index.wsgi
# WSGI 文件内容
def application(env, functiion):
functiion('200 ok',[('Content-Type', 'text/html')])
return [b'This is a test ']
# 虚拟主机文件添加内容
<VirtualHost *:80>
ServerName wsgi.gov
WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
6. 加密访问(HTTPS)
Apache通过openssl服务可以实现HTTPS访问。
# 安装ssl并生成加密密钥
[root@rhserver Desktop]# dnf install mod_ssl -y
[root@rhserver Desktop]# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/gov.key -x509 -days 365 -out /etc/httpd/gov.crt
# 编辑虚拟主机配置文件 添加443端口以及80端口网页重写
[root@rhserver Desktop]# vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName login.gov
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
<VirtualHost *:443>
ServerName login.gov
DocumentRoot "/var/www/html/login"
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/gov.crt
SSLCertificateKeyFile /etc/httpd/gov.key
</VirtualHost>
# 编辑SSL配置文件,指定密钥地址
[root@rhserver Desktop]# vim /etc/httpd/conf.d/ssl.conf
# 修改文件内容
SSLCertificateFile /etc/httpd/gov.crt
SSLCertificateKeyFile /etc/httpd/gov.key
7. 代理服务器
- 正向代理
正向代理主机可以使客户机通过代理对指定网络进行访问,而客户机无法直接访问指定网络。
[root@rhserver Desktop]# dnf install squid -y #安装squid
[root@rhserver Desktop]# vim /etc/squid/squid.conf #编辑配置文件
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256
[root@rhserver Desktop]# systemctl restart squid
[root@rhserver Desktop]# firewall-cmd --permanent --add-port=3128/tcp
[root@rhserver Desktop]# firewall-cmd --reload
在客户机浏览器设置代理IP,端口为3128
- 反向代理
客户端可以通过代理端访问到服务器端,一般由服务提供商进行配置,向客户提供服务。
[root@rhclient Desktop]# vim /etc/squid/squid.conf
# vhost 支持虚拟域名 vport 支持虚拟端口
http_port 80 vhost vport
# 取得数据的服务端IP ,端口
cache_peer 172.25.254.20 parent 80 0 proxy-only
[root@rhserver Desktop]# systemctl restart squid
[root@rhserver Desktop]# firewall-cmd --permanent --add-server=http,squid
[root@rhserver Desktop]# firewall-cmd --reload
8. 配置练习
-
虚拟主机
创建不同的发布目录以及文件
编辑虚拟主机配置文件
客户机解析文件
-
多语言脚本
-
PHP
安装PHP支持,编写网页。
客户机访问
-
Perl + CGI
编写CGI文件,编辑主配置文件。
客户机访问。
-
Python + WSGI
安装python3-mod_wsgi.x86_64;创建wsgi发布目录与发布文件,编辑文件内容并赋予执行权限。
编辑虚拟主机配置文件
客户机添加地址解析,并进行访问。
-
HTTPS加密
安装ssl,生成认证文件。
编辑httpd的SSL配置文件,修改认证文件路径。
修改虚拟主机配置文件,实现网页重写。
客户机直接访问IP
-
Squid
-
反向代理
服务器IP为192.168.158.129;代理服务器IP为192.168.158.128;
在代理服务器安装Squid,并进行配置。
客户机访问代理服务器,显示为主服务器内容
-
正向代理
在代理服务器端编写配置文件,关闭防火墙
在客户端浏览器设置代理
可正常上网