分类: ghost

  • Ubuntu 配置 Nginx + Ghost

    Ubuntu 配置 Nginx + Ghost

    按照惯例,买好机器,登录进去。建议是境外服务器,可以省掉备案环节。但是不要干坏事哦。

    1. 更新系统

    apt-get update
    apt-get upgrade

    2. 创建 ssh-key

    ssh-keygen -t rsa -b 4096 -C "my-email@meathill.com"

    3. 添加 authorized_keys

    使用 ssh key 登录可以大大提升服务器的安全性。

    首先,将你的电脑上的公 key 添加到服务器 ~/.ssh/authorized_keys。接着编辑服务器上的 /etc/ssh/sshd_config,禁用密码登录。

    ChallengeResponseAuthentication no
    PasswordAuthentication no

    最后重启 ssh 服务:service ssh restart

    4. 安装 Node.js

    按照 NodeSource Node.js Binary Distributions 的指引,安装对应版本的 Node.js—— Ghost 要求 LTS,所以目前只能用 v12。

    curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
    sudo apt-get install -y nodejs

    5. 安装 MySQL/MariaDB

    Ghost 默认使用 SQLite 作为数据库,不过生产环境中 SQLite 性能不够,因此建议直接安装并使用成熟可靠的数据库软件。这里建议使用 MariaDB,开源免费。不同版本的 MariaDB 源可以在 MariaDB Repositories 找到,我一般使用清华的源:

    sudo apt-get install software-properties-common
    sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
    sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mirrors.ustc.edu.cn/mariadb/repo/10.5/ubuntu focal main'
    
    # 添加完仓库和 key 之后,就可以安装了
    sudo apt update
    sudo apt install mariadb-server

    MariaDB 的安装过程没有配置密码的环节,可以使用 重置 MariaDB root 密码 一文中介绍的方法先重置 root 密码,然后创建需要的用户和数据库。

    create user 'ghost'@'localhost' identified by 'ghost';
    create database `ghost`;
    grant all on `ghost`.* to 'ghost'@'localhost';

    6. 安装 ghost-cli

    ghost-cli 是 ghost 提供的命令行管理工具,可以大大减少我们管理 Ghost 实例的时间。

    # 安装
    npm i ghost-cli -g
    
    # 创建一个目录安装 ghost
    cd /var/www
    mkdir ghost
    cd ghost

    Ghost 要求我们不能用 root 用户维护实例,所以这时可能需要创建一个新用户,并赋予其 sudo 权限(方便操作目录权限之类的),我习惯命名为 ghost-admin:

    adduser ghost-admin
    usermod -aG sudo ghost-admin

    接着,切换到 ghost-admin,安装 ghost

    su ghost-admin
    cd /var/www
    # 改变权限
    sudo chown -R ghost-admin:ghost-admin ghost
    cd /var/www/ghost
    ghost install

    安装完成之后,执行 ghost ls,可以看到正在运行的 Ghost 实例,就是一切正常了:

    + sudo systemctl is-active ghost_fav-meathill-com
    ┌──────────────────┬────────────────┬─────────┬──────────────────────┬─────────────────────────┬──────┬─────────────────┐
    │ Name             │ Location       │ Version │ Status               │ URL                     │ Port │ Process Manager │
    ├──────────────────┼────────────────┼─────────┼──────────────────────┼─────────────────────────┼──────┼─────────────────┤
    │ my-ghost │ /var/www/ghost │ 3.31.5  │ running (production) │ http://my-ghost.com │ 2368 │ systemd         │
    └──────────────────┴────────────────┴─────────┴──────────────────────┴─────────────────────────┴──────┴─────────────────┘

    接下来,编辑 config.production.json,把数据库配置和域名配置都写进去,就基本可用了。

    7. 安装并配置 Nginx

    首先,配置源。目前 Ubuntu 20.04,代号 focal,命令如下:

    deb https://nginx.org/packages/ubuntu/ focal nginx
    deb-src https://nginx.org/packages/ubuntu/ focal nginx
    sudo apt update
    sudo apt install nginx

    增加配置文件,并进行反向代理,即把外来的访问反向代理给 Ghost 服务:

    
    cp /etc/nginx/site-available/default /etc/nginx/site-available/ghost.conf
    ln -s /etc/nginx/site-available/ghost.conf /etc/nginx/site-enabled/ghost.conf

    配置文件大体如下:

    server {
        listen 80;
        listen [::]:80;
    
        server_name my-ghost.com;
        root /var/www/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 服务或者重新加载配置即可:

    # 一般建议先检查一下配置有没有问题
    nginx -t
    service nginx restart

    总结

    这次主要更新了使用 ghost-cli 安装和配置的内容,服务器也改用 MariaDB,新的技术使得安装配置都更简单了。