指南
Apache 一般代指的 Apache HTTP Server (官方文档)。Apache HTTP Server 是介于应用程序和用户之间的一个中间件系统。
Apache 在网站工作过程中,起着非常重要的作用。下面列出一些常见的设置场景,供用户参考
场景
域名绑定
当服务器上只有一个网站时,不做域名绑定也 可以访问网站。但从安全和维护考量,域名绑定不可省却。
假设用户已经上传网站到服务器,那么域名绑定操作步骤如下:
- 确保域名解析已经生效
- 使用 WinSCP 等工具登录云服务器
- 修改 Apache虚拟机主机配置文件,将其中的 ServerName 项的值修改为你的域名
<VirtualHost *:80>
ServerName www.mydomain.com # 此处修改为你的域名
DocumentRoot "/data/wwwroot/mysite2"
... - 保存配置文件,重启 Apache 服务
设置伪静态
使用和设置 Apache 伪静态有三个步骤:
- 打开 Apache模块配置文件,检查 Rewrite 模块是否启用(LAMP 环境默认已经开启 Rewirte)
- 保证 Apache 虚拟主机配置文件中 VirtualHost 配置段中增加 AllowOverride All
- 给需要使用伪静态的网站的根目录中增加.htaccess文件,并在其中配置伪静态规则
范例:重定向
- 开启Apache的rewrite模块
- 在网站根目录中增加.htaccess文件
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 "/empirecmsall-image-guide" "/xdocs/empirecms-image-guide"
Redirect 301 "/wordpress-image-guide" "/xdocs/wordpressold-image-guide"
</IfModule>
范例:隐藏后缀名
<IfModule mod_rewrite.c>
RewriteRule ^test$ test.php
ErrorDocument 404 /404.txt
</IfModule>
配置 Apache 使用不同端口访问网站
当服务有多个网站,但不想通过域名访问,可以配置 Apache 使用不同的端口访问不同的网站,步骤如下:
-
确保在安全组中已放通相应端口
-
使用 WinSCP 等工具登录云服务器
-
修改 Apache虚拟机主机配置文件,修改虚拟主机 ** VirtualHost ** 端口 号
<VirtualHost *:81>
ServerName wordpress.example.com
#ServerAlias example.com
DocumentRoot "/data/wwwroot/web1"
ErrorLog "logs/wordpress-error_log"
CustomLog "logs/wordpress-access_log" common
<Directory "/data/wwwroot/web1">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:82>
ServerName wordpress.example.com
#ServerAlias example.com
DocumentRoot "/data/wwwroot/web2"
ErrorLog "logs/wordpress-error_log"
CustomLog "logs/wordpress-access_log" common
<Directory "/data/wwwroot/web2">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost> -
保存配置文件,在 Apache 主配置文件 httpd.conf 中监听对应端口
#Listen 12.34.56.78:80
Listen 80
Listen 81
Listen 82 -
重启 Apache 服务
Require 指令控制外部访问
Require 指令用于控制外部访问,下面是常见的场景:
<Directory xxx/www/yoursite>
<RequireAll>
# 允许所有
Require all granted
# 允许所有
Require all granted
# 仅允许本地
Require local
# 拒绝所有
Require all denied
# 只允许特需域名访问
Require host google.com
# 只允许特定 IP 段
Require ip 192.120 192.168.100 192.168.1.1
</RequireAll>
</Directory>
拒绝通过 IP 访问应用
编辑网站的 Apache 虚拟主机配置段为如下格式,其中 xxx.xxx.xxx.xxx 表示服务器 IP 地址
NameVirtualHost xxx.xxx.xxx.xxx
<virtualhost xxx.xxx.xxx.xxx:80>
ServerName xxx.xxx.xxx.xxx
<Directory />
Order Allow,Deny
Deny from all
</Directory>
</virtualhost>
设置 Apache 并发连接数
有大量访问的时候速度很慢,或403错误后反复刷新才能访问等问题,可能是性能造成的。
一方面,需要提高服务器配置, 另外一方面需要通过修改 Apache 并发参数以提升性能:
- 登录服务器后运行命令
httpd -V
,查询当前Apache的NPM工作模式httpd -V
AH00558: httpd: Could not reliably determine the server's fully qualified domain name
Server version: Apache/2.4.6 (CentOS)
Server built: Aug 8 2019 11:41:18
Server's Module Magic Number: 20120211:24
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count) - 修改 /etc/httpd/conf/httpd.conf,根据服务器配置的承载能力修改并发相关参数。比如:MaxClients 设置为2000,就可以处理2000个并发请求
<IfModule prefork.c>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 256
MaxRequestsPerChild 3000
</IfModule>
HTTP 跳转 HTTPS
方案一:修改.htaccess文件
.htaccess文件提供了一种目录级别的修改配置的方式。
#任何情况下均强制跳转
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#指定域名下的自动跳转
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
#指定文件夹的自动跳转
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} folder
RewriteRule ^(.*)$ https://www.yourdomain.com/folder/$1 [R,L]
方案二:修改vhost文件
要想开启自动跳转功能,请确保 Apache 的 Rewirte 模块加载,然后按照以下方案进行设置:
- 整站跳转
如果需要整站跳转,则在网站的配置文件(/etc/http/vhost/vhost.conf)的 <Directory> 标签内添加:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R=301] - 只对某个目录的页面进行自动跳转,请将 yourfolder 改成自己的目录名
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301] - 只将带 www 的 URL 跳转至 HTTPS,请将 www.yourdomain.com 改成自己想要设置跳转的域名
RewriteEngine On
RewriteRule ^/(.*)$ www.yourdomain.com/$1 [R=301] - 对除了某一个页面的其他所有页面进行 HTTPS 跳转
这段配置的作用是指除了 /tz.php 页面用 http 访问,其他页面均为 https 访问,/tz.php 可改为自己实际要 http 访问的后缀。
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]