当前位置: > Ubuntu >

Ubuntu上安装和配置Node.JS & Nginx

时间:2016-05-14 11:11来源:linux.it.net.cn 作者:IT

教你一步一步的怎么在Ubuntu上安装和配置 Node.JS & Nginx运行网页.

Step 1: 更新源,使用apt-get packages

sudo apt-get update

Step 2: 安装必要的包来编译 node 

sudo apt-get install g++ curl libssl-dev apache2-utils build-essential

Step 3: 下载最新版的note

sudo wget http://nodejs.org/dist/v0.10.30/node-v0.10.30.tar.gz

Step 4: 解压zip文件

sudo tar -xzf node-v0.10.30.tar.gz

Step 5: 进入到解压的文件夹位置然后build源

cd node-v0.10.30
sudo ./configure
sudo make
sudo make install

Step 6: 使用apt-get安装nginx

sudo apt-get install nginx

Step 7: 给app创建一个目录

sudo mkdir -p /var/www/mynodeapp

Step 8: 给app创建一个upstart脚本

sudo nano /etc/init/mynodeapp.conf

把下面的代码粘贴到 upstart 脚本去(不要忘记根据自己情况最后一行修改下"server.js" )

#!upstart
author "me"
description "mynodeapp"
setuid "ubuntu"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
console log
env NODE_ENV=production
exec /usr/local/bin/node /var/www/mynodeapp/server.js

Step 9: 启动upstart脚本

sudo start mynodeapp

Step 10: 配置nginx的域名,反向代理到upstart脚本去

sudo nano /etc/nginx/sites-enabled/mynodeapp

把下面的代码复制进去 (如果Note.js app服务监听的是另一个端口的话记得修改下倒数第二行哦)

upstream mynodeapp {
    server 127.0.0.1:3000;
        keepalive 64;
}

server {
        listen 80;
        server_name .myawsomedomian.com;
        access_log /var/log/nginx/mynodeapp.log;

        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;                             
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass http://mynodeapp/;
                proxy_redirect off;
                proxy_http_version 1.1;
        }
}

Step 11: 启动nginx

sudo service nginx restart

好了. Node.JS 应用可以成功从网页访问了



(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容