RAKsmart服务器安装LNMP一键包的系列教程:环境配置与优化

文章目录

针对RAKsmart服务器安装LNMP环境后的 进阶配置与优化指南,涵盖Nginx、MySQL、PHP的核心优化项,帮助提升服务器性能和安全性,主机推荐小编RAKsmart服务器安装LNMP一键包的系列教程:环境配置与优化。

一、Nginx进阶配置

1. 虚拟主机(多站点管理)

  • 添加站点
    /etc/nginx/conf.d/目录下为每个站点创建独立配置文件(如example.com.conf):nginx复制下载server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } # 日志分割 access_log /var/log/nginx/example.com.access.log; error_log /var/log/nginx/example.com.error.log; }
    • 执行 nginx -t 测试配置,systemctl reload nginx 生效。

2. 启用Gzip压缩

nginx

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_vary on;

3. 浏览器缓存策略

nginx

location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
    expires 30d;
    add_header Cache-Control "public, no-transform";
}

4. HTTP/2与SSL证书(Let’s Encrypt)

  • 安装Certbotbash复制下载sudo apt install certbot python3-certbot-nginx
  • 申请证书bash复制下载certbot –nginx -d example.com -d www.example.com
  • 强制HTTPS与HTTP/2
    在Nginx配置中修改监听端口:nginx复制下载listen 443 ssl http2; listen [::]:443 ssl http2; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

二、MySQL安全与性能调优

1. 安全加固

bash

mysql_secure_installation

按提示操作:移除匿名用户、禁止远程root登录、删除测试数据库等。

2. 调整my.cnf参数

编辑 /etc/mysql/my.cnf(根据内存调整,假设服务器内存4GB):

ini

[mysqld]
innodb_buffer_pool_size = 1G          # 内存的50%-70%
max_connections = 200                 # 根据负载调整
wait_timeout = 600
key_buffer_size = 256M
query_cache_type = 0                  # 关闭查询缓存(适用于高并发)

3. 数据库定时备份

  • 创建备份脚本 /root/mysql_backup.sh:bash复制下载#!/bin/bash mysqldump -u root -p’your_password’ –all-databases | gzip > /backup/mysql_$(date +%Y%m%d).sql.gz
  • 添加定时任务(每天凌晨3点备份):bash复制下载crontab -e 0 3 * * * /bin/bash /root/mysql_backup.sh

三、PHP环境定制

1. 切换PHP版本(以Ubuntu为例)

bash

sudo apt install php8.2-fpm php8.2-common php8.2-mysql
sudo update-alternatives --config php  # 选择默认版本

2. 安装常用扩展

bash

sudo apt install php8.2-opcache php8.2-redis php8.2-imagick

3. 优化php.ini

ini

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 120
opcache.enable=1
opcache.memory_consumption=128

四、性能对比与建议

  • PHP 7.4 vs 8.2
    PHP 8.2 JIT编译器可提升20%-30%执行效率,推荐新项目使用。
  • Opcache
    务必启用OPcache减少PHP脚本编译开销。
  • Redis
    使用Redis替代文件Session存储,提升并发能力。

五、验证与测试

  • Nginx配置检查
    nginx -t
  • Gzip压缩生效
    curl -H "Accept-Encoding: gzip" -I http://example.com | grep "Content-Encoding"
  • SSL评级
    访问 SSL Labs Test 检测证书配置。

通过以上配置,可显著提升服务器响应速度、安全性及资源利用率。建议每次修改后监控服务器负载(如htopnmon)和日志(journalctl -u nginx)。

 主机推荐小编温馨提示:以上是小编为您整理发布的RAKsmart服务器安装LNMP一键包的系列教程:环境配置与优化,更多知识分享可持续关注我们,raksmart机房更有多款云产品免费体验,助您开启全球上云之旅。

原文链接:,转发请注明来源!

发表回复

要发表评论,您必须先登录