Ubuntu服务器部署WordPress个人博客完整指南

Xin-dudu 发布于 2025-07-28 284 次阅读


目标:通过Nginx+MySQL+PHP在标准80端口部署高性能WordPress站点


一、服务器初始化

1. 更新系统并安装基础工具

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget unzip

2. 配置防火墙(UFW)

sudo ufw allow 22/tcp       # 保留SSH访问
sudo ufw allow 80/tcp       # 开放HTTP端口
sudo ufw allow 443/tcp      # 开放HTTPS端口
sudo ufw enable

二、安装运行环境

1. 安装Nginx

sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

2. 安装MySQL

sudo apt install -y mysql-server
sudo mysql_secure_installation  # 按提示设置root密码并加固

3. 安装PHP 8.1 + 扩展

sudo apt install -y php8.1-fpm php8.1-mysql php8.1-curl php8.1-gd php8.1-mbstring php8.1-xml php8.1-zip
sudo systemctl restart php8.1-fpm

三、配置数据库

1. 创建专用数据库

sudo mysql -u root -p  # 登录MySQL


执行SQL命令(密码自定义):

CREATE DATABASE wordpress_db;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'YourPassword123!';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

四、部署WordPress

1. 下载并解压

cd /var/www
sudo wget https://wordpress.org/latest.zip
sudo unzip latest.zip
sudo mv wordpress yourdomain.com  # 改为你的域名或项目名
sudo rm latest.zip

2. 设置权限

sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com

3. 配置Nginx

sudo nano /etc/nginx/sites-available/yourdomain.com


粘贴以下配置(替换yourdomain.com):

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

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


启用配置:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx

五、安装WordPress

  1. 访问安装页面
    浏览器打开 http://yourdomain.com
  2. 填写数据库信息
  • 数据库名:wordpress_db
  • 用户名:wp_user
  • 密码:YourPassword123!
  • 数据库主机:localhost
  • 表前缀:wp_(默认)
  1. 完成安装:设置站点标题、管理员账号和密码。

六、安全优化

1. 限制PHP文件执行(防木马)

sudo nano /var/www/yourdomain.com/wp-content/uploads/.htaccess

添加:
php_flag engine off

2. 自动续签SSL证书(Certbot)

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
sudo certbot renew --dry-run  # 测试自动续期

3. 禁用XML-RPC(防暴力破解)

sudo nano /etc/nginx/snippets/disable-xmlrpc.conf


添加:

location = /xmlrpc.php {
    deny all;
    return 404;
}


在Nginx配置中引入:

include snippets/disable-xmlrpc.conf;

七、日常维护命令

备份数据库mysqldump -u wp_user -p wordpress_db > wordpress_backup.sql
查看Nginx日志sudo tail -f /var/log/nginx/error.log
更新所有插件cd /var/www/yourdomain.com && sudo wp plugin update --all

此作者没有提供个人介绍。
最后更新于 2025-07-28