nginx+php-fpm+mysql分離部署詳解


相信大家將這三者部署到同一台主機應該已經不陌生了,今天在這里,給大家演示一下如何將三者部署到三台主機上。

實驗系統:CentOS 6.6_x86_64

實驗前提:大部分軟件使用編譯安裝,請提前准備好編譯環境,防火牆和selinux都關閉

實驗軟件:nginx-1.9.3 mariadb-10.0.20 php-5.6.11 memcache-2.2.7 xcache-3.2.0

實驗拓撲:

一、安裝nginx

  1.解決依賴關系:

    需要專門安裝pcre-devel包:

1 yum -y install pcre-devel

  2.添加nginx用戶:

1 useradd -r nginx

  3.解壓並編譯安裝nginx:

 1 tar xf nginx-1.9.3.tar.gz 
 2 cd nginx-1.9.3
 3 ./configure \
 4   --prefix=/usr/local/nginx \                    //安裝位置  5   --sbin-path=/usr/local/nginx/sbin/nginx \            //程序文件  6   --conf-path=/etc/nginx/nginx.conf \                //配置文件安裝位置  7   --error-log-path=/var/log/nginx/error.log \           //錯誤日志安裝位置  8   --http-log-path=/var/log/nginx/access.log \           //訪問日志安裝位置  9   --pid-path=/var/run/nginx/nginx.pid  \              //pid文件位置 10   --lock-path=/var/lock/nginx.lock \                //鎖文件位置 11   --user=nginx \                            //運行進程時使用的用戶身份 12   --group=nginx \                           //運行進程時使用的用戶組 13   --with-http_ssl_module \                      //支持ssl模塊 14   --with-http_flv_module \                      //支持flv模塊 15   --with-http_stub_status_module \                 //支持stub_status模塊 16   --with-http_gzip_static_module \                 //支持gzip_static模塊 17   --http-client-body-temp-path=/var/tmp/nginx/client/ \    //存儲HTTP客戶端請求body體的臨時文件位置 18   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \        //存儲HTTP代理的臨時文件位置 19   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \        //存儲fasycgi臨時文件位置 20   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \         //存儲uwsgi臨時文件位置 21   --http-scgi-temp-path=/var/tmp/nginx/scgi \          //存儲scgi臨時文件位置 22   --with-pcre                             //支持pcre庫 23 make && make install

  4.提供腳本文件:

  1 vim /etc/init.d/nginx
  2 --------------------------------
  3 #!/bin/sh
  4 #
  5 # nginx - this script starts and stops the nginx daemon
  6 #
  7 # chkconfig:   - 85 15 
  8 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
  9 #               proxy and IMAP/POP3 proxy server
 10 # processname: nginx
 11 # config:      /etc/nginx/nginx.conf
 12 # config:      /etc/sysconfig/nginx
 13 # pidfile:     /var/run/nginx.pid
 14  
 15 # Source function library.
 16 . /etc/rc.d/init.d/functions
 17  
 18 # Source networking configuration.
 19 . /etc/sysconfig/network
 20  
 21 # Check that networking is up.
 22 [ "$NETWORKING" = "no" ] && exit 0
 23  
 24 nginx="/usr/local/nginx/sbin/nginx"
 25 prog=$(basename $nginx)
 26  
 27 NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 28  
 29 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 30  
 31 lockfile=/var/lock/subsys/nginx
 32  
 33 make_dirs() {
 34    # make required directories
 35    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 36    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 37    for opt in $options; do
 38        if [ `echo $opt | grep '.*-temp-path'` ]; then
 39            value=`echo $opt | cut -d "=" -f 2`
 40            if [ ! -d "$value" ]; then
 41                # echo "creating" $value
 42                mkdir -p $value && chown -R $user $value
 43            fi
 44        fi
 45    done
 46 }
 47  
 48 start() {
 49     [ -x $nginx ] || exit 5
 50     [ -f $NGINX_CONF_FILE ] || exit 6
 51     make_dirs
 52     echo -n $"Starting $prog: "
 53     daemon $nginx -c $NGINX_CONF_FILE
 54     retval=$?
 55     echo
 56     [ $retval -eq 0 ] && touch $lockfile
 57     return $retval
 58 }
 59  
 60 stop() {
 61     echo -n $"Stopping $prog: "
 62     killproc $prog -QUIT
 63     retval=$?
 64     echo
 65     [ $retval -eq 0 ] && rm -f $lockfile
 66     return $retval
 67 }
 68  
 69 restart() {
 70     configtest || return $?
 71     stop
 72     sleep 1
 73     start
 74 }
 75  
 76 reload() {
 77     configtest || return $?
 78     echo -n $"Reloading $prog: "
 79     killproc $nginx -HUP
 80     RETVAL=$?
 81     echo
 82 }
 83  
 84 force_reload() {
 85     restart
 86 }
 87  
 88 configtest() {
 89   $nginx -t -c $NGINX_CONF_FILE
 90 }
 91  
 92 rh_status() {
 93     status $prog
 94 }
 95  
 96 rh_status_q() {
 97     rh_status >/dev/null 2>&1
 98 }
 99  
100 case "$1" in
101     start)
102         rh_status_q && exit 0
103         $1
104         ;;
105     stop)
106         rh_status_q || exit 0
107         $1
108         ;;
109     restart|configtest)
110         $1
111         ;;
112     reload)
113         rh_status_q || exit 7
114         $1
115         ;;
116     force-reload)
117         force_reload
118         ;;
119     status)
120         rh_status
121         ;;
122     condrestart|try-restart)
123         rh_status_q || exit 0
124             ;;
125     *)
126         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
127         exit 2
128 esac
129 --------------------------------
130 chmod +x /etc/init.d/nginx
131 chkconfig --add nginx
132 chkconfig nginx on
133 service nginx start

  5.測試訪問頁面,nginx安裝完畢:

二、安裝mysql

  1.添加mysql用戶和創建數據目錄:

1 useradd -r mysql
2 mkdir -pv /mydata/data
3 chown -R mysql:mysql /mydata/data

  2.解壓並初始化mysql:

1 tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/
2 cd /usr/local/
3 ln -sv mariadb-10.0.20-linux-x86_64 mysql
4 cd mysql/
5 chown -R root:mysql .
6 scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

  3.提供配置文件:

1 cp support-files/my-large.cnf /etc/my.cnf
2 vim /etc/my.cnf
3 修改此文件中thread_concurrency的值為你的CPU個數乘以2,比如:thread_concurrency = 2
4 另外還需要添加如下行指定mysql數據文件的存放位置:datadir = /mydata/data

  4.提供腳本文件:

1 cp support-files/mysql.server /etc/init.d/mysqld
2 chkconfig --add mysqld
3 chkconfig mysqld on
4 service mysqld start

  使用mysql目錄的下的bin/mysql去連接mysql,出現如下畫面表示連接成功:

  5.輸出mysql的man手冊至man命令的查找路徑:   

    編輯/etc/man.config,添加如下行即可:MANPATH  /usr/local/mysql/man

  6.輸出mysql的頭文件至系統頭文件路徑/usr/include:
    這可以通過簡單的創建鏈接實現: 
1 ln -sv /usr/local/mysql/include  /usr/include/mysql

  7.輸出mysql的庫文件給系統庫查找路徑:

1 echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
2 ldconfig

三、安裝PHP

  1.解決依賴關系:

1 yum -y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel

  2.編譯安裝php:

 1 ./configure --prefix=/usr/local/php \      //安裝位置
 2  --with-mysql \                   //支持mysql
 3  --with-pdo-mysql \                //支持pdo模塊
 4  --with-mysqli \                  //支持mysqli模塊         
 5  --with-openssl \                  //支持openssl模塊
 6  --enable-fpm \                   //支持fpm模式
 7  --enable-sockets \                //啟用socket支持
 8  --enable-sysvshm \                //啟用系統共享內存支持
 9  --enable-mbstring \                //使多字節字符串的支持
10  --with-freetype-dir \              //設置FreeType安裝前綴路徑
11  --with-jpeg-dir \                //設置libjpeg安裝前綴路徑
12  --with-png-dir \                 //設置libpng安裝前綴路徑
13  --with-zlib-dir \                //設置libz安裝前綴路徑
14  --with-libxml-dir=/usr \            //設置libxml2安裝路徑
15  --enable-xml \                 
16 --with-mhash \                 //支持mhash 17 --with-mcrypt \                 //支持mcrypt 18 --with-config-file-path=/etc \        //配置文件路徑 19 --with-config-file-scan-dir=/etc/php.d \ //配置文件掃描路徑 20 --with-bz2 \               //支持BZip2 21 --with-curl                   //支持curl 22 make && make install

  3.提供配置文件:

1 cp php.ini-production /etc/php.ini

  4.為php-fpm提供腳本:

1 cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
2 chmod +x /etc/init.d/php-fpm
3 chkconfig --add php-fpm
4 chkconfig php-fpm on

  5.提供php-fpm配置文件並編輯:

1 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
2 ------------------------------------------------------
3 pm.max_children = 150
4 pm.start_servers = 8
5 pm.min_spare_servers = 5
6 pm.max_spare_servers = 10
7 pid = /usr/local/php/var/run/php-fpm.pid
  6.啟動php-fpm服務:
1 service php-fpm start

四、整合nginx與PHP

  1.nginx服務器建立網頁文件存放目錄/www,並修改其權限:

1 mkdir /www
2 chown -R nginx:nginx /www

  2.修改nginx配置文件:

 1 vim /etc/nginx/nginx.conf
 2 --------------------------------------
 3 location / {
 4             root   /www;
 5             index  index.php index.html index.htm;
 6         }
 7 
 8 location ~ \.php$ {
 9             root           /www;
10             fastcgi_pass   192.168.19.92:9000;
11             fastcgi_index  index.php;
12             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
13             include        fastcgi_params;
14         }

  3.修改fastcgi_params文件為:

 1 fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
 2 fastcgi_param  SERVER_SOFTWARE    nginx;
 3 fastcgi_param  QUERY_STRING       $query_string;
 4 fastcgi_param  REQUEST_METHOD     $request_method;
 5 fastcgi_param  CONTENT_TYPE       $content_type;
 6 fastcgi_param  CONTENT_LENGTH     $content_length;
 7 fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
 8 fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
 9 fastcgi_param  REQUEST_URI        $request_uri;
10 fastcgi_param  DOCUMENT_URI       $document_uri;
11 fastcgi_param  DOCUMENT_ROOT      $document_root;
12 fastcgi_param  SERVER_PROTOCOL    $server_protocol;
13 fastcgi_param  REMOTE_ADDR        $remote_addr;
14 fastcgi_param  REMOTE_PORT        $remote_port;
15 fastcgi_param  SERVER_ADDR        $server_addr;
16 fastcgi_param  SERVER_PORT        $server_port;
17 fastcgi_param  SERVER_NAME        $server_name;
  4.在PHP服務器上,建立nginx用戶,要保證和nginx服務器上的nginx用戶id號、組id號一致:
  5.修改php-fpm配置文件,並重啟:
1 vim /usr/local/php/etc/php-fpm.conf
2 ---------------------------------------------
3 listen = 192.168.19.92:9000         //監聽物理網卡地址,供其它機器調用
4 user = nginx                           //php-fpm以nginx用戶運行
5 group = nginx
6 ---------------------------------------------
7 service php-fpm restart

  6.在PHP服務器上創建/www目錄,並開啟nfs服務:

1 mkdir /www
2 chown -R nginx:nginx /www
3 vim /etc/exports
4 ---------------------------------------------
5 /www    192.168.19.0/24(rw,no_root_squash)
6 ---------------------------------------------
7 service nfs start

  7.nginx服務器掛載nfs文件,並測試php,測試成功后刪除index.php:

1 mount -t nfs 192.168.19.92:/www /www
2 vim /www/index.php
3 ---------------------------------------
4 <?php
5      phpinfo();
6 ?>
7 --------------------------------------
8 service nginx restart

五、整合PHP與MYSQL

  在mysql服務器上創建php服務器能夠訪問的數據庫和用戶:

1 /usr/local/mysql/bin/mysql
2 --------------------------------------------
3 CREATE DATABASE wordpress;
4 GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.19.92' IDENTIFIED BY '123456';
5 FLUSH PRIVILEGES;

六、安裝wordpress

  1.在/www文件夾下放入網頁文件

  2.訪問http://192.168.19.83,並按提示進行安裝,配置沒問題則會安裝成功

七、為php安裝xcache

  1.解壓並安裝:

1 tar xf xcache-3.2.0.tar.gz 
2 cd xcache-3.2.0
3 /usr/local/php/bin/phpize
4 ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
5 make && make install

    完成后,會出現:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

  2.加載模塊:

1 vim /etc/php.ini
2 -----------------------------------
3 extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so        //找到extension配置的地方,加上此句

八、為php安裝memcache

  1.解壓並安裝:

1 tar xf memcache-2.2.7.tgz 
2 cd memcache-2.2.7
3 /usr/local/php/bin/phpize
4  ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
5 make && make install

    完成后,會出現:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

  2.加載模塊:

1 vim /etc/php.ini
2 -----------------------------------
3 extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so        //找到extension配置的地方,加上此句

  3.兩個加速模塊都安裝完畢,重啟php-fpm:

1 service php-fpm restart

  4.創建一個php測試頁,並查看模塊是否加載成功:

至此,演示完畢。如果您發現了什么問題,請及時聯系我,謝謝! QQ:82800452


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM