在 2核4G 的服务器上安装 MySQL + Redis + PHP + Nginx 是完全可行的,尤其适用于中小型项目、测试环境或轻量级生产应用。以下是详细的部署建议和优化配置,确保系统稳定运行。
✅ 一、环境要求概览
| 组件 | 资源占用(大致) | 说明 |
|---|---|---|
| Nginx | 10-50MB | 轻量级,高效 |
| PHP-FPM | 20-100MB | 取决于进程数 |
| MySQL | 300-800MB | 可调优减少内存 |
| Redis | 30-100MB | 内存数据库,按需使用 |
总内存占用约 500-1000MB,2核4G 完全够用。
✅ 二、安装步骤(以 Ubuntu 20.04/22.04 为例)
1. 更新系统
sudo apt update && sudo apt upgrade -y
2. 安装 Nginx
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
验证:浏览器访问服务器 IP,应看到 Nginx 欢迎页。
3. 安装 PHP(以 PHP 8.1 为例)
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip php8.1-redis -y
启动 PHP-FPM:
sudo systemctl enable php8.1-fpm
sudo systemctl start php8.1-fpm
4. 安装 MySQL
sudo apt install mysql-server -y
sudo mysql_secure_installation
运行安全脚本设置 root 密码、移除匿名用户等。
⚠️ 提示:设置强密码,不要远程开放 root 登录。
5. 安装 Redis
sudo apt install redis-server -y
sudo systemctl enable redis-server
sudo systemctl start redis-server
默认 Redis 监听 127.0.0.1:6379,安全。如需远程访问,修改 /etc/redis/redis.conf。
✅ 三、配置 Nginx + PHP
创建网站配置文件
sudo nano /etc/nginx/sites-available/example.com
内容示例:
server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location ~ /.ht {
deny all;
}
}
启用站点:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
创建测试 PHP 文件
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/index.php
访问 http://your_server_ip,看到 PHP 信息页即成功。
✅ 四、关键优化建议(2核4G 环境)
1. MySQL 优化(/etc/mysql/mysql.conf.d/mysqld.cnf)
[mysqld]
# 减少内存使用
key_buffer_size = 16M
max_allowed_packet = 64M
thread_stack = 192K
thread_cache_size = 8
max_connections = 100
table_open_cache = 400
# InnoDB 优化
innodb_buffer_pool_size = 512M # 推荐:物理内存的 50%-70%
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 2
修改后重启 MySQL:
sudo systemctl restart mysql
2. PHP-FPM 优化(/etc/php/8.1/fpm/pool.d/www.conf)
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500
重启:
sudo systemctl restart php8.1-fpm
3. Redis 优化(/etc/redis/redis.conf)
maxmemory 256mb
maxmemory-policy allkeys-lru
限制 Redis 最大内存,避免撑爆系统。
4. 开启 Swap(重要!)
2核4G 建议添加 1-2G Swap 防止 OOM:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# 永久生效
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
✅ 五、安全建议
- 使用
ufw防火墙:sudo ufw allow 'Nginx Full' sudo ufw allow ssh sudo ufw enable - 禁用 MySQL 远程登录(除非必要)
- 定期更新系统和软件
- 使用
.env文件管理敏感配置,不要暴露在 Web 目录
✅ 六、监控资源使用
htop # 实时查看 CPU/内存
df -h # 查看磁盘
free -h # 查看内存
ss -tuln # 查看端口占用
✅ 总结
| 项目 | 推荐配置 |
|---|---|
| OS | Ubuntu 20.04/22.04 LTS |
| Nginx | ✔️ |
| PHP | 8.1 FPM |
| MySQL | 8.0 |
| Redis | 6.0+ |
| 内存 | 4G + 2G Swap |
| 适用场景 | 博客、API 服务、小型电商、后台系统等 |
✅ 结论:2核4G 完全可以稳定运行 LNMP + Redis,但需合理配置和监控。
如你使用的是 宝塔面板、OneinStack、AMH 等一键包,也可以快速部署,但手动安装更轻量、可控。
需要我提供一键脚本或部署 Laravel/WordPress 示例吗?欢迎继续提问!
云计算导航