一、环境准备
系统更新
sudo apt update && sudo apt upgrade -y
安装必要工具
sudo apt install -y curl wget unzip
二、安装Nginx、MySQL、PHP
安装Nginx
sudo apt install -y nginx sudo systemctl start nginx sudo systemctl enable nginx
安装MySQL
sudo apt install -y mysql-server sudo mysql_secure_installation # 按提示设置root密码和安全选项
安装PHP及扩展
sudo apt install -y php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip sudo systemctl restart php8.1-fpm # 根据实际PHP版本调整
三、配置MySQL数据库
登录MySQL
sudo mysql -u root -p
创建数据库和用户
CREATE DATABASE wordpress; CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'YourStrongPassword123!'; GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'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
复制配置文件
cd /var/www/html/wordpress cp wp-config-sample.php wp-config.php
编辑配置文件
sudo nano wp-config.php
修改以下参数:
define('DB_NAME', 'wordpress'); define('DB_USER', 'wpuser'); define('DB_PASSWORD', 'YourStrongPassword123!');
五、配置Nginx虚拟主机
创建配置文件
sudo nano /etc/nginx/sites-available/wordpress.conf
粘贴以下内容
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:/run/php/php8.1-fpm.sock; # 确认PHP版本路径 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
启用配置并测试
sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/ sudo nginx -t # 检查配置语法 sudo systemctl reload nginx
六、配置HTTPS(Let's Encrypt)
安装Certbot
sudo apt install -y certbot python3-certbot-nginx
获取SSL证书
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
按提示输入邮箱并同意条款
选择是否重定向HTTP到HTTPS(推荐选2)
自动续期测试
sudo certbot renew --dry-run
七、完成安装
访问网站 打开浏览器访问
https://your-domain.com
,按WordPress向导完成安装。
常见问题处理
权限问题:确保
/var/www/html/wordpress
目录属主为www-data
。502 Bad Gateway:检查PHP-FPM是否运行,且Nginx配置中PHP socket路径正确。
SSL证书错误:确认域名解析已生效,防火墙开放80/443端口。
完成以上步骤后,您的WordPress站点将通过Nginx运行,并启用HTTPS加密访问。
评论区