栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Ⅲ第三章学习博客(Apache)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Ⅲ第三章学习博客(Apache)

Linux修炼之旅第三章!

本章内容介绍了Web服务器软件Apache再Linux系统中的管理及优化web,包括Apache的安装应用,配置,访问控制,以及Apache中的虚拟主机和正反向代理,干货满满
下面我们就开始吧


文章目录
  • Linux修炼之旅第三章!
  • 一、Apache是什么
  • 二、Apache的安装和启用
  • 三、Apache的基本配置
  • 1.端口修改
  • 2.默认发布文件
  • 3.默认发布目录
  • 四、Apache的访问控制
  • 1.ip黑白名单
  • 2.基于用户认证
  • 五、Apache的虚拟主机
  • 六、Apache的语言支持
  • 1.php
  • 2.cgi
  • 3.wsgi
  • 七、Apache的加密访问
    • http转https
  • 八、正向代理
  • 九、正向代理


一、Apache是什么

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。
在web被访问时通常使用http://的方式,这个功能就是Apache提供的。

二、Apache的安装和启用

首先安装httpd服务,然后再做一些火墙的设定。

[root@westoslinux ~] dnf install httpd -y
[root@westoslinux ~] systemctl enable --now httpd  #开启服务并设定服务位开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@westoslinux ~] firewall-cmd --permanent --add-service=http  #在火墙中永久开启http访问
success
[root@westoslinux ~] firewall-cmd --permanent --add-service=https #在火墙中永久开启https访问
success
[root@westoslinux ~] firewall-cmd --reload #刷新火墙使设定生效
success

三、Apache的基本配置 1.端口修改

首先在默认发目录中编辑主界面的显示信息,方便测试
vim /var/www/html/index.html
写入内容hello shyshy
实验看是前先查看端口号,显示默认端口(80)
用如下命令查看netstat -antlupe | grep http

然后再主配置文件中修改端口号为8080

[root@westoslinux html] vim /etc/httpd/conf/httpd.conf #修改端口号 
将Listen 80 改为Listen 8080
[root@westoslinux html] systemctl restart httpd
[root@westoslinux html] firewall-cmd --add-port=8080/tcp
success

上述步骤实际操作演示


结果测试
再主机浏览器中输入nodea主机的ip再跟上:8080
172.25.254.126:8080

2.默认发布文件

第一小节介绍了端口的管理
第二小节来介绍如何管理发布文件
首先再原本的默认发布目录下新建一个html文件
vim /etc/www/html/test.html
位了直观测试效果直接编辑文件为
被改过了!
然后再主配置文件中再默认发布文件前加上test.html
vim /etc/httpd/conf/httpd.conf
编辑效果

然后重启服务
systemctl restart httpd
打开测试浏览器测试可以演到test.html的内容

那么如果此时直接删掉test.html文件,但是不对主配置文件进行修改会发生什么呢?
我们来测试一下
rm -rf test.htm
可以看到
删除后打开浏览器测试可以发现又开始显示index.html的内容了

3.默认发布目录

第二节介绍了修改默认发布文件
那么现在就来介绍默认发布目录的修改
在主配置文件中
vim /etc/httpd/conf/httpd.conf
在默认路径配置的地方修改为以下内容

#documentRoot "/var/www/html" #把默认路径注释掉
documentRoot "/www/westos"    #换成自己想设置的路径

   Require all granted

#
# Relax access to content within /var/www.
#

    AllowOverride None
    # Allow open access:
    Require all granted

#以上五行是授权指令

然后建立在文件中添加的默认路径并创建配置文件

[root@westoslinux html] mkdir /www/westos -p
[root@westoslinux html] cd /www/westos/
[root@westoslinux westos] vim index.html
hello shy

修改安全上下文,避免Selinux影响实验结果

[root@westoslinux westos] semanage fcontext -a -t httpd_sys_content_t '/www(/.*)?'
[root@westoslinux westos] restorecon -RvvF /www/
Relabeled /www from unconfined_u:object_r:default_t:s0 to system_u:object_r:httpd_sys_content_t:s0
Relabeled /www/westos from unconfined_u:object_r:default_t:s0 to system_u:object_r:httpd_sys_content_t:s0
Relabeled /www/westos/index.html from unconfined_u:object_r:default_t:s0 to system_u:object_r:httpd_sys_content_t:s0


最后测试前重启服务
systemctl restart httpd
此时在测试主机打开浏览器输入172.25.254.126就会显示路径在/www/westos/的文件index.html中的内容

四、Apache的访问控制 1.ip黑白名单

首先将配置文件中前面实验设定的默认发布目录注释掉,恢复原样,以免影响后面的实验。
vim /etc/httpd/conf/httpd.conf

黑白名单的设定文件
vim /etc/httpd/conf/httpd.conf


    Order allow,deny
    Allow from all
    Deny from 172.25.254.26 #测试机的ip
 #相当于在设置黑名单

白名单则刚好相反
设定部分的文件为


    Order deny,allow
    Allow from 172.25.254.26
    Deny from all 
 #相当于在设置白名单

然后可以在默认发布目录中编辑一个实验用的主页面文件

设定完以后重启服务
systemctl restart httpd
设置完白名单以后测试一下
注意这次我们的实验实在html目录中新建了一个westos目录
测试时需要输入ip和该目录
打开测试机172.25.254.26的浏览器,输入
172.25.254.126/westos

2.基于用户认证

首先设置两个用户以及用户对应的密码
作为测试用的用户

[root@westoslinux westos] htpasswd -cm /etc/httpd/.htpasswd admin
New password: 
Re-type new password: 
Adding password for user admin
[root@westoslinux westos] cat /etc/httpd/.htpasswd 
admin:$apr1$lZuyQNke$8f2BJYKFiNQ2FuUHBi5sL0
[root@westoslinux westos] htpasswd -m /etc/httpd/.htpasswd shy
New password: 
Re-type new password: 
Adding password for user shy
[root@westoslinux westos] cat /etc/httpd/.htpasswd 
admin:$apr1$lZuyQNke$8f2BJYKFiNQ2FuUHBi5sL0
shy:$apr1$M1QVIJ24$quiT.xsHVIOc7gLe2n2I7/

值得注意的是:第二次开始设置用户就不能再输入-c了,否侧会导致错误

然后再打开配置文件
vim /etc/httpd/conf/httpd.conf
指定用户shy


   AuthUserFile "/etc/httpd/.htpasswd"
   Authname "Please input username and password !!"
   AuthType basic
   RequireUser  shy


设置完毕后重启服务
systemctl restart httpd
此时只有shy用户可以登录
用admin用户登录会被拒绝

shy用户登录


如果是让所有用户都可以
就将shy换成valid-user

[root@westoslinux westos] vim /etc/httpd/conf/httpd.conf  #所有用户都可以

   AuthUserFile "/etc/httpd/.htpasswd"
   Authname "Please input username and password !!"
   AuthType basic
   Require  valid-user


[root@westoslinux westos] systemctl restart httpd
五、Apache的虚拟主机

比如百度的界面中有很多的子界面,不可能用一台主机的ip来配置每个子链接
会造成难以想象的浪费
这时候就可以用apache中的虚拟机来结局这个问题
具体步骤如下
先配置两个子链接的默认发布文件,先创建发布目录,再在其目录中编辑文件

 mkdir -p /var/www/virtual/westos.org/{linux,luck}
 echo linux.westos.org > /var/www/virtual/westos.org/linux/index.html
 echo luck.westos.org > /var/www/virtual/westos.org/luck/index.html

然后编辑虚拟机的文件,使用户访问子链接时可以自动跳转访问相应的默认发布文件。

vim /etc/httpd/conf.d/vhosts.conf #一下内容为编辑内容的代码,粘贴时删去注释部分

 #默认,不用指定名字
documentRoot /var/www/html
CustomLog logs/default.log combined


  #端口号80前替换为*就必须指定名字
ServerName linux.westos.org
documentRoot /var/www/virtual/westos.org/linux
CustomLog  logs/linux.log combined



ServerName luck.westos.org
documentRoot /var/www/virtual/westos.org/luck
CustomLog logs/luck.log combined


然后再真机中测试
先配置hosts文件
vim /etc/hosts
加上
172.25.254.126 www.westos.org linux.westos.org luck.westos.org

再打开浏览器分别测试
默认发布文件(相当于主界面)

子界面

六、Apache的语言支持 1.php

安装php
dnf install php -y
书写php语言的文件

vim /var/www/html/index.php


重启服务
systemctl restart httpd
在真机浏览器中测试

2.cgi

书写cgi语言的文件

[root@westoslinux html] dnf install httpd-manual -y
[root@westoslinux html] mkdir /var/www/html/cgi
[root@westoslinux html] vim /var/www/html/cgi/index.cgi
[root@westoslinux html] cd /var/www/html/cgi/
[root@westoslinux cgi] ls
index.cgi
[root@westoslinux cgi] cat index.cgi 
#!/usr/bin/perl
printf "Content-type:text/htmlnn";
printf `date`;
[root@westoslinux html] semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgi(/.*)?' #设定安全上下文
[root@westoslinux cgi]# restorecon -RvvF /var/www/html/cgi/
Relabeled /var/www/html/cgi from unconfined_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:httpd_sys_script_exec_t:s0
Relabeled /var/www/html/cgi/index.cgi from unconfined_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:httpd_sys_script_exec_t:s0

编辑配置文件,并在配置文件中写入如下内容

[root@westoslinux cgi] vim /etc/httpd/conf.d/vhosts.conf

Options +ExecCGI
AddHandler cgi-script .cgi

[root@westoslinux cgi] systemctl restart httpd #重启服务
[root@westoslinux cgi] chmod +x index.cgi #赋权

测试

3.wsgi

首先安装 python3-mod_wsgi,使主机支持wsgi语言
dnf install python3-mod_wsgi
然后书写wsgi语言的配置文件

[root@westoslinux cgi] mkdir /var/www/html/wsgi 创建wsgi目录
[root@westoslinux cgi] semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/wsgi(/.*)?' #解决安全上下问的问题
[root@westoslinux cgi] restorecon -RvvF /var/www/html/wsgi/
Relabeled /var/www/html/wsgi from unconfined_u:object_r:httpd_sys_content_t:s0 to system_u:object_r:httpd_sys_script_exec_t:s0
[root@westoslinux cgi] vim /var/www/html/wsgi/index.wsgi 
[root@westoslinux cgi] vim /etc/httpd/conf.d/vhosts.conf
[root@westoslinux cgi] systemctl restart httpd

其中
编辑文件
vim /var/www/html/wsgi/index.wsgi
的内容为

def application(env, westos):
 westos('200 ok',[('Content-Type', 'text/html')])
 return [b'hello westos linux web']


编辑文件
vim /etc/httpd/conf.d/vhosts.conf
的内容为


 ServerName wsgi.westos.org
 WSGIscriptAlias / /var/www/html/wsgi/index.wsgi


测试结果

七、Apache的加密访问

首先先开启http加密服务

[root@westoslinux ~] firewall-cmd --permanent --add-servic
success
[root@westoslinux ~] firewall-cmd --permanent --add-service=http
success
[root@westoslinux ~] firewall-cmd --reload
success
[root@westoslinux ~] netstat -antulpe | grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      0          28231      1010/httpd          
[root@westoslinux ~] netstat -antulpe | grep httpds


然后再安装必须要用到的服务,并设置密钥

[root@westoslinux ~] dnf search http #找到带SSL/TLS的那一行的软件,SSL/TSL代表加密服务
[root@westoslinux ~] dnf install mod_ssl.x86_64 #安装它
[root@westoslinux ~] cd /etc/httpd/conf.d/
[root@westoslinux conf.d] vim ssl.conf
[root@westoslinux conf.d] openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/westos.org.key -x509 -days 365 -out /etc/httpd/westos.org.crt #生成密钥
Generating a RSA private key
......................+++++
...............................+++++
writing new private key to '/etc/httpd/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) []:shannxi
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 []:linux@westos.org
[root@westoslinux conf.d]# ls /etc/httpd/westos.org.key #查看文件是否生成成功
/etc/httpd/westos.org.key
[root@westoslinux conf.d]# ls /etc/httpd/westos.org.crt
/etc/httpd/westos.org.crt


编辑文件
vim /etc/httpd/conf.d/ssl.conf
将上一步中生成的key文件和crt文件绝对路劲写入到文档中
生成自己的证书

测试前不要忘记重启服务
systemctl restart httpd
测试
在测试主机中输入网址
https://172.25.254.126
http后的s就代表加密文件
网址旁边有个锁

点开以后就可以查看证书信息了

http转https

在实际应用中,由于加密访问也是需要一定时间的,如果所有的链接都用加密访问,会对效率产生很大的影响,通过配置可以在需要加密服务时再切换,比如有用户密码登录的界面在进行加密,就可以在很大程度上提升访问效率。
如何进行上述操作呢?
首先创建一个测试目录

[root@westoslinux ~] mkdir /var/www/virtual/westos.org/login -p #创建测试目录
[root@westoslinux ~] echo login.westos.org > /var/www/virtual/westos.org/login/index.html #将字符串写进登陆界面,就相当于把之前创建的访问主界面设置为现在实验访问的界面,用于测试

编辑配置文件
vim /etc/httpd/conf.d/vhosts.conf
内容为
对应注释如下,使用时删除


documentRoot /var/www/html
CustomLog logs/default.log combined
 #第一块内容表示默认的访问界面


ServerName login.westos.org
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 #^(/.*)$表示客户地址栏中输入的地址,https://%{HTTP_HOST}$1表示客户主机访问RewriteRule后面跟的第一串字符的值,这样就可以代替输入网址,通用,减少后期维护的麻烦
#第二块内容表示用户输入的网址,如果输入了需要加密的网址,就会自动跳转到443端口,即需要加密的网址,https


Servername login.westos.org
documentRoot /var/www/virtual/westos.org/login
CustomLog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/httpd/westos.org.crt
SSLCertificateKeyFile /etc/httpd/westos.org.key
 #跳转到的需要加密的网址,模板可以从manual中看到


重启服务
systemctl restart httpd
然后就可以打开浏览器测试了
输入http://login.westos.org
就会自动跳转到https://login.westos.org
这么做的意义就是加密也是需要时间的,将需要输入密码的地址进行加密,可以大大提升效率。

八、正向代理

类似于翻墙
将一个另一个地方的内容缓存在代理主机中,再反馈给服务主机
一般是私人服务器
客户主机需要什么就缓存什么,然后反馈客户主机
实验中就是让本来不能上网的主机上网
先在代理主机中安装squid服务
dnf install squid -y
然后卸载http服务,以免影响实验结果
dnf remove httpd
编辑文件
vim /etc/squid/squid.conf为

[root@westoslinux ~] ip route add default via 172.25.254.250 #设置网关为一台可上网主机的ip
[root@westoslinux ~] echo nameserver 114.114.114.114 > /etc/resolv.conf  #配置dns
[root@westoslinux ~] firewall-cmd --add-service=squid
success
[root@westoslinux ~] vim /etc/resolv.conf 
[root@westoslinux ~] ping www.baidu.com #没网的主机126可以上网了
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=51 time=25.9 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=51 time=29.4 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=3 ttl=51 time=26.9 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=4 ttl=51 time=28.8 ms
^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7ms
rtt min/avg/max/mdev = 25.879/27.724/29.363/1.413 ms
[root@westoslinux ~] systemctl restart squid #重启服务

测试时,在没有网的主机(26)中打开浏览器进行配置



可以看到没有联网的主机26,可以通过126主机的代理服务,实现网络连接了。

九、正向代理

类似于企业搭建的服务器
比如西安的用户访问深圳的地址
就可以先访问西安的服务主机,然后获取深圳主机的地址中的内容
提升效率
配置代理主机126
配置上个实验中的文件

[root@westoslinux ~] cd /usr/share/doc/squid/
[root@westoslinux squid] vim /etc/squid/squid.conf
 58 # And finally deny all other access to this proxy
 59 http_access allow all
 60 
 61 # Squid normally listens to port 3128
 62 http_port 80 vhost vport
 63 cache_peer 172.25.254.26 parent  80 0 proxy-only

[root@westoslinux squid] rpm -qa | grep httpd #可以看到没装httpd
[root@westoslinux squid] systemctl restart squid
[root@westoslinux squid] dnf install firefox #安装浏览器
[root@westoslinux squid] systemctl restart squid

在测试主机(26)中
书写一个默认发布文件
vim /var/www/html/index.html
输入hello shy
安装http服务
dnf install httpd
配置火墙
firewall -cmd --permanent --add-service=http
firewall -cmd --reload
然后在126主机中打开浏览器
输入172.25.254.126(nodea主机ip)
可以看到显示的内容为刚刚在主机中写的index.html文件

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/599319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号