10 minutes to build the CENTOS7+NODE.JS+NEST.JS+CADDY+PM2 online environment

Source: Internet
Author: User
Tags ssl certificate nodesource

CENTOS7+NODE.JS+NEST.JS+CADDY+PM2 installation and on-line environment configuration

node. JS Installation

Official Installation Instructions Address Https://github.com/nodesource/distributions

Execute command

curl -sL https://rpm.nodesource.com/setup_8.x | bash -
sudo yum install -y nodejs

At this point, the Noded.js installation is complete

Version view

node -v
npm -v

Nest.js Installation

Official Document Address Https://github.com/nestcn/docs/blob/master/5.0/firststeps.md

First install execution command

npm i -g @nestjs/cli

Create a project in the future just under the path you need to do

nest new project

Execute the following command to start the project directly under Dev test environment: Dev can restart automatically after modifying existing files

npm run start:dev

Browser input localhost:3000 can be accessed, port modification in src/manin.ts

Because node. JS environment, each project starts to occupy a port, so in order to implement multi-site, multi-domain binding access will need to use a Web service such as Nginx,caddy to do proxy here we use simple and powerful caddy to achieve

Installing Caddy

Official Document Address Https://caddyserver.com/tutorial

The install command is generated on the Https://caddyserver.com/download page based on your own server and the required plugins.

Without plug-ins, execute the following command directly

curl https://getcaddy.com | bash -s personal

After the installation is complete, you can start the static web site by executing the following command directly at the Web site root directory.

caddy

Browser input localhost:2015 can be accessed

The following is mainly for the Nest.js Project Multi-site situation configuration instructions

Create a Caddyfile file under the path you want

vi Caddyfile

The configuration content example below

For example your domain name is www.google.com, you need to access your localhost:6000 this project through the domain name, similarly, other projects are also configured

www.google.com {    proxy / localhost:6000}www.facebook.com {    proxy / localhost:6001}

Note: Remember the ' {' and '/' spaces in the configuration above, otherwise it will cause an error

After configuring, press ESC and enter: Wq Save exit

Then execute the following name to tell Caddy where your profile is, and load the startup

caddy -conf /路径/Caddyfile

You may be asked to enter your email address, which is caddy to configure the SSL certificate for your automatic application, and then you will see that the domain name you configured is output.
HTTP and HTTPS addresses, which can be accessed directly

The above configuration is complete, but after you restart the server, you will find that the site is inaccessible, the server to see you will find that node. js Web server and Caddy are not automatically started, but also need to manually perform the above steps in the Start command before the line, Then you need to use the powerful tool of PM2.

Installing PM2

Official Document Address https://pm2.io/doc/en/runtime/guide/installation/

Execute the following command to start the installation

npm install pm2 -g

A command installation is complete, isn't it simple?

The following use PM2 to start the Management node. JS Run project, here to nest.js development projects, this part because of limited data, can not find PM2 start Nest.js project description, because I was also the first contact PM2, a full toss 8 hours to fix, finally Google found a solution, as follows:

For example, if your nest.js project path is cd/data/www/project01, go to the project root and execute the following command

 pm2 start npm --name project01 -- start

A table with project online named project01 will appear after execution, indicating that the project started successfully through PM2, and if you have other projects you can do the same to start

After the project that you need to start the management through PM2 is started by PM2, perform the following power-on self-start commands to enable the boot from these projects

pm2 startup

Execute the following command to save the current list of apps

Is it convenient to start a managed project via PM2 after the server restarts automatically?
But there is a problem, because these projects are using the Caddy agent, so Caddy also need to implement boot-up, or the extranet still cannot access these items through your domain name, because we have installed PM2, His convenient and powerful features allow us to configure the SYSTEMD to enable Caddy boot, we execute the following command

pm2 start caddy --name caddyStart -- -conf /你的Caddyfile路径/Caddyfile

Through the above command we implemented the PM2 start Management Caddy, and then we still have to execute the boot command and save command, as follows

Restart your server, you will find that you have been able to access your site through the domain name that the project is bound to without any manual action, is it cool?

All the configuration operations are complete! You can monitor your server's projects with powerful PM2.

Attach the commands commonly used by PM2

$ pm2 list               # 显示所有进程状态$ pm2 monit              # 监视所有进程$ pm2 logs               # 显示所有进程日志$ pm2 stop all           # 停止所有进程$ pm2 restart all        # 重启所有进程$ pm2 reload all         # 0 秒停机重载进程 (用于 NETWORKED 进程)$ pm2 stop 0             # 停止指定的进程$ pm2 restart 0          # 重启指定的进程$ pm2 startup            # 产生 init 脚本 保持进程活着$ pm2 web                # 运行健壮的 computer API endpoint (http://localhost:9615)$ pm2 delete 0           # 杀死指定的进程$ pm2 delete all         # 杀死全部进程

There are students who like Kotlin add QQ group 26588774
There are students who like Nest.js add QQ group 732189938
There are students who like Golang add QQ group 598189791
There are students who like Laravel add QQ group 212423075

Welcome to the group to talk about beauty, talk about life, of course, Chat technology O (∩_∩) o

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.