install mysql client

$ wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
$ sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
$ yum install mysql-community-client

run ghost in docker with RDS mysql

  • Step1: 设置mysql database
    百度云后台登录RDS,创建数据库dbname

  • Step2: open the port 3001

  • Step3: run ghost in docker,假设百度云服务器IP地址是180.75.100.100

$ docker pull ghost:alpine
$ mkdir -p /opt/ghost3/content
$ docker run -d \
    --rm  \
    --name ghost  \
    -e NODE_ENV=production \
    -e url=http://180.75.100.100:3001 \
    -e database__client=mysql \
    -e database__connection__host=xxxx.rds.bj.baidubce.com \
    -e database__connection__user=dbuser \
    -e database__connection__password=dbpass \
    -e database__connection__database=dbname \
    -p 3001:2368  \
    -v /opt/ghost3/content:/var/lib/ghost/content  \
    ghost:alpine
  • Step4: 设置域名huhao.ai指向并搭建nginx反向代理服务器
$ cat /etc/nginx/conf.d/huhao.conf
upstream ghost {                                                             
    server 127.0.0.1:3001;
}

server {
    listen 80;
    server_name huhao.ai;

    access_log  /var/log/nginx/blog.access.log;
    error_log   /var/log/nginx/blog.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_pass  http://ghost;
        proxy_redirect off;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-Proto $scheme;
    }
    
}
  • Step5: 设置域名指向并添加systemctl service脚本并重启ghost
$ cat /usr/lib/systemd/system/docker.ghost.service
[Unit]
Description=Ghost Application in Docker Container
Documentation=https://hub.docker.com/r/library/ghost/
After=network.target docker.service
Requires=docker.service

[Service]
Type=simple
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker stop ghost
ExecStartPre=-/usr/bin/docker rm ghost
ExecStart=/usr/bin/docker run \
        --name ghost  \
        -e NODE_ENV=production \
        -e url=http://huhao.ai \
        -e database__client=mysql \
        -e database__connection__host=xxx.xxxx.rds.bj.baidubce.com \
        -e database__connection__user=dbuser \
        -e database__connection__password=dbpass \
        -e database__connection__database=dbname \
        -p 3001:2368  \
        -v /opt/ghost3/content:/var/lib/ghost/content  \
        ghost:alpine

ExecStop=/usr/bin/docker kill ghost

[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable docker.ghost.service
$ sudo systemctl start docker.ghost.service

添加https支持

方案

服务器、域名在海外
服务器、域名在海外的话,域名可以不用备案,直接解析到海外服务器,直接使用cerbot获取https证书即可。比如我的服务器在vultr,域名是.ai后缀(国外注册)

服务器/域名在国内
服务器或者域名在国内,都需要进行备案。如果服务器在国内,又想使用国外的域名,比如我想使用huhao.ai访问一个百度云的服务器,一个可行的办法:
1.注册一个国内提供备案服务的域名(比如.com/.cn)并备案,比如huhao09.com
2.在AWS上开通CloudFront服务,将huhao.ai的域名源解析到huhao09.com 上

cerbot 一键支持https

$ git clone https://github.com/certbot/certbot
$ mv certbot /opt/certbot
$ cd /opt/certbot
$ ./certbot-auto --nginx 
$ sudo crontab -e
0 */12 * * * /opt/certbot/certbot-auto -q renew --nginx && systemctl reload nginx

AWS CloudFront 解析

  • 需要先将huhao.ai的域名解析到所在服务器上,使用cerbot获取https证书
  • AWS上添加huhao.ai的证书,重新设置huhao.ai的解析
    • Certificate body* 填写/etc/letsencrypt/live/huhao.ai/fullchain.pem的第一部分
    • Certificate private key* 填写 /etc/letsencrypt/live/huhao.ai/privkey.pem
    • Certificate chain:填写/etc/letsencrypt/live/huhao.ai/fullchain.pem的第二部分

AWS CloudFront链接

修改theme

本地下载theme文件,修改后重新build上传。

$ git clone https://github.com/TryGhost/Casper.git 
$ cd Casper
$ npm install
$ ....          #修改hbs文件、或者asset/screen.css样式
$ gscan .       #检查修改是否符合规范
$ gulp build    #构建修改后的主题
$ gulp zip      #打包构建,生成文件在dist文件夹

将生成的文件通过ghost后台的「主题」功能上传

添加Valine评论 & 访问量统计

Valine一款简洁的评论插件,不需要账号登录,直接就可以评论,对个人博客、以及希望降低评论门槛的网站来说,应该够用了。

  • 免费注册 leancloud 账号(存储评论、访问量数据)
  • 修改ghost主题的post.hbs文件
<section class="post-full-comments">
    <!-- 添加 Valine 评论 -->
    <h3>评论区</h3>
   <div id="vcomments"></div>
</section>
  • 在ghost的code injection中加载Valine.min.js 并初始化
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<script>
	new Valine({
		el: '#vcomments',
		appId: 'xxxx',
		appKey: 'xxxxx',
        notify: true, 	//有新评论进行通知
    	    verify: true, 	//评论需要验证码
    	    avatar: 'identicon', 
        meta: ['nick'], // mail, link, 
        recordIP: true,	// 记录ip地址
        visitor: true,	// 阅读量统计
    	placeholder: '欢迎评论留言!' 
	})
</script>

Nginx 优化

$ mkdir -p /var/nginx/ghostcache
$ chown nginx:nginx -R /var/nginx
$ cat /etc/nginx/nginx.conf
...
http {
    ...
    proxy_cache_path /var/nginx/ghostcache levels=1:2 keys_zone=ghostcache:60m max_size=300m inactive=24h;
    proxy_cache_key "$scheme$request_method$host$request_uri";
    proxy_cache_methods GET HEAD;
    ...
}
...
$ cat /etc/nginx/conf.d/huhao.conf
upstream ghost {                                                               
    server 127.0.0.1:3001;
}

server {
    server_name huhao.ai;

    access_log  /var/log/nginx/blog.access.log;
    error_log   /var/log/nginx/blog.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_cache ghostcache;
        proxy_cache_valid 60m;
        proxy_cache_valid 404 1m;
        proxy_cache_bypass $http_cache_control; 
        proxy_ignore_headers Set-Cookie;
        proxy_hide_header Set-Cookie;
        proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
        proxy_ignore_headers Cache-Control;
        add_header X-Cache-Status $upstream_cache_status;
        
        proxy_pass  http://ghost;
        proxy_redirect off;
        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-Proto $scheme;
    }
    
    
    location ^~ /assets/ {
        root /opt/ghost3/content/themes/customized_casper;
    }

    location ^~ /content/images/ {
        root /opt/ghost3;
    }
    
    location ^~ /ghost/ {
        proxy_pass  http://ghost;
        proxy_set_header Host $http_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 $scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/huhao.ai/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/huhao.ai/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = huhao.ai) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name huhao.ai;
    return 404; # managed by Certbot
}

后台访问

访问 /ghost

一些问题

theme主题上传失败:The image you uploaded was larger than the maximum file size your server allows

在 /etc/nginx/nginx.conf 的 http 部分增加配置,并重启nginx

client_max_body_size 5m;
$ systemctl restart nginx

REFERENCES command denied to user 'ghost'@'192.168.16.2' for table 'posts'

[2018-04-07 02:56:33] ERROR

NAME: MigrationScriptError
CODE: ER_TABLEACCESS_DENIED_ERROR
MESSAGE: alter table posts_authors add constraint posts_authors_post_id_foreign foreign key (post_id) references posts (id) - ER
_TABLEACCESS_DENIED_ERROR: REFERENCES command denied to user 'ghost'@'192.168.16.2' for table 'posts'

level:normal

Error occurred while executing the following migration: 1-create-tables.js
MigrationScriptError: alter table posts_authors add constraint posts_authors_post_id_foreign foreign key (post_id) references posts (id) - ER_TABLEACCESS_DENIED_ERROR: REFERENCES command denied to user 'ghost'@'192.168.16.2' for table 'posts'

解决方法:
grant REFERENCES on gblog.* TO 'ghost'@'%' IDENTIFIED BY 'Ju6KxwsH_n844gBK';
flush privileges;

ghosthunter无法更改result_template

问题:在ghosthunter调用过程中,传入result_template参数无效
原因:由于ghost更新,result_template中的变量无法正确传递到ghosthunter中
解决方法:修改ghosthunter/dist/jquery.ghosthunter.js中的result_template默认值

第三方的搜索服务 algolia

https://github.com/mlbrgl/ghost-algolia
https://mrvautin.com/how-to-add-search-functionality-to-your-ghost-blog/

References