在你的阿里云服务器上运行 Drupal 10 CMS,并安装了 HTTPD(Apache)、MySQL 和 PHP-FPM,为了确保服务器性能优化并能够高效运行 Drupal,以下是一些优化建议:
---
### 1. 系统层面的优化
1.1 更新系统
确保系统和软件包是最新的,以修复已知漏洞并提升性能:
bash
sudo yum update
1.2 调整内核参数
优化内核参数以提高服务器性能。编辑 `/etc/sysctl.conf` 文件,添加或修改以下内容:
bash
# 增加网络性能
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# 减少 TCP 连接超时
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
# 增加文件描述符限制
fs.file-max = 65535
# 增加内存分配
vm.swappiness = 10
vm.overcommit_memory = 1
应用更改:
bash
sudo sysctl -p
1.3 调整文件描述符限制
编辑 `/etc/security/limits.conf`,增加以下内容:
bash
* soft nofile 65535
* hard nofile 65535
然后编辑 `/etc/pam.d/common-session` 和 `/etc/pam.d/common-session-noninteractive`,添加:
bash
session required pam_limits.so
1.4 禁用不必要的服务
禁用不需要的服务以释放资源:
bash
sudo systemctl stop postfix
sudo systemctl disable postfix
---
### 2. Web 服务器(HTTPD/Apache)优化
2.1 启用压缩
启用 Gzip 压缩以减少传输数据量。编辑 `/etc/httpd/conf/httpd.conf`,确保以下内容已启用:
apache
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</IfModule>
2.2 启用缓存
启用浏览器缓存以减少重复请求。编辑 `/etc/httpd/conf/httpd.conf`,添加:
apache
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
2.3 调整 Apache 配置
编辑 `/etc/httpd/conf/httpd.conf`,优化以下参数:
apache
# 减少超时时间
Timeout 60
# 增加最大连接数
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 调整进程数(根据内存调整)
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 50
MaxRequestWorkers 50
MaxConnectionsPerChild 1000
重启 Apache:
bash
sudo systemctl restart httpd
---
### 3. PHP-FPM 优化
3.1 调整 PHP-FPM 配置
编辑 `/etc/php-fpm.d/www.conf`,优化以下参数:
ini
# 根据内存调整进程数
pm = dynamic
pm.max_children = 20
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
# 增加每个进程的内存限制
php_value[memory_limit] = 128M
# 增加脚本执行时间
php_value[max_execution_time] = 180
重启 PHP-FPM:
bash
sudo systemctl restart php-fpm
3.2 启用 OpCache
启用 PHP OpCache 以加速 PHP 执行。编辑 `/etc/php.d/10-opcache.ini`,确保以下内容已启用:
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
opcache.fast_shutdown=1
---
### 4. MySQL 优化
4.1 调整 MySQL 配置
编辑 `/etc/my.cnf`,优化以下参数:
ini
[mysqld]
# 根据内存调整
innodb_buffer_pool_size = 512M
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 2
innodb_flush_method = O_DIRECT
query_cache_type = 1
query_cache_size = 64M
max_connections = 100
key_buffer_size = 64M
tmp_table_size = 64M
max_heap_table_size = 64M
重启 MySQL:
bash
sudo systemctl restart mysqld
4.2 优化数据库表
定期优化 Drupal 数据库表:
bash
mysqlcheck -o -u root -p drupal_database_name
---
### 5. Drupal 优化
5.1 启用缓存
在 Drupal 后台启用页面缓存和区块缓存:
- 进入 Configuration > Performance,启用缓存并设置缓存时间。
5.2 使用 CDN
将静态文件(如图片、CSS、JS)托管到 CDN,以减少服务器负载。
5.3 清理日志
定期清理 Drupal 日志表:
sql
TRUNCATE TABLE watchdog;
---
### 6. 监控与维护
- 使用 `htop` 或 `glances` 监控服务器资源。
- 定期备份数据库和文件。
- 使用日志分析工具(如 GoAccess)分析访问日志。
通过以上优化,你的服务器应该能够更高效地运行 Drupal 10 CMS。如果流量增加,可以考虑升级服务器配置或使用负载均衡。
评论