Deploy a simple Node service

CD ~ create index.js file

const http = require('http');
const hostname = '0.0.0.0';
const port = 7500;
const server = http.createServer((req, res) => { 
 res.statusCode = 200;
 res.setHeader('Content-Type', 'text/plain');
 res.end('Hello World\n');
}); 
server.listen(port, hostname, () => { 
 console.log(`Server running at http://${hostname}:${port}/`);
});
Copy the code

In the code above we create a Node project with hostname 0.0.0.0, port 7500, and start an HTTP service that returns’ Hello world’ when accessing the host. Run Node index.js to start the project

kim@VM-0-12-ubuntu:~$node index.js Server running at http://0.0.0.0:7500/Copy the code

So you can open up another terminal and use CRUL and see what comes back

kim@VM-0-12-ubuntu:~$curl http://0.0.0.0:7500/ Hello WorldCopy the code

You can also access the following addresses in your local browser to check whether the project is running properly

http://public IP address of the cloud server instance :7500Copy the code

Start the service with pM2

When you exit the Index project and use the browser to access the same address, you getThis site can’t be reached

Since the service on the cloud server has stopped, that’s why PM2 is used to manage the Node threads for us. Run pm2 start index.js

kim@VM-0-12-ubuntu:~$ pm2 start index.js [PM2] Starting /home/kim/index.js in fork_mode (1 instance) [PM2] Done. ┌ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ id name │ mode │ ↺ │ status │ │ CPU The memory │ ├ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┤ │ │ 0 index │ │ │ fork 0 online │ 0 │ 24.9 MB │Copy the code

Now you can see Hello World again.

Use the nginx proxy port

The default browser is port 80 (HTTP) and port 443 (HTTPS). Since the port of our project is 7500, we must specify port 7500 when using browsing access to normally access the resources we need. To omit this step, we need nginx to do a reverse proxy.

Install nginx

Nginx. conf CD /etc/nginx/conf.d/ vi test.conf # Nginx -t start nginx systemctl start nginx.service # stop nginx systemctl stop nginx.service # Reload nginx service systemctl restart nginx.service # reload nginx configurationCopy the code

Access after installation is completehttp:// public IP, see the following page,This site can’t be reachedmissing

Take a look at the files in /etc/nginx

kim@VM-0-12-ubuntu:/etc/nginx$ ls
conf.d        fastcgi_params  koi-win     nginx.conf    scgi_params      sites-enabled  uwsgi_params
fastcgi.conf  koi-utf         mime.types  proxy_params  sites-available  snippets       win-utf
Copy the code

Site-available, site-enabled, and nginx.conf Nginx. conf contains the configuration of site-enabled.

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
Copy the code

So you can change the /etc/nginx/sites-available/default parameter and reload the nginx configuration with sudo nginx -s reload. You can also change nginx.conf directly, but the structure is as follows

http {
	server {}
}
Copy the code

/etc/nginx/sites-available/default , edit finished and exit. Sudo nginx -t tests whether the configuration is correct systemctl restart nginx restarts the nginx

kim@VM-0-12-ubuntu:/etc/nginx/sites-available$ sudo vi default [sudo] password for kim: kim@VM-0-12-ubuntu:/etc/nginx/sites-available$ systemctl restart nginx ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to restart 'nginx.service'. Multiple identities can  be used for authentication: 1. ubuntu,,, (ubuntu) 2. ,,, (kim) Choose identity to authenticate as (1-2): 2 Password: ==== AUTHENTICATION COMPLETE === kim@VM-0-12-ubuntu:/etc/nginx/sites-available$Copy the code

When accessing a public IP address, you do not need to add a port number

In addition to proxy_pass, some configuration is usually added

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
    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_set_header Connection "";
    proxy_set_header Cookie $http_cookie;
    proxy_pass  http://0.0.0.0:7500;
}
Copy the code

Deploy the local Node project

Normally we would deploy a local project to a cloud server, not write a project on a cloud service. This requires pushing the code to the cloud server. You can use PM2 for automatic deployment, but let’s do it manually. Now create a new API directory under the /var/www directory of the server. Sudo CHmod 777 API

I have a local project node_API, which is a node+mongoDB learning project, MDN express+ mongoDB practice, can be used to learn.

Terminal to switch to the root directory of the project, and then execute sudo SCP – r – VVV * [email protected]: / var/WWW/API

If the login fails, change PasswordAuthentication to true in /etc/ssh/sshd_config

Wait until the upload is complete and have a look at the current API directory

kim@VM-0-12-ubuntu:/var/www/api$ ll total 112 drwxrwxrwx 9 root root 4096 Feb 2 17:49 ./ drwxr-xr-x 4 root root 4096 Feb 2 then.. / -rw-r--r-- 1 kim kim 0 Feb 2 17:43 access.log -rw-r--r-- 1 kim kim 2189 Feb 2 17:43 app.js drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 bin/ drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 controllers/ drwxr-xr-x 2 kim kim 4096 Feb 2 17:43 models/ drwxr-xr-x 159 kim kim 4096 Feb 2 17:49 node_modules/ -rw-r--r-- 1 kim kim 127 Feb 2 17:49 nodemon.json -rw-r--r-- 1 kim  kim 52728 Feb 2 17:49 package-lock.json -rw-r--r-- 1 kim kim 578 Feb 2 17:49 package.json -rw-r--r-- 1 kim kim 8262 Feb  2 17:49 populatedb.js drwxr-xr-x 5 kim kim 4096 Feb 2 17:49 public/ drwxr-xr-x 2 kim kim 4096 Feb 2 17:49 routes/ drwxr-xr-x 2 kim kim 4096 Feb 2 17:49 views/ kim@VM-0-12-ubuntu:/var/www/api$Copy the code

Pm2 start bin/ WWW

kim@VM-0-12-ubuntu:/var/www/api$ pm2 start bin/www [PM2] Starting /var/www/api/bin/www in fork_mode (1 instance) [PM2] Done. ┌ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ id name │ namespace │ version │ mode │ │ pid uptime │ ↺ │ status │ │ CPU mem │ user │ watching │ ├ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ ─ ───┤ │ 0 │ index │ default │ N/A │ fork │ 26826 │ 2h │ 0 │ online │ 0% │ 40.0 MB │ Kim │ disabled │ 1 │ WWW │ default │ 0.0.0 │ fork │ 10733 │ 0s │ 0 │ online │ 10.0 MB │ Kim │ disabled │ └ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ Kim @ - 0-12 - ubuntu VM: / var/WWW/API $Copy the code

You can see that in addition to the previous Index project, there is now a WWW project. Browser accesshttp://49.235.172.41:3000/catalog

Sudo vi /etc/nginx/sites-available/default

location /catalog {
    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_set_header Connection "";
    proxy_set_header Cookie $http_cookie;
    proxy_pass  http://127.0.0.1:3000;
}
Copy the code

Systemctl restart nginx for the configuration to take effect.

subsequent

Things you can do later

  • Deploy a static page
  • Automatic PM2 deployment is implemented
  • Optimize deployment by referring to Express deployment best practices

reference

Pm2 tutorial How To Set Up a Node.js Application for Production on Ubuntu 16.04