最早的 blog 是用 hexo 搭建的,写了一遍搭建教程,就荒废了。
后来更换 mac,也懒得重新搭建,当时 hugo 轻量又盛行,就改用 hugo 搭建了 CrazyKids’s Blog。
这次使用 WordPress 搭建,主要是 wp 更灵活,除了可以写写 blog,主要是做资源分享用。
本教程将指导你在 Ubuntu 20.04 上使用 Nginx、MySQL 和 PHP 安装并配置 WordPress,确保你能成功搭建一个运行 WordPress 的网站。假设你已经有了自己的服务器和域名。以下使用本站域名(crazykids.tech)为例
确保系统是最新的,以避免软件包冲突。
sudo apt update
sudo apt upgrade -y
# 安装常用工具
sudo apt install vim wget -y WordPress 需要一个 Web 服务器来处理 HTTP 请求,这里我们使用 Nginx。
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx WordPress 使用 MySQL 或 MariaDB 来存储数据,这里我们使用 MySQL。
安装 MySQL:
sudo apt install mysql-server -y 运行 MySQL 安全配置脚本:
sudo mysql_secure_installation 按照提示进行配置:
登录 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; 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 下载最新版 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' ); 创建一个新的 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 到自己的域名提供商,比如阿里云,设置域名的A记录,CNAME记录
Type: A
Name: crazykids.tech
Value: 你的服务器IP
TTL: 自动或 300
Type: CNAME
Name: www
Value: crazykids.tech
TTL: 自动或 300 此时已经可以通过域名访问自己的网站了,但只能http的。
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 Certbot 默认配置了自动续期,验证续期任务是否正常:
sudo certbot renew --dry-run define('WP_HOME', 'https://crazykids.tech');
define('WP_SITEURL', 'https://crazykids.tech'); 到此,已经配置完成,可以通过https域名访问了,进入到后台https://www.crazykids.tech/wp-admin, 开始配置主题,写blog吧。
I'll be honest—…
What is Black S…