簡(jiǎn)單集群試驗(yàn)之 Nginx+php-fpm+thinkphp

組織圖
虛擬機(jī)集群

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

配置結(jié)果:

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

啟動(dòng)

添加nginx用戶
sudo useradd nginx
/usr/local/mNginx/sbin/nginx -c /usr/local/mNginx/conf/nginx.conf
-c 指定conf位置,默認(rèn)情況加載 nginx安裝目錄/conf/nginx.conf

關(guān)閉

ps -ef | grep nginx
從容停止:
kill -QUIT nginx’s-pid
快速停止:
kill -TERM nginx’s-pid
強(qiáng)制停止所有nginx進(jìn)程:
pkill -9 nginx

重啟

平滑重啟:
測(cè)試配置文件 /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

安裝依賴庫(kù)

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 時(shí)默認(rèn)從系統(tǒng)加載依賴,可選從指定目錄加載,比如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

復(fù)制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

啟動(dòng)

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

Thinkphp 3.2.3 部分

在/usr/local/mNginx/html下mkdir thp作為項(xiàng)目root目錄。
修改nginx.conf,加入重寫功能來(lái)省略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 反向代理轉(zhuǎn)發(fā)

nginx-balance.cbd 作為網(wǎng)關(guān),
nginx-phpapp01.cbd 和 nginx-phpapp02.cbd 分別為兩個(gè)應(yīng)用服務(wù)器。

最簡(jiǎn)單的架構(gòu)是app01和app02分別跑不同的模塊,根據(jù)請(qǐng)求URL分散到各自的服務(wù)器上。

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;
        }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容