基于Debian系统配置Nginx环境的Node.js应用教程

来源:
导读:目前正在解读《基于Debian系统配置Nginx环境的Node.js应用教程》的相关信息,《基于Debian系统配置Nginx环境的Node.js应用教程》是由用户自行发布的知识型内容!下面请观看由(国外服务器 - www.2bp.net)用户发布《基于Debian系统配置Nginx环境的Node.js应用教程》的详细说明。

Node.js,是当前比较流行的能够动态的快速响应内容的JavaScript框架,在有些环境下比我们使用的PHP应用都能够提高效率。目前,Node.js可以与我们常用的Nginx、Apache等服务器应用程序使用,在这篇文章中,笨笨网将分享我们在debian系统中,基于Nginx环境的配置处理前端、静态文件,然后用Node.js配置处理后端文件请求。

第一、安装和配置Nginx

1、安装nginx

apt-get install nginx

2、启动Nginx

service nginx start

3、改变工作目录

cd /etc/nginx/sites-available/

4、创建一个新的站点文件

/etc/nginx/sites-available/2bp.net

然后添加下面的脚本

#Names a server and declares the listening port

server {

listen 80;

server_name 2bp.net www.2bp.net;

#Configures the publicly served root directory

#Configures the index file to be served

root /var/www/2bp.net;

index index.html index.htm;

#These lines create a bypass for certain pathnames

#www.2bp.net/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;

}

}

5、更改工作目录

cd /etc/nginx/sites-enabled/

6、创建软连接

ln -s /etc/nginx/sites-available/2bp.net

7、删除默认信息

rm default

8、重新启动和载入Nginx

service nginx reload

第二、创建站点目录和文件

到目前为止,我们的Nginx环境已经创建完毕,我们可以将设置的域名绑定指向到当前VPS的IP地址。然后我们需要进行创建目录和文件 调试。

1、创建目录

mkdir -p /var/www/2bp.net

2、创建工作文档

cd /var/www/2bp.net

3、创建一个调试HTML文件

/var/www/2bp.net/index.html

输入下面内容

The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.

保存后退出当前文件。

第三、安装Node.js和写入设置

1、安装最新版本的Node.js

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

2、安装

nvm install 0.10

3、设置配置文件

/var/www/2bp.net/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

3、打开一个新窗口运行环境

screen

4、执行

node server.js

执行脚本。

第四、创建测试文件Test.js

/var/www/2bp.net/test.js

输入下面内容

Your Node.JS server is working.

The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.

<button type="button"

onclick="document.getElementById('sample').innerHTML = Date()">

Display the date and time.

这个时候,我们可以打开域名或者IP地址,测试文件。test.js按钮来测试Node.js的服务器提供文件服务。在测试页面中,我们可以看到当前返回的服务器时间。

最后,这样当前的NGINX和NODE.JS已经可以同时运行,如果我们有项目需要的时候,试试是否比PHP执行效率高。反正笨笨网看到不少的项目和开发已经都在用NODE.JS做后端开发。

提醒:《基于Debian系统配置Nginx环境的Node.js应用教程》最后刷新时间 2023-03-27 02:11:21,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《基于Debian系统配置Nginx环境的Node.js应用教程》该内容的真实性请自行鉴别。