在云服务器上搭建Ghost博客

Ghost是一个基于Node.js开发的免费开源博客平台,用于简化博客的写作发布等流程。本文介绍如何在Ubuntu 20.04操作系统的ECS实例上部署Ghost博客。
前提条件:
官方推荐安装需要以下版本
- Ubuntu 16.04、Ubuntu 18.04、Ubuntu 20.04 或 Ubuntu 22.04
- NGINX(SSL最低为1.9.5)
- 支持的Node.js
- MySQL 8
- 至少具有 1GB 内存的服务器
- 已注册的域名
- Systemd
在开始之前,您应该从您的域设置一个有效的 DNS A 记录,指向服务器的 IP 地址。必须提前完成此操作,以便在安装过程中配置 SSL。
服务器设置
创建新用户
打开终端并以root用户身份登录服务器:
# Login via SSH
ssh root@your_server_ip
# Create a new user and follow prompts
adduser <user>
然后将其赋予权限:
# Add user to superuser group to unlock admin privileges
usermod -aG sudo <user>
# Then log in as the new user
su - <user>
更新包
# Update package lists
sudo apt-get update
# Update installed packages
sudo apt-get upgrade
安装NGINX
sudo apt-get install nginx
使防火墙允许HTTP和HTTPS连接:
sudo ufw allow 'Nginx Full'
安装MySQL
1.执行命令,安装sql:
sudo apt-get install mysql-server
2.连接MySQL:
sudo mysql
3.执行命令,创建Ghost Blog使用的数据库:
CREATE DATABASE ghost_blog_data;
4.执行以下命令,设置root
用户密码。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '输入root账号密码';
5.退出:
\q
安装Node.js
注意需要安装受支持版本的Node.js,当前(Ghost5)使用的是16.x。
# Add the NodeSource APT repository for Node 16
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash
# Install Node.js
sudo apt-get install -y nodejs
安装Ghost-CLI
sudo npm install ghost-cli@latest -g
安装Ghost
1.创建一个目录
# Create directory: Change `sitename` to whatever you like
sudo mkdir -p /var/www/sitename
# Set directory owner: Replace <user> with the name of your user
sudo chown <user>:<user> /var/www/sitename
# Set the correct permissions
sudo chmod 775 /var/www/sitename
# Then navigate into it
cd /var/www/sitename
2.安装
ghost install
配置相关参数
- Enter your blog URL:请输入已解析的域名
- Enter your MySQL hostname:请输入数据库连接地址。按Enter保持默认即可。
- Enter your MySQL username:请输入数据库用户名,输入root后按Enter。
- Enter your MySQL password :请输入数据库密码,输入数据库密码后按Enter。
密码为刚才数据库设置的密码
- Enter your database name:输入Ghost使用的数据库名称。输入ghost_blog_data后按Enter。
此名称为刚才创建的名称
完成设置后,会输出管理员访问的地址。
配置多个ssl域名
假设你有两个url,例如example.com
和 www.example.com
,你想将 www.example.com
作为第二域名。
- 需要第二个域名的DNS记录
cd进入你的ghost目录,配置你的第二域名,例如:
ghost config url https://www.example.com
接着配置ssl
ghost setup nginx ssl
然后将域名改为旧的url:
ghost config url https://example.com
配置nginx:
cd /etc/nginx/sites-available
此时有四个文件
- example.com-ssl.conf
- example.com.conf
- www.example.com-ssl.conf
- www.example.com.conf
现在我们需要 编辑辅助域的文件 .
所以我需要编辑的文件是 www.example.com-ssl.conf
和www.example.com.conf
将return 301 https://example.com;
添加到location
后
然后检查文件是否有效:
sudo nginx -t
如果成功,则重新加载Nginx服务器
sudo nginx -s reload
大功告成。