美文网首页
CentOS搭建Ghost博客系统

CentOS搭建Ghost博客系统

作者: 颍水书生 | 来源:发表于2017-12-28 18:01 被阅读55次
    关于ghost

    Ghost 是一套基于 Node.js 构建的开源博客平台(Open source blogging platform),具有易用的书写界面和体验,博客内容默认采用体验非常好的 Markdown 语法书写,目标是取代臃肿的 Wordpress。另外,ghost还有一个非常大的亮点,就是可以用桌面版APP来写博客,大赞!

    搭建前准备清单
    • 服务器:腾讯云主机-香港服务器

    选择腾讯云的原因很简单,价格灰常有诱惑力,而且经过半年的使用,还算稳定

    因为只是写博客文章,没有其他服务,所以用香港服务器,不用备案。这个根据个人情况而定

    • 系统:CentOS 6.5_x64
    • Node v0.10.40(NodeJS号称版本帝,所以选择一个新一点的稳定版)
    • Nginx 1.80
    • Mysql 5.1.73
    • Ghost v0.7.4 full (zh)(中文汉化、支持七牛、又拍云、阿里云OSS存储)
    步骤

    首先使用ssh工具连接到你的云服务器,关于ssh的使用就不再介绍了,网络上有许多教程。

    • 安装Node
    $ wget http://nodejs.org/dist/v0.10.40/node-   v0.10.40.tar.gz  
    $ tar zxvf node-v0.10.40.tar.gz  
    $ cd node-v0.10.40  
    $ ./configure 
    $ make && make install
    

    命令执行完毕之后,检测一下环境是否配置成功。

    $ node -v
    $ v0.10.40
    
    • 安装配置Nginx
      Nginx是一款轻量级的Web 服务器/反向代理服务器。 这里我们使用nginx做端口转发

    安装编译工具及库文件

    $ yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    安装pcre
    $ wget -c http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    $ tar zxvf pcre-8.35.tar.gz
    $ cd pcre-8.35
    $ ./configure
    $ make && make install
    

    安装nginx

    $ wget -c http://nginx.org/download/nginx-1.8.0.tar.gz
    $ tar zxvf nginx-1.8.0.tar.gz
    $ cd nginx-1.8.0
    $ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
    $ make && make install
    

    创建配置文件ghost.conf,并写入以下内容:

    • http部分
    server { 
        listen    80;
        server_name    www.youdomain.com;
        # 百度SEO --Start--
        if ($http_user_agent !~* baidu.com){
                # 非百度爬虫UA,全部301跳转走 https 协议
                return 301 https://$server_name$request_uri;
      }
        # 百度SEO --End--
        
        # return   301 https://$server_name$request_uri;
        
        location / {
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   Host      $http_host;
                proxy_pass       http://127.0.0.1:2368;
                client_max_body_size 35m;
        }
    
    }
    
    • https部分
    server {  
    
      listen 443 ssl;  
    
      server_name www.youdomain.com;  
    
      ssl on;  
    
      ssl_certificate you_path/sslkey/server.crt;  
    
      ssl_certificate_key you_path/sslkey/server.key;  
    
      location / {  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_set_header   Host      $http_host;  
        proxy_set_header X-Forwarded-Proto $scheme;  
        proxy_pass         http://127.0.0.1:2368;  
    
      }
    }
    

    重启nginx服务器

    PS:全站使用https加密,个人可以使用免费证书来配置(Let's Encrypt,腾讯云、阿里云都提供免费证书服务)

    由于百度搜索引擎目前不主动抓取https网页,所以得针对百度爬虫,另外做配置,也就是将百度爬虫对http放行

    安装Mysql服务器

    $ sudo yum install mysql-server

    安装ghost

    首先下载Ghost:

    $ cd /var/www

    $ wget http://dl.ghostchina.com/Ghost-0.7.4-zh-full.zip

    $ unzip Ghost-0.7.4-zh-full.zip -d ghost

    $ cd ghost

    接着修改默认配置:

    cp config.example.js config.js

    vi config.js

    Ghost有产品模式、开发模式和测试模式等多种运行模式,这里我们需要在配置文件中找到production模式:

    生产模式

    production: {

    url: 'https://youdomain.com', # 修改为你的域名或者IP,注意加上https://

    mail: {},

    database: {

    client: 'mysql'
    connection: {
        host     : '127.0.0.1',
        user     : 'ghost', # 数据库连接的用户
        password : '123456', # 先前创建的密码
        database : 'ghost', # 先前创建的数据库
        charset  : 'utf8'
    },
    

    server: {

        host: '127.0.0.1',
        port: '2368' # 若修改该端口记得在nginx中做相应改变
    }
    

    }

    保存退出,接下来就到了见证奇迹的时刻啦,输入指令:

    npm start --production

    启动浏览器,输入之前配置的域名或者IP,我们就可以看到建立好的Ghost博客啦。 (Ctrl+C 中断掉开发者模式)

    让Ghost保持运行

    前面提到的启动 Ghost 使用 npm start --production 命令。这是一个在开发模式下启动和测试的不错的选择,但是通过这种命令行启动的方式有个缺点,即当你关闭终端窗口或者从 SSH 断开连接时,Ghost 就停止了。为了防止 Ghost 停止工作,我们得解决这个问题。

    以下有几种解决方案:

    这里我们使用PM2让Ghost保持运行:

    $ cd /var/www/ghost  
    
    $ npm install pm2 -g # 安装PM2  
    
    $ NODE_ENV=production pm2 start index.js --name "ghost"  
    
    $ pm2 startup centos  
    
    $ pm2 save
    

    PS:

    因为GFW的强大,在上一步直接使用npm安装依赖的时候可能出现无法安装的情况,这时候可以使用以下代码:

    npm install -g cnpm --registry=https://registry.npm.taobao.org  
    
    npm install pm2 -g  
    
    NODE_ENV=production pm2 start index.js --name "ghost"  
    
    pm2 startup centos  
    
    pm2 save
    

    这样一来,我们的Ghost博客就可以保持运行啦,你可以使用以下指令来控制Ghost博客:

    pm2 start/stop/restart ghost
    

    初始化Ghost

    现在所有准备工作都做好了,打开你的浏览器,在浏览器中输入https://youdomain.com/ghost/,跟着提示完成初始化之后,你就可以开始你的Ghost之旅了~

    相关文章

      网友评论

          本文标题:CentOS搭建Ghost博客系统

          本文链接:https://www.haomeiwen.com/subject/zxbvgxtx.html