The following is copied by me on segmentfault and posted here as a reminder
Link: https://segmentfault.com/q/1010000007362524
Suppose you have a host, VPS, cloud host or something, then the big question steps are as follows:
-
SSH into this host and install node. The official website has a compiled linux version, download it and configure the path.
-
Then upload your code to this host, either with git or ftp.
-
Enter the project directory, run
npm install
, and make sure all dependent packages are installed. -
Install the deployment tools
npm i -g pm2
, as the case may be, if neededsudu
. -
In the project directory, run
sudo pm2 start bin/www
-
(Optional) Configure the firewall, if it is centos, the command is similar
firewall-cmd --add-port 3000/tcp
. -
The host generally has two IPs, an external network IP and an internal network IP. You can access your website directly by accessing the external network IP + port number. For example, the external network IP is
11.22.33.44
the port that the project is listening on, and3000
the browser input11.22.33.44:3000
is sufficient.
There are a few notable
-
Generally, a soft link is created
node
and the soft link is placedpath
under the environment variable. In this way, when the node is upgraded, there is no need to modify the path, just modify the symbolic link directly. -
Git is recommended. Write a script to automatically pull the code and deploy it.
-
According to the network situation, replace the source of npm with the source of Taobao
-
pm2 is just a tool to guard your application. If the application hangs, it will restart with 0 delay. There are many other tools, see which one you prefer.
-
Some cloud services will check when you open port 80. If there is no record, you cannot access through port 80. A web page prompting for filing will be displayed. If only to run up, just change the individual port.
-
If you are using the cloud, the internal and external network port numbers can generally be viewed in the host information, but can also be viewed without ifconfig.
-
Set NODE_ENV as appropriate. If you are using pm2, you can write a startup script to set whether to deploy for production or not.
off topic
-
Advanced once can use docker. But it doesn't feel necessary now.
-
It is best to install an nginx as a proxy.
-
Services such as ssh require security configuration. Disable account passwords and log in only with credentials.