在Debian系统上优化Apache和PHP运行环境可以显著提高网站性能和安全性。以下是一些常见的优化步骤:
1. 更新系统
首先,确保你的系统是最新的:
sudo apt update && sudo apt upgrade -y
2. 安装Apache和PHP
安装Apache和PHP及其相关模块:
sudo apt install apache2 php libapache2-mod-php php-cli php-mysql -y
3. 配置Apache
编辑Apache配置文件 /etc/apache2/apache2.conf 或创建一个新的虚拟主机配置文件。
启用必要的模块
确保启用了必要的模块,例如 mod_rewrite 和 mod_deflate:
sudo a2enmod rewrite
sudo a2enmod deflate
配置虚拟主机
编辑或创建一个新的虚拟主机配置文件,例如 /etc/apache2/sites-available/yourdomain.com.conf:
:80>
ServerAdmin webmaster@yourdomain.com
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
启用虚拟主机并重启Apache:
sudo a2ensite yourdomain.com.conf
sudo systemctl restart apache2
4. 配置PHP
编辑PHP配置文件 /etc/php/7.4/apache2/php.ini(根据你的PHP版本调整路径)。
优化内存限制
增加内存限制:
memory_limit = 256M
启用OPcache
启用OPcache以提高PHP执行速度:
[opcache]
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
禁用不必要的功能
禁用不必要的PHP功能以减少安全风险:
display_errors = Off
log_errors = On
error_reporting = E_ALL
allow_url_fopen = Off
allow_url_include = Off
5. 配置缓存和压缩
在Apache配置文件中启用缓存和压缩:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
ExpiresActive On
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
6. 安全配置
确保Apache和PHP的安全配置:
禁用目录列表
在虚拟主机配置文件中禁用目录列表:
Options -Indexes
配置防火墙
使用 ufw 配置防火墙:
sudo ufw allow 'Apache Full'
sudo ufw enable
7. 监控和日志
定期检查Apache和PHP的日志文件以监控性能和错误:
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/access.log
通过以上步骤,你可以显著优化Debian系统上的Apache和PHP运行环境。根据具体需求,你可能还需要进行其他调整和优化。