1、安装node.js
首先安装node.js安装所需的软件包,并在启动板上添加可用的nodejs的PPA。之后使用以下命令安装nodejs。
- $ sudo apt-get install python-software-properties python g++ make
-
- $ sudo add-apt-repository ppa:chris-lea/node.js
-
- $ sudo apt-get update
-
- $ sudo apt-get install nodejs
2、安装nginx
现在使用apt get安装nginx web服务器。nginx在默认存储库下可用。
- $ sudo apt-get install nginx
3、创建测试node服务器
现在创建一个测试node服务器应用程序,并在主机127.0.0.1的端口3000上运行它。要创建node服务器,请创建文件~/myapp/myapp.js。
- $ cd ~/MyApp/
-
- $ vi myapp.js
并在javascript文件中添加以下内容。
- var http = require('http');
-
-
-
- http.createServer(function (req, res) {
-
- res.writeHead(200, {'Content-Type': 'text/plain'});
-
- res.end('Hello Worldn');
-
- }).listen(3000, "127.0.0.1");
-
- console.log('Server running at http://127.0.0.1:3000/');
现在使用以下命令在后台启动nodejs
在浏览器中访问。
输出:Hello Word
4、配置NGNIX
使用node.js启动演示服务器后,现在开始使用Nginx进行配置。在/etc/nginx/conf.d/目录下为域创建虚拟主机配置文件。
- $ sudo vim /etc/nginx/conf.d/example.com.conf
并添加以下内容。
- upstream myapp {
-
- server 127.0.0.1:3000;
-
- keepalive 8;
-
- }
-
-
-
- # the nginx server instance
-
- server {
-
- listen 0.0.0.0:80;
-
- server_name example.com www.example.com;
-
- access_log /var/log/nginx/example.com.log;
-
-
-
- location / {
-
- proxy_set_header X-Real-IP $remote_addr;
-
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-
- proxy_set_header Host $http_host;
-
- proxy_set_header X-NginX-Proxy true;
-
-
-
- proxy_pass http://myapp/;
-
- proxy_redirect off;
-
- }
-
- }
完成所有配置后,让我们使用以下命令重新启动nginx Web服务器。
- $ sudo /etc/init.d/nginx restart
5、验证安装程序
现在使用域名访问你的服务器,你将在http://127.0.0.1:3000/上看到相同的页面。
输出为Hello Word