Linux系统工程师 3.3 -- Apache http协议

目录

Apache http协议

1、Apache的作用

2、Apache的安装启动

 3、Apache的基本信息

4、Apache的基本配置

 1.Apache端口修改

2.默认发布文件

3.默认发布目录

5、Apache的访问控制

1.基于客户端ip的访问控制

2.基于用户认证

 6、Apache的虚拟主机

7、Apache的语言支持

#php#

#cgi#

#wsgi#

8、Apache的加密访问

9、 Squid+Apache

#squid 正向代理#

#squid 反向代理#


Apache http协议

1、Apache的作用

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

“  http://  ” 超文本传输协议
Apache        nginx        stgw        jfe        Tengine

2、Apache的安装启动

dnf install httpd -y

firewall-cmd --permanent --add-service=http      #防火墙中永久开启http服务
firewall-cmd --permanent --add-service=https    #永久开启https服务
firewall-cmd --reload                                           #刷新火墙使设定生效
systemctl enable --now httpd                              #开启服务并设定服务位开机启动
vim /var/www/html/index.html                             #http默认发布文件 

 

 3、Apache的基本信息

服务名称:    httpd
配置文件:    /etc/httpd/conf/httpd.conf    ##主配置文件
            /etc/httpd/conf.d/*.conf    ##子配置文件
默认发布目录: /var/www/html
默认发布文件: index.html      (可以添加多个)
默认端口:    80    #http       (查看端口等信息:netstat -antlupe | grep http)
            443    #https
用户:       apache
日志:       /etc/httpd/logs 

4、Apache的基本配置

 1.Apache端口修改

vim /etc/httpd/conf/httpd.conf                 #配置文件
Listen 80
firewall-cmd --permanent --add-port=80/tcp     #火墙永久添加http端口
firewall-cmd --reload
systemctl restart httpd
http://172.25.254.120:80

2.默认发布文件

vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html         #westos.html文件在前 默认优先访问
systemctl restart httpd

3.默认发布目录

mkdir /westos_apache
vim /westos_apache/index.html

[root@westosa ~]# ls -Zd /var/www/html/     #查看默认发布目录的安全上下文
system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

semanage fcontext -a -t httpd_sys_content_t '/westos_apache(/.*)?'    #更改安全上下文
restorecon -RvvF /westos_apache/              #刷新并详细显过程

vim /etc/httpd/conf/httpd.conf
DocumentRoot "/westos_apache"          #更改默认发布目录为/westos_apache,默认发布该文件夹中的index.html文件
<Directory "/westos_apache">            
     Require all granted                #允许所有人登录 (denied不允许登录)
</Directory>

systemctl restart httpd                #重启服务

5、Apache的访问控制

1.基于客户端ip的访问控制

ip白名单#
<Directory "/var/www/html">
        Order Deny,Allow                先Deny,再Allow
        Allow from 172.25.254.20        允许20号主机访问
        Deny from All                   所有人都不允许     
</Directory>

ip黑名单#
<Directory "/var/www/html">
        Order Allow,Deny                先Allow再Deny
        Allow from All                  允许所有人访问
        Deny from 172.25.254.20         20号不允许
</Directory>

2.基于用户认证

vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
        AuthUserfile /etc/httpd/htpasswdfile            ##指定认证文件
        AuthName "Please input your name and password"        ##认证提示语
        AuthType basic                                ##认证类型
        Require user admin1 admin2          ##允许通过的认证用户
        Require valid-user                           ##允许所有用户通过认证(指定用户和所有用户2选1)
</Directory>

htpasswd -cm /etc/httpd/htpasswdfile admin            ##生成认证文件
注意:
当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容

  密码错误报错如下:

 6、Apache的虚拟主机

mkdir -p /var/www/vhost/westos.org/{news,wenku}
echo "wenku 123" > /var/www/vhost/westos.org/wenku/index.html
echo "news 123" > /var/www/vhost/westos.org/news/index.html

vim /etc/httpd/conf/httpd.conf        #设置默认发布目录
DocumentRoot "/var/www/vhost"       

vim /etc/httpd/conf.d/vhost.conf

<VirtualHost _default_:80>
    DocumentRoot "/var/www/html"
    CustomLog logs/default.log combined
</VirtualHost>
<VirtualHost *:80>
  ServerName wenku.westos.org
  DocumentRoot /var/www/vhost/westos.org/news
  CustomLog logs/wenku.log combined
</VirtualHost>
<VirtualHost *:80>
  ServerName news.westos.org
  DocumentRoot /var/www/vhost/westos.org/news
  CustomLog logs/news.log combined
</VirtualHost>

测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.20  news.westos.org  wenku.westos.org

ping news.westos.org
firefox: news.westos.org

7、Apache的语言支持

 LAMP Linux+Apahce+Mysql+PHP+Perl+Python

#php#

vim /var/www/html/index.php
<?php
    phpinfo();
?>

dnf install php -y
systemctl restart httpd
firefox: 172.25.254.220/index.php

#cgi#

mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/index.cgi        #一个显示当前时间的代码
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;

chmod +x index.cgi          # 给执行权限

 ./index.cgi                         #在shell中查看发布文件是否能显示时间

[root@westosb cgidir]# ls -Zd /var/www/cgi-bin/                                        #更改安全上下文及刷新
system_u:object_r:httpd_sys_script_exec_t:s0 /var/www/cgi-bin/
[root@westosb cgidir]# semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html/cgidir(/.*)?'
[root@westosb cgidir]# restorecon -RvvF /var/www/html/cgidir/

vim /etc/httpd/conf.d/vhost.conf

<Directory "/var/www/html/cgidir">
    Options +ExecCGI
    AddHandler cgi-script .cgi
</Directory>

firefox http://172.25.254.220/cgidir/index.cgi

#wsgi#


书写wsgi的测试文件
vim /var/www/html/wsgi/index.wsgi          (python脚本缩进对齐很重要)
def application(env, westos):
  westos('200 ok',[('Content-Type', 'text/html')])
  return [b'hello  westos ahhahahahah!']

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

dnf install python3-mod_wsgi
systemctl restart httpd

vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
    ServerName wsgi.westos.org
    WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>

测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.220 wsgi.westos.org

8、Apache的加密访问

##安装加密插件
dnf install mod_ssl -y

mkdir /etc/httpd/tls

##生成证书
openssl req --newkey rsa:2048 -nodes -sha256 -keyout /etc/httpd/tls/westos.org.key -x509 -days 365 -out /etc/httpd/tls/westos.org.crt

mkdir /var/www/vhost/westos.org/login
vim /var/www/vhost/westos.org/login/index.html
login.westos.org

vim /etc/httpd/conf.d/vhosts.conf
<VirtualHost *:80>
  ServerName login.westos.org
  RewriteEngine On
  RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>

<VirtualHost *:443>
  ServerName login.westos.org
  DocumentRoot /var/www/vhost/westos.org/login
  CustomLog logs/login.log combined
  SSLEngine on
  SSLCertificateFile /etc/httpd/tls/westos.org.crt
  SSLCertificateKeyFile /etc/httpd/tls/westos.org.key
</VirtualHost>

^(/.*)$        ##客户地址栏中输入的地址
%{HTTP_HOST}    ##客户主机
$1        ##RewriteRule后面跟的第一串字符的值

systemctl restart httpd

主机:vim /etc/hosts
172.25.254.220   music.westos.org news.westos.org map.westos.org wsgi.westos.org login.westos.org

 

 

##command 方法2

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

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

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

x509 证书格式
-req 请求
-in 加载签证名称
-signkey    /etc/pki/tls/private/www.westos.com.key

9、 Squid+Apache

#squid 正向代理#

上网主机:
    配置ip可以上网 加网关和DNS
    安装squid
    vim /etc/squid/squid.conf
    systemctl restart squid

    firewall-cmd --permanent --add-service=squid
    firewall-cmd --reload

不能上网主机:
    配置ip不能上网
    下载火狐firefox
    更改浏览器配置:Preference->General->最下面Settings->
        ✅ Manual proxy configuration
        Http Proxy 172.25.254.220    Port 3128
        ✅ Use this proxy server for all protocols

测试:    ping www.baidu.com    不通
    在浏览器中访问www.baidu.com可以

#squid 反向代理#

上网主机(代理主机)    ##squid,没有数据负责缓存
    vim /etc/squid/squid.conf   
    http_port 80 vhost vport    ##vhost 支持虚拟域名 vport 支持虚拟端口
    cache_peer 172.25.254.120 parent 80 0 proxy-only    #当172.25.254.220的80端口被访问,会从172.25.254.120的80端口缓存数据
    systemctl restart squid.service

    firewall-cmd --permanent --add-service=http
    firewall-cmd --reload

不能上网主机(资源机)    ##Apache服务器
    安装httpd
    火墙添加http
    echo 120号的资源 > /var/www/html/index.html   添加网页资源

测试:
    真机中 firefox http:/172.25.254.220
    访问看到的时172.25.254.120上的数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值