前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何在Debian上安装Node.js和NGINX

如何在Debian上安装Node.js和NGINX

作者头像
双愚
发布2018-09-19 10:14:52
1.4K0
发布2018-09-19 10:14:52
举报

Node.js是一个JavaScript平台,可以提供动态的响应式内容。JavaScript通常是一种客户端浏览器语言,如HTML或CSS。但是,Node.js是一个服务器端的JavaScript平台,可与PHP相媲美。Node.js通常可以与NGINX或Apache等其他流行的服务器应用程序一起使用。在本指南中,NGINX配置为处理前端静态文件请求,Node.js配置为处理后端文件请求。

安装和配置NGINX

终端登录新的Linode后,可以立即启动本指南,它是为root用户编写的。但是,在安装之前,您可能希望确保Linode与我们的入门指南保持同步,并通过我们的“ 保护您的服务器”指南加以保护

  • 安装NGINX以及屏幕,稍后您将使用它: apt-get install nginx screen
  • 启动NGINX: service nginx start
  • 将工作目录更改为NGINX sites-available目录: cd /etc/nginx/sites-available/
  • 创建新的站点可用文件,替换example.com为您的域或IP地址: /etc/nginx/sites-available/example.com
#Names a server and declares the listening port
server {
    listen 80;
    server_name example.com www.example.com;

    #Configures the publicly served root directory
    #Configures the index file to be served
    root /var/www/example.com;
        index index.html index.htm;

    #These lines create a bypass for certain pathnames
    #www.example.com/test.js is now routed to port 3000
    #instead of port 80
    location /test.js {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
    }
}
  • 将工作目录更改为启用NGINX站点的目录: cd /etc/nginx/sites-enabled/
  • 创建新示例站点的可用文件的符号链接: ln -s /etc/nginx/sites-available/example.com
  • 删除default符号链接: rm default
  • 加载新的NGINX配置: service nginx reload

创建目录和HTML索引文件

NGINX现已配置。但是,example.com服务器块指向仍需要创建的目录和文件。

  • 创建/var/www/var/www/example.com目录: mkdir -p /var/www/example.com
  • 更改工作目录: cd /var/www/example.com
  • 创建HTML索引文件: /var/www/example.com/index.html
<!DOCTYPE html>
<html>
<body>

<br>
<br>

<center>
<p>
<b>
If you have not finished the <a href="https://linode.com/docs/websites/nodejs/nodejs-nginx-debian">guide</a>, the button below will not work.
</b>
</p>
</center>

<center>
<p>
The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.
</p>
</center>

<center>
<a href="test.js">
<button type="button">Go to test.js</button>
</a>
</center>

</body>
</html>

安装Node.js并编写Web服务器

NGINX现在正在侦听端口80并提供内容。它还配置为将/test.js请求传递到端口3000.接下来的步骤是安装Node.js,然后使用Node.js编写服务器。新服务器侦听端口3000。

  • 安装节点版本管理器: wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
  • 关闭并重新打开终端。
  • 安装Node.js: nvm install 0.10
  • 仍在/var/www/example.com目录中时,创建一个Node.js服务器: /var/www/example.com/server.js
//nodejs.org/api for API docs
//Node.js web server
var http = require("http"),                           //Import Node.js modules
    url = require("url"),
    path = require("path"),
    fs = require("fs");

http.createServer(function(request, response) {       //Create server
var name = url.parse(request.url).pathname;           //Parse URL
var filename = path.join(process.cwd(), name);        //Create filename
fs.readFile(filename, "binary", function(err, file) { //Read file
    if(err) {                                         //Tracking Errors
        response.writeHead(500, {"Content-Type": "text/plain"});
        response.write(err + "\n");
        response.end();
        return;
    }
    response.writeHead(200);                          //Header request response
    response.write(file, "binary");                   //Sends body response
    response.end();                                   //Signals to server that
 });                                                  //header and body sent
}).listen(3000);                                      //Listening port
console.log("Server is listening on port 3000.")      //Terminal output

  • 运行新的screen会话: screen
  • 按下return并运行Node.js服务器: node server.js
  • Ctrl+a然后按下退出屏幕d

创建Test.js文件

NGINX正在侦听端口80并将任何/test.js请求传递到端口3000.Node.js正在侦听端口3000并提供任何文件请求。接下来,写一个/test.js文件。

  • 创建文件: /var/www/example.com/test.js
<!DOCTYPE html>
<html>
<body>

<center>
<h2>
Your Node.JS server is working.
</h2>
</center>

<center>
<p>
The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.
</p>
</center>
<br>

<center>
<button type="button"
onclick="document.getElementById('sample').innerHTML = Date()">
Display the date and time.
</button>
<p id="sample"></p>
</center>

</body>
</html>

  • 在IP地址或域中测试NGINX服务器。使用“转到test.js”按钮测试Node.js服务器是否正在提供文件。在测试页面上,“显示日期和时间”按钮将执行JavaScript的客户端片段以返回当前时间。

Node.jsNGINX正在合作。根据您的需要将请求路由到一台服务器或另一台服务器。Node.js提供了一个包含许多工具的大型API。使用Node.js,开发人员可以在客户端或服务器端工作时保持JavaScript语言。

对于后续步骤,请查看WebSockets,iframe或框架集等技术。要在JavaScript中进行开发,请尝试Express.js,Ember.js,jQuery或模块的节点包管理器。

更多信息

有关此主题的其他信息,您可能需要参考以下资源。虽然提供这些是希望它们有用,但请注意,我们无法保证外部托管材料的准确性或及时性。

更多教程请前往腾讯云+社区学习更多知识。


参考文献:《https://www.linode.com/docs/development/nodejs/how-to-install-nodejs-and-nginx-on-debian/

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装和配置NGINX
  • 创建目录和HTML索引文件
  • 安装Node.js并编写Web服务器
  • 创建Test.js文件
  • 更多信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档