一、准备工作
在开始使用Nginx搭建WordPress之前,您需要确保已经完成以下准备工作:
- 服务器准备:一台运行Linux系统的VPS或云服务器(推荐Ubuntu 20.04/CentOS 7+)
- 域名准备:已注册的域名并做好DNS解析
- 软件要求:
- Nginx 1.18+
- MySQL 5.7+或MariaDB 10.3+
- PHP 7.4+(推荐8.0)
- WordPress最新版本安装包
二、安装Nginx和必要组件
1. 安装Nginx
对于Ubuntu/Debian系统:
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
对于CentOS/RHEL系统:
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
2. 安装MySQL/MariaDB
Ubuntu/Debian:
sudo apt install mysql-server -y
sudo mysql_secure_installation
CentOS/RHEL:
sudo yum install mariadb-server mariadb -y
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
3. 安装PHP及必要扩展
# Ubuntu/Debian
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
# CentOS/RHEL
sudo yum install php-fpm php-mysqlnd php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
三、配置数据库
- 登录MySQL:
sudo mysql -u root -p
- 创建WordPress数据库和用户:
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
- 下载最新版WordPress:
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
sudo mv wordpress /var/www/html/
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
- 配置WordPress:
cd /var/www/html/wordpress
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
修改以下数据库连接信息:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpressuser');
define('DB_PASSWORD', 'your_strong_password');
define('DB_HOST', 'localhost');
五、配置Nginx虚拟主机
- 创建Nginx配置文件:
sudo nano /etc/nginx/sites-available/wordpress
- 添加以下配置(替换your_domain.com为您的域名):
server {
listen 80;
server_name your_domain.com www.your_domain.com;
root /var/www/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/php8.0-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;
}
}
- 启用配置并测试:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
六、完成WordPress安装
- 在浏览器访问您的域名,按照WordPress安装向导完成安装
- 设置网站标题、管理员账户等信息
- 登录WordPress后台(your_domain.com/wp-admin)
七、优化和安全配置(可选)
- 启用HTTPS:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
- 配置Nginx缓存: 在Nginx配置中添加:
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
- 限制访问wp-admin:
location ~ ^/wp-admin {
allow your_ip_address;
deny all;
try_files $uri $uri/ /index.php?$args;
}
常见问题解决
- 502 Bad Gateway错误:
- 检查PHP-FPM是否运行:
sudo systemctl status php8.0-fpm
- 确保Nginx配置中的fastcgi_pass路径正确
- 文件权限问题:
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo find /var/www/html/wordpress/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/wordpress/ -type f -exec chmod 644 {} \;
- 内存限制问题: 编辑php.ini文件,增加内存限制:
memory_limit = 256M
通过以上步骤,您已经成功使用Nginx搭建了WordPress网站。Nginx相比Apache具有更高的性能和更低的内存占用,特别适合流量较大的WordPress网站。后续您可以根据需要安装主题、插件,并进一步优化网站性能。