【Nginx】php+nginx快速搭建

小亮520cl發表於2015-12-10
參考文件
http://blog.csdn.net/dyllove98/article/details/41120789
http://www.cnblogs.com/skynet/p/4146083.html

安裝php php-fpm

  1. 安裝 開發軟體包:yum -y groupinstall "Development Tools"
  2. 安裝 mysql : yum -y install mysql mysql-server mysql-devel
  3. 下載php-5.6.2 wget http://cn2.php.net/distributions/php-5.6.2.tar.gz
  4.  解壓 tar -zxvf php-5.6.2.tar.gz
  5.  切換到 php-5.6.2
  6.  ./configure \
  7. --prefix=/usr/local/php \
  8. --with-config-file-path=/usr/local/php/etc \
  9. --enable-fpm \
  10. --with-fpm-user=php-fpm \
  11. --with-fpm-group=php-fpm \
  12. --with-mysql=mysqlnd \ //這裡mysqlnd 是內部查詢命令
  13. --with-mysql-sock=/tmp/mysql.sock \
  14. --with-libxml-dir \
  15. --with-gd \
  16. --with-jpeg-dir \
  17. --with-png-dir \
  18. --with-freetype-dir \
  19. --with-iconv-dir \
  20. --with-zlib-dir \
  21. --with-mcrypt \
  22. --enable-soap \
  23. --enable-gd-native-ttf \
  24. --enable-ftp \
  25. --enable-mbstring \
  26. --enable-exif \
  27. --disable-ipv6 \
  28. --with-pear \
  29. --with-curl \
  30. --with-openssl
  31. 出現未安裝的錯誤,直接用yum 進行安裝即可 記得不要忘了裝上 -devel (libcurl libpng libcrul12等等),dir報錯就去掉--with-dir再configure
  32. 出現找不到檔案路徑的情況下 用find / -name 'name'去查詢一下
  33. 出現warning 的情況大多是因為版本已經預設安裝了,可以去掉該行

  34. make
  35. make install

點選(此處)摺疊或開啟

  1. cd php-5.6.2
  2. cp php.ini-production /usr/local/php/etc/php.ini
  3. cp /usr/local/php/etc/php-fpm.conf.default.conf php-fpm.conf
  4.  
  5.         
  6. #測試php-fpm配置
  7. /usr/local/php/sbin/php-fpm -t 或
  8. /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -t
  9. 如果出現諸如 “test is successful” 字樣,說明配置沒有問題
  10.         
  11. #啟動php-fpm
  12. /usr/local/php/sbin/php-fpm 或
  13. /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf

  14. 啟動PHP報錯ERROR: [pool www] cannot get uid for user '@php_fpm_user@' 
  15. vi php-fpm.conf 
  16. 將user與group改為系統存在的使用者,從新啟動即可!!! 
  17. user=www 
  18. group=www


php5.3.3以前
我們用php-fpm來進行重新載入配置檔案(如php.ini):
  1. /usr/local/php/sbin/php-fpm reload

  2. 注:/usr/local/php/sbin/php-fpm還有其他引數,包括:start|stop|quit|restart|reload|logrotate

php 5.3.3 後的php-fpm 不再支援 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用訊號控制:
  1. master程式可以理解以下訊號

  2. INT, TERM 立刻終止
  3. QUIT 平滑終止
  4. USR1 重新開啟日誌檔案
  5. USR2 平滑過載所有worker程式並重新載入配置和二進位制模組

  6. 示例:
  7. php-fpm 關閉:
  8. kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

  9. php-fpm 重啟:
  10. kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

  11. 檢視php-fpm程式數:
  12. ps aux | grep -c php-fpm




搭建nginx
  1. wget http://nginx.org/download/nginx-1.6.2.tar.gz //最新穩定版哦
  2. yum安裝pcre-devel

  3. 解壓nginx
  4. tar zxvf nginx-1.6.2.tar.gz

  5. 配置編譯引數
  6. cd nginx-1.6.2
  7. ./configure \
  8. --prefix=/usr/local/nginx \
  9. --with-http_realip_module \
  10. --with-http_sub_module \
  11. --with-http_gzip_static_module \
  12. --with-http_stub_status_module \
  13. --with-pcre

  14. 編譯nginx
  15. make

  16. 安裝nginx
  17. make install

新增軟連線
ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin

測試
nginx -t 


修改nginx配置檔案讓其載入php-fpm模組
  1. server部分放開php-fpm模組
  2.  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  3.         
  4.         location ~ \.php$ {
  5.             root html;
  6.             fastcgi_pass 127.0.0.1:9000;
  7.             fastcgi_index index.php;
  8.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    ----紅色是與預設配置檔案不一樣的地方
  9.             include fastcgi_params;
  10.         }
啟動
/usr/local/nginx/sbin/nginx

重啟動nginx 
nginx -s reload



訪問測試:
  ---大功告成



補充:
修改預設web路徑,與多個虛擬目錄
  1. [root@node3 conf]# more nginx.conf

  2. #user nobody;
  3. worker_processes 1;

  4. #error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;

  7. #pid logs/nginx.pid;


  8. events {
  9.     worker_connections 1024;
  10. }


  11. http {
  12.     include mime.types;
  13.     default_type application/octet-stream;

  14.     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15.     # '$status $body_bytes_sent "$http_referer" '
  16.     # '"$http_user_agent" "$http_x_forwarded_for"';

  17.     #access_log logs/access.log main;

  18.     sendfile on;
  19.     #tcp_nopush on;

  20.     #keepalive_timeout 0;
  21.     keepalive_timeout 65;

  22.     #gzip on;

  23.     server {
  24.         listen 80;
  25.         server_name test1;

  26.         location / {              ----location訪問規則詳見文件
  27.             root html;               ---nginx1域名  預設訪問web路徑,如果訪問nginx1/index.html   則訪問的是/usr/local/nginx/html/目錄
  28.             index index.html index.htm;
  29.         }

  30.         error_page 500 502 503 504 /50x.html;
  31.         location = /50x.html {
  32.             root html;
  33.         }

  34.         location ~ \.php$ {
  35.             root /var/www/html;       ---nginx1域名  預設php檔案訪問路徑.如:訪問的是nginx1/index.php 訪問的是 /var/www/html/路徑。。。一般與上面相比較沒必要配置兩個不一樣的目錄
  36.             fastcgi_pass 127.0.0.1:9000;
  37.             fastcgi_index index.php;
  38.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  39.             include fastcgi_params;
  40.         }

  41.     }
  42.     

  43.     server {
  44.         listen 80;
  45.         server_name test2;
  46.         location / {
  47.             root /var/www/html2; #目錄
  48.             index index.html index.php;
  49.         }
  50.         error_page 500 502 503 504 /50x.html;
  51.         location = /50x.html {
  52.             root html;
  53.         }
  54.  
  55.         location ~ \.php$ {
  56.             root /var/www/html2; #php檔案訪問路徑
  57.             fastcgi_pass 127.0.0.1:9000;
  58.             fastcgi_index index.php;
  59.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  60.             include fastcgi_params;
  61.         }
  62.     }

  63.    server {
  64.         listen 8080;
  65.         server_name a.ttlsa.com;
  66.         location / {
  67.             root /var/www/a.ttlsa.com; #目錄
  68.             index index.html index.php;
  69.         }
  70.         error_page 500 502 503 504 /50x.html;
  71.         location = /50x.html {
  72.             root html;
  73.         }

  74.         location ~ \.php$ {
  75.             root /var/www/a.ttlsa.com; 
  76.             fastcgi_pass 127.0.0.1:9000;
  77.             fastcgi_index index.php;
  78.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  79.             include fastcgi_params;
  80.         }
  81.     }

  82.     server {
  83.         listen 8090;
  84.         server_name b.ttlsa.com;
  85.         location / {
  86.             root /var/www/b.ttlsa.com; #目錄
  87.             index index.html index.php;
  88.         }
  89.         error_page 500 502 503 504 /50x.html;
  90.         location = /50x.html {
  91.             root html;
  92.         }

  93.         location ~ \.php$ {
  94.             root /var/www/b.ttlsa.com; 
  95.             fastcgi_pass 127.0.0.1:9000;
  96.             fastcgi_index index.php;
  97.             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  98.             include fastcgi_params;
  99.         }
  100.     }




  101.     # another virtual host using mix of IP-, name-, and port-based configuration
  102.     #
  103.     #server {
  104.     # listen 8000;
  105.     # listen somename:8080;
  106.     # server_name somename alias another.alias;

  107.     # location / {
  108.     # root html;
  109.     # index index.html index.htm;
  110.     # }
  111.     #}


  112.     # HTTPS server
  113.     #
  114.     #server {
  115.     # listen 443 ssl;
  116.     # server_name localhost;

  117.     # ssl_certificate cert.pem;
  118.     # ssl_certificate_key cert.key;

  119.     # ssl_session_cache shared:SSL:1m;
  120.     # ssl_session_timeout 5m;

  121.     # ssl_ciphers HIGH:!aNULL:!MD5;
  122.     # ssl_prefer_server_ciphers on;

  123.     # location / {
  124.     # root html;
  125.     # index index.html index.htm;
  126.     # }
  127.     #}


  128. include vhost/*.conf;
  129. }


[root@node3 conf]#more /etc/hosts
192.168.6.119 nginx1 nginx2 a.ttlsa.com b.ttlsa.com



如上訴配置檔案:

所以訪問時如下:
[root@node3 conf]# curl test1
test1
[root@node3 conf]# curl test2              
test2
[root@node3 conf]# curl a.ttlsa.com:8090
this is a.ttlsa.com
[root@node3 conf]# curl b.ttlsa.com:8090
this is b.ttlsa.com






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1869911/,如需轉載,請註明出處,否則將追究法律責任。

相關文章