Welcome to Ghost
- W_Z_C
- 共 1782 字,阅读约 4 分钟
又一次开始我的博客之旅,这一次不知道可以坚持多少时间,掐指一算这个过程至少有七八次了,幸好每次都有一些东西可以留存下来,接下来的写作计划就不说了,因为很有可能中途就放弃了,行动力还有待提高啊!
本文算是博客的开篇,老套路,就写如何部署 Ghost 博客系统吧!Ghost 从无到有,从 rc 版到稳定版,从 2.0 到 3.0,我的博客竟然还在写开篇……
1. 准备
我个人比较喜欢 Ghost 系统,这个系统的初期源码我还阅读过,并且还抽象出了一个简单框架用于公司的 web 后台,后来还写了 Express 源码刨析 的文章,这里就不赘述了。Ghost 系统最新的版本是 3.2,除了主题略有变化(感觉变丑了)之外,还有一个会员订阅的实验功能,主要用于付费文章,感觉思路很不错,谁不想通过写博客赚个小目标呢?不过遗憾的是基本上和中国用户无缘,一个是付费意识,这就不说了,另一个更重要的原因是它付费的服务是基于第三方的接口,目前这个接口对国内支持非常不友好,申请也麻烦的很,希望以后有机会可以靠博客订阅发家致富……
在国内,其实 Ghost 博客并不适合普通站长,因为它部署的成本比 wordpress、typecho 之类的系统要高很多,不提不易寻找的运行环境,就说它一直在频繁升级这件事情就已经很要命了,目前这种现象有所好转,至少升级很方便,一条命令即可搞定。
Ghost 博客系统是基于 nodejs 开发的,所以部署这个博客你需要一个 nodejs 的运行环境,显然一般的虚拟主机是无法提供的,这里推荐购买 vps。具体的供应商很多,如 linode、vultr、DigitalOcean、阿里云、腾讯云、微软、google,大部分 512M/1024M 基础型号的价格都不太贵,每个月大约5刀左右的样子,还可以容忍。
除了主机之外,剩下的就是搭建运行环境和域名购买设置等操作,而这篇文章只是单纯的介绍 Ghost 博客系统的环境安装。
Ghost 博客安装的前提需求如下:
- Ubuntu 16.04 或者 Ubuntu 18.04
- NGINX (minimum of 1.9.5 for SSL)
- Nodejs (目前推荐 10.x )
- MySQL 5.5, 5.6, or 5.7 (not >= 8.0)
- Systemd
- 至少 1GB 内存的 VPS
- 一个已经注册好的域名
2. 系统安装
当前的系统安装比较只能,操作也很友好,具体可以查看 官方文档 。具体详细步骤如下:
首先需要创建一个新的用户,用于 Ghost 安装:
# Login via SSH
ssh root@your_server_ip
# Create a new user and follow prompts
adduser
设置权限:
# Add user to superuser group to unlock admin privileges
usermod -aG sudo
# Then log in as the new user
su -
更新软件包:
# Update package lists
sudo apt-get update
# Update installed packages
sudo apt-get upgrade
安装 Nginx:
# Install NGINX
sudo apt-get install nginx
如果开启了防护墙:
sudo ufw allow 'Nginx Full'
安装 MySQL:
# Install MySQL
sudo apt-get install mysql-server
设置 MySQL 的密码,下面是 Ubuntu 18.04 的操作命令:
# To set a password, run
sudo mysql
# Now update your user with this password
# Replace 'password' with your password, but keep the quote marks!
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
# Then exit MySQL
quit
# and login to your Ubuntu user again
su -
安装 Nodejs:
# Add the NodeSource APT repository for Node 10
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash
# Install Node.js
sudo apt-get install -y nodejs
安装 Ghost-CLI:
sudo npm install ghost-cli@latest -g
创建 Ghost 系统安装目录:
# We'll name ours 'ghost' in this example; you can use whatever you want
sudo mkdir -p /var/www/ghost
# Replace with the name of your user who will own this directory
sudo chown : /var/www/ghost
# Set the correct permissions
sudo chmod 775 /var/www/ghost
# Then navigate into it
cd /var/www/ghost
开始安装:
ghost install
3. 设置域名跳转
在安装过程中,会提示你输入相关的信息,在设置 Nginx 和 SSL 选项的时候,推荐选择 No,然后自己手动配置域名证书和 Nginx 配置文件。
因为默认 Ghost 系统指支持博客系统默认 URL,如果你想添加类似 www 前缀的域名,需要重新申请证书,并且还需要修改 Nginx 配置文件,还不如自己从头配置容易掌控。
3.1 申请证书
如果你选择了 No,那么需要自己主动申请证书,需要安装 certbot,步骤如下(Ubuntu 18.06):
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
安装:
sudo apt-get install certbot python-certbot-nginx
获取证书:
sudo certbot certonly --nginx
获取证书的时候,把 www 和 非www 的域名全部填进去。证书默认生成在 /etc/letsencrypt/live/
目录下,这个证书90天后就过期了,所以,需要使用一个 cron job 来定期更新。可以使用命令 crontab -e
来编辑定时任务:
0 0 1 * * /usr/bin/certbot renew --force-renewal
5 0 1 * * /usr/sbin/nginx -s reload
3.2 Nginx 配置
Nginx 配置比较简单,首先去 Nginx 的配置目录 /etc/nginx/sites-available
,增加三个文件,以我博客的域名为例:
先增加 wangzhechao.com 非 www 域名的跳转配置:
server {
listen 80;
listen [::]:80;
server_name wangzhechao.com;
root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
location / {
return 301 https://wangzhechao.com$request_uri;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
}
如果你自己添加,请把 wangzhechao.com 改为你自己的域名。
接着,增加 www.wangzhechao.com 域名跳转:
server {
#in the following lines, replace example.com with your domain name.
server_name www.wangzhechao.com; # the URL you want to redirect from
root /var/www/ghost/system/nginx-root; # replace /var/www/example with the location of your ghost installation
location / {
return 301 https://wangzhechao.com$request_uri; # the URL you want to redirect to
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
#in the following lines, replace example.com with your domain name.
server_name www.wangzhechao.com; # the URL you want to redirect from
root /var/www/ghost/system/nginx-root; # replace /var/www/example with the location of your ghost installation
ssl_certificate /etc/letsencrypt/live/wangzhechao.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/wangzhechao.com/privkey.pem;
include /etc/nginx/snippets/ssl-params.conf;
location / {
return 301 https://wangzhechao.com$request_uri; # the URL you want to redirect to
}
}
接着添加 wangzhechao.com 的 https 配置:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name wangzhechao.com;
root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)
ssl_certificate /etc/letsencrypt/live/wangzhechao.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/wangzhechao.com/privkey.pem;
include /etc/nginx/snippets/ssl-params.conf;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:2369;
}
location ~ /.well-known {
allow all;
}
client_max_body_size 50m;
}
这三个配置分别创建三个文件,创建完成后,将它们软连接到 sites-enabled
目录:
ln -s /etc/nginx/sites-available/wangzhechao.com.conf .
ln -s /etc/nginx/sites-available/www.wangzhechao.com.conf .
ln -s /etc/nginx/sites-available/wangzhechao-ssl.com.conf .
最后,重启 Nginx 。
service nginx restart