代码改变世界

Nodejs开发环境搭建

2012-03-17 02:37  BlueDream  阅读(23783)  评论(4编辑  收藏  举报

由于还是习惯win7环境。所以搭建的环境是:

win7为开发环境。Vbox虚拟机+Ubuntu搭建nodejs编译环境。然后通过securecrt远程连接到虚拟机进行开发。

如果securecrt远程链接被拒绝需要sudo apt-get install openssh-server 

第一步:安装依赖包

1. 安装python 2.6版或者更高(ubuntu默认都已安装,可以在terminal中使用 python -v 命令查看python版本)。
2. 安装其他依赖包:
sudo apt-get install g++ curl libssl-dev apache2-utils
3. 安装git工具:

sudo apt-get install git

第二步:获取源码

git clone git://github.com/joyent/node.git

第三步:指定编译版本

1.先进入存放下载源码的文件夹:
cd node
2. 指定迁出版本:
git checkout v0.6.12 (版本的选择,遵循稳定原则)
3. 指定路径,编译执行:

mkdir ~/local
./configure –prefix=$HOME/local/node
make
make install
echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
source ~/.profile

 

第四步:设置环境变量

如果想重启后还能继续直接使用node命令,那么需要设置环境变量:
使用命令 sudo gedit /etc/profile 打开配置文件,在文件最后中添加如下两行:

export PATH="$HOME/local/node/bin:$PATH"
export NODE_PATH="$HOME/local/node:$HOME/local/node/lib/node_modules"

 

保存后重启系统使设置生效。 

 

第五步:安装npm

curl https://npmjs.org/install.sh | sh
根据需要,安装相应的包,例如express:
npm install express -gd
-g代表安装到NODE_PATH的lib里面,而-d代表把相依性套件也一起安装。如果沒有-g的话会安装目前所在的目录(会建立一个node_modules的文件夹)

 

第六步:通过npm按需安装文件包

这里我们可以引用一个实例来说明。提供一个练习Demo https://github.com/cmarin/MongoDB-Node-Express-Blog 

这个DEMO需要安装的依赖包已经标明,咱们按照命令操作即可

首先cd到自己的工作目录 git clone git://github.com/cmarin/MongoDB-Node-Express-Blog.git 获取源码。

然后首先安装数据库直接在命令行里输入sudo apt-get install mongodb(参考http://gnucto.blog.51cto.com/3391516/833535 )即可,安装完成后测试方法,终端命令行中输入:

mongo 
db.foo.save({a:1}) 
db.foo.findOne() 

 

然后 

npm install express  
npm install express-messages 
npm install ejs npm install sass 
npm install mongoose  
Then cd into the directory and run: node app.js

 

此时会看到终端的log提示 

You can debug your app with http://localhost:3000 表明已经安装成功。

此时通过ip就可以访问了。如http://192.168.1.106:3000/ 

Tips 

1. 有时候由于异常关机会导致mongodb数据库被锁住。提示Error: couldn't connect to server 127.0.0.1}  。解决方案:

sudo rm /var/lib/mongodb/mongod.lock
sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start

 

2. 在nodejs开发阶段 如果用node xxx.js 来运行时不能时时检测js文件的变化,这样调试起来就很麻烦,所以需要安装一个开发调试脚本

npm install -g node-dev
使用
node-dev app.js 来调试

 

如果需要在nodeserver之前再搭建一层nginx,基础配置如下:

 

1 安装pcre
sudo apt-get install libpcre3-dev

2 安装zlib
$ tar xzf zlib-1.2.3.tar.gz
$ cd zlib-1.2.3/
$ ./configure
$ make
$ sudo make install


nginx-1.0.8$ ./configure --prefix=<所要安装的path 比如我的安装在/home/机器名/work/nginx>
nginx-1.0.8$ make
nginx-1.0.8$ sudo make install

4 检查配置是否正确
root@ubuntu:/home/# /usr/local/nginx/sbin/nginx -t  [根据上面不同的--prefix需要找不同的路径。此处是默认安装不指定prefix的默认目录]
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5 启动nginx
root@ubuntu:/home/# /usr/local/nginx/sbin/nginx
root@ubuntu:/home/# ps -ef | grep nginx
root      1436     1  0 04:58 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    1437  1436  0 04:58 ?        00:00:00 nginx: worker process      
root      1439  1413  0 04:58 pts/0    00:00:00 grep --color=auto nginx

6 测试
root@ubuntu:/home/# curl http://localhost      
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

或者

root@ubuntu:/home/xiong# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:7b:80:c2  
          inet addr:192.168.130.131  Bcast:192.168.130.255  Mask:255.255.255.0

浏览器输入:
http://192.168.130.131/




关闭nginx:

nginx -s stop         快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。(quick exit)
nginx -s quit          平稳关闭Nginx,保存相关信息,有安排的结束web服务。(graceful exit)

 

重启nginx:

nginx -s reload       因改变了Nginx相关配置,需要重新加载配置而重载。(changing configuration,start a new worker,quitting an old worker gracefully.)
nginx -s reopen       重新打开日志文件。(reopenging log files)

 

 

以上nginx基本配置完毕,可以启动了。简单放一个基础的nginx.conf文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

error_log  logs/error.log  debug; # 第一处修改 开启日志
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    # 第二处 开启日志格式化
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    server {
        listen       9199; # 第三处修改 修改默认80端口,不然启动nginx需要root权限
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /xxx {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location / {
            proxy_pass   http://127.0.0.1:3001;  # 第四处 所有9199端口下直接proxy_pass到其他服务端
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

入门教程 http://www.nodebeginner.org/index-zh-cn.html 值得一看