1.Apache的简介及服务安装和启用
在web被访问时通常使用http://的方式,http://是一种超文本传输协议。apache是提供这种传输协议的软件。
http:// 超文本传输协议提供软件有:
Apache、nginx、stgw、jfe、Tengine
[root@westos_Apache ~]# dnf install httpd -y 安装apache
[root@westos_Apache ~]# firewall-cmd --permanent --add-service=http 允许火墙通过http服务
success
[root@westos_Apache ~]# firewall-cmd --permanent --add-service=https 允许火墙通过https服务
success
[root@westos_Apache ~]# firewall-cmd --reload 重启火墙
success
[root@westos_Apache ~]# systemctl enable --now httpd 启动apache
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
可以访问apache测试页
写一个自己的测试页
[root@westos_Apache ~]# vim /var/www/html/index.html apche默认发布目录建立index.html
1 hello westos
2 、Apache的基本信息
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf httpd主配置文件
[root@westos_Apache ~]# vim /etc/httpd/conf.d/*.conf httpd子配置文件
[root@westos_Apache ~]# cd /var/www/html/ httpd默认发布目录
[root@westos_Apache ~]# netstat -antlupe | grep httpd httpd端口为80
tcp6 0 0 :::80 :::* LISTEN 0 41538 2714/httpd
[root@westos_Apache ~]# cd /etc/httpd/logs/ httpd日志
[root@westos_Apache logs]# ls
access_log(访问日志) error_log(错误日志)
3、Apache 配置文件设置
如何更改端口
[root@westos_Apache logs]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
45 Listen 8080 将80端口改为8080
[root@westos_Apache logs]# netstat -antlupe | grep httpd 端口变为8080
tcp6 0 0 :::8080 :::* LISTEN 0 45202 3496/httpd
如何修改默认发布文件
[root@westos_Apache ~]# vim /var/www/html/westos.html
1 westos's page
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
166 <IfModule dir_module>
167 DirectoryIndex westos.html index.html 添加westos.html 文件
168 </IfModule>
[root@westos_Apache ~]# systemctl restart httpd 重启服务
访问172.25.254.100
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
166 <IfModule dir_module>
167 DirectoryIndex westos.html index.html 删除westos.html 文件
168 </IfModule>
[root@westos_Apache ~]# systemctl restart httpd 重启服务
删除后默认访问index.html ,需要访问westos.html则需要指定
如何修改默认发布目录
[root@westos_Apache ~]# mkdir /wetsosdir 建立目录
[root@westos_Apache ~]# vim /wetsosdir/index.html index.html文件中添加westos_apache
1 westos_apache
[root@westos_Apache ~]# semanage fcontext -a -t httpd_sys_content_t '/westosdir(/.*)?' 安全上下文符合程序访问的需求
[root@westos_Apache ~]# restorecon -RvvF /westosdir 刷新
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
123#DocumentRoot "/var/www/html" 改成 DocumentRoot "/westosdir" 将默认发布目录改成/westosdir
124 <Directory "/westosdir"> 在apache当中 授权这个目录可以被其他人访问
Require all granted
</Directory>
[root@westos_Apache ~]# systemctl restart httpd
4、Apache服务访问控制
[root@westos_Apache ~]# mkdir /var/www/html/westos 建立目录
[root@westos_Apache ~]# vim /var/www/html/westos/index.html 目录里建立index.html文件
1 /var/www/html/westos
由于westos目录谁都可以访问,如何进行访问控制,
如何让自己不可以访问,别人可以访问
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf 编辑配置文件
124 <Directory "/var/www/html/westos">
125 Order Allow,Deny 先访问Allow,再访问Deny
126 Allow from all 允许所有人访问
127 Deny from 172.25.254.50 不允许172.25.254.50自己访问,别人可以访问
128 </Directory>
如何让自己可以访问,别人不可以访问
[root@westos_Apache ~]# vim /etc/httpd/conf/httpd.conf
124 <Directory "/var/www/html/westos">
125 Order Deny,Allow
126 Deny from all 所有人拒绝
127 Allow from 172.25.254.50 可以让172.25.254.50访问
128 </Directory>
如何允许访问多个了?
man 5 httpd.conf查看不到
[root@westos_Apache ~]# dnf install httpd-manual -y 安装httpd手册
[root@westos_Apache ~]# systemctl restart httpd 重启httpd服务
查看手册
Allow from 172.25.254.50 如果是多个ip用空格隔开
如何用账号密码设定用户访问控制?
[root@westos_Apache ~]# cd /etc/httpd/
[root@westos_Apache httpd]# htpasswd -cm .htpasswd admin -c创建 -m指定认证文件 admin表示用户
New password:
Re-type new password:
Adding password for user admin
[root@westos_Apache httpd]# cat .htpasswd
admin:$apr1$TlfV1tWp$sAMFHLp8d85i/tV6G0mgD/
root@westos_Apache httpd]# htpasswd -m .htpasswd admin1 如果用户存在创建新的用户将-c去掉,否则会覆盖子前的
New password:
Re-type new password:
Adding password for user admin1
[root@westos_Apache httpd]# cat .htpasswd 两个用户都在
admin:$apr1$TlfV1tWp$sAMFHLp8d85i/tV6G0mgD/
admin1:$apr1$J6N/iNuO$Hq2Z1EN5CbGVh7R2Vcd3d.
admin用户可以使用和访问westos:
[root@westos_apache ~]# vim /etc/httpd/conf/httpd.conf
124 <Directory "/var/www/html/westos">
125 AuthUserFile /etc/httpd/.htpasswd 认证文件
126 AuthName "Please input username and password" 提示语
127 AuthType basic 最基本的
128 Require user admin admin用户可以使用和访问westos
129 # Require valid-user 所有在.htpasswd的用户都可以访问和使用westos (128 和 129 分别时两种访问方式)
130 </Directory>
[root@westos_Apache httpd]# systemctl restart httpd 重启服务
.htpasswd文件里所有用户都可以访问westos
[root@westos_Apache httpd]# vim /etc/httpd/conf/httpd.conf
124 <Directory "/var/www/html/westos">
125 AuthUserFile /etc/httpd/.htpasswd
126 AuthName "Please input username and password"
127 AuthType basic
128 # Require user admin
129 Require valid-user 表示.htpasswd文件里所有用户都可以访问westos
[root@westos_Apache httpd]# systemctl restart httpd 重启服务
5、Apahce虚拟主机的设定
如何让一台服务器一个ip建立多个站点
实验环境
[root@westos_Apache ~]# mkdir -p /var/www/vhost/westos.org/{news,music,bai,chengxing} j 建立三个目录
[root@westos_Apache westos.org]# echo news.westos.org >/var/www/vhost/westos.org/news/index.html
[root@westos_Apache westos.org]# echo music.westos.org >/var/www/vhost/westos.org/music/index.html
[root@westos_Apache westos.org]# echo chengxing.westos.org >/var/www/vhost/westos.org/chengxing/index.html
[root@westos_Apache westos.org]# vim /etc/httpd/conf.d/vhosts.conf 编辑子配置文件
1 <VirtualHost _default_:80> 默认站点
2 DocumentRoot /var/www/html 发布目录
3 CustomLog logs/default.log combined 日志; combined表示混合型日志(有错误日志,运行日志)
4 </VirtualHost>
5
6 <VirtualHost *:80> music站点
7 ServerName music.westos.org
8 DocumentRoot /var/www/vhost/westos.org/music
9 CustomLog logs/music.log combined
10 </VirtualHost>
11
12 <VirtualHost *:80> news站点
13 ServerName news.westos.org 域名
14 DocumentRoot /var/www/vhost/westos.org/news
15 CustomLog logs/news.log combined
16 </VirtualHost>
17
18 <VirtualHost *:80> chengxing站点
19 ServerName chengxing.westos.org 域名
20 DocumentRoot /var/www/vhost/westos.org/chengxing 默认发布目录
21 CustomLog logs/shengchan.log combined
22 </VirtualHost>
[root@westos_apache westos.org]# systemctl restart httpd.service 重启服务
[root@foundation50 Desktop]# vim /etc/hosts 在有浏览器的主机上进行地址解析
172.25.254.100 www.westos.org music.westos.org news.westos.org chengxing.westos.org
6、Apache对于语言的支持
php语言
[root@westos_Apache html]# vim index.php 编写php测试页
[root@westos_Apache html]# dnf install php.x86_64 -y 安装php软件
[root@westos_Apache html]# systemctl restart httpd 重启apache服务
[root@westos_Apache html]# cat index.php
<?php
phpinfo();
?>
访问172.25.254.100/index.php
cgi(通用网关接口)(语言perl)
APACHE不懂啥是程序,所以要找一个懂程序的把程序执行完,将结果拿出来给apache,apache给别人看,执行程序的叫cgi(通用网关接口)
cgi写法已经设置可以参考手册
实验环境:
[root@westos_Apache html]# mkdir cgi 建立目录
[root@westos_Apache html]# cd cgi/
[root@westos_Apache cgi]# vim index.cgi
1 #!/usr/bin/perl
2 print "Content-type: text/html\n\n";
3 print 'date';
[root@westos_Apache cgi]# chmod +x index.cgi 给文件执行的权限
[root@westos_Apache cgi]# ls -Zd /var/www/cgi-bin/ 查看安全上下文
system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/
[root@westos_Apache cgi]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?'^C 指定安全上下文
[root@westos_Apache cgi]# restorecon -RvvF /var/www/html/cgi/ 重新初始化
[root@westos_Apache cgi]# ./index.cgi peri代码可以运行
Content-type: text/html
date
访问发现代码直接裸露出来,apache不识别代码,当作文本
所以需要编辑子配置文件
[root@westos_Apache cgi]# vim /etc/httpd/conf.d/vhosts.conf
24 <Directory "/var/www/html/cgi"> 表示遇到cgi-script .cgi 形式的文件进行执行操作
25 Options +ExecCGI 执行动作
26 AddHandler cgi-script .cgi 触发器
27 </Directory>
[root@westos_Apache cgi]# systemctl restart httpd 重启服务
访问172.25.254.100/index.cgi
wsgi语言
[root@westos_Apache html]# vim index.wsgi
1 def application(env, westos):
2 westos( ‘200 ok’,[(‘Content-Type’, ‘text/html’)]) 需要缩进
3 return [b’hello wsgi’] 需要缩进
[root@westos_Apache html]# chmod +x index.wsgi 加执行权力
[root@westos_Apache html]# dnf install python3-mod_wsgi.x86_64 -y 安装wsgi插件
[root@westos_Apache html]# vim /etc/httpd/conf.d/vhosts.conf 编辑子配置文件,添加虚拟主机
25 <VirtualHost *:80>
26 ServerName wsgi.westos.org
27 WSGIScriptAlias / /var/www/html/index.wsgi 直接访问默认发布站点
29 </VirtualHost>
[root@westos_Apache html]# systemctl restart httpd 重启服务
[root@foundation50 Desktop]# vim /etc/hosts 添加解析
wsgi.westos.org
[root@westos_Apache html]# setenforce 0 变成警告模式
7、Apache的加密访问
https 表示加密访问
[root@westos_Apache html]# dnf install mod_ssl -y 安装加密插件
[root@westos_Apache tls]# systemctl restart httpd 重启服务
[root@westos_Apache html]# cd /etc/pki/tls 默认证书和锁存放位置
[root@westos_Apache tls]# ls
cert.pem certs (证书) ct_log_list.cnf misc openssl.cnf private(锁)
用https://172.25.254.100 访问,发现认证信息都是默认的不是我们想要的
需要生成自己的钥匙和锁
[root@westos_apache ~]# mkdir /etc/httpd/tls
[root@westos_Apache ~]# openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/westos.org.key -x509 -days 365-out /etc/httpd/tls/westos.org.crt
req表示发出请求, rsa:2048表示加密位数,-sha256加密方式,-x509证书格式
Generating a RSA private key
......+++++
.................................................+++++
writing new private key to '/etc/httpd/tls/westos.org.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN 国家
State or Province Name (full name) []:Shaanxi 省分
Locality Name (eg, city) [Default City]:Xi,an 城市
Organization Name (eg, company) [Default Company Ltd]:WESTOS 组织机构
Organizational Unit Name (eg, section) []:Linux 部门
Common Name (eg, your name or your server's hostname) []:www.westos.org 站点
Email Address []:admin@westos.org 邮箱
[root@westos_Apache ~]# ls /etc/httpd/tls/
westos.org.crt westos.org.key 生成了自己的证书和锁
注意但是不是访问所有页面都需要加密,如何指定加密?
[root@westos_Apache ~]# mkdir /var/www/vhost/westos.org/login 创建目录
[root@westos_Apache ~]# vim /var/www/vhost/westos.org/login/index.html 创建index.html文件
login.westos.org
[root@westos_apache conf.d]# vim /etc/httpd/conf.d/ssl.conf 加密配置文件,添加参数可以参考
[root@westos_Apache conf.d]# vim /etc/httpd/conf.d/vhosts.conf 添加参数,编辑配置文件
30 <VirtualHost *:443> 添加虚拟主机,https端口为443
31 Servername login.westos.org 域名
32 DocumentRoot /var/www/vhost/westos.org/login 默认发布目录,就是刚才建立的目录
33 CustomLog logs/login.log combined 日志
34 SSLEngine on 加密功能 打开
35 SSLCertificateFile /etc/httpd/tls/westos.org.crt 证书
36 SSLCertificatekeyFile /etc/httpd/tls/westos.org.key 锁
37 </VirtualHost>
[root@westos_Apache conf.d]# systemctl restart httpd 重启服务
[root@foundation50 Desktop]# vim /etc/hosts 做解析
login.westos.org
访问:
https://login.westos,org
login.westos.org
可以发现认证信息都是自己的
但是每次访问的时候都要输入https:// 麻烦,客户不会这样输入,需要将客户访问的80端口自动转换到443端口,此时就要设置一个网页重写规则
[root@westos_Apache conf.d]# vim /etc/httpd/conf.d/vhosts.conf 添加重写规则
31 <VirtualHost *:80>
32 ServerName login.westos.org
33 RewriteEngine On 重写功能打开
34 RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 解释如下图:
35 </VirtualHost>
[root@westos_Apache conf.d]# systemctl restart httpd 重启服务
访问 login.westos.org
8.squid正向代理
实验环境建立
需要两台虚拟机,squid虚拟机可以上网,westosa 不可以上网
如何让squid虚拟机成为代理,让westosa可以上网???
[root@squid ~]# yum install squid -y 安装代理服务
[root@squid ~]# vim /etc/squid/squid.conf 编辑配置文件
59 http_access allow all 更改59行为allow允许所有
65 cache_dir ufs /var/spool/squid 100 16 256 将65行放开,打开数据存放位置
[root@squid ~]# systemctl restart squid.service 重启服务
[root@squid ~]# netstat -antlupe | grep squid 3218端口已经打开
tcp6 0 0 :::3128 :::* LISTEN 0 47802 3381/(squid-1)
udp 0 0 0.0.0.0:52405 0.0.0.0:* 23 47797 3381/(squid-1)
udp6 0 0 :::48420 :::* 23 47796 3381/(squid-1)
[root@squid ~]# firewall-cmd --permanent --add-service=squid
success
[root@squid ~]# firewall-cmd --reload
success
在westosa虚拟机中打开浏览器并设置浏览器
浏览器可以访问但是westosa虚拟机无法ping通
[root@westosa ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known
9、squid反向加速代理
如何加速客户对服务器的访问
客户访问企业服务器,企业不会直接访问总部服务器,企业会在所在区域放一台缓存服务器,客户访问缓存服务器,缓存服务器有直接拿,没有在去总部服务器去取,这样大大提高了客户访问的速率,这样的传输方式叫cdn
[root@westosa ~]# dnf install httpd 总部服务器上安装apache
[root@westosa ~]# systemctl enable --now httpd 启动服务
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@westosa ~]# firewall-cmd --permanent --add-service=http
success
[root@westosa ~]# firewall-cmd --reload
success
[root@westosa ~]# echo 172.25.254.200 >/var/www/html/index.html 测试页
squid为缓存服务器(没有apache)
[root@squid ~]# less /usr/share/doc/squid/squid.conf.documented 查看squid手册
[root@squid ~]# vim /etc/squid/squid.conf 编辑配置文件
62 http_port 80 vhost vport 更改,支持虚拟主机,虚拟端口
63 cache_peer 172.25.254.200 parent 80 0 proxy-only 添加 172.25.254.200 资源主机 parent 表示父集 0 对代理备份的主机端口,没有就为0 proxy-only 表示代理缓存
[root@squid ~]# firewall-cmd --permanent --add-service=http 虽然没有安装http ,需要添加超文本传输协议
在真机上访问缓存服务器172.25.254.100 拿的是总部服务器数据