Home >Web Front-end >Front-end Q&A >Ubuntu deploys nodejs website
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
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
After checking whether Node.js has been installed and configured correctly, you can check using the following command:
node -v
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
To verify that npm is successfully installed, enter the following command in the terminal:
npm -v
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:
mkdir my-node-app cd my-node-app
npm init
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.
touch server.js
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}/`); });
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
--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}/`); });
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
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
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; } }
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
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!