在Ubuntu上优化Apache和PHP的性能,可以通过以下几个方面来实现:
安装Apache:
sudo apt update
sudo apt install apache2
启用必要的模块:
sudo a2enmod rewrite
sudo systemctl restart apache2
安装PHP及其常用扩展:
sudo apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-zip
重启Apache以应用更改:
sudo systemctl restart apache2
编辑/etc/php/7.4/apache2/php.ini
(根据你的PHP版本调整路径):
内存限制:
memory_limit = 128M
执行时间:
max_execution_time = 30
文件上传大小:
upload_max_filesize = 50M
post_max_size = 50M
错误日志:
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
OpenSSL:
openssl.cafile=/etc/ssl/certs/ca-certificates.crt
编辑/etc/apache2/apache2.conf
或/etc/apache2/httpd.conf
:
KeepAlive:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
MaxClients:
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 300
压缩:
EnableSendfile on
EnableMMAP on
EnableSendfile off
日志级别:
LogLevel warn
OPcache可以显著提高PHP脚本的执行速度。
安装OPcache:
sudo apt install php-opcache
启用OPcache:
编辑/etc/php/7.4/apache2/php.ini
,确保以下行未被注释:
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
使用如Redis或Memcached作为PHP的缓存系统,可以进一步提高性能。
安装Redis:
sudo apt install redis-server
安装PHP Redis扩展:
sudo apt install php-redis
配置PHP使用Redis:
编辑/etc/php/7.4/apache2/php.ini
,添加:
extension=redis.so
使用工具如top
、htop
、Apache Bench
(ab)等来监控服务器的性能,并根据需要进行调整。
通过以上步骤,你可以显著提高Ubuntu上Apache和PHP的性能。记得在每次更改配置后重启相应的服务以应用更改。