Apache的管理与优化

一、Apache的作用

在web被访问时通常使用http://的方式进行访问
http:超文本传输协议
超文本传输协议所需要的软件:

  • Apache
  • nginx
  • stgw
  • jfe
  • Tengine

二、Apache安装

安装命令:dnf install http.x86_64 -y

三、Apache的启动

目的命令
设置httpd服务开机自动启动systemctl enable --now httpd
查看防火墙信息firewall-cmd --list-all
在火墙中永久开启http访问firewall-cmdpermanent --add-service=http
在火墙中永久开启https访问firewall-cmd --permanent --add-service=https
使防火墙设定生效firewall-cmd --reload

四、Apache的基本信息

Apache的服务名称:httpd

配置文件地址
主配置文件/etc/httpd/conf/httpd.conf
子配置文件/etc/httpd/conf.d/*.conf
http的默认发布目录/var/www/html
默认发布文件index.html

默认端口:
http默认端口为:80
https默认端口为:443
用户名:apache
日志地址:/etc/httpd/logs

五、Apache的基本配置

5.1 Apache端口修改

Apache的端口修改

  • vim /etc/httpd/conf/httpd.conf :进入主配置文件
    listen 1111 :将端口设置为1111
    在这里插入图片描述
    firewall-cmd --permanent --add-port=1111/tcp:将1111端口添加到防火墙中
    firewall-cmd --reload:使防火墙设定生效
    semanage port -l | grep http:查找http使用的端口
    semanage port -a -t http_port_t -p tcp 1111:添加 1111为http可用端口
    在这里插入图片描述
    systemctl restart httpd 重启httpd服务
    在这里插入图片描述

5.2 默认文件发布

vim /etc/httpd/conf/httpd.conf :进入主配置文件
DirectoryIndex westos.html index.html :设定westos.html优先发布
在这里插入图片描述
systemctl restart httpd:重启服务
在这里插入图片描述

5.3 默认目录发布

vim /etc/httpd/conf/httpd.conf
DocumentRoot “/westos/html”
<Directory “/westos/html”>
Require all granted
< /Directory >
在这里插入图片描述
semanage fcontext -a -t httpd_sys_content_t ‘/westos(/.*)?’
restorecon -RvvF /westos/

六、Apache的访问控制

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

ip白名单:
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from 172.25.254.4
Deny From All
< /Directory>
在这里插入图片描述
ip黑名单:
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from All
Deny From 172.25.254.4
< /Directory>
在这里插入图片描述

6.2 基于用户认证

vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/westos”>
AuthUserFile /etc/httpd/htpasswdfile
AuthName “user and password”
AuthType basic
Require user admin / Require valid-user
< /Directory>
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

七、Apache的虚拟主机

mkdir -p /var/www/westos.com/{news,bbs,tieba}
mkdir -p /var/www/westos.com/{news,bbs,tieba}/html
echo news.westos.com > /var/www/westos.com/news/html/index.html
echo bbs.westos.com > /var/www/westos.com/bbs/html/index.html
echo tieba.westos.com > /var/www/westos.com/tieba/html/index.html
vim /etc/httpd/conf.d/vhost.conf
< VirtualHost default:80>
DocumentRoot /var/www/html
Customlog logs/default.log combined
< /VirtualHost>

<VirtualHost *:80>
Servername news.westos.com
DocumentRoot /var/www/westos.com/news/html
Customlog logs/news.log combined
< /VirtualHost>

<VirtualHost *:80>
Servername bbs.westos.com
DocumentRoot /var/www/westos.com/bbs/html
Customlog logs/bbs.log combined
< /VirtualHost>

<VirtualHost *:80>
Servername tieba.westos.com
DocumentRoot /var/www/westos.com/tieba/html
Customlog logs/tieba.log combined
< /VirtualHost>
systemctl restart httpd

在/etc/hosts文件中编辑地址索引
在这里插入图片描述

在这里插入图片描述

查询结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

编写文件完毕后在客户端登陆网页查看结果

八、Apache的语言支持

php:
vim /var/www/html/index.php

<?php phpinfo ?>

dnf install php -y
systemctl restart httpd
cgi:
vim /var/www/html/cgi/index.cgi
#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print date;
在这里插入图片描述
vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/html/cgi”>
Options +ExecCGI
AddHandler cgi-script .cgi
DirectoryIndex index.cgi
< /Directory>
在这里插入图片描述
在这里插入图片描述

九、Apache的加密访问

9.1 安装加密插件

dnf install mod_ssl -y
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 :生成证书
在这里插入图片描述

vim /etc/httpd/conf.d/vhost.conf
<VirtualHost :80>
Servername login.westos.com
RewriteEngine on
RewriteRule ^(/.
)$ https://%{HTTP_HOST}$1
< /VirtualHost>
<VirtualHost *:443>
Servername login.westos.com
DocumentRoot /var/www/westos.com/login/html
Customlog logs/login.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
< /VirtualHost>
systemctl restart httpd
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

十、正向代理与反向代理

10.1 squid正向代理

squid和apache服务只能同时开一个
在一台新创建的虚拟机上下载squid
dnf install squid -y
在这里插入图片描述

vim /etc/squid/squid.conf
http_access allow all
cache_dir ufs /var/spool/squid 100 16 256
在这里插入图片描述

systemctl restart squid.service
在Apache主机上选择打开firefox并选择NetWork Proxy
选择正确选项后输入172.25.254.204(squid主机id) 3128(端口号)
在这里插入图片描述

10.2 squid反向代理

**vim /etc/squid/squid.conf
http_port 80 vhost vport
cache_peer 172.25.254.104 parent 80 0 proxy-only
在这里插入图片描述

systemctl restart squid.service
在172.25.254.104的index.html写入172.25.254.104
访问172.25.254.204时会显示172.25.254.104的index.html内容在这里插入图片描述

在Apache和squid主机外使用一台主机打开firefox输入172.25.254.204:80(squid主机id)会转到172.25.254.104:80(Apache主机id)并显示内容

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值