由於還是習慣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. 指定路徑,編譯執行:
./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 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 )即可,安裝完成后測試方法,終端命令行中輸入:
db.foo.save({a:1})
db.foo.findOne()
然后
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 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文件的變化,這樣調試起來就很麻煩,所以需要安裝一個開發調試腳本
node-dev app.js 來調試
如果需要在nodeserver之前再搭建一層nginx,基礎配置如下:
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
3
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文件
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;
# }
#}
}