将AdonisJs 5部署到DigitalOcean

In this tutorial, I’ll be showing you how to deploy an AdonisJs 5 application to DigitalOcean. For the purpose of this tutorial, I have created a demo AdonisJs 5 application, which we’ll be deploying to DigitalOcean. The demo application can be found on GitHub.

在本教程中,我将向您展示如何将AdonisJs 5应用程序部署到DigitalOcean 。 出于本教程的目的,我创建了一个演示AdonisJs 5应用程序,我们将其部署到DigitalOcean 。 该演示应用程序可以在GitHub找到

先决条件 ( Prerequisites )

This tutorial assumes the following:

本教程假定以下内容:

创建一个液滴 ( Create a Droplet )

We’ll start by spinning up a new server, which is referred to as droplet by DigitalOcean. Login to your DigitalOcean account and create a new droplet.

我们将从启动一个新服务器开始,该服务器被DigitalOcean称为Droplet。 登录到您的DigitalOcean帐户并创建一个新的Droplet。

Create droplet

We’ll be making use of DigitalOcean's 1-Click Apps to quickly spin up our server. So under Choose an image, click on the Marketplace tab and type Node.js Quickstart then select it from the result as shown below:

我们将利用DigitalOcean的1 - Click Apps快速启动我们的服务器。 因此,在“选择图像”下 ,单击“ 市场”选项卡,然后键入“ 节点” js快速入门,然后从结果中选择它,如下所示:

Select Node.js Quickstart

Next, we’ll choose the $5/month plan, which is suitable for our demo application.

接下来,我们将选择每月5美元的计划,该计划适用于我们的演示应用程序。

Choose plan

For the datacenter region, we’ll go with the default. Next, we’ll add our SSH key. If you have already added SSH keys to DigitalOcean before, you can choose from those:

对于数据中心区域,我们将使用默认值。 接下来,我们将添加我们的SSH密钥。 如果您之前已经将SSH密钥添加到DigitalOcean ,则可以从以下选项中进行选择:

Select SSH key

If not, you’ll need to click on the New SSH Key button to add a new SSH key. You can get your SSH key by running the command below on your local computer:

如果没有,则需要单击“ 新建SSH密钥”按钮以添加新的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 and give your SSH key a name.

上面的命令将在终端上打印SSH密钥,然后您可以将其复制并粘贴到“ SSH密钥内容”字段中,并为SSH密钥命名。

Add SSH key

Finally, choose a hostname (if you don’t want the randomly generated name) for the droplet and click the Create Droplet button.

最后,为小滴选择一个主机名(如果不想使用随机生成的名称),然后单击创建小滴按钮。

We should now have a server up and running on Ubuntu 18.04 with the following software installed and configured:

现在,我们应该在Ubuntu 18.04上启动并运行服务器,并安装并配置以下软件:

Node.js Quickstart softwares

Take note of the IP address of the server as we’ll be using it to access the server in subsequent sections.

记下服务器的IP地址,因为我们将在随后的部分中使用它来访问服务器。

配置服务器 ( Configuring The Server )

As a security measure, it is recommended to carry out tasks on a server as a non-root user with administrative privileges. So before we start configuring our server, let’s create a non-root user which we’ll use to administer our server for the rest of the tutorial.

作为安全措施,建议以具有管理特权的非root用户身份在服务器上执行任务。 因此,在开始配置服务器之前,让我们创建一个非root用户,在本教程的其余部分中,我们将使用该非root用户来管理我们的服务器。

First, we need to login to the server as root. We can do that using the server’s IP address. Enter the command below on your local computer:

首先,我们需要以root身份登录到服务器。 我们可以使用服务器的IP地址来实现。 在本地计算机上输入以下命令:

ssh root@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 perform administrative tasks by using sudo command:

创建新用户后,我们需要为其赋予管理权限。 也就是说,用户将能够使用sudo命令执行管理任务:

usermod -aG sudo mezie

The command above adds the user mezie to sudo group. Now, the user can run commands with superuser privileges.

上面的命令将用户mezie添加到sudo组。 现在,用户可以使用超级用户权限运行命令。

Next, let’s set up SSH key for the newly created user. You need to copy your public key to your new server. Enter the command below on your local computer:

接下来,让我们为新创建的用户设置SSH密钥。 您需要将公钥复制到新服务器。 在本地计算机上输入以下命令:

cat ~/.ssh/id_rsa.pub

Copy the SSH key printed to the terminal.

将打印的SSH密钥复制到终端。

For the new user to login to the server with an 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, run the command below:

仍以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

Within the .ssh directory, create a new file called authorized_keys:

.ssh目录中,创建一个名为authorized_keys的新文件:

touch ~/.ssh/authorized_keys

Open the file:

打开文件:

vim ~/.ssh/authorized_keys

and 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 the new user.

现在,您的公共密钥已安装,并且您可以使用SSH密钥以新用户身份登录。

To make sure you can log in as the new user with SSH. Enter the command below in a new terminal on your local computer:

为了确保您可以使用SSH作为新用户登录。 在本地计算机的新终端中输入以下命令:

ssh mezie@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 newly created user (mezie in my case).

本教程的其余部分假定您使用新创建的用户(在我的情况下为mezie )登录服务器。

Because we chose 1-Click App while creating our droplet, ufw firewall is enabled, which keeps our server secured. Now, we need to open the firewall for only HTTP since we are not concerned with SSL in this tutorial:

由于我们在创建ufw时选择了1- 单击应用程序 ,因此启用了ufw防火墙,从而确保了服务器的安全。 现在,我们只需要为HTTP打开防火墙,因为在本教程中我们不关心SSL:

sudo ufw allow 'Nginx HTTP'

安装和配置MySQL ( Installing and Configuring MySQL )

The demo application we’re deploying in this tutorial makes use of MySQL, so we need to install and configure MySQL on our server:

我们将在本教程中部署的演示应用程序使用MySQL,因此我们需要在服务器上安装和配置MySQL:

sudo apt install mysql-server

Next, let’s configure MySQL:

接下来,让我们配置MySQL:

sudo mysql_secure_installation

Enter a root password when prompted, then answer the necessary options when prompted.

在提示时输入root密码,然后在提示时回答必要的选项。

With the MySQL configured, we need to create a database and a user. First, log in to the MySQL server:

配置了MySQL后,我们需要创建一个数据库和一个用户。 首先,登录到MySQL服务器:

mysql -u root -p

Provide the root password you enter above while installing MySQL. Once you are logged in, create a new user:

提供您在安装MySQL时在上方输入的root密码。 登录后,创建一个新用户:

CREATE USER'YOUR_USERNAME'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD';

Replace YOUR_USERNAME and YOUR_PASSWORD with your user and password respectively. Then execute the following command:

分别用您的用户名和密码替换YOUR_USERNAMEYOUR_PASSWORD 。 然后执行以下命令:

ALTER USER'YOUR_USERNAME'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR_PASSWORD';

This will allow us to use a password when connecting to MySQL as the created user. Remember to change YOUR_USERNAME and YOUR_PASSWORD to your database user and password respectively.

这将允许我们以创建的用户身份连接到MySQL时使用密码。 切记分别将YOUR_USERNAMEYOUR_PASSWORD更改为您的数据库用户和密码。

Next, create a new database:

接下来,创建一个新的数据库:

CREATE DATABASE adonis_tasks;

Then we need to grant the new user privileges to the tables on the new database:

然后,我们需要向新数据库上的表授予新用户特权:

GRANT ALL ON adonis_tasks.* TO'YOUR_USERNAME'@'localhost';

For the changes to take effect, run:

为使更改生效,请运行:

FLUSH PRIVILEGES;

Finally, exit the MySQL server:

最后,退出MySQL服务器:

exit;

引入我们的应用程序 ( Pulling in Our Application )

With everything configured on our server, we can focus on our application. We’ll be pulling in our application to our server using git.

通过在服务器上配置所有内容,我们可以专注于应用程序。 我们将使用git将应用程序拉入服务器。

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://github.com/ammezie/adonis5-tasks.git

Once cloned, run the following command:

克隆后,运行以下命令:

cd adonis5-tasks
npm install

Just like any other TypeScript application, we need to compile the TypeScript file to JavaScript. In AdonisJs, this process is called build, which can be done using the command below:

与其他TypeScript应用程序一样,我们需要将TypeScript文件编译为JavaScript。 在AdonisJs中,此过程称为build ,可以使用以下命令完成:

node ace build

This will create a build directory containing the compiled TypeScript files.

这将创建一个包含编译的TypeScript文件的build目录。

By default, AdonisJs reads most of its configuration settings from environment variables, which are store inside a .env file located in the root of every AdonisJs application. This makes it easier to make use of different configuration settings depending on the environment (development, staging, production, etc.) the application is running.

默认情况下,AdonisJs从环境变量中读取大多数配置设置,这些环境变量存储在每个AdonisJs应用程序根目录下的.env文件中。 这使得根据应用程序正在运行的环境(开发,登台,生产等)使用不同的配置设置变得更加容易。

Let’s create the .env file:

让我们创建.env文件:

touch .env

Once the file is created, we need to generate an APP_KEY for our application:

创建文件后,我们需要为我们的应用程序生成一个APP_KEY

node ace generate:key

This will output a random string, which we’ll copy and add inside the .env file. Open .env:

这将输出一个随机字符串,我们将在.env文件中复制并添加该.env 。 打开.env

vim .env

and paste the following into it:

并将以下内容粘贴到其中:

// .env

PORT=3333
HOST=127.0.0.1
NODE_ENV=production
APP_KEY=YOUR_GENERATED_KEY_COPIED_FROM_ABOVE
DB_CONNECTION=mysql
DB_HOST=localhost
DB_NAME=adonis_tasks
DB_USER=YOUR_DATABASE_USERNAME
DB_PASSWORD=YOUR_DATABASE_PASSWORD

Remember to update YOUR_DATABASE_USERNAME and YOUR_DATABASE_PASSWORD with your database details from the previous section.

请记住使用上一部分中的数据库详细信息更新YOUR_DATABASE_USERNAMEYOUR_DATABASE_PASSWORD

Since we have updated the .env file, we need to rebuild our application:

由于我们已经更新了.env文件,因此我们需要重建应用程序:

node ace build

Now, we can run the migration:

现在,我们可以运行迁移了:

node ace migration:run --force

Because we are on production, we have to use the --force flag, otherwise, the migration will not run.

因为我们正在生产中,所以必须使用--force标志,否则,迁移将不会运行。

Now, we can start our application. For this, we’ll be making use of PM2, which will start our application and restart it whenever it crashes:

现在,我们可以启动我们的应用程序了。 为此,我们将使用PM2,它将启动我们的应用程序并在崩溃时重新启动它:

pm2 start build/server.js

将Nginx设置为反向代理服务器 ( Setting Up Nginx As A Reverse Proxy Server )

We already have Nginx installed and configured, let’s set it up as a reverse proxy, which will allow us to access our 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

Open the default Nginx server configuration file:

打开默认的Nginx服务器配置文件:

sudo vim /etc/nginx/sites-available/default

and update the server block as below:

并更新server块,如下所示:

// /etc/nginx/sites-available/default

server_name DOMAIN_NAME_OR_IP_ADDRESS;

location / {
    proxy_pass http://localhost:3333;
    proxy_http_version 1.1;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

We set the server_name to that of our server IP address or domain name (if it’s set up). It is important to set proxy_pass to that of our application URL (in our case, http://localhost:3333).

我们将server_name设置为服务器IP地址或域名(如果已设置)。 将proxy_pass设置为应用程序URL的URL(在我们的示例中为http://localhost:3333 )很重要。

To make sure there are no errors in the configuration, run the command below:

要确保配置中没有错误,请运行以下命令:

sudo nginx -t

Then we can restart Nginx for our changes to take effect:

然后,我们可以重新启动Nginx来使更改生效:

sudo service nginx restart

Now, we should be able to access our application with our server’s IP_ADDRESS. If a domain name has been set up for our server, then we should be able to access our application using the domain name as well.

现在,我们应该能够使用服务器的IP_ADDRESS访问我们的应用程序。 如果已经为我们的服务器设置了域名,那么我们也应该能够使用该域名访问我们的应用程序。

You should get something similar to the image below:

您应该获得类似于下图的内容:

Application demo

结论 ( Conclusion )

In this tutorial, we have seen how to deploy an AdonisJs 5 application to DigitalOcean. Also, we saw how easy it is to spin up a new server with DigitalOcean’s 1-Click App.

在本教程中,我们已经看到了如何将AdonisJs 5应用程序部署到DigitalOcean。 此外,我们看到了使用DigitalOcean的1 - Click App启动新服务器是多么容易。

翻译自: https://scotch.io/tutorials/deploying-adonisjs-5-to-digitalocean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值