11月30日任務php
12.21 php-fpm的poolhtml
12.22 php-fpm慢執行日誌nginx
12.23 open_basedirvim
12.24 php-fpm進程管理瀏覽器
php-fpm.conf能夠設置多個pool,在其中一個pool資源耗盡,會致使其餘站點沒法訪問資源,報502錯誤。有必要把站點進行分離,分別使用單獨的pool。curl
# 文件地址:/usr/local/php-fpm/etc/php-fpm.conf [root@localhost etc]# vim php-fpm.conf ... # 新增一個pool代碼設置 [test] listen = /tmp/test.sock #listen = 127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
# 這裏設置另一個虛擬主機 # 實現不一樣虛擬主機訪問資源pool的隔離,互不影響 [root@localhost etc]# cat /usr/local/nginx/conf/vhost/aaa.com.conf server { listen 80 default_server; index index.html index.htm index.php root /data/www/default; location ~ \.php$ { include fastcgi_params; # 這裏寫對應php-fpm.conf內設置的sock fastcgi_pass unix:/tmp/test.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www/default$fastcgi_script_name; } }
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t [... 19:37:17] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@localhost etc]# /etc/init.d/php-fpm reload Reload service php-fpm done
# 已經能夠看到建立的pool --- test [root@localhost etc]# ps aux | grep php-fpm root 2895 1.7 0.3 227208 5008 ? Ss 19:37 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf) ... php-fpm 2915 0.0 0.3 227148 4712 ? S 19:37 0:00 php-fpm: pool www php-fpm 2916 1.4 0.3 227148 4708 ? S 19:37 0:00 php-fpm: pool test ... root 2937 0.0 0.0 112680 976 pts/0 S+ 19:37 0:00 grep --color=auto php-fpm
在php-fpm.conf內添加以下代碼就能夠實現相似虛擬主機配置文件分離的效果。ide
[root@localhost etc]# cat php-fpm.conf [global] pid = /usr/local/php-fpm/var/run/php-fpm.pid error_log = /usr/local/php-fpm/var/log/php-fpm.log include = etc/php-fpm.d/*.conf # 將原文件內的www、test pool模塊分別寫在不一樣的conf文件內 [root@localhost etc]# vim php-fpm.d/www.conf [www] #listen = /tmp/php-fcgi.sock listen = 127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 [root@localhost etc]# vim php-fpm.d/test.conf [test] listen = /tmp/test.sock #listen = 127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t [... 20:19:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@localhost etc]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
# 這裏test池在前,www池在後 [root@localhost etc]# ps aux | grep php-fpm root 3068 0.2 0.3 227236 4980 ? Ss 20:20 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf) php-fpm 3069 0.0 0.3 227176 4712 ? S 20:20 0:00 php-fpm: pool test php-fpm 3070 0.0 0.3 227176 4712 ? S 20:20 0:00 php-fpm: pool test php-fpm 3071 0.0 0.3 227176 4712 ? S 20:20 0:00 php-fpm: pool test php-fpm 3075 0.0 0.3 227176 4716 ? S 20:20 0:00 ... php-fpm 3079 0.0 0.3 227176 4716 ? S 20:20 0:00 php-fpm: pool test php-fpm 3080 0.0 0.3 227176 4716 ? S 20:20 0:00 php-fpm: pool test php-fpm 3081 0.0 0.3 227176 4716 ? S 20:20 0:00 php-fpm: pool test php-fpm 3089 0.0 0.3 227176 4716 ? S 20:20 0:00 php-fpm: pool www ... php-fpm 3107 0.0 0.3 227176 4724 ? S 20:20 0:00 php-fpm: pool www php-fpm 3108 0.0 0.3 227176 4724 ? S 20:20 0:00 php-fpm: pool www root 3110 0.0 0.0 112680 976 pts/0 S+ 20:20 0:00 grep --color=auto php-fpm
經過分析php的慢執行日誌,能夠得知php網站訪問緩慢緣由。php-fpm
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf [www] # 這裏監聽的sock修改成www.sock listen = /tmp/www.sock ;listen = 127.0.0.1:9000 listen.mode = 666 user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024 # 這裏添加下面兩行代碼 # 設置slowlog間隔爲1s,間隔超過1s會記錄到慢執行日誌內 # 實際工做中,能夠設置爲2s,1s太太短暫 request_slowlog_timeout = 1 # 指定slowlog文件存放路徑 slowlog = /usr/local/php-fpm/var/log/www-slow.log
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t [... 20:43:24] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@localhost etc]# /etc/init.d/php-fpm reload
[root@localhost etc]# vim /usr/local/nginx/conf/vhost/test.com.conf # 修改sock文件 fastcgi_pass unix:/tmp/www.sock; ...
[root@localhost etc]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost etc]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost etc]# vim /data/www/test.com/sleep.php <?php echo "test slow log"; sleep(2); echo "done"; ?>
要查看具體的錯誤信息,能夠開啓php.ini內的display_error參數測試
[root@localhost etc]# cat /usr/local/php-fpm/var/log/www-slow.log [... 21:13:15] [pool www] pid 3430 script_filename = /data/www/test.com/sleep.php [0x00007f616ec772d8] sleep() /data/www/test.com/sleep.php:3
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf # 添加以下代碼 php_admin_value[open_basedir]=/data/www/test.com:/tmp/
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf # 將添加的代碼內站點錯誤書寫爲aaa.com php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/
重啓php-fpm服務網站
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t [... 18:35:50] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@localhost etc]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
使用curl測試網頁訪問
# 因爲已經設置了open_basedir,網頁沒法訪問 [root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Tue, ... 10:38:45 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30
[root@localhost etc]# vim /usr/local/php-fpm/etc/php-fpm.d/test.conf # 將添加的代碼錯誤書寫爲 php_admin_value[open_basedir]=/data/www/test.com:/tmp/
重啓服務並驗證
[root@localhost etc]# /usr/local/php-fpm/sbin/php-fpm -t [... 18:39:10] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful [root@localhost etc]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done # 成功訪問 [root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, ... 10:39:20 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30
修復php.ini配置文件,關閉display_errors參數
# 不在瀏覽器上顯示錯誤信息 display_errors = Off # 開啓錯誤日誌記錄 log_errors = On # 設置存放路徑 error_log = /usr/local/php-fpm/var/log/php_errors.log # 設置錯誤記錄級別 error_reporting = E_ALL
爲了防止錯誤日誌沒法建立並修改,最好先行建立並給予權限。
[root@localhost etc]# touch /usr/local/php-fpm/var/log/php_errors.log [root@localhost etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log
爲了產生錯誤信息,先故意寫錯php代碼
[root@localhost etc]# touch /usr/local/php-fpm/var/log/php_errors.log [root@localhost etc]# chmod 777 /usr/local/php-fpm/var/log/php_errors.log php_admin_value[open_basedir]=/data/www/aaa.com:/tmp/
訪問網頁,其錯誤信息以下
[root@localhost etc]# curl -x 127.0.0.1:80 test.com/test.php -I HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Tue, ... 10:52:01 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.30 # 這裏明確指出所要訪問的網頁不在aaa.com站點 [root@localhost etc]# cat /usr/local/php-fpm/var/log/php_errors.log [... 10:52:01 UTC] PHP Warning: Unknown: open_basedir restriction in effect. File(/data/www/test.com/test.php) is not within the allowed path(s): (/data/www/aaa.com:/tmp/) in Unknown on line 0 [... 10:52:01 UTC] PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line 0
修改正確後再查看日誌(php文件已被解析)
[root@localhost etc]# cat /usr/local/php-fpm/var/log/php_errors.log ... [... 10:53:18 UTC] PHP Warning: phpinfo(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /data/www/test.com/test.php on line 2
# 動態進程管理 pm = dynamic # 靜態進程管理 # 設置了靜態進程管理的配置項只有緊跟該行的那項配置生效 pm = static
# 能夠使用ps aux進行查看 pm.max_children = 50
# 值能夠根據須要進行修改 pm.start_servers = 20
# 若是達到這個數值,php-fpm服務會自動派生新的子進程 pm.min_spare_servers = 5
# 若是高於這個數值,就開始清理空閒的子進程 pm.max_spare_servers = 35
# 對於該項而言,超出該值的話進程會所以而退出 pm.max_requests = 500
咱們須要根據實際應用時的須要對上述參數的值進行配置,配置後重啓php-fpm服務使其生效便可。