构建LNMP+WordPress的完整流程指南

来自:素雅营销研究院

头像 方知笔记
2025年06月03日 22:26

一、环境准备

在开始构建LNMP( Linux + Nginx + MySQL + PHP )环境并安装WordPress之前,需要确保拥有以下条件:

  1. 一台Linux服务器(推荐使用Ubuntu 20.04/CentOS 8)
  2. 服务器root权限或sudo权限账户
  3. 稳定的网络连接
  4. 已解析到服务器IP的域名(可选)

二、安装LNMP环境

1. 更新系统软件包

sudo apt update && sudo apt upgrade -y  # Ubuntu/Debian
# 或
sudo yum update -y  # CentOS/RHEL

2. 安装Nginx

sudo apt install nginx -y  # Ubuntu/Debian
# 或
sudo yum install nginx -y  # CentOS/RHEL

启动Nginx并设置开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx

3. 安装MySQL

sudo apt install mysql-server -y  # Ubuntu/Debian
# 或
sudo yum install mysql-server -y  # CentOS/RHEL

启动MySQL并设置开机自启:

sudo systemctl start mysql
sudo systemctl enable mysql

运行安全配置脚本:

sudo mysql_secure_installation

4. 安装PHP及相关扩展

sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y  # Ubuntu/Debian
# 或
sudo yum install php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-soap php-intl php-zip -y  # CentOS/RHEL

启动PHP-FPM并设置开机自启:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

三、配置Nginx支持PHP

  1. 编辑Nginx默认配置文件:
sudo nano /etc/nginx/sites-available/default  # Ubuntu/Debian
# 或
sudo nano /etc/nginx/conf.d/default.conf  # CentOS/RHEL
  1. 在server块中添加以下正文:
location ~ \.php$ {
include snippets/fastcgi-php.conf;  # Ubuntu/Debian
# 或 include fastcgi_params;  # CentOS/RHEL
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
  1. 测试Nginx配置并重启:
sudo nginx -t
sudo systemctl restart nginx

四、创建WordPress数据库

  1. 登录MySQL:
sudo mysql -u root -p
  1. 创建数据库和用户:
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

五、下载并安装WordPress

  1. 下载最新版WordPress:
cd /tmp
curl -O https://wordpress.org/latest.tar.gz
  1. 解压文件并移动到网站目录:
tar xzvf latest.tar.gz
sudo mv wordpress /var/www/html/
  1. 设置权限:
sudo chown -R www-data:www-data /var/www/html/wordpress  # Ubuntu/Debian
# 或 sudo chown -R nginx:nginx /var/www/html/wordpress  # CentOS/RHEL
sudo find /var/www/html/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/html/wordpress/ -type f -exec chmod 640 {} \;

六、配置WordPress

  1. 复制示例配置文件:
cd /var/www/html/wordpress
sudo cp wp-config-sample.php wp-config.php
  1. 编辑配置文件:
sudo nano wp-config.php

更新数据库连接信息:

define( 'DB_NAME', 'wordpress' );
define( 'DB_USER', 'wordpressuser' );
define( 'DB_PASSWORD', 'your_strong_password' );
define( 'DB_HOST', 'localhost' );

七、完成WordPress安装

  1. 在浏览器中访问您的服务器IP或域名:
http://your_server_ip_or_domain/wordpress
  1. 按照屏幕上的提示完成安装:
  • 选择语言
  • 输入站点标题、管理员用户名、密码和电子邮件
  • 点击”安装WordPress”
  1. 登录后台管理界面:
http://your_server_ip_or_domain/wordpress/wp-admin

八、优化和安全设置(可选)

  1. 设置Nginx虚拟主机
  2. 配置SSL证书(推荐使用Let’s Encrypt)
  3. 安装WordPress安全插件
  4. 设置定期备份
  5. 配置缓存插件提高性能

通过以上步骤,您已成功在LNMP环境下搭建了WordPress网站。接下来可以根据需要安装主题、插件,并开始发布内容。