Linux中Apache的管理和优化web

Apache的作用

在web被访问时通常使用http://的方式
http:// 超文本传输协议

http:// 超文本传输协议提供软件:
Apache
nginx
stgw
jfe
Tengine

Apache的安装
dnf install httpd.x86_64 -y
在这里插入图片描述

Apache的启用

systemctl enable --now httpd 开启服务并设定服务位开机启动
firewall-cmd --permanent --add-service=http 在火墙中永久开启http访问
firewall-cmd --reload 刷新火墙使设定生效
firewall-cmd --list-all 查看火墙信息
在这里插入图片描述

在firefox中:172.25.254.106
会出现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的基本配置
Apache端口修改
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 1111
systemctl restart httpd
在这里插入图片描述

在这里插入图片描述

默认发布文件

在10中:
vim /etc/hosts
在这里插入图片描述

在20中:
vim /var/www//html/index.html
在这里插入图片描述

此时在10中可访问到默认文件内容
在这里插入图片描述

若想修改默认发布文件可按如下操作将发布文件修改为test.html:
vim test.html
在这里插入图片描述

vim /etc/httpd/conf/httpd.conf
167 DirectoryIndex test.html westos.html
systemctl restart httpd
在这里插入图片描述

此时在10中访问www.westos.com可访问到新的发布文件
在这里插入图片描述

默认发布目录
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
在这里插入图片描述

firefox http://www.westos.com
在这里插入图片描述

Apache的访问控制
实验素材
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
< h1>westosdir’s page< /h1>
firefox http://172.25.254.106/westos

基于客户端ip的访问控制
ip白名单
vim /etc/httpd/conf/httpd.conf
<Directory “/var/www/html/westos”>
Order Deny,Allow
Allow from 172.25.254.6
Deny from All
< /Directory>
在这里插入图片描述

此时106不能访问,6可访问
在这里插入图片描述在这里插入图片描述

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

此时6不可访问,其他可访问
在这里插入图片描述

基于用户认证
vim /etc/httpd/conf/httpd.conf<Directory “/var/www/html/westos”>
AuthUserfile /etc/httpd/.http_user 指定认证文件
AuthName “Please input your name and password” 认证提示语
AuthType basic 认证类型
Require user admin 允许通过的认证用户 2选1
Require valid-user 允许所有用户通过认证 2选1
< /Directory>
在这里插入图片描述

htpasswd -cm .http_user admin 生成认证文件
在这里插入图片描述

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

此时访问westosdir需键入密码以验证用户身份
在这里插入图片描述

Apache的虚拟主机
mkdir -p /var/www/westos.com/{news,music}
echo “music’s page” >/var/www/westos.com/music/index.html
echo “news’s page” > /var/www/westos.com/news/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 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>
在这里插入图片描述

测试:
在浏览器所在主机中
vim /etc/hosts
172.25.254.106 www.westos.com music.westos.com news.westos.com
在这里插入图片描述

firefox http://www.westos.com
firefox http://music.westos.com
firefox http://news.westos.com
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Apache的语言支持
php
vim /var/www/html/index.php

<?php phpinfo(); ?>

在这里插入图片描述

dnf install php -y
systemctl restart httpd
firefox http://172.25.254.106/index.php
在这里插入图片描述

cgi
dnf install httpd-manual
systemctl restart httpd
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 index.cgi

vim /etc/httpd/conf.d/vhost.conf
<Directory “/var/www/html/cgidir”>
Options +ExecCGI
AddHandler cgi-script .cgi
< /Directory>
在这里插入图片描述

firefox http://www.westos.com/cgi/index.cgi
在这里插入图片描述

Apache的加密访问
安装加密插件
dnf install mod_ssl -y 生成证书
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

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

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

systemctl restart httpd

https://www.westos.com
下载证书后查看证书信息,发现证书已生成
在这里插入图片描述

加密虚拟主机
vim /etc/httpd/conf.d/vhosts.conf
在这里插入图片描述

systemctl restart httpd
在这里插入图片描述

网页重写功能

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

此时浏览music.westos.com会自动加密
在这里插入图片描述

Squid+Apache
squid 正向代理
实验环境:
单网卡主机node1172.25.254.6设定ip不能上网
双网卡主机node2172.25.254.206设定ip1可以连接单网卡主机,设定ip2可以上网

实验效果
让单网卡主机不能上网但浏览器可以访问互联网页

操作:
在双网卡主机206中
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
firewall-cmd --permanent --add-port=3128/tcp
firewall-cmd --reload
在这里插入图片描述

在单网卡主机20中选择
NetWork Proxy
在这里插入图片描述

172.25.254.30 3128
在这里插入图片描述

测试:
在单网卡主机中
ping www.baidu.com 不通

在浏览器中访问www.baidu.com可以
在这里插入图片描述

squid反向代理
实验环境:
172.25.254.6 Apache服务器
172.25.254.206 squid,没有数据负责缓存

vim /etc/squid/squid.conf
http_port 80 vhost vport vhost 支持虚拟域名 vport 支持虚拟端口
当172.25.254.206的80端口被访问会从172.25.254.6的80端口缓存数据
cache_peer 172.25.254.6parent 80 0 proxy-only
systemctl restart squid
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

在这里插入图片描述

测试:
在106中:
firefox http://172.25.254.206
访问看到的时172.25.254.6上的数据
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42958401/article/details/108087653
今日推荐