简单集群试验之 Nginx+php-fpm+thinkphp

作者: CbdFocus | 来源:发表于2016-04-20 14:20 被阅读1440次
    组织图 虚拟机集群

    Nginx 1.8.1 部分

    下载Nginx源码

    curl -o nginx-1.8.1.tar.gz http://nginx.org/download/nginx-1.8.1.tar.gz
    

    解压缩

    tar zxvf nginx-1.8.1.tar.gz

    安装 PCRE zlib openssl

    sudo apt-get install libpcre3 libpcre3-dev
    sudo apt-get install zlib1g zlib1g.dev
    sudo apt-get install openssl libssl-dev

    配置

    ./configure --prefix=/usr/local/mNginx --user=nginx --group=nginx --with-http_ssl_module --
    with-http_gzip_static_module
    

    配置结果:

    Configuration summary
      + using system PCRE library
      + using system OpenSSL library
      + md5: using OpenSSL library
      + sha1: using OpenSSL library
      + using system zlib library
    
      nginx path prefix: "/usr/local/mNginx"
      nginx binary file: "/usr/local/mNginx/sbin/nginx"
      nginx configuration prefix: "/usr/local/mNginx/conf"
      nginx configuration file: "/usr/local/mNginx/conf/nginx.conf"
      nginx pid file: "/usr/local/mNginx/logs/nginx.pid"
      nginx error log file: "/usr/local/mNginx/logs/error.log"
      nginx http access log file: "/usr/local/mNginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"
    
    

    安装

    make
    sudo make install
    

    启动

    添加nginx用户
    sudo useradd nginx
    /usr/local/mNginx/sbin/nginx -c /usr/local/mNginx/conf/nginx.conf
    -c 指定conf位置,默认情况加载 nginx安装目录/conf/nginx.conf

    关闭

    ps -ef | grep nginx
    从容停止:
    kill -QUIT nginx’s-pid
    快速停止:
    kill -TERM nginx’s-pid
    强制停止所有nginx进程:
    pkill -9 nginx

    重启

    平滑重启:
    测试配置文件 /usr/local/mNginx/sbin/nginx -t -c
    kill -HUP nginx’s pid

    PHP 7.0.5部分

    下载源码&&解压

    curl -o php.tar.gz http://cn2.php.net/distributions/php-7.0.5.tar.gz
    tar zxvf php.tar.gz

    安装依赖库

    sudo apt-get install curl libcurl3 libcurl3-dev
    sudo apt-get install libxml2 libxml2-dev 
    sudo apt-get install libcurl4-openssl-dev
    sudo apt-get install libmcrypt-dev libmcrypt4
    

    安装配置

    configure 时默认从系统加载依赖,可选从指定目录加载,比如php源码目录下的ext。

    ./configure --prefix=/usr/local/mPHP --enable-fpm --with-mcrypt --enable-mbstring --with-curl --
    with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --w
    ith-jpeg-dir
    
    
    Thank you for using PHP.
    
    config.status: creating php7.spec
    config.status: creating main/build-defs.h
    config.status: creating scripts/phpize
    config.status: creating scripts/man1/phpize.1
    config.status: creating scripts/php-config
    config.status: creating scripts/man1/php-config.1
    config.status: creating sapi/cli/php.1
    config.status: creating sapi/fpm/php-fpm.conf
    config.status: creating sapi/fpm/www.conf
    config.status: creating sapi/fpm/init.d.php-fpm
    config.status: creating sapi/fpm/php-fpm.service
    config.status: creating sapi/fpm/php-fpm.8
    config.status: creating sapi/fpm/status.html
    config.status: creating sapi/cgi/php-cgi.1
    config.status: creating ext/phar/phar.1
    config.status: creating ext/phar/phar.phar.1
    config.status: creating main/php_config.h
    config.status: executing default commands
    
    

    复制fpm配置文件

    sudo cp /usr/local/mPHP/etc/php-fpm.conf.default /usr/local/mPHP/etc/php-fpm.conf
    
    sudo cp /usr/local/mPHP/etc/php-fpm.d/www.conf.default /usr/local/mPHP/etc/php-fpm.d/www.conf
    并且修改user和group为 www
    

    启动

    sudo /usr/local/mPHP/sbin/php-fpm

    Thinkphp 3.2.3 部分

    在/usr/local/mNginx/html下mkdir thp作为项目root目录。
    修改nginx.conf,加入重写功能来省略URL中的index.php字样。

    user  www www;
    worker_processes  2;
    
    error_log  logs/error.log;
    error_log  logs/error.log  notice;
    error_log  logs/error.log  info;
    
    pid        logs/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        gzip  on;
    
        server {
            listen       80;
            server_name  nginx-phpapp01.cbd;
    
            #charset koi8-r;
    
            access_log  logs/host.access.log  main;
    
            location / {
                root   html/thp;
    
                index  index.html index.htm index.php;
            if (!-e $request_filename) {
                 rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                 rewrite ^(.*)$ /index.php?s=$1 last;
                 break;
               }
            }
    
            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;
    
            }
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
    
            location ~ \.php$ {
                root           html/thp;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
    
                fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
                fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
                include        fastcgi_params;
            }
        }
    }
    
    

    Nginx 反向代理转发

    nginx-balance.cbd 作为网关,
    nginx-phpapp01.cbd 和 nginx-phpapp02.cbd 分别为两个应用服务器。

    最简单的架构是app01和app02分别跑不同的模块,根据请求URL分散到各自的服务器上。

    nginx.conf修改部分:

              location /Home/Index {
                proxy_pass_header Server;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
                proxy_pass http://nginx-phpapp01.cbd;
            }
    
            location /Home/App2 {
                proxy_pass_header Server;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Scheme $scheme;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
                proxy_pass http://nginx-phpapp02.cbd;
            }
    
    

    相关文章

      网友评论

        本文标题:简单集群试验之 Nginx+php-fpm+thinkphp

        本文链接:https://www.haomeiwen.com/subject/bssalttx.html