Home >Web Front-end >Front-end Q&A >lnmp install nodejs
In modern web development, front-end technology is receiving more and more attention, and many developers will use Node.js as part of the front-end building tool. Since LNMP has become very popular, for the convenience of developers, this article will introduce how to install Node.js in an LNMP environment.
LNMP The first step to install Node.js is to install Nginx first. Nginx is a high-performance web server that can handle a large number of concurrent requests and has excellent reverse proxy and load balancing functions.
In Ubuntu system, you can use the following command to install Nginx:
sudo apt-get update sudo apt-get install nginx
After the installation is completed, you can use the following command to start Nginx:
sudo systemctl start nginx
If everything goes well, you can Enter the server's public IP address in the browser and see the Nginx welcome page.
Since Node.js is not part of the official Ubuntu repository, a PPAs (Personal Package Archives) repository needs to be added.
PPAs allow the installation of unofficial software repositories so that users can access specific software packages. The latest version of Node.js is available from the official Node.js PPA repository.
To add the PPA source of Node.js, please use the following command:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
This command will download the PPA source file to the local and add a new software package source to the system.
Next, use the following command to install Node.js:
sudo apt-get install -y nodejs
After the installation is complete, you can use the following command to check the Node.js version:
node -v
Node.js Package Manager (NPM) is a package management tool that can be used to install and manage Node.js modules. NPM is automatically installed along with Node.js, so no separate installation is required.
In order to confirm whether the NPM installation is successful, please use the following command to check the NPM version:
npm -v
Since our Node. js application will run on localhost and will not be bound to a public IP address by default, so we need to configure Nginx as a reverse proxy to proxy requests to the Node.js application.
Open Nginx's default site configuration file:
sudo nano /etc/nginx/sites-available/default
Add the following content under the server section:
location / { proxy_pass http://127.0.0.1:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
In the above code, we configure Nginx to proxy all requests to On local port 3000. This is also the port Node.js listens to by default. If your Node.js application listens on a different port, modify the proxy_pass URI accordingly.
Save and close the file. Then restart Nginx for the changes to take effect:
sudo systemctl restart nginx
Now our LNMP environment is ready and we can develop applications using Node.js program. In this tutorial, we demonstrate how to launch a simple Node.js application on the default 3000 port.
First, create an empty folder and go into it:
mkdir myapp cd myapp
Then, create a file called index.js containing the following code:
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World! '); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
The application The function is that when the client accesses the server, the "Hello, World!" message will be returned.
You can now launch the application using the following command:
node index.js
If all goes well, enter the server's public IP address and port 3000 into your browser and you should see "Hello, World!" message.
So far, this tutorial has introduced the complete process of installing Node.js in an LNMP environment, and demonstrated how to start a simple Node.js application on the default port. After following this tutorial, you will be able to continue learning Node.js-related technologies to improve your front-end development capabilities.
The above is the detailed content of lnmp install nodejs. For more information, please follow other related articles on the PHP Chinese website!