Recently, I got the cheapest Tencent cloud server, mainly used to deploy my personal blog and learn some Linux knowledge, so as to fully understand and master Web technology. It is highly recommended that the front-end staff have their own server. Before writing this article, MY cloud server has installed Mysql as well as Node, nginx, etc. The following uses node website deployment as an example to explain some common scenarios
-
Thread persistence
In general, we start a server with Node index.js on the Window CMD, and as long as we don’t close it, we can always access and call the interface. On Linux, however, if you do nothing for a long time or need to perform other operations, your Node service will be disconnected and users will no longer be able to access your site. How to do? We can fix this by installing the Forever module.
NPM install forever -g // Install forever module globallyCopy the code
Change the startup mode from node index.js to forever start index.js
Forever List // Lists all currently running services forever start -w index.js // File changes automatically restart forever Stopall // Stop all services forever Stop app.js // Stop one of the Node apps forever Stop [id] // forever List find the corresponding ID, and thenCopy the code
Of course, there are many commands, you can consult the related modules. In general, the easiest way to use it is:
forever start index.js
Copy the code
This way, even when we switch to another Linux path or exit, the Node service will still exist, meaning that others will still be able to access your site.
-
Configure nginx
Nginx is a reverse proxy server developed by the Russians and now used by many companies around the world. For the introduction and installation of nginx, you can check out the information or go to the rookie tutorial for a quick study. Here I have Nginx installed on Linux. Now I’m going to use nginx to proxy http://localhost:8089, which I just started with forever Start index.js, via my domain name to access http://localhost:8089. View the nginx configuration file path
find / -name nginx.conf
Copy the code
Switch inside to modify inside configuration
server {
listen 80; Set the listening port to 80
server_name hellocode.xyz; # Enter the domain name to go to http://localhost:8089include /etc/nginx/default.d/*.conf; Location / {proxy_pass http://118.89.33.75:8089;# Your Node website application
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
Copy the code
Exit to check whether the configuration is correct
nginx -t
Copy the code
Reload nginx
nginx -s reload
Copy the code
Open the browser and enter the url to access the website!
Cross-domain problems may be encountered in the access process, so it is necessary to set the Node side to support cross-domain. For express framework, a custom middleware in index.js
var allowCors = function(req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Methods'.'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers'.'Content-Type');
res.header('Access-Control-Allow-Credentials'.'true'); next(); }; app.use(allowCors); // Use cross-domain middlewareCopy the code
More on cross-domain issues in a later blog!