Apache的管理及优化web

1.Apache的作用
在web被访问时通常使用http://的方式
http://        ##超文本传输协议
http:// 超文本传输协议提供软件:
Apache     ##百度
nginx      ##网易,火狐,爱奇艺,360
stgw       ##腾讯
jfe        ##京东 
Tengine    ##阿里,新浪,字节跳动
curl -I baidu.com:显示网页的头信息
2.Apache的安装  
dnf install httpd.x86_64 -y

3.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                 ##刷新火墙使设定生效

4.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
5.Apache的基本配置
(1)Apache端口修改
vim /etc/httpd/conf/httpd.conf

        Listen 8080

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
systemctl restart httpd
http://172.25.254.100:8080
(2.)默认发布文件
vim /etc/httpd/conf/httpd.conf
DirectoryIndex westos.html index.html
systemctl restart httpd
默认的发布文件是index.html

将默认发布文件修改为westos.html 

(3).默认发布目录
mkdir /var/www/westos
mv /var/www/westos /var
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/var/westos"
<Directory "/var/westos">
              Require all granted
</Directory>
systemctl restart httpd

firefox http://192.168.2.100:8080

6.Apache的访问控制

实验素材
mkdir /var/www/html/westos
vim /var/www/html/westos/index.html
<h1>westosdir's page</h1>
firefox http://192.168.2.100/westos

 
(1.)基于客户端ip的访问控制
ip白名单
<Directory "/var/www/html/westos">
              Order Deny,allow
              Allow from 192.168.2.100
              Deny from All
</Directory>
将192.168.2.100主机放进白名单

192.168.2.100主机可以访问

ip黑名单
<Directory "/var/www/html/westos">
              Order Allow,Deny
              Allow from all
              Deny from 192.168.0.10
</Directory>
将192.168.2.100主机放进黑名单

192.168.2.100主机不可访问

(2).基于用户认证
htpasswd -cm /etc/httpd/htpasswdfile admin                       ##生成认证文件
注意: 当/etc/httpd/htpasswdfile存在那么在添加用户时不要加-c参数否则会覆盖源文件内容
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>
只允许admin用户通过

允许所有用户通过

7.Apache的虚拟主机 
mkdir /var/www/virtual/westos.org/{news,bbs}/html -p
echo bbs.westos.org > /var/www/virtual/westos.org/bbs/html/index.html
echo news.westos.org > /var/www/virtual/westos.org/news/html/index.html
echo default's page > /var/www/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.org
    DocumentRoot /var/www/virtual/westos.org/news/html
    CustomLog logs/news.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerName  bbs.westos.org
    DocumentRoot /var/www/virtual/westos.org/bbs/html
    CustomLog logs/bbs.log combined
</VirtualHost>
测试
在浏览器所在主机中
vim /etc/hosts
192.168.2.100 www.westos.com wenku.westos.ocm news.westos.com
  
firefox http://www.westos.org
firefox http://bbs.westos.org
firefox http://news.westos.org

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

(1)#php#

<?php
              phpinfo();
?>
dnf install php -y
systemctl restart httpd
firefox http://192.168.0.11/index.php
Apache的说明
dnf install httpd-manual -y
systemctl restart httpd

(2) #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`;

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

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

systemctl restart httpd
chmod +x /var/www/html/cgi/index.cgi

firefox http://192.168.2.100/cgi/index.cgi

(3) #wsgi#
书写wsgi的测试文件
vim /var/www/html/wsgi/index.wsgi
def application(env, westos):
      westos('200 ok',[('Content-Type', 'text/html')])
      return [b'hello westos ahhahahahah!']
dnf install python3-mod_wsgi
systemctl restart httpd
 
vim /etc/httpd/conf.d/vhost
<VirtualHost *:80>
           ServerName wsgi.westos.org
           WSGIScriptAlias / /var/www/html/wsgi/index.wsgi
</VirtualHost>
 
测试:
在浏览器所在主机中
vim /etc/hosts
192.168.2.100 wsgi.westos.org

 9.Apache的加密访问
安装加密插件
dnf install mod_ssl -y
生成证书
##command 1
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

##command 2
openssl req --newkey rsa:2048  -nodes -sha256 -keyout /etc/httpd/certs/westos.org.key 
-x509 -days 365 -out /etc/httpd/certs/westos.org.crt
  
vim /etc/httpd/conf.d/ssl.conf

 

 
10.http转https
mkdir /var/www/virtual/westos.org/login
echo login.westos.org > /var/www/virtual/westos.org/login/index.html

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

<VirtualHost *:443>
     ServerName login.westos.org
     DocumentRoot /var/www/virtual/westos.org./login
     SSLEngine on
     SSLCertificateFile /etc/httpd/certs/westos.org.crt
     SSLCertificateKeyFile /etc/httpd/certs/westos.org.key
</VirtualHost>

<VirtualHost *:80>
     ServerName login.westos.org
     RewriteEngine on
     RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1
</VirtualHost>

systemctl restart httpd

^(/.*)$                        ##客户地址栏中输入的地址
%{HTTP_HOST}      ##客户主机
$1                            ##RewriteRule后面跟的第一串字符的值
测试:
在浏览器所在主机中
vim /etc/hosts

11.cdn代理加速

在一台没有Apache的主机中下载squid使其可以缓存想要获取的数据,修改squid配置文件,以达到没有Apache但是可以访问到资源的目的
在有 Apache的主机共享目录中,编写一个文件
在没有 Apache的主机中可以访问到
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溪野~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值