插件窝 干货文章 LNMP环境下如何部署WordPress

LNMP环境下如何部署WordPress

PHP WordPress Nginx sudo 896    来源:    2025-05-08

LNMP环境下部署WordPress指南

环境准备

首先确保你已经安装了LNMP环境(Linux + Nginx + MySQL + PHP)。如果没有安装,可以使用以下方法:

1. 安装LNMP环境

推荐使用一键安装包(如lnmp.org提供的脚本):

wget http://soft.vpser.net/lnmp/lnmp1.8.tar.gz -cO lnmp1.8.tar.gz && tar zxf lnmp1.8.tar.gz && cd lnmp1.8 && ./install.sh lnmp

或者手动安装各组件:

# Ubuntu/Debian
sudo apt update
sudo apt install nginx mysql-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip php-soap php-intl

# CentOS/RHEL
sudo yum install epel-release
sudo yum install nginx mariadb-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip php-soap php-intl

WordPress安装步骤

1. 创建数据库

mysql -u root -p

在MySQL命令行中执行:

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
EXIT;

2. 下载WordPress

cd /usr/share/nginx/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzvf latest.tar.gz
sudo rm latest.tar.gz
sudo chown -R www-data:www-data wordpress

3. 配置Nginx

编辑Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.com):

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /usr/share/nginx/html/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

测试并重载Nginx配置:

sudo nginx -t
sudo systemctl reload nginx

4. 完成WordPress安装

  1. 在浏览器中访问你的域名(如http://yourdomain.com)
  2. 按照WordPress安装向导完成安装
  3. 提供之前创建的数据库信息:
    • 数据库名:wordpress
    • 用户名:wordpressuser
    • 密码:your_password
    • 数据库主机:localhost
    • 表前缀:wp_(默认)

5. 安全加固(可选但推荐)

# 设置文件权限
sudo find /usr/share/nginx/html/wordpress/ -type d -exec chmod 755 {} \;
sudo find /usr/share/nginx/html/wordpress/ -type f -exec chmod 644 {} \;

# 限制wp-config.php权限
sudo chmod 600 /usr/share/nginx/html/wordpress/wp-config.php

# 安装安全插件(如Wordfence或iThemes Security)

常见问题解决

  1. 502 Bad Gateway错误

    • 检查PHP-FPM是否运行:sudo systemctl status php7.4-fpm
    • 确保Nginx配置中的fastcgi_pass指向正确的PHP-FPM socket路径
  2. 无法上传文件

    sudo chown -R www-data:www-data /usr/share/nginx/html/wordpress/wp-content/uploads
    
  3. 内存不足: 编辑wp-config.php,添加:

    define('WP_MEMORY_LIMIT', '256M');
    
  4. 固定链接404错误: 确保Nginx配置中包含:

    location / {
       try_files $uri $uri/ /index.php?$args;
    }
    

按照以上步骤,你应该能够在LNMP环境下成功部署WordPress网站。