<strong>1,为什么用EC2搭建node.js环境。</strong> <br/><div>首先,本地虚拟机装centOs是很好的选择,但是需要在家里和公司同时搭好相同的环境比较麻烦,而且也不方便小团队一起折腾。</div> <br/><div>亚马逊的免费虚拟机只要不跑太复杂的运算大概不会有额外的费用,而且亚马逊么有被墙,国内的托管目前cnode的邀请号求不到啊,还是自己动手丰衣足食吧。</div> <br/><strong>2,如何申请免费一年的EC2</strong> <br/><div>在http://aws.amazon.com/上注册。</div> <br/><div>网上有很多教程,请自行GOOGLE。</div> <br/><div><strong>3,EC2虚拟机简单的管理和设置</strong></div> <br/><div>新建KEY,保存。</div> <br/><div>For MAC & Linux</div> <br/><div>很简单,只要一句话</div> <br/><div> <br/><pre>ssh -i user.pem ec2-user@ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com</pre> <br/></div> <br/><div>以上user.pem可以是你的KEY的路径。</div> <br/><div>For PC</div> <br/><div>putty 链接ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com</div> <br/><div>注意需要在connection->SSH->Auth里选上你的KEY,之前需要转pem到ppk。</div> <br/><div>怎么看DNS名:在管理界面里,先点击你要查看的虚拟机,点右键,再弹出的菜单上点“connect”就可以看到如下图:</div> <br/><div>其中ec2-XXX-XXX-XXX-XXX.compute-1.amazonaws.com是你的DNS名</div> <br/><div>一般不太需要调整什么,可能要改的就是端口8080要开启。</div> <br/><div><strong>4,搭建node前的准备</strong></div> <br/><div>默认的liunx中不会吧所有工具都装好,一些基本的工具,需要我们自己装。</div> <br/><div>以下:</div> <br/><div>a. make</div> <br/><div>b.gcc++</div> <br/><div>c.curl</div> <br/><div>d.git</div> <br/><div>一句话安装:</div> <br/><div> <br/><pre>sudo yum install gcc-c++ openssl-devel make curl git</pre> <br/></div> <br/><strong>5,安装node</strong> <br/><div> <br/><pre>wget http://nodejs.org/dist/node-v0.4.12.tar.gz <br/>tar -xvf node-v0.4.12.tar.gz <br/>cd node-v0.4.12 <br/>./configure <br/>make</pre> <br/></div> <br/><div>由于是在云里跑的,所以make可能会花上30多分钟, 我出去吃了个中饭,回办公室发现好了的。所以,不用在屏幕前傻等,出去走走,预防一下颈椎病。</div> <br/><div> <br/><pre>make install</pre> <br/></div> <br/><div>其他调整,由于EC2中path的问题,node装完了不一定能直接跑。所以有时候需要做些映射。</div> <br/><div> <br/><pre>sudo ln -s /usr/local/bin/node /usr/bin/node <br/>sudo ln -s /usr/local/lib/node /usr/lib/node <br/>sudo ln -s /usr/local/bin/npm /usr/bin/npm <br/>sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf</pre> <br/></div> <br/><div><strong>6,测试第一个静态文件服务</strong></div> <br/><div>先在/home/ec2-user/上建个nodeweb目录,里面放静态页面,然后可以测试了~~</div> <br/><div> <br/><pre>var libpath = require(‘path’), <br/> http = require(“http”), <br/> fs = require(‘fs’), <br/> url = require(“url”), <br/> mime = require(‘mime’); <br/>var path = “/home/ec2-user/nodeweb”; <br/>var port = 8080; <br/>http.createServer(function (request, response) { <br/> var uri = url.parse(request.url).pathname; <br/> var filename = libpath.join(path, uri); <br/> libpath.exists(filename, function (exists) { <br/> if (!exists) { <br/> response.writeHead(404, { <br/> “Content-Type”: “text/plain” <br/> }); <br/> response.write(“404 Not Found\n”); <br/> response.end(); <br/> return; <br/> } <br/> if (fs.statSync(filename).isDirectory()) { <br/> filename += ‘/index.html’; <br/> } <br/> fs.readFile(filename, “binary”, function (err, file) { <br/> if (err) { <br/> response.writeHead(500, { <br/> “Content-Type”: “text/plain” <br/> }); <br/> response.write(err + “\n”); <br/> response.end(); <br/> return; <br/> } <br/> var type = mime.lookup(filename); <br/> response.writeHead(200, { <br/> “Content-Type”: type <br/> }); <br/> response.write(file, “binary”); <br/> response.end(); <br/> }); <br/> }); <br/>}).listen(port);</pre> <br/></div> <br/><strong>7,推荐阅读:</strong> <br/><div> a.Installing Node.js On Amazon EC2 :http://www.embracingthecloud.com/2010/12/05/InstallingNodejsOnAmazonEC2.aspx</div> <br/><div> b.npm : https://github.com/isaacs/npm</div> <br/><div> c.mime : https://github.com/bentomas/node-mime</div> <br/><div><strong>8,其他~</strong></div> <br/>求推荐操作简单的配合node用数据库。 <br/> <br/>我的微博:<a href=“http://weibo.com/vitrum”>http://weibo.com/vitrum</a>
太强大了
强大是够强大,但是效率太低了,为什么不选择速度更快的免费托管商呢?例如:heroku
heroku和Joyent 都木有用过啊~~~求指导~~