因為工作上的需要,要處理新的伺服器環境和將PHP升級到7.0,其中包含了PHP的套件
- opcache
- memcached
- apache bench
本文撰寫建置設定環境的步驟,和一些問題排除,若我有寫得不對的地方,煩請留言指正!
Nginx
- nginx
- 至官網下載最新版本 http://nginx.org/en/download.html
- 選取windows版本,例如nginx/Windows-1.11.13
- 將解壓縮完的檔案放置c:/nginx
- 我安裝時的版本為1.11.13,解壓縮的資料夾為nginx-1.11.13
- 資料夾名稱的-1.11.13移除,改為nginx
- 移動到c:/底下
- 透過cmd安裝nginx
- cd c:/nginx
- start nginx
- 安裝完成後,開啟http://localhost確認是否安裝完成
- 關閉nginx
- cd c:/nginx
- nginx -s -stop
- 修改nginx.conf
- 設定網站首頁
- 搜尋location / { ,1.11.13版本預設為43行
- root為首頁路徑,原本是root html,修改成「你的網站想預設的資料夾」
location / {
root C:/www;
index index.html index.htm;
}
- 開啟php cgi
- 搜尋 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- 將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;
}- root是指網站程式的資料夾位置
- fastcgi_param要按照上述紅字修正
- 修改root,原本是root html,修改成root C:/ALLTOP/www;
- 設定網站首頁
- 設定網站回應時間
- 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;
}
- location ~ \.php$ {
- 相關參考網站
- 至官網下載最新版本 http://nginx.org/en/download.html
PHP7
- 安裝php7
- 下載php7 (nginx選nts版本)
http://windows.php.net/download/ - 將php資料夾複製到nginx資料夾底下
- 我新增資料夾為php7.1,與php5.6做區隔
- 非必要,為了打包方便
- 將php.ini-development,修改為php.ini
- 設定php.ini
- ;設定 extension_dir,Windows下一般是ext目錄:
extension_dir = "ext" - ; 修改時區:
date.timezone = Asia/Taipei - enable_dl = On
- cgi.force_redirect = 0
- cgi.fix_pathinfo=1
- fastcgi.impersonate = 1
- cgi.rfc2616_headers = 1
- extension=php_gettext.dll
- extension=php_mbstring.dll
- 確保這行在extension=php_exif.dll之前
- extension=php_exif.dlls
- 設定session的path
- session.save_path = "C:/nginx/php/tmp/"
- 這個路徑可以自訂,但建議打絕對路徑
- session.save_path = "C:/nginx/php/tmp/"
- include_path = ".;C:\ALLTOP\www\ALLTOP\common;C:\ALLTOP\www\ALLTOP\module\sign;"
- 暫時不確定pear是否要使用,若有需要再加上
- 設定opcache
- zend_extension="php_opcache.dll"
- 設定extension的地方開啟opcache
- 可搜尋extension=php_mbstring.dll
- 搜尋php_opcache
- opcache.enable=1
- opcache.enable_cli=1
- zend_extension="php_opcache.dll"
- 安裝xdebug
- 下載 2.5.3對應PHP版本 https://xdebug.org/download.php
- zend_extension = php_xdebug.dll
- 在php.ini底部加入下列設定
- [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"
- [XDebug]
- ;設定 extension_dir,Windows下一般是ext目錄:
- 安裝VC 11 C++ https://www.microsoft.com/zh-tw/download/details.aspx?id=30679
- 選取vcredist_x64
- RunHiddenConsole (隱藏命令行窗口程式)
- 設定快速開啟&停止的bat
- nginx_server_start.bat
- @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%
- @echo off
- nginx_server_stop.bat
- @echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
- @echo off
- nginx_server_start.bat
- 下載php7 (nginx選nts版本)
- class name不能有相同的function name,改以建構式處理
- 例如commom.php內有個class go_db,不能有function go_db,要將go_db內的程式放到下列
- 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;
}
} - 要改成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;
}
}
- 安裝sqlsrv
- 下載檔案
- Microsoft® ODBC Driver 11 for SQL Server® – Windowshttps://www.microsoft.com/en-us/download/details.aspx?id=36434
- sqlsrv (nginx下載nts版本)
https://pecl.php.net/package/sqlsrv/4.1.6.1/windows - pdo_sql_srv (nginx下載nts版本)
https://pecl.php.net/package/pdo_sqlsrv/4.1.6.1/windows - 安裝Microsoft SQL Server 2012 Native Client
http://go.microsoft.com/fwlink/?LinkID=239648&clcid=0x404
- 下載檔案
- 所有php檔案的<?,都要改成<?php
- define功能
- 原先define功能會寫這樣
- define(PRG, basename(__FILE__));
第一個參數要改成字串,應該要改成 - define('PRG', basename(__FILE__));
- define(PRG, basename(__FILE__));
- 原先define功能會寫這樣
- 參考網站:https://www.saotn.org/install-microsoft-sql-server-driver-php/
apache bench
- 下載apache
- http://www.apachehaus.com/cgi-bin/download.plx
- 找到Apache 2.4 Server Binaries,下載對應的系統版本
- 2017/4/10 下載的是 httpd-2.4.25-x86-vc14-r1
- 目錄在Apache24/bin/ab.exe
- 移動到你想要執行的目錄
- 我放在c:/
- 執行cmd,並移動到ab的目錄下 (因為我放在c:/,故我的使用方式為)
- cd c:/
- ab -h (可看到ab的help)
- 使用範例:
- ab -n 100 -c 10 https://www.google.com.tw/?gfe_rd=cr&ei=y1wBWaDGIovs4AKGiIbgBg
- 100:100次點擊
- 10:10個使用者
- 詳細結果可參考保哥的說明 http://blog.miniasp.com/post/2008/06/30/Using-ApacheBench-ab-to-to-Web-stress-test.aspx
- 測試本機請使用127.0.0.1,而非localhost
- ab -n 100 -c 10 https://www.google.com.tw/?gfe_rd=cr&ei=y1wBWaDGIovs4AKGiIbgBg
- 無法執行出現「VCRUNTIME140.DLL」
- 參考資料
opcache
- php 5.5後,opcache已經內建,不需要額外下載dll,只需要開啟php.ini相關設定
- php.ini設定
- zend_extension="php_opcache.dll"
- 設定extension的地方開啟opcache
- 可搜尋extension=php_mbstring.dll
- 搜尋php_opcache
- opcache.enable=1
- opcache.enable_cli=1
- zend_extension="php_opcache.dll"
- 於phpinfo.php可搜尋到opcache已經開啟
- opcache gui查詢程式
- 下載 https://github.com/rlerdorf/opcache-status 或 https://github.com/amnuts/opcache-gui
- 解壓縮後放在www底下
- 我的設定是C:\www
- 於瀏覽器執行php程式
- http://localhost/opcache-status-master/opcache.php
- http://localhost/opcache-gui-master/index.php
- 參考網站:
留言
張貼留言