Linux--RH254---unit 9 apache web服务

unit 9 apache
一、apache的安装

yum install httpd -y          ##安装apache
systemctl start httpd         ##开启apache
systemctl enable httpd        ##开机启动apache
systemctl stop firewalld      ##关闭防火墙
systemctl disable firewalld   ##开机关闭防火墙



二、apache基本信息
1.apache的默认发布文件
index.html
2.apache的配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
3.apache的默认发布目录
/var/www/html
4.apache的默认端口
80

三、apache的基本配置
1.修改默认发布文件

vim /var/www/html/westos.html    ##编辑westos.html文件

<h1>westos</h1>


vim /var/www/html/index.html     ##编辑index.html文件

<h1>linux</h1>


vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件

164     DirectoryIndex westos.html index.html    ##默认发布文件修改为westos.html


systemctl restart httpd          ##重启httpd

测试

当访问172.25.254.131时,访问结果是/var/www/html/westos.html文件的内容



2.修改默认发布目录
##当selinux是disable状态##
mkdir /westos/www/test -p          ##创建/westos/www/test目录
vim /westos/www/test/westos.html   ##编辑/westos/www/test目录下westos.html文件
<h1>hello</h1>
vim /etc/httpd/conf/httpd.conf      ##修改apache配置文件
119 #DocumentRoot "/var/www/html"   ##注释掉原来的默认发布目录
120 DocumentRoot "/westos/www/test" ##指定默认发布目录为/westos/www/test
121 <Directory "/westos/www/test">  
122     Require all granted
123 </Directory>

systemctl restart httpd


测试

当访问172.25.254.131时,访问结果是/westos/www/test/westos.html文件的内容


##当selinux是enforcing状态##
vim /etc/httpd/conf/httpd.conf      ##修改apache配置文件
119 #DocumentRoot "/var/www/html"   ##注释掉原来的默认发布目录
120 DocumentRoot "/westos/www/test" ##指定默认发布目录为/westos/www/test
121 <Directory "/westos/www/test">  
122     Require all granted
123 </Directory>
systemctl restart httpd
semanage fcontext -a -t httpd_sys_content_t '/westos(/.*)?' ##修改默认的文件上下文

restorecon -RvvF /westos                                    ##刷新



测试

当访问172.25.254.131时,访问结果是/westos/www/test/westos.html文件的内容



3.apache的访问控制
vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
DocumentRoot "/var/www/html"             ##使用原来的默认发布目录
#DocumentRoot "/westos/www/test"         ##注释掉/westos/www/test发布目录
systemctl restart httpd                  ##重启httpd
cd /var/www/html/                        ##切换至/var/www/html/目录下
mkdir admin                              ##创建目录admin
cd admin/                                ##切换至该目录下
vim index.html                           ##编辑index.html文件

<h1>admin</h1>



测试

当访问172.25.254.131/admin时,访问结果是/var/www/html/admin/index.html文件的内容



##设定ip的访问
*)vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     Order Allow,Deny                       ##先执行Allow,再执行Deny
126     Allow from All                         ##Allow所有用户访问
127     Deny from 172.25.254.131               ##拒绝172.24.254.131用户访问
128 </Directory>

systemctl restart httpd                  ##重启httpd


测试

当访问172.25.254.131/admin时,允许所有用户访问但是拒绝172.25.254.131用户访问



*)vim /etc/httpd/conf/httpd.conf           ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     Order Deny,Allow                       ##先执行Deny,再执行Allow
126     Allow from 172.25.254.131              ##允许172.24.254.131用户访问
127     Deny from All                          ##拒绝所有用户访问
128 </Directory>

systemctl restart httpd                  ##重启httpd



测试

当访问172.25.254.131/admin时,只允许172.25.254.131用户访问



##设定用户的访问##
htpasswd -cm /etc/httpd/accessuser admin               ##用admin账户创建Apache密码文件(第一次创建密码文件需要有c参数,m表示使用md5加密)
cat /etc/httpd/accessuser                              ##查看用户账户和密码
vim /etc/httpd/conf/httpd.conf                         ##修改apache配置文件
124 <Directory "/var/www/html/admin">
125     AuthUserFile /etc/httpd/accessuser                 ##用户认证文件
126     AuthName "please input your name and password!!"   ##用户认证提示信息
127     AuthType basic                                     ##认证类型
128     Require valid-user                                 ##认证用户,认证文件中的所有用户均可访问
        [Require user admin]                               ##认证用户,只允许认证文件中的admin用户访问,和上面一行二者选其一即可
129 </Directory>

systemctl restart httpd                  ##重启httpd



测试

当访问172.25.254.131/admin时,需填写用户认证信息



4.apache语言支持
*)php html cgi
默认支持html语言
*)php语言
cd /var/www/html/             ##切换至/var/www/html/
vim index.php                 ##编辑index.php
<?php
        phpinfo();
?>
yum install php -y            ##安装php

systemctl restart httpd       ##重启httpd


测试

当访问172.25.254.131/index.php时,访问结果为


*)cgi语言
cd /var/www/html              ##切换至/var/www/html/
mkdir /var/www/html/cgi       ##创建cgi目录
cd /var/www/html/cgi          ##切换到cgi目录下
vim index.cgi                 ##编写index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
chmod +x index.cgi               ##给index.cgi添加可执行权限
vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件
130 <Directory "/var/www/html/cgi">
131         Options +ExecCGI
132         AddHandler cgi-script .cgi
133 </Directory>

systemctl restart httpd          ##重启httpd

测试

当访问172.25.254.131/cgi/index.cgi时,访问结果为



四、apache虚拟主机
1.定义:

可以让我们的一台apache服务器在被访问不同域名的时候显示不同的主页

2.建立测试页
cd /var/www/                    ##切换至/var/www/
mkdir virtual                   ##创建virtual目录
mkdir virtual/news.westos.com -p    
mkdir virtual/news.westos.com/html -p

echo "news.westos.com's page" > virtual/news.westos.com/html/index.html



3.配置

*)默认

cd /etc/httpd/conf.d/
vim default.conf                        ##未指定域名的访问都访问default
<Virtualhost    _default_:80>           ##虚拟主机开启的端口
        DocumentRoot "/var/www/html"    ##虚拟主机的默认发布目录
        CustomLog "logs/default.log" combined    ##虚拟主机日志
</Virtualhost>

systemctl restart httpd                 ##重启httpd


测试:
在客户主机中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com


访问news.wesstos.com时,访问结果的内容是/var/www/html/westos.html下的内容



*)指定

vim news.conf                            ##指定域名news.westos.com的访问到指定默认发布目录

<Virtualhost    *:80>                    ##虚拟主机开启的端口
        ServerName "news.westos.com"     ##指定域名
        DocumentRoot "/var/www/virtual/news.westos.com/html"   ##虚拟主机的指定默认发布目录
        CustomLog "logs/news.log" combined                     ##虚拟主机日志
</Virtualhost>
<Directory "/var/www/virtual/news.westos.com/html">            ##默认发布目录的访问授权
        Require all granted
</Directory>

systemctl restart httpd                  ##重启httpd



测试:
在客户主机中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com


访问news.wesstos.com时,访问结果的内容是/var/www/virtual/news.westos.com/html/下的内容



五、HTTPS
1.定义:

Hyper text transfer protocol over Secure socker layer

2.安装

yum install mod_ssl crypto-utils -y            ##安装mod_ssl,crypto-utils


genkey www.westos.com                          ##调用genkey,同时为生成的文件指定唯一名称


记录生成的证书(www.westos.com.crt)和关联的私钥(www.westos.com.key)的位置


继续使用对话框,并选择合适的密钥大小。(默认的2048位密钥为推荐值)


在生成随机数时比较慢,敲键盘和移动鼠标可以加速


拒绝向认证机构(CA)发送证书请求(CSR)。


拒绝加密私钥


为服务器提供合适的身份。Common Name必须与服务器的主机全名完全匹配。(注意,任何逗号都应使用前导反斜线[\]进行转义)


vim /etc/httpd/conf.d/login.conf               ##编辑login.conf
<Virtualhost    *:443>
        ServerName "login.westos.com"          ##指定域名
        DocumentRoot "/var/www/virtual/login.westos.com/html"   ##虚拟主机的指定默认发布目录
        CustomLog "logs/login.log" combined                     ##虚拟主机日志
        SSLEngine on                                            ##开始https功能
        SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt       ##SSLCertificateFile指向生成证书的位置
        SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key  ##SSLCertificateKeyFile指向关联的私钥的位置
</Virtualhost>
<Directory "/var/www/virtual/login.westos.com/html">
        Require all granted
</Directory>
<Virtualhost    *:80>                         ##网页重写,把所有80端口的请求全部重定向由https来处理
        ServerName "login.westos.com"
        RewriteEngine on
        RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
### ^(/.*)$         客户主机在地址栏中写入的所有字符,不好看换行符
### https://        定向成为的访问协议
### %{HTTP_HOST}    客户请求主机
### $1              $1的值就表示^(/.*)$的值
### [redirect=301]  临时重定向,302永久重定向
mkdir /var/www/virtual/login.westos.com/html -p
echo "login.westos.com's page" > /var/www/virtual/login.westos.com/html/index.html

systemctl restart httpd




3.测试:
在客户主机中添加解析
vim /etc/hosts

172.25.254.131 www.westos.com news.westos.com login.westos.com


访问http://login.westos.com会自动跳转到https://login.westos.com实现网页数据加密传输


Web客户端可能会发出它不认可证书发行者的警告。这种情况适用自签名证书。要求Web客户端
绕过证书认证。(对于Firefox,请选择“I Understand the Risks” [我了解风险]、“Add Exception” [

添加例外]和“Confirm Security Exception”[确认安全例外]。)



六、
1.正向代理
服务端:

yum install squid.x86_64 -y                  ##安装squid


systemctl start squid                        ##开启squid
netstat -antlpe | grep squid                 ##查看squid端口
vim /etc/squid/squid.conf                    ##编辑配置文件
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 57
 58 # Squid normally listens to port 3128
 59 http_port 3128
 60
 61 # Uncomment and adjust the following to add a disk cache directory.
 62 cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                      ##重启squid


客户端:
firefox

Edit-->Preferences-->Advanced-->Network-->Settings-->Manual proxy configuration-->HTTP Proxy(172.25.254.60)-->Port(3128)-->勾上Use this proxy server for all protocols--OK


在客户端未开启网络的情况下可以通过服务端访问www.baidu.com



2.反向代理
在做这个实验之前,应确保环境纯净
rht-vmctl reset server                      ##重置虚拟机

rht-vmctl view server                       ##开启虚拟机


vim /etc/sysconfig/network-scripts/ifcfg-eth0   ##配置网络
DEVICE=eth0
BOOTPROTO=none
IPADDR=172.25.254.231
PREFIX=24
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no

PERSISTENT_DHCLIENT=1


systemctl restart network                   ##重启网络
vim /etc/yum.repos.d/rhel_dvd.repo          ##配置yum源
# Created by cloud-init on Thu, 10 Jul 2014 22:19:11 +0000
[rhel_dvd]
gpgcheck = 0
enabled = 1

baseurl = http://172.25.254.60/rhel7



yum install squid.x86_64 -y                 ##安装squid


systemctl start squid                       ##开启squid
vim /etc/squid/squid.conf                   ##编辑配置文件
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 57
 58 # Squid normally listens to port 3128
 59 http_port 80 vhost vport
 60 cache_peer 172.25.254.131 parent 80 0 no-query
 61 # Uncomment and adjust the following to add a disk cache directory.
 62 cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                     ##重启squid


测试
vim /etc/hosts

172.25.254.231 www.westos.com


在测试端访问172.25.254.131,是通过访问172.25.254.231来访问172.25.254.131的



3.轮叫机制
在真机中执行:
linux.sh linux                ##新建linux虚拟机
在linux中:

vim /etc/sysconfig/network-scripts/ifcfg-eth0          ##配置网络


systemctl restart network                   ##重启网络


vim /etc/yum.repos.d/rhel_dvd.repo          ##配置yum源

yum install httpd -y                        ##安装httpd

systemctl start httpd                       ##开启httpd

systemctl stop firewalld                    ##关闭火墙


mkdir /var/www                        ##创建/var/www目录
mkdir /var/www/html                   ##创建/var/www/html目录
vim /var/www/html/westos.html    ##编辑westos.html文件

<h1>linux</h1>


vim /etc/httpd/conf/httpd.conf   ##修改apache配置文件

164     DirectoryIndex westos.html index.html    ##默认发布文件修改为westos.html


systemctl restart httpd          ##重启httpd
在server中:
vim /etc/squid/squid.conf                        ##编辑配置文件
# And finally deny all other access to this proxy
http_access allow all
# Squid normally listens to port 3128
http_port 80 vhost vport
cache_peer 172.25.254.131 parent 80 0 no-query originserver name=web1 round-robin
cache_peer 172.25.254.132 parent 80 0 no-query originserver name=web2 round-robin
cache_peer_domain www.westos.com web1 web2
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /var/spool/squid 100 16 256

systemctl restart squid                         ##重启squid


测试
vim /etc/hosts

172.25.254.231 www.westos.com


在测试端访问www.westos.com,是通过访问172.25.254.231来轮流访问web1,web2


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值