Debian 7 x64 搭建 Ghost 博客

安装基本

apt-get update && apt-get install -y unzip nano curl libpcre+* grep zlib1g* build-essential

安装 Nginx

1.添加源,安装最新版 nginx

curl -L http://nginx.org/keys/nginx_signing.key -o nginx_signing.key && apt-key add nginx_signing.key
nano /etc/apt/sources.list

# Debian 7
deb http://nginx.org/packages/mainline/debian/ wheezy nginx
deb-src http://nginx.org/packages/mainline/debian/ wheezy nginx

# Debian 8
deb http://nginx.org/packages/mainline/debian/ jessie nginx
deb-src http://nginx.org/packages/mainline/debian/ jessie nginx

2.安装

apt-get update && apt-get install nginx -y
nginx -v

3.nginx 配置
备份原 nginx 文件

mkdir /data/ghost -p
cd /etc/nginx/conf.d
cp default.conf ghost.conf

编辑nano ghost.conf内容:

server {
    listen 80;
    server_name 域名.com;
    root /data/ghost;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $http_host;
      proxy_pass http://127.0.0.1:2368;
    }
}

如使用 SSL

1.安装 SSL 基本环境

cd
apt-get install git -y
git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
# 安装前需停止 nginx 并且将域名解析到该 IP
service nginx stop
# 制作证书(和续期)
./letsencrypt-auto certonly --standalone --email user@email.com -d 域名.com
# 生成的证书路径
/etc/letsencrypt/live/域名.com/fullchain.pem(fullchain.cert)& privkey.pem(privkey.key)

2.复制证书到 ssl 路径

mkdir /etc/nginx/ssl
cp /etc/letsencrypt/live/域名.com/fullchain.pem /etc/nginx/ssl/fullchain.pem
cp /etc/letsencrypt/live/域名.com/privkey.pem /etc/nginx/ssl/privkey.pem

3.配置内容:

cd /etc/nginx
nano ghost.conf

##################################
listen 443 ssl http2;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_buffer_size 1400;
add_header Strict-Transport-Security max-age=15768000;
ssl_stapling on;
ssl_stapling_verify on;

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $http_host;
  proxy_pass http://127.0.0.1:2368;
  # 支持 https
  add_header Cache-Control "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0";
  proxy_set_header X-Forwarded-Proto https;
}

4.重启 nginx

/etc/init.d/nginx 

安装 Mysql

1.安装 mysql

apt-get install mysql-server mysql-client -y

2.修改配置文件nano /etc/mysql/my.cnf
第一种:
[mysqld] 直接插入

collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

重启 mysql

service mysql restart

3.新建数据库

# 用 root 用户登录 mysql
mysql -u root -p

# 创建 ghost 数据库
create database ghost

# 创建 ghost 用户和密码
create user 'ghost'@'localhost' identified by 'Password'

# 赋予 ghost 对 'ghost'@'localhost' 访问权限
grant all privileges on ghost.* to 'ghost'@'localhost'

#重载权限表
flush privileges

# 退出
Ctrl + C

4.数据库文件路径

/var/lib/mysql/

安装 Node

curl -sL https://deb.nodesource.com/setup_4.x | bash - && apt-get install -y nodejs

安装 Ghost

1.创建目录,安装 Ghost

cd /data/ghost
wget https://ghost.org/zip/ghost-latest.zip
unzip ghost-*.zip
cp config.example.js config.js

2.修改配置nano config.js基本内容:

config = {
    // ### Production
    // When running Ghost in the wild, use the production environment.
    // Configure your URL and mail settings here
    production: {
        url: 'http://域名.com',
        mail: {},
    // 不使用 sqlite3
    //      database: {
    //      client: 'sqlite3',
    //      connection: {
    //          filename: path.join(__dirname, '/content/data/ghost.db')
    //      },
    //      debug: false
    //  },

        // 配置 MySQL 数据库
        database: {
             client: 'mysql',
             connection: {
              host     : 'localhost',
              user     : '数据库用户',
              password : '数据库密码',
              database : 'ghost',
              charset  : 'utf8'
             },
             debug: false
        },

        server: {
            host: '127.0.0.1',
            port: '2368'
        }
    },

3.安装运行

npm install --production
npm start

4.访问
解析域名主机,访问 http://域名.com/ghost

5.守护 Ghost 进程

方式一(PM2 可开机自启动,程序崩溃自动重新打开,可监控运行)

npm install pm2 -g
NODE_ENV=production pm2 start index.js --name "ghost"
pm2 startup 
pm2 save

#########################
命令:
pm2 kill ghost
pm2  ghost

方式二(forever 不能开机自启动)

npm install forever -g
NODE_ENV=production forever start index.js

#########################
命令:
forever restart index.js
forever stop index.js
forever list

方式三(supervisor 可开机自启动,程序崩溃自动重新打开)

apt-get install supervisor
service supervisor start
cd /etc/supervisor/conf.d
nano ghost.conf
# 填入
[program:ghost]
command = node {ghost_path}/index.js
directory = {ghost_path}/ghost
user = root
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/ghost.log
stderr_logfile = /var/log/supervisor/ghost_err.log
environment = NODE_ENV="production"

#########################
命令:
supervisorctl reload ghost
supervisorctl start ghost

问题集
1.新站部署完了,最后访问出现502 Bad Gateway
有可能是数据库连接问题,也可能是 nginx 未运行。

2.Ghost 的伪静态,就算自定义 URL . 也会被自动转换成而非是 -

3.npm start出现系列错误

npm ERR! /data/ghost/域名.com/npm-debug.log

ghost/node_modules/里面缺少依赖模块,如果主机性能或网络没问题npm install --production执行的时会下载安装。

4.一切东西都弄好后,如果用一键 lnmp 或者 oneinstack 安装环境会出现前台样式的错误。
删除这些类似内容即可

location ~ .*\.(js|css)?$ {
 expires 7d;
 access_log off;
}

5.邮箱配置

mail: {
    transport: 'SMTP',
    from: '发件人地址',
    options: {
             host: 'smtp 服务器',
             secureConnection: true,
             port: 465, // true → 465 & false → 25
             auth: {
                    user: '邮箱地址',
                    pass: '邮箱密码'
                    }
            }
},

一键安装
https://github.com/bayunjiang/InstallGhost
https://github.com/hwdsl2/setup-ghost-blog