首页 > 系统相关 >13_How to Deploy NodeJs app on Ubuntu in Production

13_How to Deploy NodeJs app on Ubuntu in Production

时间:2023-06-07 22:25:25浏览次数:57  
标签:13 NodeJs app sudo proxy install using Copy

 

地址:https://www.codewithharry.com/blogpost/deploy-nodejs-app-on-ubuntu/

 

How to deploy a Node.js application in production

In this post, we will see how to run and deploy NodeJS apps in production. Follow the steps below:

Step 1 - Install Nodejs

Let's download nodejs from Nodesource. NodeSource is a company which provides enterprise-grade Node support and maintains a repository containing the latest versions of Node.js.

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Let's install nodejs now:

sudo apt-get install -y nodejs

Check the installation of node and npm using the following commands:

node --version && npm --version

You will see the versions of nodejs and npm

Step 2 - Creating a sample nodejs file

Let's create a sample app and paste some basic code into it.

sudo vi app.js

Paste the following code inside it:

const express = require('express')
const app = express()
const port = 3000

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

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Lets now install express so that we can run this app server:

npm install express

run the application

node app.js

You should now be able to see the hello world page when you visit http://server-ip:3000

Step 3 - Using pm2 as a process manager

Let's install and use pm2 as a process manager. Install pm2 using the commands below:

sudo npm i pm2 -g

Start the application using the following command:

pm2 start app.js

Step 4 - Configuring Nginx as a reverse proxy

Now let's configure Nginx as a reverse proxy. This will help us get the security features from Nginx. Also, we can serve static content using Nginx.

Let's install Nginx using the following command:

sudo apt install nginx

Let's create a conf file for our Nodejs app using the command below

sudo vi /etc/nginx/sites-available/nodeApp

Copy the following content to this file

server{
  server_name 165.232.177.116;

      location / {
        proxy_pass http://localhost:3000;
        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;
    }
}

Activate this configuration using the command below:

sudo ln -s /etc/nginx/sites-available/nodeApp /etc/nginx/sites-enabled

Visit http://your-ip/ and your application should work fine. Happy coding!

 

 

 

 

标签:13,NodeJs,app,sudo,proxy,install,using,Copy
From: https://www.cnblogs.com/hechunfeng/p/17464732.html

相关文章

  • 12_How to deploy Flask apps on Ubuntu VPS Using gunicorn and Ngnix
      地址:https://www.codewithharry.com/blogpost/flask-app-deploy-using-gunicorn-nginx/ HowtodeployflaskapponUbuntuVPSusingNginxandgunicornInthispost,wewillseehowtodeployflaskapplicationsusinggunicornWSGIserverandnginxasarev......
  • 10_How deploy a Django application using Nginx & Gunicorn in Production
     地址:https://www.codewithharry.com/blogpost/django-deploy-nginx-gunicorn/  HowtohostDjangoApplicationusinggunicorn&nginxinProductionInthispost,wewillseehowtousenginxwithgunicorntoservedjangoapplicationsinproduction. Dj......
  • [刷题笔记] Luogu P3073 [USACO13FEB]Tractor S
    ProblemSolution和汽车拉力比赛差不多,思路都是二分,二分\(d\),但是汽车拉力比赛从一个路标开始搜即可,本题没有给定起点。一条合法路径起点是未知的,不得随便从一个点开始搜,否则可能找不到正确路径。怎么处理呢?容易想到对于每一个二分的\(d\),开一个\(n^2\)的循环,从每一个点开始搜......
  • 【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
    问题描述在创建AzureAppService时,服务端的配置使用Java8+Tomcat8.5。默认的根目录页面显示出AppServiceTomcat版本信息,存在一定的安全隐患。如何来避免这个问题呢? 问题解答因为在初始创建AppService时,Azure会根据所选Stack,WebServer的信息默认生成首页内容。大多是情况......
  • Appium模拟登录微信
    fromappiumimportwebdriverfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.common.byimportByfromselenium.webdriver.supportimportexpected_conditionsasECserver='http://localhost:4723/wd/hub'desired_c......
  • flutter 的 in_app_web_view实现下载功能
    flutter与前端交互,利用in_app_web_view实现下载功能:首先下载库,终端输入flutterpubaddflutter_inappwebview之后导出import'package:flutter_inappwebview/flutter_inappwebview.dart';即可使用。创建in_app_web_view:InAppWebView(initialOptions:InA......
  • LDGRB-01 3BSE013177R1 ABB模块 DCS S800I/O 全新
    LDGRB-013BSE013177R1ABB模块DCSS800I/O全新LDGRB-013BSE013177R1ABB模块DCSS800I/O全新 什么是仪器仪表中的基金会现场总线?与传统概念相比,现场总线有哪些优势?什么是仪器仪表中的基金会现场总线? 基础现场总线是一种串行、双向、全数字网络,用作工厂或工厂自动......
  • uniapp render.js
    1、操作数据<template><viewclass="content"><view@click="renderScript.emitData">获取renderjs数据</view>{{name}}</view></template><script>exportdefault{data(){......
  • app默认开机自启动/不自启动
    需求:将应用添加到应用白名单,应用开机后将会在后台自启动,黑名单则相反修改内容:将对应apk包名添加到黑/白名单array数组当中修改文档:vendor/mediatek/proprietary/packages/apps/DuraSpeed/res/values/config.xml 其它内部逻辑代码:vendor/mediatek/proprietary/packages/apps......
  • 字节技术面都过了,薪资都谈好了20K*13结果还是被刷了,问HR,原因是。。
    面试被拒开端分享下自己的求职小故事。在一家公司软件测试技术面试已经过了,然后和最终面试官沟通了下,面试官提出来一个薪资数字,我接受了这个提议并和hr同步了这个数字。再然后被拒了,理由就是期望薪资和职级不匹配。我询问后有邮件回复我为什么面试官和面试的地区公司hr说了不算。......