linux学习 lesson22 http服务

一、Apache服务

什么是Apache

阿帕奇 (Apache HTTP Server)也就是apache服务器。它是Apache软件基金会管理下的一个开放源代码的服务器软件,可以理解为电脑上的一个应用程序。简单地说它的作用就是将你的电脑变成一台服务器,让你的电脑开放特定的网络端口,用以接收来自网络上发送到这台机器的HTTP请求,对请求的内容进行处理并作出相应的响应。

安装

yum install http -y                ##安装appche在此之前先配好yum源
systemctl start httpd              ##开启阿帕奇服务
systemctl enable httpd             ##开机自启动

firewall-cmd --permanent --add-service=http       ##火墙允许阿帕奇工作
firewall-cmd --reload                             ##重启火墙
firewall-cmd --list-all                           ##查看是否添加成功,若servere行成功显示http字样表示成功

在这里插入图片描述

测试

在真机上打开浏览器输入安装过阿帕奇的虚拟机ip。
在这里插入图片描述

安装成功!

apache的基础设置和信息

查看html内容

文件所在位置/var/www/html/index

vim /var/www/html/index.html     ##网页显示内容编写

在这里插入图片描述

在这里插入图片描述

http菜单显示
yum install httpd-manual -y                              ##安装apache菜单插件

在这里插入图片描述
重启apache,在网页上测试。

在这里插入图片描述

基础信息

主配置目录: /etc/httpd/conf
主配置文件: /etc/httpd/conf/httpd.conf
子配置目录: /etc/httpd/conf.d
子配置文件: /etc/httpd/conf.d/*.conf
默认发布目录: /var/www/html
默认发布文件: index.html
默认端口: 80
默认安全上下文:httpd_sys_content_t
程序开启默认用户:apache
apache日志: /etc/httpd/logs/

二、默认发布文件修改

cd /etc/httpd/conf/
vim httpd.conf                       ##更改配置文件
DirectoryIndex test.html index.html

在这里插入图片描述

  cd /var/www/html/
   vim test.html                                ##编写test发布文件
   systemctl restart httpd.service 

在这里插入图片描述

测试

在这里插入图片描述
默认发布文件换为了test.html的内容,试验成功!

三、默认发布目录修改

mkdir -p /kris/html
vim /etc/httpd/conf/httpd.conf
####编写内容

119 #DocumentRoot "/var/www/html"             ##注释原本发目录地址
120 DocumentRoot "/kris/html"                      ##填写新的发目录
121 <Directory "/kris">
122     Require all granted
123 </Directory>

在这里插入图片描述

cd /kris/html/
vim index.html                  ##编写发布文件

在这里插入图片描述

ls -Zd /var/www/html                                                        ##原本目录的安全上下文
ls -Zd /kris                                                                          ##新建目录的安全上下文
semanage fcontext -a -t httpd_sys_content_t '/kris(/.*)?' ##更改安全上下文诗其与原目录一致
restorecon -FvvR /kris
systemctl restart httpd

##注意:如果安全上下文显示为? 说明selinux处于disable模式,则不用修改。

测试

在这里插入图片描述

四、默认端口修改

vim /etc/httpd/conf/httpd.conf

编写内容:

41 #Listen 12.34.56.78:80
42 Listen 8080                              ##修改端口为8080

在这里插入图片描述

 firewall-cmd --permanent --add-port=8080/tcp             ##火墙允许8080端口使用
 firewall-cmd --reload 
 systemctl restart httpd.service 

在这里插入图片描述
####测试
浏览器输入:172.25.254.108:8080
在这里插入图片描述

五、apache虚拟机

浏览器所在主机做地址解析

su -
vim /etc/hosts
在这里插入图片描述

还原apache配置文件

vim /etc/httpd/conf/httpd.conf
在这里插入图片描述

在这里插入图片描述
重启apache
浏览器输入网址www.kris.com成功!
在这里插入图片描述
这样的话所有的地址都会指向172.25.254.108.那么如何让他们访问不同的页面呢?我们需要用到apache虚拟机。

apache虚拟机搭建

mkdir -p /var/www/virtual/kris.com/music
mkdir -p /var/www/virtual/kris.com/sport
vim /var/www/virtual/kris.com/music/index.html
在这里插入图片描述
vim /var/www/virtual/kris.com/sport/index.html
在这里插入图片描述
vim /etc/httpd/conf.d/music.conf
在这里插入图片描述
cd /etc/httpd/conf.d/
cp music.conf sport.conf
vim sport.conf
在这里插入图片描述
systemctl restart httpd.service

测试

在这里插入图片描述

在这里插入图片描述

六、apache内部的访问控制

1、针对主机的访问控制

访问黑名单设置

mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
在这里插入图片描述
在这里插入图片描述

vim /etc/httpd/conf/httpd.conf
在这里插入图片描述

  <Directory "/var/www/html/westos">
          Order Allow,Deny		#列表读取顺序,后读的会覆盖前重复的内容
          Allow from ALL
          Deny from 172.25.254.65
  </Directory>				#不允许ip为172.25.254.65的用户访问
测试

systemctl restart httpd

68主机上测试

在这里插入图片描述
##主机ip因为在黑名单内所以被拒绝。

208虚拟机上测试

在这里插入图片描述
##虚拟机没有在黑名单中可以访问。

访问白名单设置

vim /etc/httpd/conf/httpd.conf
在这里插入图片描述

  <Directory "/var/www/html/westos">
          Order Deny,Allow
          Allow from 172.25.254.65
          Deny from All
  </Directory>				#只允许ip为172.25.254.65的用户访问

systemctl restart httpd

68主机上测试

在这里插入图片描述

208虚拟机上测试

在这里插入图片描述

2、针对用户的访问控制

mkdir -p /var/www/virtual/kris.com/music/html/admin
cd /var/www/virtual/kris.com/music/html/admin
vim index.html

#编写要显示的内容
在这里插入图片描述

cd /etc/httpd/conf.d
vim music.conf
编写文件:
在这里插入图片描述
cd /etc/httpd/
htpasswd -cm userpass admin
htpasswd -m userpass admin1
创建两个用户。并输入密码。
cat userpass ##查看密码
在这里插入图片描述
systemctl restart httpd。

测试:

在网页上输入:music.kris.com/html/admin/
admin用户登陆成功
admin1 用户被拒绝
在这里插入图片描述
在这里插入图片描述

七、apache支持的语言

html

PHP

yum install -y
vim /var/www/html/index.php
在这里插入图片描述
systemctl restart httpd
#测试:
http://172.25.254.108/index.php
在这里插入图片描述

cgi

mkdir /var/www/html/cgi
semanage fcontext -a -t httpd_sys_script_exec_t ‘/var/www/html/cgi(./*)?’
restorecon -RvvF /var/www/html/cgi/
vim /var/www/html/cgi/index.cgi

!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date;

chmod +x /var/www/html/cgi/index.cgi

cd /etc/httpd/conf.d/
vim adefault.conf

<Directory “/var/www/html/cgi”>
Options +ExecCGI ##该目录下的cgi文件可执行
AddHandler cgi-script .cgi ##以cgi-script和.cgi结尾的文件都要执行

systemctl restart httpd.service

#测试:
http://172.25.254.108/cgi/index.cgi

八、squid

Squid cache:

(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器。Squid有广泛的用途,从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid主要设计用于在Linux一类系统运行。
Squid是一种在Linux系统下使用的优秀的代理服务器软件。squid不仅可用在Linux系统上,还可以用在AIX、Digital Unix、FreeBSD、HP-UX、Irix、NetBSD、Nextstep、SCO和Solaris等系统上。Squid与Linux下其它的代理软件如Apache、Socks、TIS FWTK和delegate相比,下载安装简单,配置简单灵活,支持缓存和多种协议。

正向代理

yum install squid
vim /etc/squid/squid.conf
59 http_port 3128
62 cache_dir ufs /var/spool/squid 100 16 256
在squid上面缓存250的http数据
#vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=172.25.254.250
在浏览器上设置http接口
perforence advanced-network setting-http_Proxy 172.25.254.108 Port 3128 #为squid缓存接口

vim /etc/squid/squid.conf
在这里插入图片描述
在squid上面缓存64的http数据

在squid主机的浏览器设置http服务端和端口,通过3128squid端口缓存能上网的64真机上的数据
在这里插入图片描述
在这里插入图片描述

squid反向代理

设置server为squid主机

yum install squid
vim /etc/squid/squid.conf
59 http_port 80 vhost vport

60 cache_peer 172.25.254.108 parent 80#通过80端口向108访问apache从而缓存数据 0 proxy-only
62 cache_dir ufs /var/spool/squid 100 16 256
systemctl restart squid
systemctl stop firewalld
vim /etc/squid/squid.conf

在这里插入图片描述
有时候会因为客户端的网速太慢从而导致访问终端浏览器时的过程太长,不能达到客户的标准,此时有一个squid浏览器能更快地通过apache访问到数据,于是将其客户端想要访问的数据通过squid从终端服务器上面高速缓存下来,并且近距离的发送到客户主机上面,从而实现了两次加速,即反向代理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值