使用 LNMP 搭建 WordPress 的完整步骤
一 准备与版本选择
二 部署 LNMP 环境
<?php phpinfo(); ?>),访问 http://服务器IP/info.php 确认 PHP 解析正常。三 创建 WordPress 数据库与用户
mysql -u root -p。CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'YourStrongPassw0rd!';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
四 部署 WordPress 并配置 Nginx
cd /usr/share/nginx/html
sudo wget https://cn.wordpress.org/wordpress-6.4.4-zh_CN.zip
sudo yum install unzip -y # Ubuntu/Debian 可用:sudo apt install unzip -y
sudo unzip wordpress-6.4.4-zh_CN.zip
cd /usr/share/nginx/html/wordpress
sudo cp wp-config-sample.php wp-config.php
sudo vim wp-config.php
修改数据库信息:define('DB_NAME', 'wordpress');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', 'YourStrongPassw0rd!');
define('DB_HOST', 'localhost');
server {
listen 80;
server_name _;
root /usr/share/nginx/html/wordpress;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; # 如使用 socket,改为 fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
}
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart php-fpm
五 安装、域名与常见问题
mysql -u root -p
USE wordpress;
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://PUBLIC_IP', 'http://DOMAIN') WHERE option_name IN ('home','siteurl');
EXIT;
location / 中加入 try_files $uri $uri/ /index.php?$args;,然后 sudo systemctl reload nginx。wp-config.php 底部加入define("FS_METHOD","direct");
define("FS_CHMOD_DIR", 0777);
define("FS_CHMOD_FILE", 0777);
并确保网站目录属主为 Nginx 运行用户(如 sudo chown -R nginx:nginx /usr/share/nginx/html/wordpress)。