In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to deploy a Node.js application using pm2 and Nginx in Ubuntu. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
* * step-install Node.js LTS
In this guide, we will start our experiment from scratch. First, we need to install Node.js on the server. I will use Nodejs LTS version 6.x, which can be installed from the nodesource repository.
Install the python-software-properties package from the Ubuntu repository and add the "nodesource" Nodejs repository.
Sudo apt-get install-y python-software-properties curl-sL https://deb.nodesource.com/setup_6.x | sudo-E bash-
Install the * version of Nodejs LTS:
Sudo apt-get install-y nodejs
After the installation is complete, review the node and npm versions.
Node-v npm-v
Check the node.js version
Step 2-generate Express sample App
I will use the simple web application framework generated by the express-generator package for sample installation. Express-generator can be installed using the npm command.
Install express-generator with npm:
Npm install express-generator-g
-g: install the software package inside the system.
I will run the application as a normal user, not as a root or superuser. We first need to create a new user.
Create a user named yume:
Useradd-m-s / bin/bash yume passwd yume
Log in to the new user using the su command:
Su-yume
Next, generate a new simple web application with the express command:
Express hakase-app
Command creates a new project directory, hakase-app.
Using express-generator to generate Application Framework
Go to the project directory and install all the dependencies required by the application.
Cd hakase-app npm install
Then test and start a new simple application with the following command:
DEBUG=myapp:* npm start
By default, our express application runs on port 3000. Now the IP address of the access server is 192.168.33.10:
Express nodejs runs on port 3000
This simple web application framework now runs on port 3000 as a 'yume' user.
Step 3-install pm2
Pm2 is a node package that can be installed using the npm command. (with root privileges, if you are still logged in as yume, run the command exit to become root again):
Npm install pm2-g
Now we can use pm2 for our web application.
Enter the application directory hakase-app:
Su-yume cd ~ / hakase-app/
Here you can see a file called package.json, which is displayed with the cat command.
Cat package.json
Configure the express nodejs service
You can see that the start line has a command for nodejs to start the express application. We will use this command with the pm2 process manager.
Use the pm2 command to run the express application as follows:
Pm2 start. / bin/www
Now you can see results like this:
Run nodejs app using pm2
Our express application is running in pm2 with the name www,id 0. You can use the show option show nodeid | name to get more information about applications running under pm2.
Pm2 show www
Pm2 service status
If you want to see the log of our application, you can use the logs option. It includes access and error logs, and you can see the HTTP status of the application.
Pm2 logs www
Pm2 Service Log
You can see that our program is running. Now, let's make it boot itself.
Pm2 startup systemd
Systemd: Ubuntu 16 uses systemd.
You will see the message that you want to run the command with the root user. Use the exit command to go back to the root user and run the command.
Sudo env PATH=$PATH:/usr/bin / usr/lib/node_modules/pm2/bin/pm2 startup systemd-u yume-- hp / home/yume
It generates a systemd configuration file for launching the application. When you restart the server, the application will run automatically.
Pm2 add services to boot self-startup
Step 4-install and configure Nginx as a reverse proxy
In this guide, we will use Nginx as the reverse proxy for node applications. There is Nginx in the Ubuntu repository, install it with the apt command:
Sudo apt-get install-y nginx
Next, go to the sites-available directory and create a new virtual host profile.
Cd / etc/nginx/sites-available/ vim hakase-app
Paste the following configuration:
Upstream hakase-app {# Nodejs app upstream server 127.0.0.1 server 3000; keepalive 64;} # Server on port 80 server {listen 80; server_name hakase-node.co; root / home/yume/hakase-app; location / {# Proxy_pass configuration 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_max_temp_file_size 0; proxy_pass http://hakase-app/; proxy_redirect off; proxy_read_timeout 240s;}}
Save the file and exit vim.
In the configuration:
The node application runs using the domain name hakase-node.co.
All traffic from nginx is forwarded to node app running on port 3000.
Test the Nginx configuration to make sure there are no errors.
Nginx-t
Enable Nginx and make it boot automatically.
Systemctl start nginx systemctl enable nginx
Step 5-Test
Open your web browser and visit the domain name (mine is): http://hakase-app.co.
You can see the express application running in the Nginx web server.
Nodejs app runs in pm2 and Nginx
Next, restart your server and make sure your node app boots:
Pm2 save sudo reboot
If you log in to your server again, check the node app process. Run the following command as the yume user.
Su-yume pm2 status www
On Ubuntu how to use pm2 and Nginx to deploy a Node.js application is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.