Ghost介绍
Ghost 是一套基于 Node.js 构建的开源博客平台(Open source blogging platform),具有易用的书写界面和体验,博客内容默认采用 Markdown 语法书写,支持插入codepen, YouTube等外联。
Ghost目标是取代臃肿的 Wordpress,界面简洁,专注写作,支持在线预览和在线写作。
Ghost属于动态博客,页面并不是像Hexo,Jekyll这类静态博客,在编译的时候会生成所有页面。Ghost有前台和后台。后台负责写作,发布文章,系统配置,等等。
Ghost的优势和劣势
这里有篇文章是这样评论Ghost的优缺点的
- 优势
技术上,采用NodeJs,在可预见的未来里,无疑比PHP有更多优势,并发能力远超Wordpress,虽然NodeJs后期维护成本高,但是我们只是借它做博客而已。
易用性上,专注写作,评论,超炫皮肤,完美支持 MarkDown,没有Wordpress那么臃肿,回归到博客最原始的状态,传递文字最原始的力量。
使用上,便捷,随时随地编辑,比Hexo,Jekyll这类静态博客要书写方便,特别是在不同电脑上写作时(并支持写作手机端写作)。 - 劣势
需要配套支持Node环境的虚拟机,一般免费的很少支持,这时必须得掏腰包了。
后台简陋,许多功能还未完善,不过写作这一块没啥大问题。
Ghost的亮点
- 采用Mysql作为数据库,通用快速上手,这里也可以用其他数据库比如Sqlite。
- Nginx作为反向代理,配置多个Ghost博客,同时也能增加了网站的负载。
- 非常简易化的Ubuntu的Node.js安装方法,不用编译打包。
- 安装系统服务,开机重启Ghost服务,免去日后以后操作。
- 采用Font Awesome作为社交按钮,也可以自定义图标。
- highlight.js 作为主题的代码高亮引擎
- 整合Disqus评论系统,建立属于自己的Discuss圈
- 国外优秀免费Ghost主题资源分享
- 整合百度统计以及百度分享
环境
- vultr VPS(自带翻墙~~)
- dynadot域名注册(无需备案)
- CentOS 7
- Node 10.13.0
- Mysql(MariaDB) 5.5.60
- Ghost 2.6.2
- Nginx 1.14.1
步骤
- 使用SecureCRT、Xshell等工具连接你的云服务器或VPS,具体使用方法请自行百度。
检查更新CentOS。
[root@host /]# yum update -y
[root@host /]# yum install -y wget
安装gcc、g++以及其他工具
- Nodejs安装需要gcc 4.8以上版本,CentOS 7默认安装的gcc为4.8.5,所以无需再像CentOS 6那样手动升级了。
- 安装vim,curl,pm2等基础工具。
安装Nodejs
已经安装服务的跳过,安装教程参见另一篇文章
npm -v
6.7.0 #显示版本的说明已经安装成功
安装Nginx
以下为centOS7下安装nginx教程:
[root@host /]# sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装Nginx:
[root@host /]# yum install nginx -y
设置Nginx服务器自动启动:
[root@host /]# systemctl enable nginx.service
启动Nginx并查看Nginx服务状态:
[root@host /]# systemctl start nginx.service
[root@host /]# systemctl status nginx.service
到此Nginx就安装完成了,到浏览器地址栏输入你的云服务器或VPS的IP后回车,就能看到“Welcome to Nginx!”字样了。如果还是无法访问,检查是否开启防火墙
# centOS7
firewall-cmd --state --查看防火墙状态
systemctl stop firewalld.service --停止firewall
systemctl disable firewalld.service --永久停止停止firewall
# centOS6
servcie iptables stop --临时关闭
chkconfig iptables off --永久关闭防火墙
安装MySQL
Ghost默认使用的是Sqlite3数据库,如果内容多的话,推荐改用MySQL数据库。如果不需要则跳过此步(存的数据会在core目录下面)。
重要:这里是我遇到的(第一个坑)。
从CentOS 7开始,CentOS使用MariaDB代替了MySQL数据库,MariaDB系MySQL的一个分支,使用方法和MySQL基本一致,主要由开源社区维护,采用GPL授权许可。开发此分支的主要原因之一:Orcale公司收购了MySQL,所以MySQL有闭源的可能,因此社区采用了开源的MariaDB来规避此风险。
(注:以下除指令以外,均使用MySQL来代称MariaDB)
原先CentOS 6安装MySQL的指令在7中已不适用:
[root@host /]# yum install mysql mysql-server #CentOS 6适用
[root@host /]# yum install mariadb-server -y #CentOS 7适用
启动MySQL服务:
[root@host /]# service mysqld start #CentOS 6适用
[root@host /]# systemctl start mariadb.service #CentOS 7适用
设置开机启动服务:
[root@host /]# chkconfig mysqld on #CentOS 6适用
[root@host /]# systemctl enable mariadb.service #CentOS 7适用
输入以下指令配置MySQL:
[root@host /]# mysql_secure_installation
然后根据以下弹出来选项的分别进行设置:
Set root password? [y/n] # 设置root密码
anonymous users? [y/n] # 删除匿名用户
Disallow root login remotely? [y/n] # 禁止root用户远程登录
Remove test database and access to it? [y/n] # 删除默认的 test 数据库
Reload privilege tables now? [y/n] # 刷新授权表使修改生效
为避免数据库里的中文出现乱码,还需配置MySQL字符集编码:
通过vim指令打开my.cnf文件:
[root@host /]# vim /etc/my.cnf
然后按i
键进入编辑模式,在原有的基础上插入以下内容:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
输入完成后按esc
键,再输入:wq
保存退出。
重启mysql:
[root@host /]# service mysqld restart #CentOS 6适用
[root@host /]# systemctl restart mariadb.service #CentOS 7适用
新建一个数据库,用来存放博客的数据:
打开mysql:
[root@host /]# mysql -u root -p #回车在新弹出的输入栏里输入密码
创建ghost数据库:
MariaDB [(none)]> create database ghost;
新建用户:
MariaDB [(none)]> grant all privileges on ghost.* to 'ghost'@'%' identified by '123456'; # 新建一个用户ghost,密码为123456,用户名及密码自行更改
重新读取权限表中的数据到内存,不用重启MySQL就可以让权限生效:
MariaDB [(none)]> flush privileges;
到此数据库准备完成!
安装Ghost
跳转到Ghost存放目录上级菜单:
[root@host /]# mkdir -p home/www/ghost
[root@host /]# cd home/www
-----------------
如果建好目录直接进行跳转
[root@host /]# cd home/www
转到www
目录,然后在线下载Ghost(第二个坑,网上大部分教程里的那条指令已经无法使用):
最新版英文包:
[root@host www]# curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
解压zip压缩包:
[root@host www]# unzip ghost.zip -d ghost
转到解压后的ghost
文件夹下:
cd ghost
修改默认配置(第三个坑):
Ghost目前最新英文原版的版本号为2.6.2,解压后的文件夹下已经没有config.example.js这个文件,加载配置文件的方式已经更改,0.7.4的方式已不再适用。
由于我使用的是2.x版的Ghost,所以下面介绍仅做2.x的介绍,至于0.7.4网上有详细教程,这里不再赘述。
转到配置文件所在的目录:
[root@host ghost]# cd core/server/config/env/
里面有4个json文件,这就是Ghost的配置文件了:
config.develogment.json #开发模式
config.production.json #生产模式(真正发布需要用到这个)
config.testing.json #测试模式
config.testing-mysql.json #mysql测试模式
先备份一下以防万一
[root@host env]# cd ..
[root@host config]# cp -r env envbak
首先更改开发模式的配置文件-config.develoment.json,第一将url改为你的公网IP地址或域名;第二将默认的Sqlite3数据库改成MySQL数据库,并配置相关connection属性(不用MySQL的可以跳过第二条):
[root@host env]# vim config.development.json
原文件里的内容:
{
"url": "http://localhost:2368",
"database": {
"client": "sqlite3",
"connection": {
"filename": "content/data/ghost-dev.db"
},
"debug": false
},
"paths": {
"contentPath": "content/"
},
"privacy": {
"useRpcPing": false,
"useUpdateCheck": true
},
"useMinFiles": false,
"caching": {
"theme": {
"maxAge": 0
},
"admin": {
"maxAge": 0
}
}
}
修改后的内容:
{
"url": "http://xx.xx.xx.xx", #这里配置的就是访问博客时地址栏输入的东西,将其修改成你的公网IP或域名,注意保留http://
"database": {
"client": "mysql",
"connection": {
"host": "127.0.0.1",
"user": "ghost数据库用户名",
"password": "ghost数据库密码",
"database": "ghost",
"charset": "utf8"
},
"debug": false
},
"paths": {
"contentPath": "content/"
},
"privacy": {
"useRpcPing": false,
"useUpdateCheck": true
},
"useMinFiles": false,
"caching": {
"theme": {
"maxAge": 0
},
"admin": {
"maxAge": 0
}
}
}
修改好后保存退出。开发环境就配置好了。
生产环境操作同理,只是内容稍有不同。以下为修改后的内容:
{
"url": "http://xx.xx.xx.xx",
"server": {
"port": 2368
},
"database": {
"client": "mysql",
"connection": {
"host": "127.0.0.1",
"user": "ghost数据库用户名",
"password": "ghost数据库密码",
"database": "ghost",
"charset":"utf8"
}
},
"paths": {
"contentPath": "content/"
},
"logging": {
"level": "info",
"rotation": {
"enabled": true
},
"transports": ["file", "stdout"]
}
}
修改好后保存退出,产品模式配置完成。
输入生产环境的启动指令,启动Ghost:
[root@host env]# npm start --production
至此,如果运行没报错的话,那么Ghost相关配置都完成了。
配置Nginx作为Ghost的反向代理
本地Ghost已经部署好了,但是我们想让外网可以访问到你的博客页面,那么我们就需要配置Nginx来作为Ghost的反向代理。其他如Apache等web服务器也可以做反向代理,这里我们只介绍Nginx。
进入Nginx配置目录,更改Nginx配置文件:
[root@host /]# vim /etc/nginx/conf.d/default.conf
将server_name
后面的lihx.icu
改为你的IP或者域名:
# 配置http
server {
listen 80;
server_name lihx.top;
location / {
# 重定向为https
rewrite ^(.*)$ https://$host$1 permanent;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# 使用https访问
server {
listen 443;
server_name lihx.top;
location / {
proxy_pass http://127.0.0.1:2368;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
输入完成后保存退出。
重启Nginx服务:
[root@host conf.d]# sudo systemctl reload nginx
如果重启失败,说明配置文件设置错误,回去检查一下书写是否正确,尤其注意末尾的,
号。如果没有报错,那么就大功告成!
- 打开浏览器,输入你设置的IP或域名,你就能外网访问你的博客页面了,然后尽情地玩耍吧。
补充
我们之前使用的npm start --production
命令在开发或测试模式下用用还行,但是如果我们关闭终端窗口或从ssh断开连接时,Ghost就会停止.
为了防止Ghost停止运行,我们要使用进程管理器配置Ghost,让其永久运行:
npm install pm2 -g # 安装PM2
[root@host ghost]# NODE_ENV=production pm2 start index.js --name "ghost"
[root@host ghost]# pm2 startup centos
[root@host ghost]# pm2 save
开启/停止/重启Ghost:
[root@host ghost]# pm2 start ghost
[root@host ghost]# pm2 stop ghost
[root@host ghost]# pm2 restart ghost