安装nginx
实验环境:192.168.11.10:NGINX负载均衡,动静分离
192.168.11.20:安装httpd或者nginxweb服务器,安装php
192.168.11.21:安装httpd或者nginxweb服务器,安装php
192.168.11.22:安装httpd或者nginxweb服务器,安装php
可以自己下载NGINX压缩包也可以yum安装,下面用tar包演示安装
官网下载1.18版本再通过xshell的rz传输进去
1. yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make --安装依赖包
2. 可以自己下载NGINX压缩包也可以yum安装,下面用tar包演示安装,官网下载1.18版本再通过xshell的rz传输进去,用压缩包安装的方法不会自动创建用户,
所以我们第一步创建用户:useradd -s /sbin/nologin -M nginx,第二步解压:tar -zxvf nginx-1.18.0.tar.gz
第三步开启全部模块:./configure --prefix=/usr/local/nginx --user=nginx--group=nginx --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre
第四步编译:make,服务器CPU6核的话可以make -j 6,第五步安装:make install,第六步进入安装后的目录:cd /usr/local/nginx/,
看到有四个文件夹就可[root@LVS nginx]# ls
conf html logs sbin
3. netstat -atunp | grep 80 --查看是否有NGINX的80端口
./nginx -s reload --重载
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ 创建快捷方式到/usr/sbin/
nginx -s stop --关闭
4. [root@LVS sbin]# nginx -t --检查是否有错误,显示以下信息表示没错
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5. vim /usr/local/nginx/conf/nginx.conf --打开配置文件
location部分添加如下:
43 location / {
44 root html;
45 index index.html index.htm;
46 if ($request_uri ~* \.html$){
47 proxy_pass http://htmlservers;
48 }
49 if ($request_uri ~* \.php$){
50 proxy_pass http://phpservers;
51 }
52 proxy_pass http://picservers;
53 }
在最后的花括号上一行添加如下:
123 upstream htmlservers {
124 server 192.168.11.21:80;
125 server 192.168.11.22:80;
126 }
127 upstream phpservers {
128 server 192.168.11.20:80;
129 }
130 upstream picservers {
131 server 192.168.11.21:80;
132 server 192.168.11.22:80;
133 }
6. nginx -s reload --重载一下
安装haproxy
- 实验环境:192.168.11.10:haproxy负载均衡服务器
192.168.11.20:rs1,装好httpd,固定ip,同网段可ping通,可ping百度
192.168.11.21:rs2,装好httpd,固定ip,同网段可ping通,可ping百度
192.168.11.22:rs3,装好httpd,固定ip,同网段可ping通,可ping百度
1. tar -zxvf haproxy-2.4.4.tar.gz --先从其他地方下载好tar包用xshell的rz传输过去,假设我是在/opt下解压的
2. cd haproxy-2.4.4/ --安装的步骤可参考vim README和vim Makefile文档
3. make TARGET=linux-glibc PREFIX=/usr/local/haproxy --编译到指定路径
4. make install PREFIX=/usr/local/haproxy --指定安装路径,可以用echo $?查询上条命令错误1或成功0
5. cd /usr/local/haproxy/
6. mkdir etc --存配置文件的文件夹
7. vim /usr/local/haproxy/etc/haproxy.cfg --新建配置文件,内容如下,全为小写:
1 global
2 log 127.0.0.1 local0
3 #Log 127.0.0.1 Local1 notice
4 #Log loghost local0 info
5 maxconn 4096
6 chroot /usr/local/haproxy
7 uid 99 #所属运行的用户uid
8 gid 99
9 #所属运行的用户组
10 daemon #以后台形式运行haproxy
11 nbproc 1
12 #启动1个haproxy实例。# #工作进程数量(CPU数量),实际工作中,应该设置成和CPU核>心数-一样。这样可 以发挥出最大的性能。
13 pidfile /usr/local/haproxy/run/haproxy.pid #将 所有进程写入pid文件
14 #debug #调试错误时用
15 #quiet #安静
16 defaults
17 log global
18 log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为Local3.系统中Local1-7, 用户自己定义
19 mode http
20 #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
21 option httplog
22 #日志类别,记载http日志
23 option httpclose #每次请求完毕后主动关闭htt通道, haproxy不支持keep- alive,只能模拟这种模式的实现
24 #每次请求完毕后主动关闭htt通道,haxy不支持备存,只能模拟这种模式的实现 option
25 option dontlognull #不记录空连接,产生的日志
26 #不记录空连接,产生的日志 option
27 option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端i
28 option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
29 #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
30 retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
31 #2次连接失败就认为服务器不可用,主要通过后面的检查检查 maxconn 2000
32 maxconn 2000 #最大连接数
33 balance roundrobin #负载均衡算法
34 stats uri /haproxy- stats #haproxy监控页面的访问地址#可通过http://LocaLhost: 80/haproxy-stats
35 timeout connect 5000 #连接超时时间。单位:ms毫秒
36 timeout client 5000 #客户端连接超时时间
37 timeout server 5000 #服务器端连接超时时间
38 mode http
39 option httpchk GET /index.html
40 #健康检测#注意实际工作中测试时,应该下载某-一个 页面来进行测试,因此这个页面应该是个小页面,而不要用首页面.这里是每隔一-秒检查一次页面.
41 frontend http
42 bind 0.0.0.0:80
43 default_backend http_back
44 backend http_back
45 server rs1 192.168.11.20:80 weight 3 check --权重可以设成不一样
46 server rs2 192.168.11.21:80 weight 3 check
47 server rs3 192.168.11.22:80 weight 3 check
8. 制作启动haproxy服务的脚本:
cd /opt/haproxy-2.4.4/examples/ --/opt下解压的tar包,会有原始文件夹
9. cp haproxy.init /etc/init.d/haproxy --复制启动脚本的例子到/etc/init.d/haproxy
步骤8和9可以合并cp /opt/haproxy-2.4.4/examples/haproxy.init /etc/init.d/haproxy
10. chmod 777 /etc/init.d/haproxy --给755或者干脆给777权限
11. vim /etc/init.d/haproxy --编辑启动脚本,修改如下内容:
1 #!/bin/sh
2 #
3 # chkconfig: - 85 15
4 # description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited \
5 # for high availability environments.
6 # processname: haproxy
7 # config: /usr/local/haproxy/etc/haproxy.cfg
8 # pidfile: /usr/local/haproxy/run/haproxy.pid
9
10 # Script Author: Simon Matter <simon.matter@invoca.ch>
11 # Version: 2004060600
12
13 # Source function library.
14 if [ -f /etc/init.d/functions ]; then
15 . /etc/init.d/functions
16 elif [ -f /etc/rc.d/init.d/functions ] ; then
17 . /etc/rc.d/init.d/functions
18 else
19 exit 0
20 fi
21
22 # Source networking configuration.
23 . /etc/sysconfig/network
24
25 # Check that networking is up.
26 [ ${NETWORKING} = "no" ] && exit 0
27
28 # This is our service name
29 BASENAME=`haproxy`
30
31 BIN=/usr/sbin/haproxy
32
33 CFG=/usr/local/haproxy/etc/haproxy.cfg
34 [ -f $CFG ] || exit 1
35
36 PIDFILE=/usr/local/haproxy/run/haproxy.pid
37 LOCKFILE=/usr/local/haproxy/run/haproxy
38
39 RETVAL=0
对照以上内容修改,内容中有些文件夹还没有,所以下面还得创建文件夹,不然脚本会报错
12. mkdir /usr/local/haproxy/run --脚本里有这个文件夹,所以得创建
13. [root@LVS examples]# ls /usr/local/haproxy/ --创建好了
doc etc run sbin share
14. chown nobody -R /usr/local/haproxy/ --给nobody用户启动脚本执行权限
15. ls /etc/init.d/ --查看是否为绿色,绿色为可执行,不是绿色就chmod 755或者777给权限
16. vim /etc/rsyslog.conf --编辑haproxy日志文件,添加内容local3,local0:
72 # Save boot messages also to boot.log
73 local7.* /var/log/boot.log
74 local3.* /var/log/haproxy.log
75 local0.* /var/log/haproxy.log
76
77
78 # ### begin forwarding rule ###
17. systemctl restart rsyslog --重启日志服务
18. /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg --启动haproxy服务
这里查看一下80端口是否被haproxy占用
[root@LVS init.d]# netstat -atunp | grep haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 48072/haproxy
udp 0 0 0.0.0.0:40442 0.0.0.0:* 48072/haproxy
udp 0 0 0.0.0.0:56966 0.0.0.0:* 47920/haproxy
19. kill -9 PID --例如 kill -9 47705,关闭该服务
20. /etc/init.d/haproxy start/stop/status --也可利用脚本启动,关闭,查看状态
21. http://192.168.11.10/haproxy-stats --启动完服务,在浏览器输入监控网页
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。