[工作] nginx + windows + php7 安裝筆記

因為工作上的需要,要處理新的伺服器環境和將PHP升級到7.0,其中包含了PHP的套件
  1. opcache
  2. memcached
  3. apache bench
本文撰寫建置設定環境的步驟,和一些問題排除,若我有寫得不對的地方,煩請留言指正!


Nginx
  1. nginx
    1. 至官網下載最新版本 http://nginx.org/en/download.html
      1. 選取windows版本,例如nginx/Windows-1.11.13
    2. 將解壓縮完的檔案放置c:/nginx
      1. 我安裝時的版本為1.11.13,解壓縮的資料夾為nginx-1.11.13
      2. 資料夾名稱的-1.11.13移除,改為nginx
      3. 移動到c:/底下
    3. 透過cmd安裝nginx
      1. cd c:/nginx
      2. start nginx
    4. 安裝完成後,開啟http://localhost確認是否安裝完成
    5. 關閉nginx
      1. cd c:/nginx
      2. nginx -s -stop
    6. 修改nginx.conf
      1. 設定網站首頁
        1. 搜尋location / { ,1.11.13版本預設為43行
        2. root為首頁路徑,原本是root html,修改成「你的網站想預設的資料夾」
          location / {        
            root   C:/www;           
            index  index.html index.htm;    
          }
      2. 開啟php cgi
        1. 搜尋 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        2. 將location ~ \.php$ { }的註解打開
          location ~ \.php$ {        
              root           C:/www;        
              fastcgi_pass   127.0.0.1:9000;        
              fastcgi_index  index.php;        
              fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;                include        fastcgi_params;    
          }
          1. root是指網站程式的資料夾位置
          2. fastcgi_param要按照上述紅字修正
        3. 修改root,原本是root html,修改成root C:/ALLTOP/www;
    7. 設定網站回應時間
      1. location ~ \.php$ {        
          root           C:/www;        
          fastcgi_pass   127.0.0.1:9000;        
          fastcgi_index  index.php;        
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        
          include        fastcgi_params;        
          fastcgi_read_timeout 600s;       
        }
    8. 相關參考網站
      1. http://mccusik.blogspot.tw/2014/08/nginxphpmariadb-windows.html
      2. http://abcg5.pixnet.net/blog/post/114727133-windows-server%E4%B8%8A%2C%E5%9F%B7%E8%A1%8Cnginx-%2B-php-7
      3. http://www.cnblogs.com/huayangmeng/archive/2011/06/15/2081337.html
PHP7
  1. 安裝php7
    1. 下載php7 (nginx選nts版本)
      http://windows.php.net/download/
    2. 將php資料夾複製到nginx資料夾底下
      1. 我新增資料夾為php7.1,與php5.6做區隔
      2. 非必要,為了打包方便
    3. 將php.ini-development,修改為php.ini
    4. 設定php.ini
      1. ;設定 extension_dir,Windows下一般是ext目錄:
        extension_dir = "ext"
      2. ; 修改時區:
        date.timezone = Asia/Taipei
      3. enable_dl = On
      4. cgi.force_redirect = 0
      5. cgi.fix_pathinfo=1
      6. fastcgi.impersonate = 1
      7. cgi.rfc2616_headers = 1
      8. extension=php_gettext.dll
      9. extension=php_mbstring.dll
        1. 確保這行在extension=php_exif.dll之前
      10. extension=php_exif.dlls
      11. 設定session的path
        1. session.save_path = "C:/nginx/php/tmp/"
          1. 這個路徑可以自訂,但建議打絕對路徑
      12. include_path = ".;C:\ALLTOP\www\ALLTOP\common;C:\ALLTOP\www\ALLTOP\module\sign;"
        1. 暫時不確定pear是否要使用,若有需要再加上
      13. 設定opcache
        1. zend_extension="php_opcache.dll"
          1. 設定extension的地方開啟opcache
          2. 可搜尋extension=php_mbstring.dll
        2. 搜尋php_opcache
          1. opcache.enable=1
          2. opcache.enable_cli=1
      14. 安裝xdebug
        1. 下載 2.5.3對應PHP版本 https://xdebug.org/download.php
        2. zend_extension = php_xdebug.dll
        3. 在php.ini底部加入下列設定
          1. [XDebug]
            ;其實這行可開可不開,這行主要就是開啟偵錯的堆疊程序的功能,是否需要當然就是因人而異嘍
            xdebug.default_enable = "On"
            ;行就是必需的了,若是要針對網頁的偵錯,這個就一定要打註3
            xdebug.remote_enable = 1
            ;除錯的協定,若是php3以前的版本,就請改為"gdb" ;官方設明文件中有提到,從xdebug 2.1以後的版本只支援"dbgp"這個協定
            xdebug.remote_handler = "dbgp"
            ;要除錯的host name或是IP(不用加上埠號)
            xdebug.remote_host = "localhost"
            ;xdebug要用到的埠號
            xdebug.remote_port = 9000
            ;這個項目一樣是看個人需求,看是否需要將查錯的資料儲存出來,設定其存放的目錄
            xdebug.trace_output_dir = "C:\nginx\xdbug\tmp"
    5. 安裝VC 11 C++ https://www.microsoft.com/zh-tw/download/details.aspx?id=30679
      1. 選取vcredist_x64
    6. RunHiddenConsole (隱藏命令行窗口程式)
      1. http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip
    7. 設定快速開啟&停止的bat
      1. nginx_server_start.bat
        1. @echo off
          set php_home_dir=C:/nginx/php7.1
          set nginx_home_dir=C:/nginx
          set PHP_FCGI_MAX_REQUESTS=1000
          echo Starting PHP FastCGI...
          RunHiddenConsole %php_home_dir%/php-cgi.exe -b 127.0.0.1:9000 -c %php_home_dir%/php.ini
          echo Starting nginx...
          RunHiddenConsole %nginx_home_dir%/nginx.exe -p %nginx_home_dir%
      2. nginx_server_stop.bat
        1. @echo off
          echo Stopping nginx...
          taskkill /F /IM nginx.exe > nul
          echo Stopping PHP FastCGI...
          taskkill /F /IM php-cgi.exe > nul
          exit
  2. class name不能有相同的function name,改以建構式處理
    1. 例如commom.php內有個class go_db,不能有function go_db,要將go_db內的程式放到下列
    2. public function go_db(){
             $this->setConn(); // 用SESSION值代入連線參數
             $connectionInfo = array(
                 "UID"          => $this->UserName,
                 "PWD"          => $this->Password,
                 "Database"     => $this->DataBase,
                 "CharacterSet" => "UTF-8",
             );
             $this->conn = sqlsrv_connect($this->ServerName, $connectionInfo);
             if ($this->conn) {
                 return true;
             } else {
                 die(print_r(sqlsrv_errors(), true));
                 return false;
             }
         }
    3. 要改成public function __construct(){
             $this->setConn(); // 用SESSION值代入連線參數
             $connectionInfo = array(
                 "UID"          => $this->UserName,
                 "PWD"          => $this->Password,
                 "Database"     => $this->DataBase,
                 "CharacterSet" => "UTF-8",
             );
             $this->conn = sqlsrv_connect($this->ServerName, $connectionInfo);
             if ($this->conn) {
                 return true;
             } else {
                 die(print_r(sqlsrv_errors(), true));
                 return false;
             }
         }
  3. 安裝sqlsrv
    1. 下載檔案
      1. Microsoft® ODBC Driver 11 for SQL Server® – Windowshttps://www.microsoft.com/en-us/download/details.aspx?id=36434
      2. sqlsrv (nginx下載nts版本)
        https://pecl.php.net/package/sqlsrv/4.1.6.1/windows
      3. pdo_sql_srv (nginx下載nts版本)
        https://pecl.php.net/package/pdo_sqlsrv/4.1.6.1/windows
      4. 安裝Microsoft SQL Server 2012 Native Client
        http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x404
  4. 所有php檔案的<?,都要改成<?php
  5. define功能
    1. 原先define功能會寫這樣
      1. define(PRG, basename(__FILE__));
        第一個參數要改成字串,應該要改成
      2. define('PRG', basename(__FILE__));
  6. 參考網站:https://www.saotn.org/install-microsoft-sql-server-driver-php/
apache bench

  1. 下載apache
    1. http://www.apachehaus.com/cgi-bin/download.plx
    2. 找到Apache 2.4 Server Binaries,下載對應的系統版本
      1. 2017/4/10 下載的是 httpd-2.4.25-x86-vc14-r1
    3. 目錄在Apache24/bin/ab.exe
    4. 移動到你想要執行的目錄
      1. 我放在c:/
  2. 執行cmd,並移動到ab的目錄下 (因為我放在c:/,故我的使用方式為)
    1. cd c:/
    2. ab -h (可看到ab的help)
  3. 使用範例:
    1. ab -n 100 -c 10 https://www.google.com.tw/?gfe_rd=cr&ei=y1wBWaDGIovs4AKGiIbgBg
      1. 100:100次點擊
      2. 10:10個使用者
    2. 詳細結果可參考保哥的說明 http://blog.miniasp.com/post/2008/06/30/Using-ApacheBench-ab-to-to-Web-stress-test.aspx
    3. 測試本機請使用127.0.0.1,而非localhost
  4. 無法執行出現「VCRUNTIME140.DLL」
    1. 下載VC套件 https://www.microsoft.com/en-US/download/details.aspx?id=48145
  5. 參考資料
    1. http://blog.miniasp.com/post/2008/06/30/Using-ApacheBench-ab-to-to-Web-stress-test.aspx
    2. http://luckyboy7527.pixnet.net/blog/post/97965031-%5B2015.03%E6%9B%B4%E6%96%B0-%5Dwindows-%E7%92%B0%E5%A2%83-apache-http-server-2.2.25-%E5%AE%89
opcache

  1. php 5.5後,opcache已經內建,不需要額外下載dll,只需要開啟php.ini相關設定
  2. php.ini設定
    1. zend_extension="php_opcache.dll"
      1. 設定extension的地方開啟opcache
      2. 可搜尋extension=php_mbstring.dll
    2. 搜尋php_opcache
      1. opcache.enable=1
      2. opcache.enable_cli=1
  3. 於phpinfo.php可搜尋到opcache已經開啟
  4. opcache gui查詢程式
    1. 下載 https://github.com/rlerdorf/opcache-status 或 https://github.com/amnuts/opcache-gui
    2. 解壓縮後放在www底下
      1. 我的設定是C:\www
    3. 於瀏覽器執行php程式
      1. http://localhost/opcache-status-master/opcache.php
      2. http://localhost/opcache-gui-master/index.php
  5. 參考網站:
    1. https://mini.nidbox.com/diary/read/8924339
    2. http://www.ithome.com.tw/news/101602
    3. https://cnzhx.net/blog/zendopcache-accelerate-php/

留言