LNMP架构负载均衡

nginx的搭建

1、安装

安装前注意:

模块依赖性:Nginx的一些模块需要其他第三方库的支持,例如gzip模块需要zlib 库,rewrite模块需要pcre库,ssl功能需要openssl库等。

(1) 确保底层库都已安装

运行 yum install zlib* pcre* openssl*

十分钟让你学会LNMP架构负载均衡

 

(2) 创建一个用户和用户组

创建www组与www用户 ,理论上来讲用户与用户组的名称无所谓,但尽量要有意义

groupadd www

useradd -g www -s /usr/sbin/nologin www

(3) 编译安装nginx

第一步:下载nginx

wget

十分钟让你学会LNMP架构负载均衡

十分钟让你学会LNMP架构负载均衡

第二步:解压并进入目录

tar ….

十分钟让你学会LNMP架构负载均衡

Cd …

十分钟让你学会LNMP架构负载均衡

第三步:配置

先查看configure的配置项,并保存成一个帮助文档

十分钟让你学会LNMP架构负载均衡

将常用的配置项保存成一个安装脚本文件

Vim nginx_install.sh

内容如下:

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_image_filter_module --with-pcre --http-client-body-temp-path=/usr/local/nginx/tmp/client_body_temp --http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi_temp --http-proxy-temp-path=/usr/local/nginx/tmp/proxy_temp --http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/tmp/scgi_temp

给sh文件加上可执行的权限

十分钟让你学会LNMP架构负载均衡

然后执行

第四步:编译安装

make && make install

2、管理

(1)查看帮助文档

Cd /usr/local/nginx/sbin

./nginx -h

十分钟让你学会LNMP架构负载均衡

(2)启动nginx

直接执行安装目录下的sbin目录下的nginx

十分钟让你学会LNMP架构负载均衡

(3)关闭nginx

可以执行 nginx -s stop 或者 执行 pkill nginx

十分钟让你学会LNMP架构负载均衡

(4)热启动

可以执行 nginx -s reload (前提是nginx已经在运行中)

十分钟让你学会LNMP架构负载均衡

扩展:如果想用service 服务名 选项来重启或停止nginx,可以按以下步骤操作:

知识点:service 服务名 能操作成功的前提是 /etc/rc.d/init.d目录下要有与服务名同名的可执行文件

十分钟让你学会LNMP架构负载均衡

所以我们可以在这个目录下新建nginx文件

vim /etc/rc.d/init.d/nginx

内容:

#!/bin/sh

# chkconfig: - 85 15

# description: nginx is a World Wide Web server. It is used to serve

start() {

echo 'Starting Nginx ...'

/usr/local/nginx/sbin/nginx > /dev/null 2>&1 &

}

stop() {

echo 'Stoping Nginx ...'

/usr/local/nginx/sbin/nginx -s stop > /dev/null 2>&1 &

}

reload() {

echo 'Reloading Nginx ...'

/usr/local/nginx/sbin/nginx -s reload

}

if [ $# -ne 1 ]

then

echo 'please input one params like start|restart|stop|reload'

exit 1

fi

case "$1" in

'start')

start

;;

'stop')

stop

;;

'restart')

stop

sleep 2

start

;;

'reload')

reload

;;

'*')

echo 'please input one params like start|restart|stop|reload'

;;

esac

记得给这个文件添加可执行权限

chmod +x /etc/rc.d/init.d/nginx

加入到开机自启动:

chkconfig --add nginx

chkconfig --level 345 nginx on

五、虚拟主机配置

1、配置文件介绍

Nginx的配置文件位于安装目录下的conf目录下

可以用以下命令查看非注释部分的内容

egrep -v '#|^$' nginx.conf

-v 代表取反(去掉匹配到的内容)

十分钟让你学会LNMP架构负载均衡

在配置文件中,http段里面的server段是配置虚拟主机使用的。如果想要配置多个虚拟主机,则就在http段里面创建多个 server段即可。

 

注意配置文件中,每一行要使用分号结束,指令与{之间有空格。

Listen 后面可以是端口号,也可以是IP:端口号

Server_name 后面可以是域名,多个域名之间空格隔开,也可以是IP地址

在 http{}代码段里添加 client_max_body_size 200m; 以支持 php 上传大文件。(请根据自己项目需求来定值)

十分钟让你学会LNMP架构负载均衡

六、编译 php与nginx整合

1、编译安装php

(1) 第一步:安装PHP

1) 下载php

shell># cd ~

shell># wget

2) 解压并进入目录

shell># tar zxf php-5.6.24.tar.gz

shell># cd php-5.6.24

3) 将configure参数及详情解析另存为一个文件,以供学习参考用:

shell># ./configure --help > php_configure.txt

4) 编写一个辅助shell文件,帮我们配置php

shell># vim php_install.sh

为了避免写错,建议大家直接复制以下内容(都写在一行上,不要换行):

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --enable-opcache --with-zlib-dir --with-bz2 --with-libxml-dir=/usr --with-gd --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-iconv --disable-ipv6 --enable-static --enable-inline-optimization --enable-sockets --enable-soap --with-openssl --with-curl

以上内容的解释如图

十分钟让你学会LNMP架构负载均衡

把刚刚的shell文件加上可执行权限:

shell># chmod +x ./php_install.sh

5) 执行shell文件进行软件的配置和环境检测

shell># ./php_install.sh

6) 编译软件并且进行安装

shell># make && make install

(2) 第二步:配置

1) 复制配置文件

shell># cp php.ini-production /usr/local/php/etc/php.ini

shell># cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

赋予其可执行权限

shell># chmod +x /etc/rc.d/init.d/php-fpm

拷贝产生php-fpm的配置文件

shell># cd /usr/local/php/etc

shell># cp php-fpm.conf.default php-fpm.conf

2) 配置php.ini

shell># vim php.ini

· 找到;date.timezone = 修改为 date.timezone = Asia/Shanghai

· 根据自己的需求调整以下选项的值

error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

display_errors = On

max_execution_time = 60

max_input_time = 60

memory_limit = 256M

post_max_size = 256M

upload_max_filesize = 256M

3) 配置php-fpm.conf

shell># vim php-fpm.conf

· 找到user = nobody 和 group = nobody,将nobody改成www

· 找到listen.owner=nobody和listen.group= nobody,将nobody改成www

4) 将php-fpm加入服务并自动启动

shell># service php-fpm start

shell># chkconfig --add php-fpm

shell># chkconfig --level 345 php-fpm on

2、配置nginx支持php

第一步:在nginx.conf中找到location ~ .php$

十分钟让你学会LNMP架构负载均衡

复制并去掉注释,将root改成你的虚拟主机的路径

保存并重载配置文件,

shell># /usr/local/nginx/sbin/nginx -s reload

在html目录下新建一个PHP文件,代码如下:

十分钟让你学会LNMP架构负载均衡

在浏览器输入php页面的网址,可以发现并不能访问

十分钟让你学会LNMP架构负载均衡

原因是官方给的默认配置文件中,

十分钟让你学会LNMP架构负载均衡

要求要把php文件放到/scripts目录下。其实这个要求我们不一定要遵从。

改进的方案如下:

十分钟让你学会LNMP架构负载均衡

保存并重载

十分钟让你学会LNMP架构负载均衡

就可以让nginx支持访问类似http://www.a.com/index.php以及http://www.a.com/index.php?id=5这样的页面了。

3、优化nginx配置文件

第二步:优化

如果把所有的server代码段都放在nginx.conf里面的话,会让nginx.conf显得又臃肿又乱。建议把它们分离去。

例如,直接把所有的server代码段都剪切到/usr/local/nginx/conf/vhost.conf里面

然后在nginx.conf里面换成

include vhost.conf

十分钟让你学会LNMP架构负载均衡

虚拟主机的代码都移到了vhost.conf里面了

十分钟让你学会LNMP架构负载均衡

至此,nginx.conf已经很优化了,我们再来优化vhost.conf

第三步:优化vhost.conf

将root语句和 index语句都提取到location外面

十分钟让你学会LNMP架构负载均衡

第四步:再一次优化vhost.conf

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

这两个语句都剪切到fastcgi.conf里面去

只留下include fastcgi.conf;

十分钟让你学会LNMP架构负载均衡

fastcgi.conf文件中新增了以下两句:

十分钟让你学会LNMP架构负载均衡

至此,nginx已经支持访问php页面,并且nginx.conf和vhost.conf已经很优化了。

只是还无法支持像thinkphp的pathinfo形式的访问

http://www.a.com/index.php/Admin/public/login

4、与ThinkPHP整合

第一步:将vhost.conf的location ~ .php$ { 的$去掉

十分钟让你学会LNMP架构负载均衡

可以发现能支持pathinfo的访问方式,但是无法识别出模块、控制器和方法

十分钟让你学会LNMP架构负载均衡

然后将location代码段写成

location ~ .php {

fastcgi_split_path_info ^(.+.php)(/.*)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

include fastcgi.conf;

十分钟让你学会LNMP架构负载均衡

保存并重载nginx

就可以支持thinkphp的pathinfo形式的访问

十分钟让你学会LNMP架构负载均衡

第二步:优化vhost.conf

将这两句

fastcgi_split_path_info ^(.+.php)(/.*)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

都剪切到 fastcgi.conf里面,只留下include fastcgi.conf; 语句即可

十分钟让你学会LNMP架构负载均衡

第三步:开启伪静态功能

在server里面的location / { 代码段里,写上

try_files $uri /index.php$uri;

保存并重载nginx

就可以支持thinkphp的rewrite访问

http://www.a.com/Admin/public/login

七、负载均衡

1、负载均衡原理图

十分钟让你学会LNMP架构负载均衡

2、实现一个简单的负载均衡

服务器列表

本机 192.168.81.1

VM 192.168.81.6

腾讯云 123.207.231.180

负载均衡器的配置

upstream demo {

server 192.168.81.1;

server 123.207.231.180;

}

server {

listen 80;

server_name fzjh.com;

location / {

proxy_pass http://demo;

proxy_set_header Host $host;

proxy_set_header X-Peal-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}}十分钟让你学会LNMP架构负载均衡

并且要确保 fzjh.com要解析到负载均衡器的IP地址

十分钟让你学会LNMP架构负载均衡

保存配置文件,并且重载

访问效果:

十分钟让你学会LNMP架构负载均衡

十分钟让你学会LNMP架构负载均衡

猜你喜欢

转载自www.cnblogs.com/muyo/p/9058592.html