1.更新系统与安装必要组件
sudo apt update && sudo apt upgrade -y #系统更新
sudo apt install nginx mysql-server php-fpm php-mysql php-xml php-curl php-gd php-mbstring php-zip php-intl unzip curl -y #安装nginx mysql php
2.创建 MySQL 数据库与用户
sudo mysql #登陆mysql
在mysql下执行
CREATE DATABASE 替换为数据库名称 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER '替换为数据库用户名'@'localhost' IDENTIFIED BY '替换为数据库密码';
GRANT ALL PRIVILEGES ON wp.* TO '替换为数据库用户名'@'localhost';
FLUSH PRIVILEGES;
EXIT;
3.下载并配置 WordPress
cd /var/www
sudo wget https://wordpress.org/latest.zip
sudo unzip latest.zip
sudo mv wordpress /var/www/jiahao.chat
sudo chown -R www-data:www-data /var/www/jiahao.chat
sudo chmod -R 755 /var/www/jiahao.chat
复制配置文件模板:
cd /var/www/jiahao.chat
sudo cp wp-config-sample.php wp-config.php
编辑数据库配置:
sudo nano wp-config.php
修改如下几行:
define( 'DB_NAME', 'wp' );
define( 'DB_USER', '替换为数据库用户名' );
define( 'DB_PASSWORD', '替换为数据库密码' );
define( 'DB_HOST', 'localhost' );
4.配置 Nginx 站点
创建 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/jiahao.chat
粘贴以下内容:
server {
listen 80;
server_name www.jiahao.chat jiahao.chat;
root /var/www/jiahao.chat;
index index.php index.html index.htm;
access_log /var/log/nginx/jiahao.access.log;
error_log /var/log/nginx/jiahao.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-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/jiahao.chat /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
5.申请免费 SSL 证书(Let’s Encrypt)
安装 Certbot:
sudo apt install certbot python3-certbot-nginx -y
申请证书:
sudo certbot --nginx -d jiahao.chat -d www.jiahao.chat
看到需要输入邮箱就输入常用邮箱
其他全选Y没有影响
证书自动续期:
sudo systemctl enable certbot.timer