Home > Web Front-end > Front-end Q&A > Ubuntu deploys nodejs website

Ubuntu deploys nodejs website

王林
Release: 2023-05-27 22:25:07
Original
1009 people have browsed it

Ubuntu is a popular open source operating system that many developers choose to deploy their web applications. One of the popular web development languages ​​is Node.js, which provides a fast and flexible way to build web applications. In this article, I will introduce how to deploy a Node.js website on Ubuntu operating system.

Step One: Install Node.js

First, you need to install Node.js. You can enter the following command in the terminal to install:

sudo apt-get update
sudo apt-get install nodejs
Copy after login

You also need to install the soft link of Node.js in order to run the node command in the terminal. Enter the following command:

sudo ln -s /usr/bin/nodejs /usr/bin/node
Copy after login

After checking whether Node.js has been installed and configured correctly, you can check using the following command:

node -v
Copy after login

If everything is fine, you should see Node.js version number.

Step 2: Install npm

npm is a package manager for Node.js that can easily manage the dependencies of web applications. To install npm, run the following command in the terminal:

sudo apt-get install npm
Copy after login

To verify that npm is successfully installed, enter the following command in the terminal:

npm -v
Copy after login

Step 3: Deploy Node on Ubuntu. js website

Now that you have Node.js and npm installed, the next step is to deploy your Node.js website on Ubuntu.

The first step is to create your Node.js application. If you haven't created a Node.js application yet, please create one by following these steps:

  1. Create a project folder and go into it:
mkdir my-node-app
cd my-node-app
Copy after login
  1. Initialize one New Node.js Application:
npm init
Copy after login

The npm init command will guide you to enter various details about your application such as name, version number, description, etc. Finally, you will have a configuration file included in the package.json file.

  1. Create the server file and start writing the code:
touch server.js
Copy after login

In the server.js file you can write your 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}/`);
});
Copy after login

Here In the code example, we created an HTTP server and listened to port 3000 on the server. When the server receives the request, it will return "Hello World" in the browser.

Next, you need to install Express, one of the most popular Node.js frameworks, using npm. In your project directory, run the following command:

npm install express --save
Copy after login

--save flag will automatically add dependencies in the package.json file.

Add the following code in the server.js file to build the application using Express:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
   res.send('Hello World!');
});

app.listen(port, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
});
Copy after login

In this example, we define a route using the app.get() method, which is the same as the root path matches, and sends "Hello World!" in response.

Step 4: Start the Node.js service

Now, your Node.js website is ready to be deployed on Ubuntu. In the terminal, go into your Node.js application folder and start the server:

cd my-node-app
node server.js
Copy after login

You can now use your web browser to visit http://localhost:3000 and you should see "Hello World! "information.

Step 5: Deploy the website to the production environment

Finally, in order to deploy your Node.js website in the production environment, you need to use reverse proxy software to connect the Node.js server to the web server. In this example we will use Nginx.

First, install Nginx:

sudo apt-get install nginx
Copy after login

Then, open the configuration file /etc/nginx/sites-available/default and add the following lines:

server {
   listen 80;

   server_name example.com;

   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;
   }
}
Copy after login

In this example, We set the name of the server to example.com and configure Nginx to proxy all requests from 127.0.0.1:3000.

Save and close the file, and restart the Nginx service:

sudo service nginx restart
Copy after login

Now you can access your Node.js website using your domain name.

Summary

In this article, we learned how to deploy a Node.js website on Ubuntu operating system. We installed Node.js and npm and wrote a simple Node.js application using the Express framework. Finally, we connect the application to the Nginx reverse proxy so that it can run in a production environment. Hope this article helps you deploy Node.js website on Ubuntu!

The above is the detailed content of Ubuntu deploys nodejs website. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template