在centos下安装nodejs, npm和Ghost博客


  • administrators

    之前研究过一段时间的Node.js,也找到过几个不错的站,其中Ghost博客就是一个用Node.js写的开源博客,好久没弄了,接下来尝试在新环境配置一下。

    默认是装过LNMP环境的,如果没有,先装nginx

    sudo yum install nginx -y
    sudo service nginx start 开启nginx
    sudo chkconfig nginx on 设置nginx为开机启动
    

    首先安装Node.js和npm

    [root@li1473-187 ~]# node -v
    -bash: node: command not found
    [root@li1473-187 ~]# curl -sL https://rpm.nodesource.com/setup | bash -
    [root@li1473-187 ~]# yum install -y nodejs
    [root@li1473-187 ~]# yum install -y gcc-c++ make
    [root@li1473-187 ~]# node -v
    v0.10.48
    [root@li1473-187 ~]# curl -L https://www.npmjs.com/install.sh | sh
    [root@li1473-187 ~]# npm -v
    4.2.0
    [root@li1473-187 ~]# sudo ln -s /usr/local/bin/node /usr/bin/node
    ln: creating symbolic link `/usr/bin/node': File exists
    

    接下来下载Ghost博客

    [root@li1473-187 ~]# curl -L https://ghost.org/zip/ghost-latest.zip -o ghost.zip
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 4091k  100 4091k    0     0  82370      0  0:00:50  0:00:50 --:--:--  237k
    [root@li1473-187 ~]# unzip -uo ghost.zip -d ghost
    [root@li1473-187 ~]# mv ghost /home/wwwroot/
    [root@li1473-187 ~]# cd /home/wwwroot/
    [root@li1473-187 wwwroot]# ls
    default  ghost  testweb  wordpress-4.7.2-zh_CN.tar.gz[root@li1473-187 wwwroot]# cd ghost/
    [root@li1473-187 ghost]# npm install –production
    npm WARN npm npm does not support Node.js v0.10.48
    npm WARN npm You should probably upgrade to a newer version of node as we
    npm WARN npm can't make any promises that npm will work with this version.
    npm WARN npm You can find the latest version at https://nodejs.org/
    npm ERR! arg Argument starts with non-ascii dash, this is probably invalid: [ '–production' ]
    npm ERR! addLocal Could not install /home/wwwroot/ghost/–production 
    
    

    3.安装到这里后发现版本不支持,卸载掉再研究吧。

    [root@li1473-187 ~]# yum remove nodejs npm -y
    

    重新安装Node.js,根据Ghost官方的建议,这次安装v0.10.40版本

    [root@li1473-187 ~]# wget http://nodejs.org/dist/v0.10.40/node-v0.10.40.tar.gz
    [root@li1473-187 ~]# tar zxvf node-v0.10.40.tar.gz
    [root@li1473-187 ~]# cd node-v0.10.40
    [root@li1473-187 ~]# ./configure
    [root@li1473-187 ~]# make && make install
    [root@li1473-187 ~]# node -v
    v0.10.40
    [root@li1473-187 ~]# npm -v
    1.4.28
    

    下载Ghost

    [root@li1473-187 ~]# wget http://dl.ghostchina.com/Ghost-0.7.4-zh-full.zip
    [root@li1473-187 ~]# unzip Ghost-0.7.4-zh-full.zip -d ghost
    [root@li1473-187 ~]# mv ghost /home/wwwroot/
    [root@li1473-187 ghost]# cd  /home/wwwroot/
    [root@li1473-187 wwwroot]# chmod -R 777 ghost
    chmod: changing permissions of 'ghost/.user.ini': Operation not permitted
    [root@li1473-187 wwwroot]# cd ghost
    [root@li1473-187 ghost]# cp config.example.js config.js
    [root@li1473-187 ghost]# ls
    config.example.js  content  Gruntfile.js  LICENSE       npm-shrinkwrap.json  PRIVACY-en.md  README-en.md  SECURITY-en.md
    config.js          core     index.js      node_modules  package.json         PRIVACY.md     README.md
    [root@li1473-187 ghost]# vim config.js
     production: {
     url: 'http://ghost.kokwind.net', 如果选择使用sqlite则在config.js中只需要修改url地址
     mail: {},
     database: {
     client: 'sqlite3',
     connection: {
     filename: path.join(__dirname, '/content/data/ghost.db')
     },
     debug: false
     },
    

    Ghost-0.7.4-zh-full这个版本默认集成 sqlite3 原生库,因sqlite读写数据量太大时会影响速度,我们也可以改用mysql

    首先 使用phpmyadmin,添加用户和数据库

    修改config.js,变成mysql

    [root@li1473-187 ~]# vim config.js
    production: {  
        url: 'http://ghost.kokwind.net', //这里是你自己VPS主机的域名
        mail: {},
        database: {
            client: 'mysql'
            connection: {
                host     : '127.0.0.1',
                user     : 'ghost', //mysql用户名
                password : 'xxxxxxxx', //密码
                database : 'ghost', //之前创建的ghost数据库名称
                charset  : 'utf8'
            },
        server: {
                host: '127.0.0.1',
                port: '2368'//若修改该端口记得在nginx中做相应改变
            }
        }
    

    配置nginx,PS:之前忘了写,在搬瓦工上配置才想起来,贴过来补上。

    [root@localhost ~]# cd /usr/local/nginx/conf/vhost
    [root@localhost vhost]# cp www.kokwind.net.conf blog.kokwind.net.conf
    [root@localhost vhost]# vim blog.kokwind.net.conf
    server
        {
            listen 80;
            #listen [::]:80;
            server_name xxx.kokwind.net; <-- 网址
            index index.html index.htm index.php default.html default.htm default.php;
            root  /home/wwwroot/ghost; <-- ghost路径
    
            include none.conf;
            #error_page   404   /404.html;
            include enable-php.conf;
    
            location / {
                proxy_set_header   X-Real-IP $remote_addr;
                proxy_set_header   Host      $http_host;
                proxy_pass         http://127.0.0.1:2368;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    
            access_log  /home/wwwlogs/ghost.log;
        }
    [root@localhost vhost]# /etc/init.d/nginx  reload
    Reload service nginx...  done
    

    现在可以启动ghost博客了

    [root@li1473-187 ~]# cd /home/wwwroot
    [root@li1473-187 wwwroot]# cd ghost
    [root@li1473-187 ghost]# ls
    config.example.js content Gruntfile.js LICENSE npm-debug.log package.json PRIVACY.md README.md
    config.js core index.js node_modules npm-shrinkwrap.json PRIVACY-en.md README-en.md SECURITY-en.md
    [root@li1473-187 ghost]# npm start
    
    > ghost@0.7.4 start /home/wwwroot/ghost
    > node index
    
    Migrations: Up to date at version 004
    Ghost is running in development...
    Listening on 127.0.0.1:2368
    Url configured as: http://localhost:2368
    Ctrl+C to shut down
    

    访问网址可以看到

    如果是通过 npm start 启动 Ghost 的话,只要你关闭了远程连接,Ghost 也就停了,所以需要安装 forever

    [root@li1473-187 bin]# which node
    /usr/local/bin/node
    [root@li1473-187 bin]# ln -s /usr/local/bin/node /usr/bin/node
    [root@li1473-187 bin]# npm install forever -g
    

    注意:这条指令将 forever 安装到全局环境。安装的时候系统会提示一些 WARN,这是因为 forever 依赖的 Node.js 版本过低,没关系,不用理会。

    [root@li1473-187 ~]# service nginx restart
    Stoping nginx...  done
    Starting nginx...  done
    [root@li1473-187 ~]# cd /home/wwwroot/ghost
    [root@li1473-187 ghost]# NODE_ENV=production forever start index.js
    warn:    --minUptime not set. Defaulting to: 1000ms
    warn:    --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
    info:    Forever processing file: index.js
    [root@li1473-187 ghost]#
    

    不顺利的开始,顺利的结局。over!

    追记:
    在搬瓦工上同样配置启动时,出现了下面的错误

    [root@localhost ghost]# npm start
    
    > ghost@0.7.4 start /home/wwwroot/ghost
    > node index
    
    
    ERROR: Cannot find module '/home/wwwroot/ghost/node_modules/sqlite3/lib/binding/node-v11-linux-ia32/node_sqlite3.node'
    
     Error: Cannot find module '/home/wwwroot/ghost/node_modules/sqlite3/lib/binding/node-v11-linux-ia32/node_sqlite3.node'
        at Function.Module._resolveFilename (module.js:338:15)
        at Function.Module._load (module.js:280:25)
    

    重装了一下sqlite3就好了

    [root@localhost ghost]# npm install sqlite3 --save
    |
    > sqlite3@3.1.1 install /home/wwwroot/ghost/node_modules/sqlite3
    > node-pre-gyp install --fallback-to-build
    

Log in to reply
 

Looks like your connection to MainFrame was lost, please wait while we try to reconnect.