企业中Apache的管理及优化(web)
一.Apache的作用
1.Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,是目前世界上使用最广泛的一种web server,它以跨平台,高效和稳定而闻名,可以运行在几乎所有广泛使用的计算机平台上。Apache的特点是简单、速度快、性能稳定,并可做代理服务器来使用。Apache是用C语言开发的基于模块化设计的web应用,总体上看起来代码的可读性高于php代码,它的核心代码并不多,大多数的功能都被分割到各种模块中,各个模块在系统启动时按需载入。支持SSL技术,支持多个虚拟主机。Apache是以进程的Prefork模式(还有基于线程的Worker模式)为基础的结构,进程要比线程消耗更多的系统开支,不太适合于多处理器环境,因此,在一个Apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。在web中访问网页时,通常使用http://的方式(超文本传输协议提供的软件)ttp:// 超文本传输协议提供软件: Apache nginx stgw jfe Tengine;最常见的是前面两种。
2.Apache的特性
几乎可以运行在所有的计算机平台上.
支持最新的http/1.1协议
简单而且强有力的基于文件的配置(httpd.conf).
支持通用网关接口(cgi)
支持虚拟主机.
支持http认证.
集成perl.
集成的代理服务器
可以通过web浏览器监视服务器的状态, 可以自定义日志.
支持服务器端包含命令(ssi).
支持安全socket层(ssl).
具有用户会话过程的跟踪能力.
支持fastcgi
二.Apache的安装
dnf install httpd.x86_64 -y
三.Apache的启用
systemctl enable --now httpd 开启服务并设定服务位开机启动
firewall-cmd --list-all 查看火墙信息
firewall-cmd --permanent --add-service=http 在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https 在火墙中永久开启https访问
firewall-cmd --reload 刷新火墙使设定生效
安装好Apache 后网页访问172.25.254.20的界面,此时还没有配置Apache
四.Apache的基本信息
服务名称: httpd
配置文件:
/etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf.d/*.conf 子配置文件
默认发布目录: /var/www/html
默认发布文件: index.html
默认端口: 80 http
443 https
用户: apache
日志: /etc/httpd/logs
五.Apache的基本配置
1.Apache端口修改(默认端口为80)
操作运用的命令
vim /etc/httpd/conf/httpd.conf
Listen 1111
firewall-cmd --permanent --add-port=1111/tcp
firewall-cmd --reload
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 6666
systemctl restart httpd
(1) vim /etc/httpd/conf/httpd.conf 修改默认端口号为1111
(2)火墙信任1111端口 firewall-cmd --permanent --add-port=1111/tcp
firewall-cmd --reload
修改主配置文件的端口信息后,重启服务起不来
(3)进一步需要添加端口的信息 http_port_t,用到的命令如下
semanage port -l | grep http
semanage port -a -t http_port_t -p tcp 6666
systemctl restart httpd
2.默认发布文件的设定
vim /etc/httpd/conf/httpd.conf 默认发布文件的位置为/var/www/html
建立文件 vim /var/www/html/index.html 写入内容 Welcome for Summer ,重启服务 ,打开浏览器访问172.25.254.20
建立文件 vim /var/www/html/test.html 写入内容 Welcome for linux ,重启服务 ,打开浏览器访问172.25.254.20
当输入为westos.com 时访问不了 此时需要填一下地址解析 /etc/hosts
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/
systemctl restart httpd
(1)首先建立目录 mkdir -p /westos/html 然后编写文件 vim /westos/html/index.html 写入 hello /westos/html 重启服务,打开浏览器,发现内容未改变.
(2)由于SElinux是开启的 故需查看/var/www/html 的安全上下文,并将 /westos的安全上下文修改
(3)vim /etc/httpd/conf/httpd.conf 注释掉原来的目录 并给新的目录授权
(4)重启服务 测试 此时访问的内容发生了改变
六.Apache的访问控制
实验素材:#实验素材#
mkdir /var/www/html/westosdir
vim /var/www/html/westosdir/index.html 写入 Welcome for /var/www/html/westosdir
然后浏览器测试:
1.基于客户端ip的访问控制
(1)ip白名单 进入 vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
Order Deny,Allow
Allow from 172.25.254.20
Deny from All
</Directory>
(2)ip黑名单 进入 vim /etc/httpd/conf/httpd.conf
<Directory "/var/www/html/westos">
Order Allow,Deny
Allow from All
Deny from 172.25.254.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 admin 允许通过的认证用户 2选1
Require valid-user 允许所有用户通过认证 2选1
</Directory>
htpasswd -cm /etc/httpd/htpasswdfile admin ##生成认证文件
注意:当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
操作步骤:
首先通过 htpasswd -cm /etc/httpd/htpasswdfile admin 生成认证文件
然后编写配置文件:
退出重启服务 ,测试
七.Apache的虚拟主机
1.实验素材的建立
mkdir -p /var/www/westos.com/{tieba,xueshu}
echo "welcom to tieba" >/var/www/westos.com/tieba/index.html
echo "welcome to xueshu" > /var/www/westos.com/xueshu/index.html
2.写虚拟主机相关信息文件
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.com
DocumentRoot "/var/www/westos.com/wenku"
CustomLog logs/wenku.log combined</VirtualHost>
<VirtualHost *:80>
ServerName news.westos.com
DocumentRoot "/var/www/westos.com/news"
CustomLog logs/news.log combined
</VirtualHost>
3.重启服务 测试
测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.20 www.westos.com wenku.westos.ocm news.westos.com
firefox http://www.westos.com
firefox http://tieba.westos.com
firefox http://xueshu.westos.com
八.Apache的语言支持
1. vim /var/www/html/index.php 写入
<?php
phpinfo();
?>
访问网页 172.25.254.20/index.php 加载不出来 需要安装php
2.安装php
dnf install php -y
systemctl restart httpd
firefox http://172.25.254.20/index.php
3.建立CGI目录
(1)cgi
mkdir /var/www/html/cgidir
(2)进入 vim /var/www/html/cgidir/index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
此时显示会出现问题 代码直接在外面
(3)vim /etc/httpd/conf.d/vhost.conf
<Directory "/var/www/html/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
firefox http://192.168.0.11/cgidir/index.cgi
九.Apache的加密访问
(1)安装加密插件
dnf install mod_ssl -y
(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 证书
测试结果:
通过上述机密后,xueshu.westos.com 不能加密访问 当做调整后,可以重写直接跳到加密访问
(1)修改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 "/www/westos.com/login"
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
^(/.*)$ ##客户地址栏中输入的地址
%{HTTP_HOST} ##客户主机
$1 ##RewriteRule后面跟的第一串字符的值
2.测试
在不加密的时候 不可直接跳转。
加密但是未进行网页重写
加密后和进行网页重写后直接会跳到https 就能直接访问
十.squid 正向代理(给客户做的)
1.实验环境设定
(1)单网卡主机设定ip不能上网
双网卡主机设定ip1可以连接单网卡主机,设定ip2可以上网
(2)实验效果
让单网卡主机不能上网但浏览器可以访问互联网页
2.具体操作如下
(1)首先安装squid dnf install squid -y 开启开机启动squid服务 systemctl enable --now squid
若火墙是开启的需要设定火墙信任 squid的默认3128端口
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
若火墙未开启,此时无需上面的信任操作
(2)在双网卡主机上 进入squid的主配置文件 vim /etc/squid/squid.conf 编写59 和65行
(3)在单网卡主机的浏览器中进入Network Proxy 设定
(4)测试结果 在单网卡主机上能通过浏览器访问网页,ping www.baidu.com ping 不通
十一.squid的反向代理(给企业做的)
(1)实验环境 10主机为squid服务器 没有数据负责缓存
20主机为Apache服务器
(2)反向代理的原理图 若西安客户需要访问qq 则直接从西安的服务器里面获取数据要快得多,西安的服务器是squid服务器 里面通过配置后 有深圳服务器里面的缓存数据,这种加速叫作cdn加速。
(3)当客户最开始访问squid是时,此时访问不到,因为访问网络端口为80,则需要在squid中开启80端口
在10主机上做如下配置 vim /etc/squid/squid.conf 开启squid的80端口
开启80端口后 在客户机上能访问squid 但是显示没有缓存
(3)要将远程主机及Apache服务器的数据传到squid上 来获得缓存。编写Squid的主配置文件
配置完的测试结果为 客户机能访问数据内容