Ubuntu服务器搭建Ghost博客

前言

本文转载自 Ghost Blog安装笔记–淮城一只猫,并有少量原创内容

之前使用过 ghost 搭建博客,但因为速度慢、太难上手、没有好看的主题再加上服务器需要用于其他用途,迁移回了 hexo 。现在除了难于上手外问题基本都解决了,并且 ghost 还有了很多新特性让我难以抗拒,于是乎就搬回了 ghost 。

我用的是阿里云轻量级应用服务器,学生机,Ubuntu18.04LTS,1 CPU/2G RAM/40G ROM.废话不多说,开始整活,详细介绍一下搭建 ghost 博客的具体步骤。

创建新管理员用户

由于 Ghost 不允许 root 安装,所以需要新建一个账户并让他以 root 身份登录

# 更新软件
sudo apt update && sudo apt upgrade         

# ghost不允许root用户安装,所以需要新建个<user>用户
adduser <user>                              

# 给予<user>用户为超级权限
usermod -aG sudo <user>                     

# 登录用户
su - <user>                                 

这里的 用户名可以自己任意修改,例如 tom 、 gh 、 lic 等。
但不能使用 ghost 作为用户名,因为和 ghost-cli 用户名产生冲突。

安装 Node.js 环境和 ghost-cli

# 下载并安装 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
# 添加下面这句话到 ~/.bashrc 或 ~/.zshrc
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

# 查看最新的 nodejs-lts 版本
nvm ls-remote --lts 
# 安装 nodejs。版本号可能会有变化
nvm install 12.16.1

# 安装 nrm 并更换 npm 源为 taobao 镜像
npm install nrm -g
nrm use taobao

# 安装 ghost-cli
npm install ghost-cli@latest -g

安装Nginx环境

sudo apt-get install nginx
nginx -v                                    

# 关闭防火墙
sudo ufw allow 'Nginx Full'                 

安装数据库

我使用的是 MariaDB,而不是官方推荐的 MySql
MariaDB 的安装很简单,只需要点击这个链接,选择你的操作系统,复制安装脚本,粘贴到你的命令行就好了

例如Ubuntu 18.04安装10.4版本的数据库:

sudo apt-get install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/ubuntu bionic main'
sudo apt update
sudo apt install mariadb-server

安装完成之后,使用 mysqladmin -u root -p password <your password> 命令修改 root 密码

创建网站目录,并安装 ghost

# 创建ghost文件夹的网站目录,这个名字可以自己随便定义
sudo mkdir -p /var/www/ghost

# 使文件夹拥有权限,<user>是当前用户名
# 例如当前用户名是ubuntu,那么应该是:sudo chown ubuntu:ubuntu /var/www/ghost
sudo chown <user>:<user> /var/www/ghost

# 设置读写权限
sudo chmod 775 /var/www/ghost

# 定位
cd /var/www/ghost

# 进行Ghost博客平台安装
ghost install

安装过程根据主机性能来决定,下面是选项配置讲解:

  • Enter your blog URL: 博客展示地址,格式:http(s):(www.)example.com,不支持ip地址。
  • Enter your MySQL hostname: 数据库主机地址,回车默认是localhost。
  • Enter your MySQL username: / Enter your MySQL password: 数据库用户名/密码,上面安装的数据库用户名是root,密码是自己设置的。
  • Enter your Ghost database name: Ghost博客数据库名
  • Do you wish to set up “ghost” mysql user? 设置数据库用户名为ghost,这个为了避免权限太大导致数据瞎搞,Ghost官方推荐是y。
  • Do you wish to set up Nginx? 自动化Nginx,推荐y。
  • Do you wish to set up SSL? 配置SSL证书,推荐y,如果你有证书问题,请查看文章最下面的问题。
  • Enter your email (For SSL Certificate):个人邮箱,配置ssl证书需要的邮箱地址。
  • Do you wish to set up Systemd? 使用ghost进程管理工具,推荐y,个人觉得海星。
  • Do you want to start Ghost? 启动Ghost博客就可以进入网址访问。

CDN 加速

我使用的是腾讯云的免费CDN,使用 CDN 本身没有什么难度,我用的时候主要有下面两个困惑

SSL 证书下载

在上一步 ghost install 过程中,我们免费申请了 letsencrypt 的免费 SSL 证书,而使用 cdn 就需要提供 SSL 证书,哪我们的证书在哪里呢?

网上的资料不多,我最后通过谷歌找到了,就在我们的服务器的 /etc/letsencrypt/yoursite.com 中,文件目录如下:

91c7cb115a3d8ee6

腾讯云使用的就是 fullchain.ceryoursite.com.key 两个证书

cdn 的 CNAME 记录和 A 记录解析冲突

勇敢地把 A 记录删除掉就 OK了,具体原因我也不是很懂

图片优化

安装并配置 qn-store 后,我们在 ghost 后台上传的图片就直接存储在七牛云图床上,而不是在服务器上了

在/var/www/ghost/content/adapters文件下新建个storage文件夹

cd /var/www/ghost/content/adapters/storage # 定位
git clone https://github.com/Minwe/qn-store.git # 拉取源码
cd qn-store # 定位
npm install # 安装模块依赖

然后打开/var/www/ghost/config.production.json文件,添加代码:

{
  // ...
  "storage": {
    "active": "qn-store",
    "qn-store": {
      "accessKey": "your access key", // https://portal.qiniu.com/user/key获取AK密匙
      "secretKey": "your secret key", // https://portal.qiniu.com/user/key获取SK密匙
      "bucket": "your bucket name", // 存储对象空间名字
      "origin": "http://xx.xx.xx.glb.clouddn.com", // 七牛CDN地址
      "fileKey": {
        "safeString": true,
        "prefix": "YYYYMM/"
      }
    }
  }
  // ...
}

最后重启Ghost

cd /var/www/ghost
ghost restart
本作品采用《CC 协议》,转载必须注明作者和本文链接
licoded
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!