以下是优化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