以下是優化Ubuntu Apache2下PHP運行環境的關鍵步驟:
sudo apt update && sudo apt upgrade
sudo apt install php libapache2-mod-php php-fpm php-opcache
sudo a2enmod rewrite deflate
sudo systemctl restart apache2
/etc/php/版本號/apache2/php.ini
:
memory_limit = 256M
max_execution_time = 300
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
/etc/php/版本號/fpm/pool.d/www.conf
:pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
display_errors = Off
,記錄日志至/var/log/php_errors.log
。注意:根據服務器資源(CPU/內存)調整進程數,生產環境建議使用PHP-FPM替代mod_php
以降低內存占用。每次修改配置后需重啟服務:
sudo systemctl restart apache2 php版本號-fpm