一.环境准备
Vultr VPS
CentOS 7.3 x64
Node v10.15.3
MariaDB
Ghost 2.19.1
Nginx 1.14.2
二.开始安装
使用 Xshell 等工具连接你的云服务器或VPS,具体的使用方式请 Google 咯。
2.1 更新 CentOS
[root@bwh /]# yum update -y[root@bwh /]# yum install -y wget
2.2 安装 vim,curl
[root@bwh /]# yum install -y vim[root@bwh /]# yum install -y curl
如需要安装其他工具,例如 gcc-c++ 等,请根据终端相关提示进行安装
2.3 安装 Nodejs
1 安装 EPEL
[root@bwh /]# yum install epel-release -y
2 安装 Nodejs 和 npm
[root@bwh /]# curl -sL https://rpm.nodesource.com/setup_10.x | bash -[root@bwh /]# yum install -y nodejs
安装完成后,在终端输入 node -v 回车,显示版本号则表示安装成功。
2.4 安装 Nginx
添加Nginx软件库
[root@bwh /]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
接着安装 Nginx
[root@bwh /]# yum -y install nginx
设置 Nginx 自动启动
[root@bwh /]# systemctl enable nginx.service
启动 Nginx 并查看 Nginx 服务状态
[root@bwh /]# systemctl start nginx.service[root@bwh /]# systemctl status nginx.service
在浏览器中输入IP地址,可以看到默认的Nginx的网页了。
2.5 安装 MySQL(MariaDB)
CentOS 7开始,CentOS使用MariaDB代替了MySQL数据库,MariaDB系MySQL的一个分支,使用方法和MySQL基本一致,主要由开源社区维护,采用GPL授权许可。开发此分支的主要原因之一:Orcale公司收购了MySQL,所以MySQL有闭源的可能,因此社区采用了开源的MariaDB来规避此风险
1.安装 MariaDB
[root@bwh /]# yum install mariadb-server -y
2.启动 MariaDB 服务
[root@bwh /]# systemctl start mariadb.service
3.设置开机启动
[root@bwh /]# systemctl enable mariadb.service
4.配置 MariaDB
[root@bwh /]# 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] #是否刷新授权表使修改生效
5.配置 MariaDB 字符集编码
[root@bwh /]# vim /etc/my.cnf
插入以下内容
[client]default-character-set=utf8
[mysql]default-character-set=utf8
[mysqld]character-set-server=utf8
collation-server=utf8_general_ci
Esc
– :wq
保存退出
6.重启 MariaDB
[root@bwh /]# systemctl restart mariadb.service
7.新建数据库,用以存放你的博客数据
[root@bwh /]# mysql -u root -p #输入root数据库用户密码
MariaDB [(none)]> create database exampleDb; #创建 exampleDb 数据库
MariaDB [(none)]> insert into mysql.user(Host,User,Password) values("localhost","xxx",password("xxx")); #新增用户,xxx替换成你自己的用户名/密码
MariaDB [(none)]> flush privileges; #刷新权限表
MariaDB [(none)]> grant all privileges on exampleDb.* to 'xxx'@'%' identified by 'xxx'; # 赋予用户权限,xxx 表示用户名/密码MariaDB [(none)]> flush privileges; #赋予用户权限后,再次刷新权限表
至此,数据库设置完成。
2.6 安装 Ghost
1.创建Ghost存放目录
[root@bwh /]# mkdir var/www[root@bwh /]# mkdir var/www/sites
2.转到www目录,在线下载 Ghost
[root@bwh www]# curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
3.解压下载的 ghost.zip 文件到 sites
目录下
[root@bwh www]# unzip ghost.zip -d sites
4.cd
进入 sites 目录,使用以下命令初始化 Ghost 包
[root@bwh sites]# npm install 或 yarn
5.修改 Ghost 默认配置
[root@bwh sites]# cd core/server/config/env/
里面有4个Ghost的配置文件
config.develogment.json #开发配置
config.production.json #生产配置(发布到你的服务器需要用到这个)
config.testing.json #测试配置
config.testing-mysql.json #mysql测试配置
5.1 更改开发配置文件 config.development.json
[root@bwh env]# vim config.development.json
将原内容替换成以下
{
"url": "http://example.com", #将example.com修改成你的域名
"database": {
"client": "mysql",
"connection": {
"host": "127.0.0.1",
"user": "用户名", #这里是在 2.5节7 中设置的用户
"password": "用户密码",
"database": "exampleDb",
"charset": "utf8"
},
"debug": false
},
"paths": {
"contentPath": "content/"
},
"privacy": {
"useRpcPing": false,
"useUpdateCheck": true
},
"useMinFiles": false,
"caching": {
"theme": { "maxAge": 0 },
"admin": { "maxAge": 0 }
}
}
Esc
– :wq
保存退出,同理修改生产配置文件 config.production.json
,修改后内容如下
{
"url": "http://example.com",
"server": {
"port": 2368
},
"database": {
"client": "mysql",
"connection": {
"host": "127.0.0.1",
"user": "用户名",
"password": "用户密码",
"database": "exampleDb",
"charset":"utf8"
}
},
"paths": {
"contentPath": "content/"
},
"logging": {
"level": "info",
"rotation": {
"enabled": true
},
"transports": ["file", "stdout"]
}
}
Esc
– :wq
保存退出,Ghost 相关的配置完成。进入 sites
目录,使用下面的命令启动 Ghost,在浏览器中输入你的公网ip就可以查看效果。
[root@bwh sites]# npm start --production
2.7 配置 Nginx 反向代理
服务器 Ghost 已经部署好了,但是我们想在外网通过域名访问到自己的博客,那么我们就需要配置 Nginx 来作为 Ghost 的反向代理。
进入 Nginx 配置目录,新建 Ghost 博客的 Nginx 配置文件
[root@bwh /]# vim /etc/nginx/conf.d/your_sites.conf
以下内容输入到文件中
server {
listen 80;
server_name example.com; #example.com改为你的域名
access_log /var/log/nginx/ghost.log;
error_log /var/log/nginx/ghost_error.log;
proxy_buffers 16 64k;
proxy_buffer_size 128k;
location / {
proxy_pass http://127.0.0.1:2368; # 代理到Ghost配置文件配置的服务器
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;
}
}
Esc
– :wq
保存后退出。将/etc/nginx/conf.d/ 目录下的默认配置文件 default.conf 修改为default.conf.bak,让 Nginx 只引用 your_sites.conf。
[root@bwh conf.d]# mv default.conf default.conf.bak
重启Nginx服务。
[root@bwh conf.d]# systemctl restart nginx.service
2.8 使用进程管理器 pm2 让 ghost 永久后台运行
在2.6节中我们在终端 npm start ghost,此时博客在终端跑起来了,但是如果一旦终端关闭,我们的博客就无法访问了,为了让应用程序一直在后台运行,我们使用进程管理器 pm2 来管理它,详细的使用方式可以查看这里。使用下面命令安装 pm2
[root@bwh conf.d]# npm install -g pm2
pm2 启动 ghost
[root@bwh ghost]# NODE_ENV=production pm2 start index.js --name "ghost"
使用 pm2 命令重启/停止/启动 ghost
[root@host ghost]# pm2 restart ghost[root@host ghost]# pm2 stop ghost[root@host ghost]# pm2 start ghost
你的域名做好解析后,打开浏览器,输入你设置的IP或域名,就能外网访问你的博客了。
最后修改于 2019-03-31