Debian安装Node.js Nginx

Node.js是一个JavaScript平台,可以动态,快速正确响应请求。 JavaScript通常是客户端,浏览器语言如HTML或CSS。 但是,Node.js是一个服务器端的JavaScript平台,与PHP相当。 Node.js经常与其他流行的服务器应用程序(如NGINX或Apache)一起工作。 在本文中,NGINX配置为处理前端,静态文件请求,Node.js配置为处理后端文件请求。

安装配置NGINX



1.安装

  1. apt-get install nginx

2.启动Nginx

  1. service nginx start

3.切换目录到sites-available

  1. cd /etc/nginx/sites-available/

4.新建一个sites-available文件,把example.com替换成你的域名
/etc/nginx/sites-available/example.com:

  1. #Names a server and declares the listening port
  2. server {
  3.   listen 80;
  4.   server_name example.com www.example.com;
  5.  
  6.   #Configures the publicly served root directory
  7.   #Configures the index file to be served
  8.   root /var/www/example.com;
  9.       index index.html index.htm;
  10.  
  11.   #These lines create a bypass for certain pathnames
  12.   #www.example.com/test.js is now routed to port 3000
  13.   #instead of port 80
  14.   location /test.js {
  15.       proxy_pass http://localhost:3000;
  16.       proxy_set_header Host $host;
  17.   }
  18. }

5.切换目录到sites-enabled

  1. cd /etc/nginx/sites-enabled/

6.创建一个软链接指向example sites-available文件

  1. ln -s /etc/nginx/sites-available/example.com

7.删除default软链接

  1. rm default

8.重载nginx配置

  1. service nginx reload

创建目录和HTML Index文件



NGINX现已配置。 但是,example.com server块里涉及的目录和文件需要手动创建。
1.创建/var/www和/var/www/example.com目录

  1. mkdir -p /var/www/example.com

2.切换目录

  1. cd /var/www/example.com

3.创建HTML Index文件
/var/www/example.com/index.html:

  1. <!DOCTYPE html>
  2.   <html>
  3.   <body>
  4.  
  5.   <br>
  6.   <br>
  7.  
  8.   <center>
  9.   <p>
  10.   <b>
  11.   If you have not finished the <a>guide</a>, the button below will not work.
  12.   </b>
  13.   </p>
  14.   </center>
  15.  
  16.   <center>
  17.   <p>
  18.   The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.
  19.   </p>
  20.   </center>
  21.  
  22.   <center>
  23.   <a>
  24.   <button>Go to test.js</button>
  25.   </a>
  26.   </center>
  27.  
  28.   </body>
  29.   </html>

安装Node.js



NGINX现在正在侦听80端口并已能提供服务。 它还配置/test.js请求转到端口3000.接下来的步骤是安装Node.js,然后使用Node.js编写服务器。 新服务器侦听端口3000。
1.安装Node版本管理器

  1. curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash

2.关闭并重新打开终端
3.安装Node.js

  1. nvm install 0.10

4.创建一个Node.js服务器
/var/www/example.com/server.js:

  1. //nodejs.org/api for API docs
  2.   //Node.js web server                         
  3.   var http = require("http"),                           //Import Node.js modules
  4.       url = require("url"),                             
  5.       path = require("path"),
  6.       fs = require("fs");
  7.  
  8.   http.createServer(function(request, response) {       //Create server
  9.   var name = url.parse(request.url).pathname;           //Parse URL
  10.   var filename = path.join(process.cwd(), name);        //Create filename
  11.   fs.readFile(filename, "binary", function(err, file) { //Read file
  12.       if(err) {                                         //Tracking Errors
  13.           response.writeHead(500, {"Content-Type": "text/plain"});
  14.           response.write(err + "\n");
  15.           response.end();
  16.           return;
  17.       }
  18.       response.writeHead(200);                          //Header request response
  19.       response.write(file, "binary");                   //Sends body response
  20.       response.end();                                   //Signals to server that
  21.    });                                                  //header and body sent
  22.   }).listen(3000);                                      //Listening port
  23.   console.log("Server is listening on port 3000.")      //Terminal output

5.运行一个新的screen会话

  1. screen

6.回车运行Node.js服务器

  1. node server.js

7.Ctrl+a+d退出screen

创建Test.js文件



NGINX目前侦听端口80,并将任何/test.js请求传递到端口3000。Node.js侦听端口3000并对任何请求响应。 接下来,写一个/test.js文件。
1.创建文件
/var/www/example.com/test.js:

  1. <!DOCTYPE html>
  2.   <html>
  3.   <body>
  4.         
  5.   <center>
  6.   <h2>
  7.   Your Node.JS server is working.
  8.   </h2>
  9.   </center>
  10.                 
  11.   <center>
  12.   <p>
  13.   The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.
  14.   </p>
  15.   </center>
  16.   <br>
  17.         
  18.   <center>
  19.   <button
  20.  sample').innerHTML = Date()">
  21.   Display the date and time.
  22.   </button>
  23.   <p></p>
  24.   </center>
  25.         
  26.   </body>
  27.   </html>

2.浏览器输入IP地址或域名测试NGINX服务器。 点击“Go to test.js”按钮测试Node.js服务器是否正常。 在测试页上,“Display the date and time”按钮将执行javascript的客户端代码段,以返回当前时间。
Node.js和NGINX目前已经能一起正常运行。 根据您的需要你可以更新路由把请求转到任何服务器。 Node.js提供了许多API及工具。 使用Node.js,开发人员可以在客户端或服务器端工作时使用JavaScript语言。

标签:DebianNginx 发布于:2019-11-21 12:22:39