When node was developed, it was developed for the server locally. Where should it be put and how to start the service after the development is completed
First of all, I assume that the application of the subject has been running normally locally and the function test has been completed. The problem now is how to make the application run “online”.
In fact, the problem of the subject is that he is not familiar with some concepts, so long as he is familiar with them, there will be no doubt.
Web applications need to run on real servers, which is what you call “where to put them.”
The server is basically a computer. How do you run the application locally and how do you run it on the server?
Log in to your server through a command line tool (Mac terminal or Windows CMD) and it will be basically the same as on your local computer.
However, if you want to run an application on your own, you have to know something else besides the application itself.
Server You can choose Cloud Server (ECS) of Alibaba Cloud or Meituan-Dianping Cloud. The cheapest configuration is about 100 yuan a month. For cloud servers, you can almost treat your computer as if it were your own. Meituan-Dianping is a little cheaper than Ali.
You can also choose a virtual host, the price is cheap, but the authority is limited, which is equivalent to using someone else’s computer. I haven’t used the virtual host, so I won’t say much.
Choosing a server is like buying a computer. You have to “buy back” the computer first.
When buying a server, you need to choose a system, usually Linux. However, you will see CentOS, Ubuntu, Debian, etc. These are different “versions” of Linux, which are similar to each other. Do not choose Windows.
You can choose what you are familiar with, or you can make a decision after you know their advantages and disadvantages. I always use CentOS, the latest version is CentOS7 64 bit.
Finally, you will have a server with Linux installed. Usually you will have a server IP and a set of administrator user names and passwords to log in to the server. All your operations on the server need to be performed after logging in.
The command to log in to the server through the terminal is
ssh firstname.lastname@example.org. This command lets you log in to the server as root.
To run a fully accessible application, you probably need the following software:
Nginx: Reverse proxy server, used to process user’s access request.
npmYou should be familiar with managing all kinds of library packages you need.
Database: Install the database you are using, perhaps MongoDB or MySQL.
Now you need to upload the local application to the server. I didn’t mention the hard disk on the server earlier. If you choose a server in Alibaba Cloud or Meituan-Dianping Cloud, there will usually be two hard disks, one system disk, about 25G, and the other data disk, the capacity of which is your own choice.
The server in Meituan-Dianping already loaded the data disk when it gave it to you, that is
/dataDirectory, Alibaba Cloud may need to manually load the hard disk, it is recommended to also load
/dataDirectory, easy to manage.
The most primitive way to upload is FTP, which uploads your application folder to the server.
node_modulesDon’t upload the folder, there is a big pile of various libraries. We will install them directly on the server later.
However, Git is now commonly used for uploading. For example, I usually push it to Github locally and the server retrieves it from Github. How to use Git requires you to study alone.
Now suppose you have uploaded the application and passed it
npm installThe command installs all the required libraries, so the application can now run. The operation method is the same as yours. Log in to the server in the “terminal” inside, enter the application folder, and run
Start the application in this way and the application will stop when you close the terminal. So you need a tool-PM2.
npm install -g pm2After installation, pass
pm2 start yourApp.jsTo start the application. Please read the documentation about the use of pm2, which is very simple.
Here is a problem that needs your attention: environment. Usually the application runs in the “development” environment when developing locally, but it runs in the “production” environment on the server. The operating environment is specified at startup, for example
NODE_ENV=production pm2 start yourApp.jsIt is to tell pm2 to start the application in the production environment.
The difference between the two environments is that you write your own code. For example, the development environment uses one set of user name and password to connect the database, the production environment uses another set, or some libraries are only loaded in the development environment, and they do not need to be loaded in the production environment.
Now, you can access the application through < your server ip>:<your app port >. For example, suppose you access the application locally by
localhost:3000, then now put
localhostChange to your server IP.
Domain name and Nginx
The above access address is too ugly to be used by the users. This is what you need to have a “domain name” and parse it into your server.
For registering domain names and setting up DNS resolution, you need to learn separately.
After you have the domain name, you can access the application through < domain name >: < port >, which is not enough. Usually users only access the service through the “domain name”, which is actually the visited HyperText Transport Protocol (your application may be on port 3000), so we need Nginx to forward the user’s request to visit HyperText Transport Protocol to port 3000.
What you need to do is modify Nginx’s configuration file to realize this function. How to set up Nginx port forwarding, you also need to learn separately.
Purchase server (or virtual host);
Install necessary software (Nginx, Node.js, npm, database, etc.);
Dependencies required for installing applications;
Install pm2 and start the application with pm2.
Set up domain name resolution and Nginx so that users can access applications through domain names.
Things to Learn Alone:
Purchase servers and systems
Basic Linux Commands and Operations
Use of pm2
Purchase and Analysis of Domain Names
Nginx usage and settings
Things to Expand Learning:
SSH Key (Password-Free Login to Server)
Git upload and version management
So far, you can run a basic application.
I only do the development myself in my spare time. I grope for the above, and maybe there are many unprofessional places or operations. I’ve also been looking for best practices and trying my best to improve my work. I welcome expert comments.
If you have any questions about the details, please leave a comment. Typing is not easy, if it helps Like For Likes to adopt it. Thank you