首先确保你已经安装了LNMP环境(Linux + Nginx + MySQL + PHP)。如果没有安装,可以使用以下方法:
推荐使用一键安装包(如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
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;
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
编辑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
# 设置文件权限
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)
502 Bad Gateway错误:
sudo systemctl status php7.4-fpm
无法上传文件:
sudo chown -R www-data:www-data /usr/share/nginx/html/wordpress/wp-content/uploads
内存不足:
编辑wp-config.php
,添加:
define('WP_MEMORY_LIMIT', '256M');
固定链接404错误: 确保Nginx配置中包含:
location / {
try_files $uri $uri/ /index.php?$args;
}
按照以上步骤,你应该能够在LNMP环境下成功部署WordPress网站。