云服务器搭建 WordPress 完整部署教程(Linux,推荐 Ubuntu 22.04 / CentOS 7/9)

云服务器搭建 WordPress 完整部署教程(Linux,推荐 Ubuntu 22.04 / CentOS 7/9)

环境:云服务器(公网IP),远程工具:Xshell / FinalShell;预装软件栈:Nginx + PHP + MySQL(MariaDB) + WordPress
前提:云服务器控制台安全组放行端口 80(HTTP)、443(HTTPS)、22(SSH);已经购买域名并完成备案(国内服务器必须备案才能使用80/443)

QQ20261020-010741

一、连接云服务器

  1. 获取服务器公网IP、root账号密码/密钥
  2. FinalShell/Xshell 使用 ssh root@你的公网IP 连接
  3. 首次登录,输入密码

建议新建普通用户,日常操作不直接用root,下面演示先用root操作。

二、更新系统 & 安装环境
👉 Ubuntu 22.04

 更新软件源
apt update && apt upgrade -y

 安装 Nginx、PHP、MariaDB、PHP扩展
apt install nginx mariadb-server php-fpm php-mysql php-cli php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip -y

👉 CentOS 7

yum update -y
yum install epel-release -y
yum install nginx mariadb-server php-fpm php-mysqlnd php-cli php-curl php-gd php-mbstring php-xml php-xmlrpc php-zip -y
systemctl enable nginx mariadb php-fpm
systemctl start nginx mariadb php-fpm

启动并设置开机自启(Ubuntu)

systemctl enable --now nginx
systemctl enable --now mariadb
systemctl enable --now php8.1-fpm

Ubuntu22.04 默认php版本是8.1,确认php版本:php -v,命令里php8.1-fpm替换成你对应的版本。

三、配置数据库 MariaDB(MySQL)

  1. 执行数据库安全初始化
mysql_secure_installation

依次交互:

  • Enter current password for root (enter for none): 回车
  • Set root password? [Y/n] Y → 设置数据库root密码(务必记住)
  • Remove anonymous users? Y
  • Disallow root login remotely? Y
  • Remove test database and access to it? Y
  • Reload privilege tables now? Y
  1. 登录数据库,创建WordPress专用库和账号
mysql -u root -p

输入刚才设置的数据库root密码,进入mysql命令行,执行下面SQL(修改自定义密码)

CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER wpuser@localhost IDENTIFIED BY '你的数据库密码';
GRANT ALL PRIVILEGES ON wordpress. TO wpuser@localhost;
FLUSH PRIVILEGES;
EXIT;

记录:数据库名:wordpress,用户:wpuser,密码:你自己设置的密码,后面WP安装要用。

四、下载并解压 WordPress

 进入网站目录
cd /var/www/html

 下载最新版wordpress
wget [https://cn.wordpress.org/latest-zh_CN.tar.gz](https://cn.wordpress.org/latest-zh_CN.tar.gz)

 解压
tar -zxvf latest-zh_CN.tar.gz

 移动文件到网站根目录(把wordpress文件夹内全部内容移出来)
mv wordpress/ ./

 设置目录权限(Nginx网页目录权限非常关键,权限不对会上传、主题插件报错)
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

CentOS 用户:chown -R nginx:nginx /var/www/html

五、配置 Nginx 站点
新建 Nginx 配置文件

nano /etc/nginx/sites-available/wordpress

粘贴下面配置,把 你的域名.com 替换成你自己域名

server {
    listen 80;
    server_name 你的域名.com www.你的域名.com;
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock; Ubuntu,php版本对应修改
         CentOS 替换这一行:fastcgi_pass 127.0.0.1:9000;
    }

    location ~ /\.ht {
        deny all;
    }
}

启用站点(Ubuntu)

ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

CentOS 不需要sites-enabled,直接把配置放到 /etc/nginx/conf.d/wordpress.conf

检查Nginx配置语法

nginx -t

输出 test is successful 代表没问题,重载nginx

systemctl reload nginx

QQ20261020-010757

六、访问网页安装 WordPress
浏览器打开:http://你的域名 或者 http://服务器公网IP

  1. 选择语言 → 继续
  2. 填写数据库信息:
    • 数据库名:wordpress
    • 用户名:wpuser
    • 数据库密码:刚才设置的密码
    • 数据库主机:localhost
    • 表前缀:默认 wp_ 可保持不变
  3. 提交 → 运行安装程序
  4. 设置网站标题、管理员账号密码、邮箱,完成安装。

登录后台地址:http://域名/wp-admin

七、可选:配置HTTPS(SSL证书,推荐)
使用acme.sh或者certbot免费申请Let’s Encrypt证书(域名必须解析到服务器IP)
Ubuntu安装certbot:

apt install certbot python3-certbot-nginx -y
certbot --nginx -d 你的域名.com -d www.你的域名.com

按照提示完成,自动修改Nginx配置实现HTTPS,自动续期。

八、常见坑&优化

  1. 403权限报错:目录属主不对,重新执行 chown www-data:www-data
  2. 上传文件大小限制:修改php.ini,upload_max_filesize、post_max_size
 Ubuntu查找php.ini路径
php --ini
  1. 伪静态失效:Nginx配置里 try_files $uri $uri/ /index.php?$args; 必须正确
  2. 服务器开启防火墙(ufw/firewalld),记得放行80、443端口
 Ubuntu ufw
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 22/tcp
ufw reload

九、安全建议

  1. 不要使用弱密码,数据库密码、wp后台密码分开
  2. 禁止root直接SSH登录,修改ssh配置
  3. 定期备份数据库 + /var/www/html网站文件
  4. 不要随意安装不知名主题插件
posted @ 2026-10-20 01:09  BLOGAME  阅读(19)  评论(0)    收藏  举报