LNMP(Linux + Nginx + MySQL + PHP)是最流行的 Web 服务器架构之一。本教程将带你使用脚本自动化部署完整的 LNMP 环境,无需手动逐个配置。
一、LNMP 架构介绍
- Linux – 操作系统(Ubuntu/CentOS)
- Nginx – Web 服务器,高性能反向代理
- MySQL – 数据库管理系统
- PHP – 服务器端脚本语言
二、部署前准备
# 检查系统版本
cat /etc/os-release
# 检查内存
free -h
# 检查磁盘空间
df -h
# 更新系统包
apt update && apt upgrade -y # Ubuntu/Debian
yum update -y # CentOS
三、创建自动化部署脚本
步骤 1:创建脚本文件
cat > lnmp-install.sh << "EOF"
#!/bin/bash
# LNMP 自动化安装脚本
# 适用于 Ubuntu 20.04/22.04
set -e
# 配置变量
MYSQL_ROOT_PASSWORD="your_root_password"
MYSQL_DATABASE="wordpress"
MYSQL_USER="wpuser"
MYSQL_PASSWORD="wppassword"
NGINX_PORT="80"
echo "======================================"
echo "开始安装 LNMP 环境"
echo "======================================"
# 步骤 1: 安装 Nginx
echo "[1/5] 安装 Nginx..."
apt install nginx -y
systemctl enable nginx
systemctl start nginx
# 步骤 2: 安装 MySQL
echo "[2/5] 安装 MySQL..."
apt install mysql-server -y
systemctl enable mysql
systemctl start mysql
# 设置 MySQL 安全
mysql_secure_installation << MYSQL_EOF
y
${MYSQL_ROOT_PASSWORD}
${MYSQL_ROOT_PASSWORD}
y
y
y
y
MYSQL_EOF
# 创建数据库和用户
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE DATABASE ${MYSQL_DATABASE};"
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "CREATE USER @localhost IDENTIFIED BY ;"
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "GRANT ALL PRIVILEGES ON ${MYSQL_DATABASE}.* TO @localhost;"
mysql -u root -p${MYSQL_ROOT_PASSWORD} -e "FLUSH PRIVILEGES;"
# 步骤 3: 安装 PHP 及扩展
echo "[3/5] 安装 PHP..."
apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
# 步骤 4: 配置 Nginx
echo "[4/5] 配置 Nginx..."
cat > /etc/nginx/sites-available/default << NGINX_EOF
server {
listen ${NGINX_PORT};
server_name _;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
NGINX_EOF
# 步骤 5: 重启服务
echo "[5/5] 重启服务..."
systemctl restart nginx
systemctl restart php-fpm
systemctl restart mysql
echo "======================================"
echo "LNMP 安装完成!"
echo "Nginx 端口:${NGINX_PORT}"
echo "网站根目录:/var/www/html"
echo "======================================"
EOF
chmod +x lnmp-install.sh
四、执行安装脚本
# 执行安装脚本
sudo ./lnmp-install.sh
# 等待安装完成(约 5-10 分钟)
五、验证安装结果
# 检查 Nginx 状态
systemctl status nginx
# 检查 MySQL 状态
systemctl status mysql
# 检查 PHP 状态
systemctl status php-fpm
# 测试 PHP
# 浏览器访问 http://你的服务器 IP 查看结果
六、配置虚拟主机
# 创建网站目录
mkdir -p /var/www/yourdomain.com/public_html
# 设置权限
chown -R www-data:www-data /var/www/yourdomain.com
chmod -R 755 /var/www/yourdomain.com
# 创建 Nginx 配置
cat > /etc/nginx/sites-available/yourdomain.com << EOF
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/public_html;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
EOF
# 启用站点
ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
七、性能优化建议
# 1. 优化 PHP 配置
vim /etc/php/8.1/fpm/php.ini
memory_limit = 256M
max_execution_time = 300
upload_max_filesize = 64M
post_max_size = 64M
# 2. 启用 Nginx 缓存
# 3. 配置 MySQL 查询缓存
# 4. 启用 Gzip 压缩
# 5. 配置浏览器缓存
总结
通过以上步骤,你已经完成了 LNMP 环境的自动化部署。使用脚本可以大大提高部署效率,减少人为错误。