使用Linux实现负载均衡和动静分离

------------恢复内容开始------------

------------恢复内容开始------------

安装配置nginx

安装nginx所需依赖包:

[root@niexj21 ~]# yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel

下载源码包:

 

[root@niexj21 ~]# wget http://nginx.org/download/nginx-1.14.1.tar.gz

解压:

[root@niexj21 ~]# tar xvf nginx-1.14.1.tar.gz -C /usr/local/src/

编译安装:

[root@niexj21 ~]# cd /usr/local/src/nginx-1.14.1

 

[root@niexj21 nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --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

具体的参数说明可以使用help查看

[root@niexj21 nginx-1.14.1]# ./configure --help   ##或者看官网文档说明

 

[root@niexj21 nginx-1.14.1]# make && make install

添加专门运行nginx的用户:

 

[root@niexj21 conf]# useradd -s /sbin/nologin -M -u 2000 nginx

修改配置文件:

[root@niexj21 conf]# vim /usr/local/nginx/conf/nginx.conf

改:2 #user  nobody;

为:2 user  nginx nginx;

在下面配置中定义分发策略

43 location / {
44 root html;
45 index index.html index.htm;
46 }

添加配置后

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.1.22:80;
125 server 192.168.1.23:80;
126 }
127 upstream phpservers {
128 server 192.168.1.24:80;
129 server 192.168.1.25:80;
130 }
131 upstream picservers {
132 server 192.168.1.22:80;     
133 server 192.168.1.23:80;
134 }

##后期工作中,根据工作中的需要,配置成具体业务的IP地址

添加软连接,方便启动nginx

[root@niexj21 conf]# ln -s /usr/local/nginx/sbin/nginx /sbin/nginx

检查配置是否有问题

[root@niexj21 conf]# 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   #成功

启动并检查是否启动成功

[root@niexj21 conf]# nginx

[root@niexj21 conf]# netstat -antup | grep nginx

部署Apache服务器组:

安装并生成静态测试文件和启动Apache服务

[root@niexj22 ~]# yum install -y httpd

[root@niexj22 ~]# echo niexj22 > /var/www/html/index.html
[root@niexj22 ~]# systemctl start httpd

[root@niexj23 ~]# yum install -y httpd

[root@niexj23 ~]# echo niexj22 > /var/www/html/index.html
[root@niexj23 ~]# systemctl start httpd

部署PHP服务器组

[root@niexj24 ~]# yum install -y php

[root@niexj24 ~]# vim /var/www/html/index.php

niexj24
<?php
phpinfo():
?>

[root@niexj24 ~]# systemctl start httpd

[root@niexj25 ~]# yum install -y php

[root@niexj25 ~]# vim /var/www/html/index.php

niexj25
<?php
phpinfo();
?>

 

 最后进行测试

 

 

------------恢复内容结束------------

------------恢复内容结束------------

posted @ 2021-10-04 11:50  Niexj00  阅读(59)  评论(0)    收藏  举报