Centos7.9如何搭建Ghost博客
温馨提示:这篇文章已超过385天没有更新,请注意相关的内容是否还可用!
Ghost是一个基于Node.js开发的免费开源博客平台,用于简化个人博客和在线出版物的在线发布过程。对于个人网站,实例规格为1vCPU 2GiB或2vCPU 4GiB就能满足基本需求。下面阿豪教大家如何在CentOS 7操作系统上部署Ghost博客
安装Web环境
因为Centos7系统默认是没有Nginx库的,所以我们需要运行以下命令添加Nginx软件库
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
1、安装Nginx
# yum -y install nginx
2、设置Nginx服务器自动启动。
# systemctl enable nginx.service
3、启动Nginx并查看Nginx服务状态
# systemctl start nginx.service
# systemctl status nginx.service
4、在浏览器中输入IP地址,可以看到默认的Nginx网页
安装Ghost需要的工具
更新系统。确保您的服务器系统处于最新状态
# yum -y update
安装EPEL
# yum install epel-release -y
安装Node.js和npm
# yum install nodejs npm --enablerepo=epel -y
安装node.js管理工具
# npm install -g n
安装稳定版本的node.js
本教程安装node.js的版本为12.16.3
# n 12.16.3
运行命令n,选择已安装的node.js 12.16.3版本。
编辑环境配置文件
# vi ~/.bash_profile
按 i 进入编辑模式,在文件末尾添加下列信息。
export N_PREFIX=/usr/local/bin/node export PATH=$N_PREFIX/bin:$PATH
安装Ghost
创建Ghost安装目录。
# mkdir -p /var/www/ghost
进入Ghost安装目录,下载最新版本的Ghost安装包。
PS:如果wget没有安装则运行 yum install wget -y安装
# cd /var/www/ghost
# wget https://ghost.org/zip/ghost-latest.zip
# mv ghost-latest.zip ghost.zip
解压Ghost安装包
# yum install unzip -y
# unzip ghost.zip
安装gcc和c++编译器。
# yum -y install gcc gcc-c++
使用npm安装Ghost。
# npm install -production
修改/var/www/ghost/core/shared/config/env目录下的config.development.json文件
# vi /var/www/ghost/core/shared/config/env/config.development.json
配置config.development.json文件中的URL为您的Ghost博客域名
按 i 进入编辑模式,在文件末尾添加下列信息。
按下esc退出编辑模式,并输入:wq保存并退出
运行npm start命令启动Ghost,检查是否安装成功。
启动成功示例如下,您可以按Ctrl+C组合键关闭Ghost。
配置Nginx作为Ghost的反向代理
进入Nginx配置目录。
# cd /etc/nginx/conf.d/
新建Ghost博客的Nginx配置文件
# vi /etc/nginx/conf.d/ghost.conf
按 i 进入编辑模式,在文件末尾添加下列信息。
将以下内容输入到ghost.conf中,把server_name改成Ghost实际的域名
upstream ghost { server localhost:2368; } server { listen 80; server_name localhost; access_log /var/log/nginx/ghost.access.log; error_log /var/log/nginx/ghost.error.log; proxy_buffers 16 64k; proxy_buffer_size 128k; location / { proxy_pass http://ghost; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_X_forwarded_for; proxy_set_header X-Forwarded-Proto https; } }
按下esc退出编辑模式,并输入:wq保存并退出
修改默认的配置文件default.conf为default.conf.bak,使Nginx只应用于ghost.conf
# cd /etc/nginx/conf.d/
# mv default.conf default.conf.bak
重启Nginx服务
# systemctl restart nginx.service
启动Ghost
# cd /var/www/ghost/
# npm start
访问Ghost博客。
在浏览器输入http://服务器IP或http://域名,访问Ghost。
如果需要对博客进行编辑修改,在浏览器输入http://服务器IP/ghost
Ghost如何后台运行
Ghost安装完成后正常启动是在当前终端启动的,如果关闭终端后就无法访问,可以运行下面命令让它在后台运行
# npm install pm2 -g
pm2启动:
# cd /var/www/ghost/
PS:AhaoYw 为您要运行的服务自定义名称
# pm2 start "/usr/local/bin/npm" --name "AhaoYw" -- start .
pm2一些操作
pm2 list
pm2 stop 自定义服务名
pm2 restart 自定义服务名
pm2 delete 自定义服务名