nginx+php坏境搭建+nginx实现代理以及负载均衡

本文主要内容包括

  • 源码安装nginx(可以下载更高版本的nginx)
  • 安装php-fpm以及配置nginx将php动态请求转发至php-fpm
  • nginx做代理服务器
  • nginx做负载均衡

一. 安装nginx

  • 下载nginx安装包
    wget http://nginx.org/download/nginx-1.10.3.tar.gz

  • 安装依赖
    挂载光盘, 执行
    yum -y install gcc gcc-c++ openssl openssl-devel cyrus-sasl-md5 zlib

  • 安装nginx需要指定zlib和pcre的源码位置

下载源码包wget http://zlib.net/zlib-1.2.11.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf  zlib-1.2.11.tar.gz
tar -zxvf pcre-8.41.tar.gz
tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --with-openssl=/usr/local/src/openssl-1.0.2q --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11
make && make install
  • 如果使用stream四层负载均衡, 加入配置 --with-stream

二. 常用配置

  • 创建nginx用户useradd -r web
  • 修改配置文件,
cd /usr/local/nginx
vi ./conf/nginx.conf  #./以web运行, 
user  web;
  • 启动nginx
    ./sbin/nginx
  • 浏览器访问, 192.168.1.132, 即能看到nginx欢迎页面

启动:sbin/nginx
立即停止:sbin/nginx –s stop
平滑停止:sbin/nginx –s quit
重载配置:sbin/nginx –s reload
重开日志:sbin/nginx –s reopen

三. nginx运行php

1.安装php, 开启php-fpm

  • 获取源码包解压
wget http://cn2.php.net/get/php-7.2.1.tar.gz/from/this/mirror
#解压
mv mirror php-7.2.tar.gz
tar -zxvf php-7.2.1.tar.gz
cd php-7.2.1
  • 安装依赖
yum -y install libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-dev freetype freetype-devle zlib zlib-devel glibc glibc-devel glib2 glib2-devel libxml2-devel libcurl-devel libpng-devel freetype-devel
  • 安装libmcrypt
获取源码包
https://pan.baidu.com/s/1bBfG6z9TlErw5y58eGe8Hw 密码:wlld
解压
tar jxf libmcrypt-2.5.8.tar.bz2

编译配置
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt

编译
make

安装
make install
  • 编译安装
./configure --prefix=/usr/local/php-fpm --enable-fpm --with-openssl --with-mysqli --with-pdo-mysql  --enable-mbstring  --with-zlib   --enable-sockets --with-curl --with-pcre-regex --with-mcrypt=/usr/local/libmcrypt --with-gd --with-png-dir --with-freetype-dir --enable-gd-native-ttf  --enable-opcache

make && make install

# 拷贝配置文件
cp php.ini-development /usr/local/php-fpm/lib/

2. 管理PHP-FPM

PHP的FPM需要独立运行, 有自己的独立的配置文件. 等等.
默认情况下, FPM监听某个(127.0.0.1:9000)端口, 等待nginx(或者其他的web服务器)将请求转过来.

由于PHP独立运行了, 再修改PHP的配置, 就不需要重启web服务器(nginx)了, 重启PHP-FPM即可.

  • fpm配置文件
 cd /usr/local/php-fpm/

 # 生成配置文件
 cp etc/php-fpm.conf.default etc/php-fpm.conf
 
 cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
  • 修改配置文件
vi etc/php-fpm.conf 如下
daemonize = yes

vi etc/php-fpm.d/www.conf
user = web
group = web
listen = 127.0.0.1:9000
  • 开启: sbin/php-fpm

3.配置Nginx将PHP请求转发给PHP-FPM

nginx的配置文件结构:
http段的server段, 就表示一台主机(虚拟主机).

在每台虚拟主机, server段中, 提供对请求脚本的解析工作:
location指令, 匹配请求的URL脚本. 以.php结尾的请求, 交给PHP-FPM处理

编辑nginx配置文件,
cd /usr/local/nginx/
vi conf/nginx.conf
将配置文件如下内容注释去掉, 以php结尾的文件, 转发到127.0.0.1:9000处理,

user  web;
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #修改如下内容(重要)
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

  • 重新加载nginx ./sbin/nginx -s reload
  • 在html下建立phpinfo.php文件, 输入
<?php
phpinfo();

四. 虚拟主机的配置

1. 增加虚拟主机

http中的server就是一个虚拟主机
增加server段, 就是增加虚拟主机

  • 在nginx.conf配置文件中 增加如下内容
server {
        listen 8080;
        root /usr/local/nginx/html/shop;

        server_name shop.test.com;
        access_log logs/shop.log;
        error_page 404  /404.html;
        error_page 500 502 503 504 /50x.html;

        location / {
                index  index.php  index.html      index.htm;
        }
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }
    } 
  • 在./html/下创建shop文件夹,
mkdir html/shop
chown web:web html/shop/
vi html/shop/phpinfo.php

2. 支持pathinfo, 重写index.php规则

nginx默认不支持pathinfo.
pathinfo: URL的一部分, 请求脚本到请求参数间的部分, 称之为pathinfo.
PHP程序要处理pathinfo, 前提是web服务器支持URL的pathinfo部分才可以.
通过额外的解析pathinfo指令可以完成:

location / {
            try_files $uri $uri/ /index.php?$query_string;
            index  index.html index.htm;
        }

在 location ~ .php$ 解析段中, 增加分析pathinfo的功能

location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                # 增加如下两行
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                
                include        fastcgi_params;                
        }

  • 完整配置示例如下()
server {
        listen       8888;
        server_name  localhost;

        root   /usr/local/nginx/html/shop;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #重要
            try_files $uri $uri/ /index.php?$query_string;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
  • 重新加载nginx

五. 代理服务器Nginx

浏览器会请求nginx服务器, nginx将请求转发给web服务器. 此时 站在浏览器的角度, nginx就代替(代理)了web服务器的功能, 称之为反向代理服务器.
测试环境
web02:nginx(192.168.1.132)
web01:nginx(192.168.1.131)

1. 配置

  • nginx支持的 proxy_pass的指令, 可以完成代理转发. 直接转发Http请求数据.
    演示如下: 在linux的nginx配置, 将请求抓发到别的web服务器上, 例如代理转发到web01的apache上.
  • 修改nginx的配置文件, 添加如下内容:
server {
        # 监听8888端口
        listen 8888;
        # 配置域名
        server_name proxy.nginx.com;
        # 所有请求都转发到http://192.168.1.131:80上
        location / {
                proxy_pass      http://192.168.1.131:80;
           proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto https;
           proxy_redirect off;

        }
    }

proxy_set_header Host $host; 请求的主机域名
proxy_set_header X-Real-IP $remote_addr; 转的目标IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 转发的目标IP
proxy_buffing off; 关闭nginx代理缓冲..

2. 通过配置转发, 帮助ajax请求获取外域数据

配置如下

server {
        listen 8888;
        server_name proxy.nginx.com;
        location / {
            // 正常处理
        }
        #以ajax开头的请求, 做转发
        #其他正常处理
        location /ajax {
                proxy_set_header Host $host;
                proxy_set_header x-Real-IP $remote_addr;
                proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;
                # 设置转发地址
                proxy_pass http://api.other.com;
        }
    }

六. 负载均衡配置

upstream 可以配置一个服务器集群
通过配置, 可以实现动静分离

#配置负载均衡
    server {
        listen 8999;
        server_name lb.nginx.com;
        location / {
                root    html;
                index   index.html index.htm;
                # 定义规则
                # 如果是html结尾, 就转发到html服务器上
                # php转发到php上
                # 其他, 则转发到静态资源上
                # if和括号之间, 要有空格
                if ($uri ~* \.html$){
                        # 服务器池
                        proxy_pass http://htmlservers;
                }
                if ($uri ~* \.php$){
                        proxy_pass http://phpservers;
                }

                proxy_pass http://picservers;

        }
    }
    # 定义服务器集群
    upstream htmlservers {
        server 192.168.1.131:80 weight=2;
        server 192.168.1.133:80 weight=1;
    }
    upstream phpservers {
        server 192.168.1.131:80;
        server 192.168.1.133:80;
    }
    upstream picservers {
        server 192.168.1.131:80;
        server 192.168.1.133:80;
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355