开源世界

centos部署安装ghost博客:一步步实践Ghost cms


ghost的配置环境搭建对于新手来说一点也不友好,根据官方文档的配置过程错误连篇,虽然都是基础错误,但对于新手来说有些地方还是莫名其妙,这些坑应该还是活埋了一些人,包括我,搞了一天才完成;
Google的搜索结果大部分都不能用,加上自己近期打算迁移到ghost,所以才有了这篇文章
剧透:ghost没有中文;没有分类功能

环境要求

  • centos 7 minimal x86_64
  • ghost只支持两个版本的nodejs (2019年6月10日)
  • <7.x 不支持
  • 8.x LTS 支持
  • 9.x 不支持
  • 10.x LTS 支持
  • >11.x 不支持
  • 内存≥1GB
  • 如果小于1GB,则需要添加swap分区

提示:<user>为普通用户,因为ghost不建议使用root账户执行,但是有些操作需要使用sudo,所以新建一个普通用户并加入sudo授权

创建用户

# 使用root新建用户<user>为用户名,-m为创建用户主目录
useradd <user> -m
passwd <user>
#输入<user>的密码,输入时不显示
visudo
#找到,在下面加入新行,参照上一行的格式
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
<user>  ALL=(ALL)       ALL
 

添加repo

添加nginx repo

sudo vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
 

添加mariadb repo

sudo vim /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
 

添加nodejs repo

curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash -
 

添加yarn repo

sudo curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
 

安装软件

sudo yum install gcc-c++ make nginx mariadb yarn nodejs -y
 

创建目录

sudo mkdir -p /var/www/html/ghost
# 设置权限
sudo chown -R <user>:<user> /var/www/html/ghost
sudo chmod -R 775 /var/www/html/ghost
 

安装ghost-cli

sudo npm install ghost-cli@latest -g
 

启动服务

#先启动nginx和mariadb,安装过程需要连接数据库
sudo systemctl enable nginx && \
sudo systemctl enable mariadb && \
sudo systemctl start nginx && \
sudo systemctl start mariadb
 

设置mariadb数据库

sudo mysql_secure_installation
#在此处设置mariadb root的密码,后面全部回车保持默认即可
 

添加ghost的数据库及所属用户

在开始正式安装前,需要先添加一下数据库及所属用户,不然会在步骤中提示错误,因为ghost建议使用普通用户执行,所以普通用户没有权限登陆mysql,即便是密码正确的情况下;ubuntu18也需要这一步

mysql -uroot -p<刚刚设置的mariadb root的密码>
create database <数据库名称>;
create user <数据库用户名> identified by '<数据库密码>';
grant all privileges on <数据库名称>.* to <数据库用户名>@'localhost' identified by '<数据库密码>';
flush privileges;
exit
 

开始安装ghost

cd /var/www/html/ghost
ghost install

# 如果vps在国内的话,有时候可能会遇到下载失败的问题,再次执行安装的时候会提示

A SystemError occurred.

Message: Current directory is not empty, Ghost cannot be installed here.
#只需要执行rm -rf *,删除全部文件,然后再次执行安装命令即可;如果还是提示目录非空的话,那就删除 .ghost-cli 文件

# 正常安装过程
✔ Checking system Node.js version
✔ Checking logged in user
✔ Checking current folder permissions
System checks failed with message: 'Linux version is not Ubuntu 16 or 18'
Some features of Ghost-CLI may not work without additional configuration.
For local installs we recommend using `ghost install local` instead.
? Continue anyway? Yes #提示系统不是ubuntu,是否继续?当然
System stack check skipped
ℹ Checking operating system compatibility [skipped]
✔ Checking for a MySQL installation
✔ Checking memory availability
✔ Checking for latest Ghost version
✔ Setting up install directory
✔ Downloading and installing Ghost v2.23.3
✔ Finishing install process
? Enter your blog URL: http://192.168.70.134 #我在本地测试使用的是IP,如果在vps的话,应该输入站点的域名
? Enter your MySQL hostname: localhost #数据库的机器名称,本机回车即可
? Enter your MySQL username: ghost #上面的SQL语句中创建的数据库用户名
? Enter your MySQL password: [hidden] #上面SQL语句中的数据库密码(非root密码)
? Enter your Ghost database name: ghost_prod #上面SQL语句中的数据库名称
✔ Configuring Ghost
✔ Setting up instance
+ sudo useradd --system --user-group ghost
? Sudo Password [hidden] #当前用户的密码
+ sudo chown -R ghost:ghost /var/www/html/ghost/content
✔ Setting up "ghost" system user
ℹ Setting up "ghost" mysql user [skipped]
Nginx is not installed. Skipping Nginx setup.
ℹ Setting up Nginx [skipped] #跳过了nginx自动配置
Nginx setup task was skipped, skipping SSL setup
ℹ Setting up SSL [skipped] #跳过了nginx ssl配置
? Do you wish to set up Systemd? Yes #是否添加systemd服务,当然
✔ Creating systemd service file at /var/www/html/ghost/system/files/ghost_192-168-70-134.service
+ sudo ln -sf /var/www/html/ghost/system/files/ghost_192-168-70-134.service /lib/systemd/system/ghost_192-168-70-134.service #centos系统记住这个服务名,下面会用到
+ sudo systemctl daemon-reload
✔ Setting up Systemd
? Do you want to start Ghost? No #最后一步选择的是No,因为跳过了nginx设置
 

添加nginx配置文件

由于nginx的conf.d目录中已有一个default.conf文件,如果这个机器只有一个ghost网站的话,那么将其重命名,让其失效,然后再次创建一个新的ghost.conf文件

sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak && \
sudo vim /etc/nginx/conf.d/ghost.conf
 
# 粘贴以下内容

server {
    listen 80;
    listen [::]:80;

    server_name 192.168.70.12; #你的域名
    root /var/www/html/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)

    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:2368;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}
 

重启nginx

sudo systemctl restart nginx
 

启动网站

在centOS上不能使用ghost start命令,会报错

+ sudo systemctl is-active ghost_192-168-70-134
A ProcessError occurred.

Message: Command failed: /bin/sh -c sudo -S -p '#node-sudo-passwd#'  systemctl is-active ghost_192-168-70-134

unknown

Exit code: 3
 

在centos7上面得使用

sudo systemctl start ghost_192-168-70-134
 

查看状态

ghost status
+ sudo systemctl is-active ghost_192-168-70-134
┌────────────────┬─────────────────────┬─────────┬──────────────────────┬───────────────────────┬──────┬─────────────────┐
│ Name           │ Location            │ Version │ Status               │ URL                   │ Port │ Process Manager │
├────────────────┼─────────────────────┼─────────┼──────────────────────┼───────────────────────┼──────┼─────────────────┤
│ 192-168-70-134 │ /var/www/html/ghost │ 2.23.3  │ running (production) │ http://192.168.70.134 │ 2368 │ systemd         │
└────────────────┴─────────────────────┴─────────┴──────────────────────┴───────────────────────┴──────┴─────────────────┘
 

到这,安装就完成了;但是还差一步

添加防火墙

sudo firewall-cmd --add-service=http --permanent && \
sudo firewall-cmd --add-service=https --permanent && \
sudo firewall-cmd --reload
 

设置网站管理员账户

使用域名访问站点后台创建管理员账户

domain.com #网站前台
domain.com/ghost #网站后台

原文链接https://www.cyberpioneer.net/archives/51.html

更多Ghost讨论,请参考社区  https://www.techsir.com/club/coder

希望看到您的想法,请您发表评论x