接下配准备配置,先查看下配置文件的目录。看到有个extra目录,类似nginx的extra目录,可以把一些定制化的配置放在extra目录里,然后在主配置文件httpd.conf把它include进来
这样降低配置文件之间的耦合性,不仅方便管理,而且配置文件个别错误,不影响其余配置。这里就准备新建一个反向代理相关的配置文件,放在extra目录下
先从主配置文件拷贝一些模板性的配置
设置主配置文件ServerName
[root@linux-node1 conf]# pwd
/usr/local/httpd/conf
[root@linux-node1 conf]# vim httpd.conf
[root@linux-node1 conf]# grep ServerName httpd.conf
# ServerName gives the name and port that the server uses to identify itself.
ServerName linux-node1.example.com:80
[root@linux-node1 conf]#
apache和nginx一样,默认会Include一些配置文件,下面的proxy-html.conf就是默认加载的配置文件。我没做任何操作,安装之后系统默认的
查看它默认加载的模块
找到负载均衡的部分,很多proxy开头的部分。复制相关参数
在extra目录下新建httpd-proxy.conf文件。文件名没要求,可以自定义。但是最好能通过配置文件知道做什么用的
把上面主配置文件的反向代理和负载均衡相关的配置复制进来。
同时配置后端节点。下面的/demo 是自定义的。可以随便写,比如写成/test123
[root@linux-node1 extra]# pwd
/usr/local/httpd/conf/extra
[root@linux-node1 extra]# cat httpd-proxy.conf
#
#proxy demo
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
ProxyRequests Off
<Proxy balancer://mycluster>
BalancerMember :8080
BalancerMember :8080
</Proxy>
ProxyPass /demo balancer://mycluster
ProxyPassReverse /demo balancer://mycluster
[root@linux-node1 extra]#
在主配置文件把这个配置文件中Include进来
检查语法,没发现语法错误
[root@linux-node1 extra]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@linux-node1 extra]#
启动时没启动成功
[root@linux-node1 extra]# /usr/local/httpd/bin/apachectl -k restart
[root@linux-node1 extra]# netstat -lntp | grep 80
[root@linux-node1 extra]# /usr/local/httpd/bin/apachectl -t
Syntax OK
[root@linux-node1 extra]# /usr/local/httpd/bin/apachectl -k restart
httpd not running, trying to start
[root@linux-node1 extra]# netstat -lntp | grep 80
[root@linux-node1 extra]#
看日志提示,是缺少模块
[root@linux-node1 logs]# pwd
/usr/local/httpd/logs
[root@linux-node1 logs]#
[root@linux-node1 logs]# tail -f error_log
[Sun Feb 26 23:00:09.288503 2017] [mpm_worker:notice] [pid 19856:tid 139866031015808] AH00292: Apache/2.4.18 (Unix) configured -- resuming normal operations
[Sun Feb 26 23:00:09.288632 2017] [core:notice] [pid 19856:tid 139866031015808] AH00094: Command line: '/usr/local/httpd-2.4.18/bin/httpd'
[Sun Feb 26 23:48:31.206384 2017] [mpm_worker:notice] [pid 19856:tid 139866031015808] AH00298: SIGHUP received. Attempting to restart
[Sun Feb 26 23:48:31.210297 2017] [proxy_balancer:emerg] [pid 19856:tid 139866031015808] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Sun Feb 26 23:48:31.210351 2017] [:emerg] [pid 19856:tid 139866031015808] AH00020: Configuration Failed, exiting
[Sun Feb 26 23:50:12.882611 2017] [proxy_balancer:emerg] [pid 20021:tid 139920264771456] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Sun Feb 26 23:50:12.882703 2017] [:emerg] [pid 20021:tid 139920264771456] AH00020: Configuration Failed, exiting
百度下错误
如果你遇到了服务无法启动的情况,也不报错的,查看logs下的日志,首次安装报错如下:
AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
我想日志已经说的很清楚了,这里编译安装后,配置文件conf/httpd.conf没有开启mod_slotmem_shm模块,编辑该文件,放开
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
在httpd-proxy.conf配置文件中添加这一行