Home  >  Article  >  Web Front-end  >  Server to build nodejs environment

Server to build nodejs environment

王林
王林Original
2023-05-28 11:28:38621browse

In modern web development, Node.js has become an indispensable component. As a JavaScript runtime environment, it allows us to write server-side applications through JavaScript. In this article, we will introduce how to set up a Node.js environment on the server.

  1. Preparation

Before we start, we need to make some preparations. First, you need a server with Internet access. Secondly, you need an account and password that can log in to the server using the ssh protocol. Finally, you need to choose a Linux distribution as the server's operating system.

In this article, we will use Ubuntu Server 18.04 LTS as an example for demonstration. If you are using another Linux distribution or Windows, there may be some differences, but most of the operations should be common.

  1. Install Node.js

Before starting to build the Node.js environment, we need to install Node.js and npm first. npm is the package management tool for Node.js. It can help us install, manage and upgrade various modules of Node.js.

First, we need to update the system’s package list:

sudo apt-get update

Then, we can install Node.js and npm:

sudo apt-get install nodejs npm

After the installation is complete, you can use the following command Check the versions of Node.js and npm:

node -v
npm -v

If the version number is output, it means that Node.js and npm have been successfully installed.

  1. Installing Nginx

To allow others to access our Node.js application, we need a web server, and Nginx is very suitable at this time. Nginx is a high-performance HTTP and reverse proxy server that can be used to host static files, handle HTTP requests, and forward requests to other servers.

Installing Nginx is very simple:

sudo apt-get install nginx

After the installation is completed, you can use the following command to start the Nginx service:

sudo systemctl start nginx

We can also use the following command to confirm whether Nginx has started :

sudo systemctl status nginx

If the word "active (running)" is output, it means that Nginx has run successfully.

  1. Writing Node.js Application

Now that we have successfully installed Node.js and Nginx, we can start writing our Node.js application. Here we take a simple example to create an HTTP server and return a static "Hello World" page.

Create a file called "app.js" and copy the following code into the file:

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!
');
});

server.listen(8000, () => {
  console.log('Server running at http://localhost:8000/');
});

This application creates an HTTP server listening on port 8000. When a request is received, it will return an HTTP status code of 200 (meaning "OK"), an HTTP response header named "Content-Type" and a response body with the content "Hello, World!".

Next, we can use the following command to run the application:

node app.js

If everything goes well, the console will output "Server running at http://localhost:8000/" like this Information.

Now, we can access the application in the browser with the URL "http://localhost:8000/". If everything goes well, you will see a page that says "Hello, World!"

  1. Use pm2 to manage Node.js applications

Although we can directly use Node.js to run applications, this method is not suitable for use in a production environment . For better management and monitoring of applications we can use pm2.

pm2 is an open source Node.js application manager that can help us manage the life cycle of multiple applications, including starting, stopping, restarting and monitoring. It also provides various features such as auto-restart, cluster mode, load balancing, logging, and more.

Installing pm2 is very simple:

sudo npm install -g pm2

After the installation is complete, we can use the following command to start our application:

pm2 start app.js

This will start an application called "app" application and runs in the background. We can use the following command to view the status of the application:

pm2 status

If everything is OK, the console will output information similar to the following statement:

┌─────┬─────────┬───────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ id  │ name    │ namespace │ state │ pm2 id │ version │ mode   │ pid │ uptime    │ restarts │
├─────┼─────────┼───────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ 0   │ app     │ default   │ online│ 0      │ 0.0.0   │ cluster│ 7842│ 0s        │ 0        │
└─────┴─────────┴───────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘

This means that our application It has started successfully and is running in cluster mode in the background. Now, we can use Nginx to forward HTTP requests to the application running on port 8000.

  1. Configuring Nginx

Now, our application is ready to run on localhost:8000, but we need to make the application accessible to others. At this time, we need to use Nginx to forward the HTTP request to port 8000.

First, we need to edit the Nginx configuration file. Open the default configuration file using the following command:

sudo nano /etc/nginx/sites-available/default

Add the following content at the end of the file:

location / {
  proxy_pass http://localhost:8000;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

This will forward all HTTP requests to the localhost:8000 address.

Next, we need to restart the Nginx server:

sudo systemctl restart nginx

Finally, use the following command to see if the application is running properly:

pm2 status

Now, we can Enter the IP address of the server and you will see our application.

Summarize

In this article, we introduce how to set up a Node.js environment on the server. We installed Node.js and npm, used Nginx to host static files and forward HTTP requests, created a simple Node.js application, and used pm2 to manage the life cycle of the application. Although there may be some fine-tuning in different environments, these steps should help you quickly set up a Node.js environment on the server and run your application.

The above is the detailed content of Server to build nodejs environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use jquery timerNext article:How to use jquery timer