如何使用 Systemd 和 Nginx 部署 Node.js 应用程序

介绍

当部署 Web 应用程序到 Droplet 时,可能会诱惑只使用开发中使用的相同类型的设置,即通过在终端中运行 ruby app.rbnode server.js 来启动服务器。 这是简单而简单的,同时提供可见的日志。 甚至可以使用 屏幕tmuxnohup 让它即使在 SSH 会话停止时仍在运行。

人们可以使用永远和corntab来处理此事. 这个教程提供了一个更坚固但更复杂的解决方案. 使用 [系统化 (https://wiki.archlinux.org/index.php/systemd) (在Arch和Fedora,以及CentOS上可以在未来获得),网络应用程序可以彻底管理:日志,上行时间,资源和安全通过cgroups,高级守护进程启动都可以以统一的方式访问,控制和微调.

这个教程使用一个简单的Node.js应用程序,但适用于大多数,如果不是所有,其他人也一样(他们是Ruby,Python,等等)。对于PHP的Web应用程序,它是建议使用一个更专业的LAMP(https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu)或LEMP(https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-12-04)堆栈。

命令将为 Fedora 和 Arch 提供,请仔细观察,以避免错误的配置和/或混淆。当没有指示时,命令对于两种系统都是相同的。

系统预览

  • 具有 ** systemd 的服务器. Arch Linux 和 Fedora dropplets 是默认配置的。** Nginx* ,作为反向代理 http 和 websocket 服务器使用。** Git* ,用于安装 nvm,并在使用 git 时拖动您的应用程序.** Root* 访问. 也可以作为正常用户登录和 sudo 所有命令,或到** su -** 或** sudo su -** 到 root 提示。

安装包

阿克:

1# pacman -Sy
2# pacman -S nginx git

费德拉:

1# yum install nginx git

初步应用程序

这些是你可以根据自己的喜好定制的设置,但他们必须在开始之前决定并设置。

用户

该应用程序将在其自己的独立用户帐户中运行。选择一个名称,它应该与应用程序有关,使其易于记住和维护。

1# useradd -mrU srv-node-sample

◎ 港口

要避免冲突,请选择高端口,这里使用15301

应用程序设置

对于Node.js(和Ruby,Python...),有两种选择:要么使用系统的运行时间,要么使用用户特定的安装(例如使用 nvm, rbenv, RVM, virtualenv,等等)。

使用系统节点

阿克:

1# pacman -S nodejs

费德拉:

1# yum install nodejs

使用用户特定的安装

这必须安装在应用程序的主目录中,即 /home/srv-node-sample,最容易通过登录作为该用户来完成:

1# su srv-node-sample
1$ cd
2$ curl https://raw.github.com/creationix/nvm/master/install.sh | sh
3$ source ~/.nvm/nvm.sh
4$ nvm install 0.10
5$ nvm alias default 0.10

接下来,请注意在哪里安装了node binary:

1$ which node
2/home/srv-node-sample/.nvm/v0.10.22/bin/node

部署您的应用程序

當您登入「srv-node-sample」時,請部署您的代碼. 這只是一個例子,您的流程會有所不同。

1$ git clone [email protected]:user/repo.git .
2$ npm install
3$ grunt deploy

对于本教程,使用以下样本应用程序:

1js
2var http = require('http');
3http.createServer(function(req, res) {
4    res.end('<h1>Hello, world.</h1>');
5}).listen(15301);

然后回到 root:

1$ exit

Nginx 设置

本教程只简要涵盖必要的配置,有关配置 Nginx 的更详细的教程,请参阅 [)或 nginx 手册

把它放在你的服务器块上:

1location / {
2    proxy_pass http://localhost:15301/;
3    proxy_set_header Host $host;
4    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5}

随后,他创建了自己的DAEMON:

1# systemctl enable nginx
2# systemctl restart nginx

系统设置

创建应用程序的服务文件,在 /etc/systemd/system/node-sample.service

有几个变量需要填写:

  • [node 二进制] 这是哪个节点作为 srv-node 样本用户的输出。 无论是 /usr/bin/node 还是上面提到的 ~/.nvm/... 路径。 * [主文件] 这是您的应用程序的主要文件。
 1[Service]
 2ExecStart=[node binary] /home/srv-node-sample/[main file]
 3Restart=always
 4StandardOutput=syslog
 5StandardError=syslog
 6SyslogIdentifier=node-sample
 7User=srv-node-sample
 8Group=srv-node-sample
 9Environment=NODE_ENV=production
10
11[Install]
12WantedBy=multi-user.target

现在开始服务:

1# systemctl enable node-sample
2# systemctl start node-sample

使用

状态

1# systemctl status node-sample
2node-sample.service
3   Loaded: loaded (/etc/systemd/system/node-sample.service; enabled)
4   Active: active (running) since Fri 2013-11-22 01:12:15 UTC; 35s ago
5 Main PID: 7213 (node)
6   CGroup: name=systemd:/system/node-sample.service
7           └─7213 /home/srv-node-sample/.nvm/v0.10.22/bin/node /home/srv-nod...
8
9Nov 22 01:12:15 d02 systemd[1]: Started node-sample.service.

日志

1# journalctl -u node-sample
2-- Logs begin at Thu 2013-11-21 19:05:17 UTC, end at Fri 2013-11-22 01:12:15 UTC. --
3Nov 22 01:12:15 d02 systemd[1]: Starting node-sample.service...
4Nov 22 01:12:15 d02 systemd[1]: Started node-sample.service.
5Nov 22 01:12:30 d02 node-sample[7213]: Sample message from application

重新启动、停止等

强力重新启动:

1# systemctl restart node-sample

停止应用:

1# systemctl stop node-sample

应用程序会自动重新启动,如果它死亡或被杀死:

 1# systemctl status node-sample
 2node-sample.service
 3   Loaded: loaded (/etc/systemd/system/node-sample.service; enabled)
 4   Active: active (running) since Fri 2013-11-22 01:12:15 UTC; 35s ago
 5 Main PID: 7213 (node)
 6   CGroup: name=systemd:/system/node-sample.service
 7           └─7213 /home/srv-node-sample/.nvm/v0.10.22/bin/node /home/srv-nod...
 8
 9Nov 22 01:12:15 d02 systemd[1]: Started node-sample.service.
10
11# kill 7213
12
13# systemctl status node-sample
14node-sample.service
15   Loaded: loaded (/etc/systemd/system/node-sample.service; enabled)
16   Active: active (running) since Fri 2013-11-22 01:54:37 UTC; 6s ago
17 Main PID: 7236 (node)
18   CGroup: name=systemd:/system/node-sample.service
19           └─7236 /home/srv-node-sample/.nvm/v0.10.22/bin/node /home/srv-nod...
20
21Nov 22 01:54:37 d02 systemd[1]: node-sample.service holdoff time over, sch...t.
22Nov 22 01:54:37 d02 systemd[1]: Stopping node-sample.service...
23Nov 22 01:54:37 d02 systemd[1]: Starting node-sample.service...
24Nov 22 01:54:37 d02 systemd[1]: Started node-sample.service.

PID已经改变,表明应用程序确实被杀死并重新启动。

网页

如果应用程序使用websockets,则必须在 Nginx 配置中添加以下行:

1proxy_set_header Upgrade $http_upgrade;
2proxy_set_header Connection "upgrade";
3proxy_http_version 1.1;

Nginx 需要重新加载:

1# systemctl reload nginx
Submitted by: Félix Saparelli
comments powered by Disqus