Linux —— Apache

目录

1. Apache的作用

2. Apache的安装

3. Apache的启用

4. Apache的基本信息

5. Apache的基本配置

5.1 Apache端口的修改

5.2 默认发布文件

5.3 默认发布目录

6. Apache的访问控制

6.1 实验素材

6.2 基于客户端ip的访问控制

6.3 基于用户认证的访问控制

7. Apache的虚拟主机

8. Apache的语言支持

8.1 html   ## 默认支持 

8.2 php

8.3 perl

​9. Apache的加密访问

10. Squid+Apache

10.1 squid正向代理【代购式】

10.2 squid反向代理【分店式】


1. Apache的作用

在web被访问时通常使用http://方式

http://                 ## 超文本传输协议

http://   超文本传输协议提供软件:

Apache:目前主流

nginx:轻量级

stgw:腾讯

jfe:京东

Tengine:阿里巴巴

2. Apache的安装

dnf install httpd.x86_64 -y

3. Apache的启用

  • 安装好后查看httpd状态

  • 启用Apache,并设定开机启动
systemctl enable --now httpd   ## 开启服务并设定服务位开机启动

 

 

  • 添加并开启服务http和https
firewall-cmd --list-all                        ## 查看火墙信息
firewall-cmd --permanent --add-service=http    ## 在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https   ## 在火墙中永久开启https访问
firewall-cmd --reload                          ## 刷新火墙设定生效

 

 

4. Apache的基本信息

服务名称:httpd

配置文件:

/etc/httpd/conf/httpd.conf   ## 主配置文件

/etc/httpd/conf.d/*.conf       ## 子配置文件

默认发布端口:

/var/www/html

默认端口:

80     ## http

443   ## https

用户:

apache

日志:

/etc/httpd/logs

5. Apache的基本配置

5.1 Apache端口的修改

  • 首先查询端口号

  • 修改端口号

  •  我们使用的是8080端口,在这里我们要先让火墙允许8080访问

  • 测试成功,输入后进入测试页面 

  • 查看可以直接访问的端口列表
semanage port -l | grep http

  • 若设置端口号不在列表中,比如设置的端口号是6666
vim /etc/httpd/conf/httpd.conf   ## 设置端口号为6666
Listen 6666   ## 
firewall-cmd --permanent --add-port=6666/tcp
firewall-cmd --reload
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 6666
systemctl restart httpd

  • 将端口号6666加入其中

5.2 默认发布文件

vim /etc/httpd/conf/httpd.conf
## DirectoryIndex dsd.html index.html
systemctl restart httpd

  •  默认打开的是dsd.index文件

5.3 默认发布目录

vim /etc/httpd/conf/httpd.conf
### 设定默认发布目录及权限设定
DocumentRoot "/dsd/html"
<Directory "/dsd/html">
  Require all granted
</Directory>
###
semanage fcontext -a -t httpd_sys_content_t '/dsd(/.*)?'   ## 修改安全上下文
restorecon -RvvF /dsd/   ## 刷新
systemctl restart httpd
firefox http://192.168.43.20
  • 修改默认访问目录

  • 访问被拒绝,这是因为我们没有对其进行授权

  • 授权后我们发现在警告模式下可以访问,但是在强制模式下还是无法访问,查看目录信息

  •  修改安全上下文,与默认包目录一致

6. Apache的访问控制

6.1 实验素材

mkdir /var/www/html/dsd
vim /var/www/html/dsd/index.html
## <h1>dsddir's page </h1>
在本机中firefox http://192.168.43.20/dsd,
若通过ssh登陆的话通过ssh @root192.168.43.20 -X 方式登陆

6.2 基于客户端ip的访问控制

vim /etc/httpd/conf/httpd.conf
## 设置192.168.43.10客户端无法访问,此时我们在192.168.43.10客户端执行
firefox http://192.168.43.20/dsd
## 白名单
<Directory "/var/www/html/dsd">
         Order Deny,Allow
         Allow from 192.168.43.10
         Deny from All
</Directory>

## 黑名单
<Directory "/var/www/html/dsd">
         Order Deny,Allow
         Allow from All
         Deny from 192.168.43.10
</Directory>
  • 先Allow后Deny情形 

  •  先Deny后Allow情形 

纠察其原因是,由于我们先执行的拒绝访问后执行允许访问操作,由于允许操作为Allow all,将前面执行的拒绝访问操作直接覆盖掉,最终结果为允许所有客户机访问。

注意:一定将范围小的名单设置为后读!

6.3 基于用户认证的访问控制

<Directory "/var/www/html/dsd">
          AuthUserfile /etc/httpd/htpassfile             ## 指定认证文件
          AuthName "Please input your name and password" ## 认证提示语
          AuthType basic                                 ## 认证类型
          Require user admin                             ## 允许通过的认证用户,二选一
          Require valid-user                             ## 允许所有用户通过认证,二选一
</Directory>
htpasswd -cm /etc/httpd/htpassfile admin                 ## 生成认证文件
htpasswd -m /etc/httpd/htpassfile dsd                    ## 当/etc/httpd/htpassfile存在时在添加用户时不要加-c参数,否则会覆盖源文件内容
cat /etc/httpd/htpassfile

 

  • 或者直接在配置文件中设置用户

7. Apache的虚拟主机

  • 设定客户端解析(浏览器所在的主机中)
vim /etc/hosts
###
192.168.43.20 www.dsd.com
###
修改过后,我们在浏览器中输入192.168.43.20或者www.dsd.com均可以访问

 

vim /etc/hosts
###
192.168.43.20 www.dsd.com news.dsd.com wenku.dsd.com
###
修改过后,我们在浏览器中输入www.dsd.com、news.dsd.com、wenku.dsd.com均可以访问

 

 但是上述展示的都是同一个page的信息,如何设置其打开指定的页面

  • 创建存放目录并为页面编辑内容

 

mkdir -p /var/www/dsd.com/{news,wenku}

echo default.dsd.com's page > /var/www/html/index.html
echo wenku.dsd.com's page > /var/www/dsd.com/wenku/index.html
echo news.dsd.com's page > /var/www/dsd.com/news/index.html
### 对应关系
news.dsd.com   -------->   /var/www/dsd.com/news/index.html
wenku.dsd.com   -------->   /var/www/dsd.com/wenku/index.html
###
  • 存放主机对应关系
### 存放主机对应关系
cd /etc/httpd/conf.d/
vim VirtualHost.conf   ## 编写对应关系信息

<VirtualHost _default_:80>
           DocumentRoot "/var/www/html"
           CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
          ServerName wenku.dsd.com
          DocumentRoot "/var/www/dsd.com/wenku"
          CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost *:80>
          ServerName news.dsd.com
          DocumentRoot "/var/www/dsd.com/news"
          CustomLog logs/news.log combined
</VirtualHost>
systemctl restart httpd

 

8. Apache的语言支持

8.1 html   ## 默认支持 

8.2 php

vim /var/www/html/index.php
###
<?php
    phpinfo();
?>
###
## 此时会发现显示内容为空,这是因为我们系统中此时还没有php
dnf install php -y
systemctl restart httpd
firefox http://192.168.43.20/index.php

8.3 perl

mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi   ## 编写cgi文件
###
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello,World.";
###
###
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date` ;
###
chmod +x /var/www/html/cgidir/index.cgi

 

但是这个时候我们在浏览器上访问会发现浏览器上读取的直接是文件中的内容而并非执行后的结果,这显然不是我们想要的结果。

 

vim /etc/httpd/conf.d/VirtualHost.conf
###
<Directory "/var/www/html/cgidir">
           Options +ExecCGI
           AddHandler cgi-script .cgi
</Directory>
###

 在这里要注意,selinux若处于Enforcing状态,会出现如下结果:

所以在浏览器访问之前,我们一定要将selinux状态修改为Permissive或disable状态,以防对实验结果产生影响

9. Apache的加密访问

openssl genrsa -out /etc/pki/tls/private/www.dsd.com.key 2048   ## 生成私钥

openssl req -new -key /etc/pki/tls/private/www.dsd.com.key -out /etc/pki/tls/certs/www.dsd.com.csr   ## 生成证书签名文件

openssl x509 -req -days 365 -in /etc/pki/tls/certs/www.dsd.com.csr -signkey /etc/pki/tls/private/www.dsd.com.key -out /etc/pki/tls/certs/www.dsd.com.crt   ## 生成证书

x509       ## 证书格式
-req       ## 请求
-in        ## 加载签证名称
-signkey /etc/pki/tls/private/www.dsd.com.key   ## 指定签证key文件

cd /etc/httpd/conf.d/
vim Vhost.conf
cat Vhost.conf
###编辑内容
<VirtualHost *:443>
	ServerName login.dsd.com
	DocumentRoot "/www/dsd.com/login"
	CustomLog logs/login.log combined
	SSLEngine on
	SSLCertificateFile /etc/pki/tls/certs/www.dsd.com.crt
	SSLCertificateKeyFile /etc/pki/tls/private/www.dsd.com.key
</VirtualHost>
<VirtualHost *:80>
	ServerName login.dsd.com
	RewriteEngine on
	RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>
###结束
^(/.*)$        ## 客户主机在浏览器地址栏中输入的字符
https://       ## 强制客户访问加密
%{HTTP_HOST}   ## 客户主机
$1             ## 表示RewriteRule后接入的第一串字符 

测试:

 

vim /etc/hosts
###
192.168.43.20 login.dsd.com
###
firefox http://login.dsd.com
当访问此网页时会自动跳转到https://login.dsd.com

10. Squid+Apache

10.1 squid正向代理【代购式】

实验环境:

单网卡主机设定ip不能上网

双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网

实验效果:

让单网卡主机不能上网,但是浏览器可以访问互联网页

测试:

## 双网卡主机中
dnf install squid -y
vim /etc/squid/squid.conf
###
59 http_access allow all
65 cache_dir ufs /var/spool/squid 100 16 256
###

systemctl restart squid
firewall-cmd --permanent --add-port=3128/tcp   ## 设置端口,用来接入
firewall-cmd --reload   ## 刷新

在单网卡主机浏览器中如下操作:

测试结果:

在单网卡主机中

ping www.baidu.com 不通

但是在浏览器中可以访问www.baidu.com

10.2 squid反向代理【分店式】

实验环境:

192.168.43.20   ## Apache服务器

192.168.43.21   ## squid,没有数据负责缓存

vim /etc/squid/squid.conf
###
62 http_port 80 vhost vport                         ## vhost支持虚拟域名,vport支持虚拟端口
63 cache_peer 192.168.43.20 parent 80 0 proxy-only  ## 当192.168.43.21端口被访问会从192.168.43.20端口缓存数据
###
systemctl restart squid

 

测试: 

firefox http://192.168.43.21   ## 访问看到的是192.168.43.20上的数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值