- 资源规划
- 安装介质
- 安装Apache
- 权限配置
- 开机启动
- 配置
- 用于指定Apache的运行目录,服务启动之后自动将目录切换为当前目录,后续用到的所有相对路径都是相对该目录的
- 监听的IP地址与端口号(可配置多行),示例:Listen 12.34.56.78:80
- 网页超时时间,默认为300秒
- 导入其它配置文件,Apache依照字母顺序解析配置文件
- 运行服务的用户/用户组
- 自定义错误页
- 设定默认字符集
- 日志级别
- 此指令指示httpd在递送中如果需要读取一个文件的内容,它是否可以使用内存映射
- 如果操作系统支持,Apache将默认使用内存映射,对于挂载了NFS的文件夹,可以单独指定禁用内存映射
- EnableMMAP off
- 默认值:作用域覆盖项状态模块兼容性,使用操作系统内核的sendfile支持来将文件发送到客户端
- 需要加载的其他文件
- 启停
- 验证
- 附录
- 参考
资源规划
| 组件 | bigdata-node1 | bigdata-node2 | bigdata-node3 |
|---|---|---|---|
| OS | centos7.6 | centos7.6 | centos7.6 |
| Apache | N.A | N.A | httpd、apachectl |
安装介质
安装Apache
yum install httpd -y# 查看安装版本rpm -qa | grep httpdwhereis httpd
权限配置
whereis httpd
开机启动
chkconfig httpd onsystemctl enable httpd.service
配置
vi /etc/httpd/conf/httpd.conf
■ 普通站点
# 用于指定Apache的运行目录,服务启动之后自动将目录切换为当前目录,后续用到的所有相对路径都是相对该目录的ServerRoot "/etc/httpd"# 监听的IP地址与端口号(可配置多行),示例:Listen 12.34.56.78:80Listen 80# 网页超时时间,默认为300秒Timeout 65# 导入其它配置文件,Apache依照字母顺序解析配置文件Include conf.modules.d/*.conf# 运行服务的用户/用户组User apacheGroup apache# 管理员邮箱#ServerAdmin root@localhost# 网站服务器的域名(端口保持与监听端口一致)ServerName bigdata-node3:80# 网站数据目录(默认:/var/www/html)DocumentRoot "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist"# 自定义错误页ErrorDocument 500 /50x.htmlErrorDocument 502 /50x.htmlErrorDocument 503 /50x.htmlErrorDocument 504 /50x.html<Directory "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory><IfModule dir_module># 定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问DirectoryIndex index.html index.htm</IfModule><Files ".ht*">Require all denied</Files># 错误日志文件ErrorLog "logs/error_log"# 日志级别LogLevel warn<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule># 访问日志文件(控制日志记录的行都是以CustomLog开头,注释掉所有CustomLog开头的行可以完全禁止日志)CustomLog "logs/access_log" combined</IfModule><IfModule mime_module>TypesConfig /etc/mime.typesAddType application/x-compress .ZAddType application/x-gzip .gz .tgzAddType text/html .shtmlAddOutputFilter INCLUDES .shtml</IfModule># 设定默认字符集AddDefaultCharset UTF-8<IfModule mime_magic_module>MIMEMagicFile conf/magic</IfModule># 此指令指示httpd在递送中如果需要读取一个文件的内容,它是否可以使用内存映射# 如果操作系统支持,Apache将默认使用内存映射,对于挂载了NFS的文件夹,可以单独指定禁用内存映射#EnableMMAP off# 默认值:作用域覆盖项状态模块兼容性,使用操作系统内核的sendfile支持来将文件发送到客户端EnableSendfile on# 需要加载的其他文件IncludeOptional conf.d/*.conf
■ 反向代理
1. 基于“[]”的配置方式
# 用于指定Apache的运行目录,服务启动之后自动将目录切换为当前目录,后续用到的所有相对路径都是相对该目录的ServerRoot "/etc/httpd"# 监听的IP地址与端口号,示例:Listen 12.34.56.78:80Listen 80# 网页超时时间,默认为300秒Timeout 65# 导入其它配置文件,Apache依照字母顺序解析配置文件Include conf.modules.d/*.conf# 运行服务的用户/用户组User apacheGroup apache<Directory "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory># 自定义错误页ErrorDocument 500 /50x.htmlErrorDocument 502 /50x.htmlErrorDocument 503 /50x.htmlErrorDocument 504 /50x.html<Files ".ht*">Require all denied</Files># 错误日志文件ErrorLog "logs/error_log"# 日志级别LogLevel warn<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule># 访问日志文件(控制日志记录的行都是以CustomLog开头,注释掉所有CustomLog开头的行可以完全禁止日志)CustomLog "logs/access_log" combined</IfModule><IfModule mime_module>TypesConfig /etc/mime.typesAddType application/x-compress .ZAddType application/x-gzip .gz .tgzAddType text/html .shtmlAddOutputFilter INCLUDES .shtml</IfModule># 设定默认字符集AddDefaultCharset UTF-8<IfModule mime_magic_module>MIMEMagicFile conf/magic</IfModule># 此指令指示httpd在递送中如果需要读取一个文件的内容,它是否可以使用内存映射# 如果操作系统支持,Apache将默认使用内存映射,对于挂载了NFS的文件夹,可以单独指定禁用内存映射#EnableMMAP off# 默认值:作用域覆盖项状态模块兼容性,使用操作系统内核的sendfile支持来将文件发送到客户端EnableSendfile on<Proxy balancer://proxy>BalancerMember http://bigdata-node3:8081 loadfactor=1#BalancerMember http://bigdata-node1:29898 loadfactor=1#BalancerMember http://bigdata-node2:29898 loadfactor=1</Proxy>NameVirtualHost *:80<VirtualHost *:80># 管理员邮箱#ServerAdmin email@email.com# 网站数据目录(默认:/var/www/html)DocumentRoot "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist"# 定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问DirectoryIndex index.html index.htm# 网站服务器的域名ServerName bigdata-node3:80# 错误日志文件ErrorLog "logs/bigdata-node3-error_log"#CustomLog "logs/bigdata-node3-access_log" commonRewriteEngine on# !表示请求不被转发#ProxyPass / images/ !# 匹配上的regex部分会带到后端的url#ProxyPassMatch ^/images !ProxyPass /api/ balancer://proxy/api/ProxyPassReverse /api/ balancer://proxy/api/ProxyPass /hystrix/ balancer://proxy/hystrix/ProxyPassReverse /hystrix/ balancer://proxy/hystrix/ProxyPass /admin/ balancer://proxy/admin/ProxyPassReverse /admin/ balancer://proxy/admin/ProxyPass /webjars/ balancer://proxy/webjars/ProxyPassReverse /webjars/ balancer://proxy/webjars/ProxyPass /proxy.stream/ balancer://proxy/proxy.stream/ProxyPassReverse /proxy.stream/ balancer://proxy/proxy.stream/ProxyRequests OffProxyPreserveHost on</VirtualHost># 需要加载的其他文件IncludeOptional conf.d/*.conf
简化配置(注意:Location路径匹配不要以“/”结尾):
<VirtualHost *:80># 管理员邮箱#ServerAdmin email@email.com# 网站数据目录(默认:/var/www/html)DocumentRoot "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist"# 定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问DirectoryIndex index.html index.htm# 网站服务器的域名ServerName bigdata-node3:80# 错误日志文件ErrorLog "logs/bigdata-node3-error_log"#CustomLog "logs/bigdata-node3-access_log" commonRewriteEngine on# !表示请求不被转发#ProxyPass / images/ !# 匹配上的regex部分会带到后端的url#ProxyPassMatch ^/images !<Location /api >ProxyPass balancer://proxy/apiProxyPassReverse balancer://proxy/api</Location><Location /hystrix >ProxyPass balancer://proxy/hystrixProxyPassReverse balancer://proxy/hystrix</Location><Location /admin >ProxyPass balancer://proxy/adminProxyPassReverse balancer://proxy/admin</Location><Location /webjars >ProxyPass balancer://proxy/webjarsProxyPassReverse balancer://proxy/webjars</Location><Location /proxy.stream >ProxyPass balancer://proxy/proxy.streamProxyPassReverse balancer://proxy/proxy.stream</Location>ProxyRequests OffProxyPreserveHost on</VirtualHost>
1. 基于“mod_proxy.so”的配置方式
# 用于指定Apache的运行目录,服务启动之后自动将目录切换为当前目录,后续用到的所有相对路径都是相对该目录的ServerRoot "/etc/httpd"# 监听的IP地址与端口号,示例:Listen 12.34.56.78:80Listen 80# 网页超时时间,默认为300秒Timeout 65# 导入其它配置文件,Apache依照字母顺序解析配置文件Include conf.modules.d/*.conf# 运行服务的用户/用户组User apacheGroup apache<Directory "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory># 自定义错误页ErrorDocument 500 /50x.htmlErrorDocument 502 /50x.htmlErrorDocument 503 /50x.htmlErrorDocument 504 /50x.html<Files ".ht*">Require all denied</Files># 错误日志文件ErrorLog "logs/error_log"# 日志级别LogLevel warn<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule># 访问日志文件(控制日志记录的行都是以CustomLog开头,注释掉所有CustomLog开头的行可以完全禁止日志)CustomLog "logs/access_log" combined</IfModule><IfModule mime_module>TypesConfig /etc/mime.typesAddType application/x-compress .ZAddType application/x-gzip .gz .tgzAddType text/html .shtmlAddOutputFilter INCLUDES .shtml</IfModule># 设定默认字符集AddDefaultCharset UTF-8<IfModule mime_magic_module>MIMEMagicFile conf/magic</IfModule># 此指令指示httpd在递送中如果需要读取一个文件的内容,它是否可以使用内存映射# 如果操作系统支持,Apache将默认使用内存映射,对于挂载了NFS的文件夹,可以单独指定禁用内存映射#EnableMMAP off# 默认值:作用域覆盖项状态模块兼容性,使用操作系统内核的sendfile支持来将文件发送到客户端EnableSendfile onLoadModule proxy_module modules/mod_proxy.soLoadModule proxy_http_module modules/mod_proxy_http.so<IfModule proxy_module.c>ProxyPass /api http://bigdata-node3:8081/apiProxyPassReverse /api http://bigdata-node3:8081/apiProxyPass /hystrix http://bigdata-node3:8081/hystrixProxyPassReverse /hystrix http://bigdata-node3:8081/hystrix</IfModule>NameVirtualHost *:80<VirtualHost *:80># 管理员邮箱#ServerAdmin email@email.com# 网站数据目录(默认:/var/www/html)DocumentRoot "/cib/jup/services/JavaunifiedPlatform_Governor_Web_1.0.0-SNAPSHOT/dist"# 定义主页文件,当访问到网站目录时如果有定义的主页文件,网站会自动访问DirectoryIndex index.html index.htm# 网站服务器的域名ServerName bigdata-node3:80# 错误日志文件ErrorLog "logs/bigdata-node3-error_log"CustomLog "logs/bigdata-node3-access_log" common</VirtualHost># 需要加载的其他文件IncludeOptional conf.d/*.conf
■ 单虚拟主机
监听的IP地址与端口号(可配置多行),示例:Listen 12.34.56.78:80
Listen 9080
网页超时时间,默认为300秒
Timeout 65
导入其它配置文件,Apache依照字母顺序解析配置文件
Include conf.modules.d/*.conf
运行服务的用户/用户组
User apache Group apache
自定义错误页
ErrorDocument 500 /50x.html ErrorDocument 502 /50x.html ErrorDocument 503 /50x.html ErrorDocument 504 /50x.html
设定默认字符集
AddDefaultCharset UTF-8
日志级别
LogLevel warn
此指令指示httpd在递送中如果需要读取一个文件的内容,它是否可以使用内存映射
如果操作系统支持,Apache将默认使用内存映射,对于挂载了NFS的文件夹,可以单独指定禁用内存映射
EnableMMAP off
默认值:作用域覆盖项状态模块兼容性,使用操作系统内核的sendfile支持来将文件发送到客户端
EnableSendfile on
需要加载的其他文件
IncludeOptional conf.d/*.conf
<a name="JcKEW"></a>## ■ **多虚拟主机**<a name="KCJiV"></a>### 1. 基于端口的多虚拟主机配置1. 模拟网站及内容。```bashmkdir -p /home/wwwroot/9080mkdir -p /home/wwwroot/9443echo "port:9080" > /home/wwwroot/9080/index.htmlecho "port:9443" > /home/wwwroot/9443/index.html
添加监听端口。
vi /etc/httpd/conf/httpd.conf
内容如下:
Listen 9080Listen 9443
添加虚拟主机配置。 ```xml
DocumentRoot “/home/wwwroot/9080” ServerName bigdata-node3:9080 DirectoryIndex index.html index.htm Options Indexes FollowSymLinks AllowOverride None Require all granted
3. 重启生效配置。bash
systemctl restart httpd
4. 验证。bash
curl http://bigdata-node3:9080
curl http://bigdata-node3:9443
<a name="3oANF"></a>
### 2. 基于主机名的多虚拟主机配置
1. 主机名配置。bash
vi /etc/hosts
内容如下:bash
192.168.0.103 www.polaris.com
192.168.0.103 bbs.polaris.com
2. 模拟网站及内容。bash
mkdir -p /home/wwwroot/www
mkdir -p /home/wwwroot/bbs
echo “www.polaris.com” > /home/wwwroot/www/index.html
echo “bbs.polaris.com” > /home/wwwroot/bbs/index.html
3. 添加虚拟主机配置。bash
vi /etc/httpd/conf/httpd.conf
内容如下:xml
Listen 80
4. 重启生效配置。```bashsystemctl restart httpd
- 验证。
curl http://www.polaris.comcurl http://bbs.polaris.com
■ SSL
参考:《SSL配置HTTPS》
启停
■ 超级用户
service httpd restartsystemctl start httpd.servicesystemctl restart httpd.servicesystemctl status httpd.servicesystemctl stop httpd.service
■ 普通用户
修改配置
vi /etc/httpd/conf/httpd.conf
内容如下:
ServerName bigdata-node1User vagrantGroup vagrant
特权端口支持
因为普通用户只能用1024以上的端口,1024以内的端口只能由root用户使用。但是为了避免每次启动都通过root用户,可以通过set UID的方式来解决此问题。一次性进行如下操作即可完成。
# 切换到root用户sudo suchown root /usr/sbin/httpdchmod u+s /usr/sbin/httpd# 切换到普通用户su - vagrant
- apachectl启停Apache
说明:-k:表示向父进程发送信号,是源于UNIX的kill命令向运行中的进程发送信号(TERM、HUP、USR1)。/usr/sbin/apachectl -k start/usr/sbin/apachectl -k stop# 推荐/usr/sbin/httpd -k start/usr/sbin/httpd -k stop
验证
# 查看服务是否运行ps -ef | grep httpdps aux |grep httpd# 查看端口netstat -anpl | grep httpd# 查看服务状态systemctl status httpd.service# 查看端口运行情况lsof -i:80# URL访问curl http://localhost
附录
附录A:Apache目录介绍
| 简介 | 路径 | | —- | —- | | 服务目录 | /etc/httpd | | 主配置文件 | /etc/httpd/conf/httpd.conf | | 扩展配置文件 | /etc/httpd/conf.d/*.conf | | 网站数据目录 | /var/www/html | | 访问日志 | /var/log/httpd/access_log | | 错误日志 | /var/log/httpd/error_log | | 服务脚本 | /etc/rc.d/init.d/httpd | | 运行目录 | /etc/httpd | | 网页文件目录 | DocumentRoot | | 静态页面 | /var/www/html | | 动态页面(CGI) | /var/www/cgi-bin | | 默认主页面 | index.html/index.php |
附录B:Apache配置说明
Apache中各个上下文之间所能影响的范围并不是相同的,相反,它们之间有着严格的包含差异关系。
| 配置节点 | .htaccess | |||||
|---|---|---|---|---|---|---|
| ╳ | ╳ | ╳ | ╳ | ╳ | ╳ | |
| √ | ╳ | ╳ | ╳ | ╳ | ╳ | |
| √ | ╳ | ╳ | ╳ | ╳ | ╳ | |
| √ | √ | ╳ | ╳ | ╳ | √ | |
| √ | √ | ╳ | √ | ╳ | √ |
参考
程序园:Apache-2.2如何合并多个匹配的Location部分
http://www.voidcn.com/article/p-cvnyrpbk-btv.html
