最早的 blog 是用 hexo 搭建的,写了一遍搭建教程,就荒废了。
后来更换 mac,也懒得重新搭建,当时 hugo 轻量又盛行,就改用 hugo 搭建了 CrazyKids’s Blog。
这次使用 WordPress 搭建,主要是 wp 更灵活,除了可以写写 blog,主要是做资源分享用。
本教程将指导你在 Ubuntu 20.04 上使用 Nginx、MySQL 和 PHP 安装并配置 WordPress,确保你能成功搭建一个运行 WordPress 的网站。假设你已经有了自己的服务器和域名。以下使用本站域名(crazykids.tech)为例
步骤 1:更新系统
确保系统是最新的,以避免软件包冲突。
sudo apt update
sudo apt upgrade -y
# 安装常用工具
sudo apt install vim wget -y
步骤 2:安装 Nginx Web 服务器
WordPress 需要一个 Web 服务器来处理 HTTP 请求,这里我们使用 Nginx。
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
步骤 3:安装 MySQL 数据库
WordPress 使用 MySQL 或 MariaDB 来存储数据,这里我们使用 MySQL。
安装 MySQL:
sudo apt install mysql-server -y
运行 MySQL 安全配置脚本:
sudo mysql_secure_installation
按照提示进行配置:
- 设置 root 密码(建议设置强密码)
- 移除匿名用户(选择 Y)
- 禁止远程 root 登录(选择 Y)
- 删除测试数据库(选择 Y)
- 刷新权限(选择 Y)
登录 MySQL 创建 WordPress 数据库:
sudo mysql -u root -p
在 MySQL 提示符下执行以下命令:
CREATE DATABASE wp_db DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY '你的密码';
GRANT ALL PRIVILEGES ON wp_db.* TO 'wp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
步骤 4:安装 PHP
WordPress 是基于 PHP 的,需要安装 PHP 和 PHP-FPM(FastCGI Process Manager)来与 Nginx 配合。
安装 PHP 及必要扩展:
sudo apt install php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y
检查 PHP 版本:
php -v
确认 PHP-FPM 是否运行:
sudo systemctl status php7.4-fpm
步骤 5:下载并配置 WordPress
下载最新版 WordPress:
cd /tmp
wget https://wordpress.org/latest.tar.gz
解压并移动到 Nginx 的 Web 目录:
tar -xvzf latest.tar.gz
sudo mv wordpress /var/www/html/wordpress
设置目录权限:
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
创建 WordPress 配置文件:
sudo mv /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
编辑 wp-config.php 文件:
sudo vim /var/www/html/wordpress/wp-config.php
修改以下行,填入之前创建的数据库信息:
define('DB_NAME', 'wp_db');
define('DB_USER', 'wp_user');
define('DB_PASSWORD', '你的密码');
define('DB_HOST', 'localhost');
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', 'utf8mb4_unicode_ci' );
步骤 6:配置 Nginx 虚拟主机
创建一个新的 Nginx 配置文件:
sudo vim /etc/nginx/sites-available/wordpress
添加以下内容
server {
listen 80;
server_name crazykids.tech www.crazykids.tech;
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/php7.4-fpm.sock; # 根据实际 PHP 版本调整
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
启用 Nginx 配置:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
步骤7: 配置域名解析
到自己的域名提供商,比如阿里云,设置域名的A记录,CNAME记录
Type: A
Name: crazykids.tech
Value: 你的服务器IP
TTL: 自动或 300
Type: CNAME
Name: www
Value: crazykids.tech
TTL: 自动或 300
此时已经可以通过域名访问自己的网站了,但只能http的。
步骤8: 安装 Certbot 和 Let’s Encrypt 插件
Certbot 是 Let’s Encrypt 的官方工具,用于申请和自动续期证书。
安装 Certbot
sudo apt install certbot python3-certbot-nginx
验证 Certbot 安装
certbot --version
使用 Certbot 申请 Let’s Encrypt 证书
sudo certbot --nginx -d crazykids.tech -d www.crazykids.tech
- d 指定你的域名,包含所有需要 HTTPS 的子域名。
- Certbot 会与 Let’s Encrypt 服务器通信,验证你的域名,并生成证书。
- 选择是否将 HTTP 流量重定向到 HTTPS(推荐选择重定向,输入 2)。
Certbot 默认配置了自动续期,验证续期任务是否正常:
sudo certbot renew --dry-run
步骤9: 更新wordpress配置
- 登录 WordPress 后台,进入 设置 > 常规,将“WordPress 地址 (URL)”和“站点地址 (URL)”更新为 https://crazykids.tech。
- 或者编辑 /var/www/wordpress/wp-config.php,添加:
define('WP_HOME', 'https://crazykids.tech');
define('WP_SITEURL', 'https://crazykids.tech');
到此,已经配置完成,可以通过https域名访问了,进入到后台https://www.crazykids.tech/wp-admin, 开始配置主题,写blog吧。