多节点部署程序
There are various platforms that help with deploying NodeJS apps to production. Zeit Now is one such platform.
有多种平台可帮助您将NodeJS应用程序部署到生产环境。 Zeit Now就是这样一个平台。
While those platforms can help, in this tutorial, we’ll be looking at how to deploy a NodeJS app to Digital Ocean. Digital Ocean compared to these other platforms is cheaper and you also can logon to your server and configure it however you like.
尽管这些平台可以提供帮助,但在本教程中,我们将研究如何将NodeJS应用程序部署到Digital Ocean。 与其他平台相比,Digital Ocean更便宜,您也可以登录到服务器并按自己的意愿进行配置。
You get more control over your deployment and also it's a great experiment to see exactly how Node apps are deployed to production.
您可以更好地控制部署,这也是一个很好的实验,可以确切地了解如何将Node应用程序部署到生产环境中。
先决条件 ( Prerequisites )
This tutorial assumes the following:
本教程假定以下内容:
- Digital Ocean account (obviously). Digital Ocean帐户(显然)。
- SSH setup on your local computer, you might want to follow https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets for a walk through. 在本地计算机上进行SSH设置时,您可能需要遵循https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplet进行遍历。
我们将要部署的内容 ( What We’ll Be Deploying )
Let’s quickly build a sample app that we’ll use for the purpose of this tutorial. It going to be pretty simple app.
让我们快速构建一个示例应用程序,以用于本教程。 这将是一个非常简单的应用程序。
// create a new directorymkdir sample-nodejs-app
// change to new directory
cd sample-nodejs-app
// Initialize npm
npm init -y
// install express
npm install express
// create an index.js file
touch index.js
Open index.js and paste the code below into it:
打开index.js并将以下代码粘贴到其中:
// index.js
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hey, I\'m a Node.js app!')
})
app.listen(3000, () => {
console.log('Server is up on 3000')
})
You can start the app with:
您可以通过以下方式启动应用程序:
node index.js
And access it on http://localhost:3000. You should get Hey, I'm a Node.js app! outputted. The complete code is available on GitHub.
并在http:// localhost:3000上访问它。 您应该得到Hey,我是Node.js应用程序! 输出。 完整的代码可在GitHub上获得 。
Now let’s take this our awesome app to production.
现在,让我们将此超赞的应用程序投入生产。
创建一个液滴 ( Create a Droplet )
Login to your Digital Ocean account and create a new droplet (server). We’ll be going to with One-click apps. Select NodeJS as shown below:
登录到您的Digital Ocean帐户并创建一个新的Droplet(服务器)。 我们将使用一键式应用程序 。 选择NodeJS,如下所示:
Next, we’ll choose the $10 plan. Though the task list app will work perfectly on the $5 plan, but we won’t be able to install the NPM dependencies because the NPM requires at least 1GB RAM for installing dependencies. Though there is a way around this by creating swap memory which is beyond the scope of this tutorial.
接下来,我们将选择10美元的计划。 尽管任务列表应用程序可以在$ 5计划中完美运行,但是我们无法安装NPM依赖项,因为NPM至少需要1GB RAM才能安装依赖项。 尽管可以通过创建交换内存来解决此问题,但这超出了本教程的范围。
Next, select a datacenter region, we’ll go with the default:
接下来,选择一个数据中心区域,我们将使用默认区域:
Next, add a new SSH key or choose from existing ones that you have added. You can get your SSH key by running the command below on your local computer:
接下来,添加新的SSH密钥,或从已添加的现有密钥中进行选择。 您可以通过在本地计算机上运行以下命令来获取SSH密钥:
cat ~/.ssh/id_rsa.pub
The command above will print your SSH key on the terminal, which you can then copy and paste in the SSH Key Content field. Also, give your SSH key a name.
上面的命令将在终端上打印您的SSH密钥,然后您可以将其复制并粘贴到“ SSH密钥内容”字段中。 另外,给您的SSH密钥命名。
Finally, choose a hostname for the droplet and click the Create button.
最后,为Droplet选择一个主机名,然后单击“ 创建”按钮。
After some couple of seconds, you’ll have your new server up and running on Ubuntu 16.04 and NodeJS version 6.11.2. Note the IP address of the server as we’ll be using it to access the server.
几秒钟后,您将启动新服务器并在Ubuntu 16.04和NodeJS版本6.11.2上运行。 记下服务器的IP地址,因为我们将使用它来访问服务器。
创建非root用户 ( Create Non-root User )
Before we start configuring the server for the task app, let’s quickly create a non-root user which we’ll use henceforth for the rest of the tutorial.
在开始为任务应用配置服务器之前,让我们快速创建一个非root用户,此后我们将在本教程的其余部分中使用该非root用户。
Tip: As a security measure, it is recommended to carry out tasks on your server as a non-root user with administrative privileges.
提示:作为安全措施,建议以具有管理特权的非root用户身份在服务器上执行任务。
First, we need to login to the server as root. We can do that using the server’s IP address:
首先,我们需要以root用户身份登录到服务器。 我们可以使用服务器的IP地址来做到这一点:
ssh root@SERVER_IP_ADDRESS
Once we are logged in to the server, we can move on to create a new user:
登录到服务器后,我们可以继续创建新用户:
adduser mezie
This will create a new user called mezie, you can name the user whatever you like. You will be asked a few questions, starting with the account password.
这将创建一个名为mezie的新用户,您可以根据需要命名该用户。 从帐户密码开始,系统将询问您几个问题。
Having created the new user, we need to give it administrative privileges. That is, the user will be able to carry out administrative tasks by using sudo command.
创建新用户后,我们需要为其赋予管理权限。 即,用户将能够使用sudo命令执行管理任务。
usermod -aG sudo mezie
The command above adds the user mezie to sudo group.
上面的命令将用户mezie添加到sudo组。
Now the user can run commands with superuser privileges.
现在,用户可以使用超级用户特权运行命令。
为新用户设置SSH密钥 ( Setup SSH Key For The New User )
You need to copy your public key to your new server. Enter the command below on your local computer:
您需要将公钥复制到新服务器。 在本地计算机上输入以下命令:
cat ~/.ssh/id_rsa.pub
This will print your SSH key to the terminal, which you can then copy.
这会将您的SSH密钥打印到终端,然后可以将其复制。
For the new user to login to the server with SSH key, we must add the public key to a special file in the user's home directory.
为了使新用户使用SSH密钥登录服务器,我们必须将公共密钥添加到用户主目录中的特殊文件中。
Still logged in as root on the server, enter the following command:
仍以root用户身份登录服务器,输入以下命令:
su - mezie
This will temporarily switch to the new user. Now you’ll be in your new user's home directory.
这将临时切换到新用户。 现在,您将位于新用户的主目录中。
Next, we need to create a new directory called .ssh and restrict its permission:
接下来,我们需要创建一个名为.ssh的新目录并限制其权限:
mkdir ~/.ssh
chmod 700 ~/.ssh
Next, within the .ssh directory, create a new file called authorized_keys:
接下来,在.ssh目录中,创建一个名为authorized_keys的新文件:
touch ~/.ssh/authorized_keys
Next, open the file with vim:
接下来,使用vim打开文件:
vim ~/.ssh/authorized_keys
Next, paste your public key (copied above) into the file. To save the file, hit esc to stop editing, then :wq and press ENTER.
接下来,将您的公共密钥(上面复制)粘贴到文件中。 要保存文件,请按esc停止编辑,然后按:wq并按ENTER 。
Next, restrict the permissions of the authorized_keys file with this command:
接下来,使用以下命令限制authorized_keys文件的权限:
chmod 600 ~/.ssh/authorized_keys
Type the command below to return to the root user:
键入以下命令以返回到root用户:
exit
Now your public key is installed, and you can use SSH keys to log in as your user.
现在,您的公共密钥已安装,您可以使用SSH密钥以用户身份登录。
To make sure you can login as the new user with SSH. Enter the command below in a new terminal on your local computer:
为了确保您可以使用SSH作为新用户登录。 在本地计算机的新终端中输入以下命令:
ssh mezie@SERVER_IP_ADDRESS
If all went well, you’ll be logged in to the server as the new user with SSH.
如果一切顺利,您将以SSH新用户身份登录到服务器。
The rest of the tutorial assumes you are logged in to the server with the new user created (mezie in my case).
本教程的其余部分假定您已使用创建的新用户(在我的情况下为mezie)登录到服务器。
在服务器上克隆应用程序 ( Clone The App on The Server )
We are going to clone the app unto the server directly in the user's home directory (that is, /home/mezie in my case):
我们将直接在用户的主目录(在我的情况下为/home/mezie )中将应用程序克隆到服务器上:
git clone https:///ammezie/sample-nodejs-app.git
Next, we install the dependencies:
接下来,我们安装依赖项:
cd sample-nodejs-app
npm install
Once the dependencies are installed we can test the app to make sure everything is working as expected. We’ll do so with:
安装依赖项后,我们可以测试该应用程序,以确保一切正常。 我们将这样做:
node index.js
The app is listening on port 3000 and can be accessed at http://localhost:3000. To test the app is actually working, open a new terminal (still on the server) and enter the command below:
该应用程序正在侦听端口3000 ,可以从http://localhost:3000进行访问。 要测试该应用程序是否正常运行,请打开一个新终端(仍在服务器上),然后输入以下命令:
curl http://localhost:3000
You should get an output as below:
您应该得到如下输出:
Hey, I'm a Node.js app!
Good! The app is up and running fine. But whenever the app crashes we’ll need to manually start the app again which is not a recommended approach. So, we need a process manager to help us with starting the app and restarting it whenever it crashes. We’ll use PM2 for this.
好! 该应用程序已启动并正常运行。 但是,每当应用程序崩溃时,我们都需要再次手动启动该应用程序,这不是推荐的方法。 因此,我们需要一个流程管理器来帮助我们启动应用程序,并在应用程序崩溃时重新启动它。 我们将为此使用PM2 。
安装PM2 ( Install PM2 )
We’ll install it globally through NPM:
我们将通过NPM在全球范围内安装它:
sudo npm install -g pm2
With PM2 installed, we can start the app with it:
安装了PM2后,我们可以使用它启动应用程序:
pm2 start index.js
Once the app is started you will get an output from PM2 indicating the app has started.
一旦启动该应用程序,您将从PM2获得输出,表明该应用程序已启动。
To launch PM2 on system startup or reboot, enter the command below:
要在系统启动或重新启动时启动PM2,请输入以下命令:
pm2 startup systemd
You’ll get the following output:
您将获得以下输出:
[PM2] Init System found: systemd
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u mezie --hp /home/mezie
Copy and run the last command from the output above:
从上面的输出中复制并运行最后一个命令:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u mezie --hp /home/mezie
Now PM2 will start at boot up.
现在,PM2将在启动时启动。
安装Nginx ( Install Nginx )
Next, we’ll install Nginx as the webserver to be used for reverse proxy which will allow us to access the app directly with an IP address or domain instead of tacking port to the IP address. Eg. 102.123.83.29:5000.
接下来,我们将安装Nginx作为用于反向代理的网络服务器,这将允许我们直接使用IP地址或域访问应用程序,而不是将端口附加到IP地址。 例如。 102.123.83.29:5000
sudo apt-get update
sudo apt-get install nginx
Because we chose One-click apps while creating our droplet, ufw firewall is setup for us and running. Now, we need to open firewall for only HTTP since we are not concerned with SSL in this tutorial:
由于我们在创建ufw时选择了“ 一键式”应用程序 ,因此ufw防火墙已为我们设置并正在运行。 现在,我们只需要为HTTP打开防火墙,因为在本教程中我们不关心SSL:
sudo ufw allow 'Nginx HTTP'
将Nginx设置为反向代理服务器 ( Set Up Nginx as a Reverse Proxy Server )
Finally, we set up Nginx as a reverse proxy server. To this, run:
最后,我们将Nginx设置为反向代理服务器。 为此,运行:
sudo vim /etc/nginx/sites-available/default
Within the server block you should have an existing location / block. Replace the contents of that block with the following configuration:
在server块内,您应该有一个现有的location /块。 用以下配置替换该块的内容:
// /etc/nginx/sites-available/default...
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
Save and exit vim.
保存并退出vim 。
Test to make sure there are no syntax errors in the configuration by running:
通过运行以下命令进行测试,以确保配置中没有语法错误:
sudo nginx -t
Then restart Nginx:
然后重新启动Nginx:
sudo systemctl restart nginx
Now you should be able to access the app with your IP_ADDRESS. You should get something similar to the image below:
现在,您应该可以使用IP_ADDRESS访问该应用了。 您应该获得类似于下图的内容:
结论 ( Conclusion )
In this tutorial, we have seen how to deploy a NodeJS app to Digital Ocean. We also saw how to setup a reverse proxy server with Nginx.
在本教程中,我们已经看到了如何将NodeJS应用程序部署到Digital Ocean。 我们还看到了如何使用Nginx设置反向代理服务器。
翻译自: https://scotch.io/tutorials/deploying-a-node-app-to-digital-ocean
多节点部署程序