如何在 FreeBSD 10.2 上安装使用 Nginx 的 Ghost
Node.js 是用于开发服务器端应用程序的开源的运行时环境。Node.js 应用使用 JavaScript 编写,能在任何有 Node.js 运行时的服务器上运行。它跨平台支持 Linux、Windows、OSX、IBM AIX,也包括 FreeBSD。Node.js 是 Ryan Dahl 以及在 Joyent 工作的其他开发者于 2009 年创建的。它的设计目标就是构建可扩展的网络应用程序。
Ghost 是使用 Node.js 编写的博客平台。它不仅开源,而且有很漂亮的界面设计、对用户友好并且免费。它允许你快速地在网络上发布内容,或者创建你的混合网站。
在这篇指南中我们会在 FreeBSD 上安装使用 Nginx 作为 web 服务器的 Ghost。我们会在 FreeBSD 10.2 上安装 Node.js、Npm、nginx 和 sqlite3。
第一步 - 安装 Node.js npm 和 Sqlite3
如果你想在你的服务器上运行 ghost,你必须安装 node.js。在这一部分,我们会从 freebsd 移植软件库中安装 node.js,请进入库目录 "/usr/ports/www/node" 并通过运行命令 "make" 安装。
<span class="kwd">cd</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">ports</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">node</span>
<span class="kwd">make</span><span class="pln"> install clean</span>
如果你已经安装了 node.js,那就进入到 npm 目录并安装它。npm 是用于安装、发布和管理 node 程序的软件包管理器。
<span class="kwd">cd</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">ports</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">npm</span><span class="pun">/</span>
<span class="kwd">make</span><span class="pln"> install clean</span>
下一步,安装 sqlite3。默认情况下 ghost 使用 sqlite3 作为数据库系统,但它也支持 mysql/mariadb 和 postgresql。我们会使用 sqlite3 作为默认数据库。
<span class="kwd">cd</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">ports</span><span class="pun">/</span><span class="pln">databases</span><span class="pun">/</span><span class="pln">sqlite3</span><span class="pun">/</span>
<span class="kwd">make</span><span class="pln"> install clean</span>
如果安装完了所有软件,还有检查 node.js 和 npm 的版本:
<span class="pln">node </span><span class="pun">--</span><span class="pln">version</span>
<span class="pln">v0</span><span class="pun">.</span><span class="lit">12.6</span>
<span class="pln">npm </span><span class="pun">--</span><span class="pln">version</span>
<span class="lit">2.11</span><span class="pun">.</span><span class="lit">3</span>
<span class="pln">sqlite3 </span><span class="pun">--</span><span class="pln">version</span>
<span class="lit">3.8</span><span class="pun">.</span><span class="lit">10.2</span>
node 和 npm 版本
第二步 - 添加 Ghost 用户
我们会以普通用户 "ghost" 身份安装和运行 ghost。用 "adduser" 命令添加新用户:
<span class="pln">adduser ghost</span>
<span class="pln">FILL </span><span class="typ">With</span><span class="typ">Your</span><span class="pln"> INFO</span>
添加用户 Ghost
第三步 - 安装 Ghost
我们会把 ghost 安装到 "/var/www/" 目录,首先新建目录然后进入到安装目录:
<span class="kwd">mkdir</span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span>
<span class="kwd">cd</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span>
用 wget 命令下载最新版本的 ghost:
<span class="kwd">wget</span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">check</span><span class="pun">-</span><span class="pln">certificate https</span><span class="pun">:</span><span class="com">//ghost.org/zip/ghost-latest.zip</span>
把它解压到 "ghost" 目录:
<span class="pln">unzip </span><span class="pun">-</span><span class="pln">d ghost ghost</span><span class="pun">-</span><span class="pln">latest</span><span class="pun">.</span><span class="pln">zip</span>
下一步,更改属主为 "ghost",我们会以这个用户安装和运行它。
<span class="kwd">chown</span><span class="pun">-</span><span class="pln">R ghost</span><span class="pun">:</span><span class="pln">ghost ghost</span><span class="pun">/</span>
都做完了的话,通过输入以下命令切换到 "ghost" 用户:
<span class="kwd">su</span><span class="pun">-</span><span class="pln"> ghost</span>
然后进入到安装目录"/var/www/ghost/":
<span class="kwd">cd</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">ghost</span><span class="pun">/</span>
在安装 ghost 之前,我们需要为 node.js 安装 sqlite3 模块,用 npm 命令安装:
<span class="pln">setenv CXX c</span><span class="pun">++</span><span class="pun">;</span><span class="pln"> npm install sqlite3 </span><span class="pun">--</span><span class="pln">sqlite</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span>
注意: 以 “ghost” 用户运行,而不是 root 用户。
现在,我们准备好安装 ghost 了,用 npm 命令安装:
<span class="pln">npm install </span><span class="pun">--</span><span class="pln">production</span>
下一步,复制配置文件 "config.example.js" 为 "config.js",用 nano 编辑器编辑:
<span class="kwd">cp</span><span class="pln"> config</span><span class="pun">.</span><span class="pln">example</span><span class="pun">.</span><span class="pln">js config</span><span class="pun">.</span><span class="pln">js</span>
<span class="kwd">nano</span><span class="pun">-</span><span class="pln">c config</span><span class="pun">.</span><span class="pln">js</span>
更改 server 模块的第 25 行:
<span class="pln">host</span><span class="pun">:</span><span class="str">'0.0.0.0'</span><span class="pun">,</span>
保存并退出。
现在用下面的命令运行 ghost:
<span class="pln">npm start </span><span class="pun">--</span><span class="pln">production</span>
通过访问服务器 ip 和 2368 号端口验证一下。
Ghost 安装完成
以 “ghost” 用户在 "/var/www/ghost" 目录安装了 ghost。
第四步 - 作为 FreeBSD 服务运行 Ghost
要在 freebsd 上以服务形式运行应用,你需要在 rc.d 目录添加脚本。我们会在 "/usr/local/etc/rc.d/" 目录为 ghost 创建新的服务脚本。
在创建服务脚本之前,为了以服务形式运行 ghost,我们需要安装一个 node.js 模块,用 npm 命令以 sudo/root 权限安装 forever 模块:
<span class="pln">npm install forever </span><span class="pun">-</span><span class="pln">g</span>
现在进入到 rc.d 目录并创建名为 ghost 的新文件:
<span class="kwd">cd</span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span>
<span class="kwd">nano</span><span class="pun">-</span><span class="pln">c ghost</span>
粘贴下面的服务脚本:
<span class="com">#</span><span class="pun">!</span><span class="str">/bin/</span><span class="pln">sh</span>
<span class="com">#</span><span class="pln"> PROVIDE</span><span class="pun">:</span><span class="pln"> ghost</span>
<span class="com">#</span><span class="pln"> KEYWORD</span><span class="pun">:</span><span class="kwd">shutdown</span>
<span class="pln">PATH</span><span class="pun">=</span><span class="str">"/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"</span>
<span class="pun">.</span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">rc</span><span class="pun">.</span><span class="pln">subr</span>
<span class="pln">name</span><span class="pun">=</span><span class="str">"ghost"</span>
<span class="pln">rcvar</span><span class="pun">=</span><span class="str">"ghost_enable"</span>
<span class="pln">extra_commands</span><span class="pun">=</span><span class="str">"status"</span>
<span class="pln">load_rc_config ghost</span>
<span class="pun">:</span><span class="pln"> $</span><span class="pun">{</span><span class="pln">ghost_enable</span><span class="pun">:=</span><span class="str">"NO"</span><span class="pun">}</span>
<span class="pln">status_cmd</span><span class="pun">=</span><span class="str">"ghost_status"</span>
<span class="pln">start_cmd</span><span class="pun">=</span><span class="str">"ghost_start"</span>
<span class="pln">stop_cmd</span><span class="pun">=</span><span class="str">"ghost_stop"</span>
<span class="pln">restart_cmd</span><span class="pun">=</span><span class="str">"ghost_restart"</span>
<span class="pln">ghost</span><span class="pun">=</span><span class="str">"/var/www/ghost"</span>
<span class="pln">log</span><span class="pun">=</span><span class="str">"/var/log/ghost/ghost.log"</span>
<span class="pln">ghost_start</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u ghost sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">"cd $ghost && NODE_ENV=production forever start -al $log index.js"</span>
<span class="pun">}</span>
<span class="pln">ghost_stop</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u ghost sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">"cd $ghost && NODE_ENV=production forever stop index.js"</span>
<span class="pun">}</span>
<span class="pln">ghost_status</span><span class="pun">()</span><span class="pun">{</span>
<span class="kwd">sudo</span><span class="pun">-</span><span class="pln">u ghost sh </span><span class="pun">-</span><span class="pln">c </span><span class="str">"NODE_ENV=production forever list"</span>
<span class="pun">}</span>
<span class="pln">ghost_restart</span><span class="pun">()</span><span class="pun">{</span>
<span class="pln">ghost_stop</span><span class="pun">;</span>
<span class="pln">ghost_start</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pln">run_rc_command </span><span class="str">"$1"</span>
保存并退出。
下一步,给 ghost 服务脚本添加可执行权限:
<span class="kwd">chmod</span><span class="pun">+</span><span class="pln">x ghost</span>
为 ghost 日志创建新的目录和文件,并把属主修改为 ghost 用户:
<span class="kwd">mkdir</span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">ghost</span><span class="pun">/</span>
<span class="kwd">touch</span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">ghost</span><span class="pun">/</span><span class="pln">ghost</span><span class="pun">.</span><span class="pln">log</span>
<span class="kwd">chown</span><span class="pun">-</span><span class="pln">R </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">ghost</span><span class="pun">/</span>
最后,如果你想运行 ghost 服务,你需要用 sysrc 命令添加 ghost 服务到开机启动应用程序:
<span class="pln">sysrc ghost_enable</span><span class="pun">=</span><span class="kwd">yes</span>
用以下命令启动 ghost:
<span class="pln">service ghost start</span>
其它命令:
<span class="pln">service ghost stop</span>
<span class="pln">service ghost status</span>
<span class="pln">service ghost restart</span>
Ghost 服务命令
相关推荐
我们直接指定到一个大空间去存储mysql数据。在CONFIGURE_ARGS=后面增加--with-charset=gb2312 \和--with-xcharset=all \ 两行才能支持中文字符。