How to run nodejs on the server

PHPz
Release: 2023-04-26 09:58:52
Original
1255 people have browsed it

With the continuous development of Internet technology, the scalability of websites and applications has become one of the key factors for the growth of institutions and enterprises. The emergence of Node.js has made server-side development more efficient and flexible. Today we will discuss how to run Node.js on the server

  1. Installing Node.js

First, you need to install Node.js on your server. You can go to the Node.js official website to download the binary file and install it, or you can use the package manager to install it. For example, on Debian/Ubuntu, you can use the following command to install:

sudo apt-get update
sudo apt-get install nodejs
Copy after login
  1. Install build tools

Make sure you have the build tools installed on the server. Because Node.js packages may need to be compiled from source, you need to install some build tools on the server. On Ubuntu, you can install it using the following command:

sudo apt-get install -y build-essential
Copy after login
  1. Create your Node.js application

We are now ready to run Node.js on the server Applied. However, there are some considerations when putting your application on the server, such as how to manage your application's dependencies and configuration files. To simplify these processes, a good option is to use npm, the most popular package manager for Node.js.

Suppose you have written your Node.js application and you want to start it on the server. You can run the following command to ensure that your application code has been submitted to the code repository or server:

git clone <repository-url>
Copy after login

Alternatively, upload your code locally to the server:

scp -r /path/to/your/app <user>@<server-ip>:/path/to/remote/location
Copy after login

Next, You need to go into your application directory and install your dependencies:

cd /path/to/your/app
npm install
Copy after login
  1. Configure your application development environment

In your production environment, you will need a A tool that can automatically restart your application to ensure stable operation 24 hours a day. A tool called PM2 is a popular option. PM2 is a Node.js process management tool that allows you to easily monitor your application and helps you automatically restart the application to ensure that it always remains stable while running.

Assuming you have installed PM2 globally, you can start your Node.js application in the background using the following command:

pm2 start app.js
Copy after login

Now, your application is running on the server, and will run with the server up and running. You can stop and restart it using the following commands:

pm2 stop app.js
pm2 restart app.js
Copy after login
  1. Using Nginx

Nginx is a popular web server that is widely used for Node.js applications deploy. Nginx allows you to use a reverse proxy to serve multiple applications through a single port. So while you may have multiple Node.js applications running, you only need to focus on one port. Nginx also provides some additional features such as load balancing and caching.

Configuring a reverse proxy on Nginx is easy. Assuming your Node.js application is running on port 3000 on localhost, you can configure Nginx to pass all traffic from a specific domain to port 3000.

You can install nginx using the following command:

sudo apt-get install -y nginx
Copy after login

Then, you need to edit the Nginx configuration file to define your application and reverse proxy rules. The Nginx configuration file is usually located in /etc/ngnix/sites-enabled/default or /etc/ninx/nginx.conf. Find the server module and add the following content to it:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Copy after login

Save and reload the Nginx configuration file:

sudo service nginx reload
Copy after login

Now your Node.js application can be accessed through the Nginx reverse proxy for a visit.

In this article, we explored how to run Node.js applications on the server. The process includes installing Node.js, using npm to install application dependencies if installed globally, using PM2 to keep the application running, and how to use Nginx for reverse proxying and load balancing. Please note that this is only an entry-level tutorial. In practical applications, you need to consider many factors, such as security and performance, and configure your server to enhance your application.

The above is the detailed content of How to run nodejs on the server. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!