Apache的部署及优化web
Apache的部署及优化web
1.Apache的基本信息
Apache:对外提供超文本传输协议的一种软件
超文本传输协议:提供网络资源的共享 在web被访问时通常使用http://的方式
http:// ##超文本传输协议
http:// 超文本传输协议
提供软件:Apache,nginx,stgw,jfe,Tengine
curl -I 网站
#可以查看被访问网站是由什么软件提供的超文本传输协议服务名称:httpd
配置文件:
/etc/httpd/conf/httpd.conf ##主配置文件
/etc/httpd/conf.d/*.conf ##子配置文件
默认发布目录:/var/www/html
默认发布文件:index.html
默认端口:80 #http
443 #https
用户:apache
日志:/etc/httpd/logs
2.Apache的安装
dnf search http ##搜索软件
dnf install httpd.x86_64 -y ##安装阿帕奇
- 1.开启服务
systemctl enable --now httpd
##设定当前开启阿帕奇服务,并且开机时也开启阿帕奇
- 2.设定火墙策略
firewall-cmd --permanent --add-service=http
##在火墙中永久开启http访问
firewall-cmd --permanent --add-service=https
##在火墙中永久开启https访问
firewall-cmd --reload
##刷新火墙,使永久设定策略生效
firewall-cmd --list-all
##查看火墙信息,表示阿帕奇在火墙策略中对外开放
- 3.看到设定成功,显示此页面(此页面为阿帕奇默认页面)
3.Apache的基本配置
实验环境准备:
gentenforce
vim /etc/sysconfig/selinux
systemctl restart httpd
##关闭selinux服务关闭,系统重启
更改Apache端口
listen 80---->8080
netstat -antlupe | grep httpd
getenforce
vim /etc/httpd/conf/httpd.conf
firewall-cmd --add-port=8080/tcp
firewall-cmd --list-all
systemctl restart httpd
netstat -antlupe | grep httpd
192.168.1.111/8080##默认端口访问不到,必须要端口号8080才能访问
默认发布文件的管理
cd /var/www/html
vim index.html
#默认发布文件,并且默认发布文件需在默认发布目录中
vim /etc/httpd/conf/httpd.conf
##搜索Index(167行)
##更改默认发布文件为两个:yitian、index.html,前面的优先级高于后面的
cd /var/www/html
vim yitian
cat yitian
vim index.html
cat index.html
##当yitian存在即访问yitian
##当yitian不存在,访问index.html
默认发布目录的管理
cd /var/www/html
ls
mkdir yitian
ls
vim /var/www/yitian/index.html
cat /var/www/yitian/index.html
vim /etc/httpd/conf/httpd.conf
##搜索Doc(122行)
<Directory “var/www/yitian”>
Require all granted
</Directory>
##授权更改目录可以被任何人访问
访问192.168.1.11 ##更改默认发布目录
4.Apache的访问控制
vim /etc/httpd/conf/httpd.conf
##搜索Doc(122行)
<Directory "/var/www/html/yitian">
Order Allow,Deny
Allow from All
Deny from 192.168.1.11
</Directory>
Allow和Deny的读取顺序:
允许任何人访问yitian
不允许192.168.1.11访问yitian
##因为先读Allow后读Deny那么Deny里面的信息会覆盖Allow中的信息
<Directory "/var/www/html/yitian">
Order Allow,Deny
Allow from 192.168.1.11
Deny from all
</Directory>
只有192.168.1.11主机可以访问
5.基于用户的访问方式
- 建立apache的目录认证文件
cd /var/www/html
htpasswd -cm /etc/httpd/.htpasswd admin
##当认证文件不存在时,需要加-c参数
htpasswd -m /etc/httpd/.htpasswd lee
##当认证文件存在时,加-c参数会删除原有内容
cat /etc/httpd/.htpasswd
##查看用户认证文件
基本认证
只有admin用户可以通过认证
所有用户可以通过认证
<Directory "/var/www/html/yitian”>
AuthUserFile /etc/httpd/.htpasswd
##指定认证文件
AuthName “Please input username and passwd”
##指定认证提示
AuthType basic
##指定认证类型
# Require user admin
##指定认证用户
##认证文件中admin用户可以通过认证
Require valid-user
##认证文件中的所有用户都可以通过认证
##两个参数指定写一个就好
浏览器信息清空
ctrl+shift+delete
再次登录时需要认证
6.Apache的虚拟主机
cd /var/www/html
mkdir -p /var/www/westos.org/{linux,shell,python}
echo linux.westos.org > /var/www/westos.org/linux/index.html
echo shell.westos.org > /var/www/westos.org/shell/index.html
echo python.westos.org > /var/www/westos.org/python/index.html
cat /var/www/westos.org/linux/index.html
cat /var/www/westos.org/shell/index.html
cat /var/www/westos.org/python/index.html
##建立apache虚拟主机
##编写apache虚拟主机的默认发布目录和默认发布文件
cd /etc/httpd/conf.d/
##进入apache的自配置目录
vim vhost.conf
##指定子配置文件
<VirtualHost _default_:80>
DocumentRoot /var/www/html
Customlog logs/default.log combined
</VirtualHost>
##Apache默认主机
<VirtualHost *:80> ##Apache虚拟主机
ServerName linux.westos.org ##虚拟主机域名
DocumentRoot /var/www/westos.org/linux ##虚拟主机默认发布目录
Customlog logs/linux.log combined
##虚拟主机日志
##combined表示混合型日志
</VirtualHost>
<VirtualHost *:80>
ServerName shell.westos.org
DocumentRoot /var/www/westos.org/shell
Customlog logs/shell.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName python.westos.org
DocumentRoot /var/www/westos.org/python
Customlog logs/python.log combined
</VirtualHost>
7.Apache的语言支持
php
apache语法模块
除了支持html还支持php语言
apache只能发布共享静态页面
vim /var/www/html/index.php
<?php
phpinfo();
?>
dnf install php -y
systemctl restart httpd
访问http://192.168.1.11/index.php
cgi
cgi调用,执行脚本之后产生结果,再发送给apache
mkdir /var/www/html/cgidir
vim /var/www/html/cgidir/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/cgidir">
Options +ExecCGI
AddHandler cgi-script .cgi
##从什么样的脚本触发
##脚本类型:cgi脚本都会执行这个动作
</Directory>
- 安装apache的帮助
- 给cgi执行权限:使cgi发布出去
访问http://192.168.1.11/cgidir/index.cgi
wsgi
用python书写,让其支持python语言
对类型作说明
- 1.书写wsgi的测试文件
mkdir wsgi
cd wsgi/
ls
vim /var/www/html/wsgi/index.wsgi
chmod +x index.wsgi
- 2.安装软件,重启httpd服务
dnf install python3-mod_wsgi-4.6.4-4.el8.x86_64 -y
systemctl restart httpd
- 3.编辑子配置文件
vim /etc/httpd/conf.d/vhost
8.Apache的加密访问
访问apache都是明文方式的数据包传输
用一把锁把明文的账户和密码包装起来
用apache服务器上的钥匙将锁打开
dnf install mod_ssl -y
##安装加密时用到的插件
##使apache支持https
cd /etc/httpd/conf.d/
ls
##在子配置目录中找到加密的配置文件
systemctl restart httpd
##重启完成之后就会有加密服务
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
firewall-cmd --list-all
##火墙默认不支持加密,修改火墙策略,设定火墙支持https
当前浏览器没有上锁
当前的锁不是特别安全,没有经过CA机构认证
查看锁的信息
将下载好的锁删掉:选择配置
删除后,又一次出现让你下载锁的界面
apache加密访问
-
1.生成配置文件
vim /etc/httpd/conf.d/ssl.conf
-
2.生成key
mkdir -p /etc/httpd/webkey
openssl genrsa -out /etc/httpd/webkey/www.westos.org.key 2048
- 3.对文件进行签证,证书相当于给CA机构提交请求
openssl req -new -key /etc/httpd/webkey/www.westos.org.key -out /etc/httpd/webkey/www.westos.org.csr
中国陕西省西安
机构为westos,部门为linux
ServerName为www.westos.org,邮箱为lee@westos.org
密码设定为两次回车
- 4.模仿CA机构授权
openssl -x509 -req -day 365 -in /etc/httpd/webkey/www.westos.org.csr -signkey /etc/httpd/webkey/www.westos.org.key -out /etc/pki/tls/certs/www.westos.org.crt
##生效时间为365天,输入请求文件,对/etc/httpd/webkey/www.westos.org.key文件签名,生成的证书为/etc/pki/tls/certs/www.westos.org.crt
x509 证书格式
-req 请求
-in 加载签证名称
mkdir /var/www/westos.org/login -p
echo login.westos.org > /var/www/westos.org/login/index.html
##只有在登陆网站时需要加密,许多页面是不需要加密的
/var/log/messages
cat /var/log/messages
journalctl -xe
##重启apache时,出现错误:提示生成key文件不存在
cp /etc/pki/tls/certs/www.westos.org.crt /etc/httpd/webkey/www.westos.org.crt
注意: 在生成文件时注意位置
openssl -x509 -req -day 365 -in /etc/httpd/webkey/www.westos.org.csr -signkey /etc/httpd/webkey/www.westos.org.key -out /etc/httpd/webkey/www.westos.org.crt
80端口自动转到443端口
访问此页面时,强制自动跳转加密
^(/.*)$ ##客户端地址栏中输入的地址
%{HTTP_HOST} ##客户主机
$1 ##RewriteRule后面跟的第一串字符的值