全文最详细的Apache的管理及优化Web(图文详解)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

前言

一、Apache的安装及启用

二、Apache的基本信息 

三、Apache的基本配置及修改 

1、默认发布文件

2、Apache端口修改

3、默认发布目录

三、Apache的访问控制 

1、基于客户端ip的访问控制 

2、基于用户认证 

四、Apache的虚拟主机

五、Apache的语言支持

六、Apache的加密访问


前言

Apache是世界使用排名第一的Web服务器软件。它可以运行在几乎所有广泛使用的计算机平台上由于其跨平台和安全性被广泛使用是最流行的Web服务器端软件之一。可用于提供超文本传输协议本质上是一个软件。

超文本传输协议http://提供该协议的软件一般有Apache、nginx、stgw、jfe、Tengine而Apache在网页应用中使用比较广泛。

一、Apache的安装及启用

dnf install httpd.x86_64 -y      dnf安装
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的基本信息 

服务名称httpd
配置文件

/etc/httpd/conf/httpd.conf 主配置文件

/etc/httpd/conf.d/*.conf子配置文件

默认发布目录/var/www/html
默认发布文件index.html
默认端口

80http

443 https)

用户:apache
日志:/etc/httpd/logs

三、Apache的基本配置及修改 

1、默认发布文件

 vim /etc/httpd/conf/httpd.conf       编辑主配置文件
DirectoryIndex westos.html index.html      添加默认发布文件westos.html
systemctl restart httpd           重启服务

2、Apache端口修改

vim /etc/httpd/conf/httpd.conf        编辑主配置文件
Listen 8080
firewall-cmd --permanent --add-port=8080/tcp        防火墙设置允许xxxx端口
firewall-cmd --reload        防火墙信息重新加载
systemctl restart httpd        重启httpd服务

 

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.0.11

三、Apache的访问控制 

1、基于客户端ip的访问控制 

素材

vim /etc/httpd/conf/httpd.conf        修改的位置

systemctl restart httpd                   每次修改完后都需要重启服务

1ip白名单       注意白名单时先Deny后Allow才可以因为顺序颠倒的话Deny会把Allow给覆盖掉
<Directory "/var/www/html/westos">
        Order Deny,Allow
        Allow from ip        
        Deny from All
</Directory>

2ip黑名单       注意黑名单时先Allow后Deny才可以因为顺序颠倒的话Allow会把Deny给覆盖掉  
<Directory "/var/www/html/westos">
        Order Allow,Deny
        Allow from All        
        Deny from ip
</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参数否则会覆盖源文件内容

白名单

只有lee可以访问

四、Apache的虚拟主机

在实际情况中不可能每访问一个网页就需要一台主机来安装Apache并部署相关环境虚拟主机实现了同一个ip下有多个站点即可以通过一个ip来访问多个网页内容。

1虚拟主机建立
mkdir /var/www/virutal/westos.org/{news,bbs}/html -p

echo news.westos.org > /var/www/virutal/westos.org/news/html/index.html
echo bbs.westos.org > /var/www/virutal/westos.org/bbs/html/index.html

vim /etc/httpd/conf.d/vhosts.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>

测试

在浏览器中访问以下内容
firefox http://www.westos.org
firefox http://lee.westos.org
firefox http://linux.westos.org

重启服务

五、Apache的语言支持

php
vim /var/www/html/index.php
<?php
        phpinfo();
?>


dnf install php -y
systemctl restart httpd
firefox http://192.168.1.10/index.php


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
</Directory>


firefox http://192.168.1.10/cgidir/index.cgi


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>

php

cgi

首先修改上下文 

找CGI 

实验成功 

wsgi 

最后别忘了给执行权限因为它是脚本

六、Apache的加密访问

网页上用户的个人信息传输如果不经过加密手段安全性将会受到极大的威胁比如说账户密码信息此时就需要一种加密手段能够给用户信息加密相当于给用户信息上锁而这个锁必须是经过专门机构认证的锁这个锁就相当于私钥锁的认证信息相当于证书签名文件生成证书需要私钥和证书签名文件二者缺一不可。

##安装加密插件
dnf install mod_ssl -y##生成证书

1.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
2.command 2
openssl req --newkey rsa:2048 \
-nodes -sha256 -keyout /etc/httpd/westos.org.key \
-x509 -days 365 -out /etc/httpd/westos.org.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 "/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 

去访问login.westos,org自动就转到https 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6