php-fpm參數最佳化【轉】

來源:互聯網
上載者:User

標籤:eve   attr   物理   following   roo   影響   conf   運行   簡單   

轉自

php-fpm參數最佳化 | Linux營運筆記
https://blog.linuxeye.com/380.html

 

 

php-fpm進程設定多少合適,設成動態還是靜態?

《lnmp一鍵安裝包》中會根據你伺服器記憶體調整php-fpm進程數。

下面是摘自Google討論話題:《 PHP-FPM on highload tips 》[牆外,FQ可參考goagent]。

When you running a highload website with PHP-FPM via FastCGI, the following tips may be useful to you

如果你的高負載網站使用PHP-FPM管理FastCGI,也許下面這些技巧對你有用

1. Compile PHP‘s modules as less as possible, the simple the best (fast);

盡量少安裝PHP模組,最簡單是最好(快)的

2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! ( On 4GB memory server);

把你的PHP FastCGI子進程數調到100或以上,在4G記憶體的伺服器上200就可以(建議壓力測試來得出自己伺服器合理的值)

3. Using SOCKET PHP FastCGI, and put into /dev/shm on Linux;

socket串連FastCGI,/dev/shm是記憶體檔案系統,socket放在記憶體中肯定會快些

4. Increase Linux "max open files", using the following command (must be root):

Linux下增加檔案開啟數,命令如下:

 
  1. cat >> /etc/security/limits.conf <<EOF
  2. * soft nproc 65535
  3. * hard nproc 65535
  4. * soft nofile 65535
  5. * hard nofile 65535
  6. EOF

5. Increase PHP-FPM open file description rlimit:

增加 PHP-FPM 開啟檔案描述符的限制:

 
  1. # vi $php_install_dir/etc/php-fpm.conf
  2. rlimit_files = 51200

6. Using PHP code accelerator, e.g eAccelerator, XCache. And set "cache_dir" to /dev/shm on Linux.

使用php代碼加速器,例如 eAccelerator, XCache.在Linux平台上可以把`cache_dir`指向 /dev/shm

/usr/local/php/etc/php-fpm.conf重要最佳化參數詳解:

 
  1. pm = dynamic

pm參數指定了進程管理方式,有兩種可供選擇:static或dynamic,從字面意思不難理解,為靜態或動態方式。如果是靜態方式,那麼在php-fpm啟動的時候就建立了指定數目的進程,在運行過程中不會再有變化(並不是真的就永遠不變);而動態則在運行過程中動態調整,當然並不是無限制的建立新進程,受pm.max_spare_servers參數影響;動態適合小記憶體機器,靈活分配進程,省記憶體。靜態適用於大記憶體機器,動態建立回收進程對伺服器資源也是一種消耗

 
  1. pm.max_children = 24

static模式下建立的子進程數或dynamic模式下同一時刻允許最大的php-fpm子進程數量

 
  1. pm.start_servers = 16

#動態方式下的起始php-fpm進程數量

 
  1. pm.min_spare_servers = 12

#動態方式下伺服器空閑時最小php-fpm進程數量

 
  1. pm.max_spare_servers = 24

#動態方式下伺服器空閑時最大php-fpm進程數量

一般php-fpm進程佔用20~30m左右的記憶體就按30m算。如果單獨跑php-fpm,動態方式起始值可設定實體記憶體Mem/30M,由於大家一般Nginx、MySQL都在一台機器上,於是預留一半給它們,即php-fpm進程數為$Mem/2/30。

LNMP在一台機器上參數(僅供參考,建議壓力測試得出):

 
  1. Mem=`free -m | awk ‘/Mem:/{print $2}‘` #我的機器記憶體是987M
  2. sed -i "[email protected]^pm.max_children.*@pm.max_children = $(($Mem/2/20))@" $php_install_dir/etc/php-fpm.conf
  3. sed -i "[email protected]^pm.start_servers.*@pm.start_servers = $(($Mem/2/30))@" $php_install_dir/etc/php-fpm.conf
  4. sed -i "[email protected]^pm.min_spare_servers.*@pm.min_spare_servers = $(($Mem/2/40))@" $php_install_dir/etc/php-fpm.conf
  5. sed -i "[email protected]^pm.max_spare_servers.*@pm.max_spare_servers = $(($Mem/2/20))@" $php_install_dir/etc/php-fpm.conf

987M記憶體:

 
  1. pm = dynamic
  2. pm.max_children = 24
  3. pm.start_servers = 16
  4. pm.min_spare_servers = 12
  5. pm.max_spare_servers = 24
Mon Nov 18 12:03:26 CST 201

php-fpm參數最佳化【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.