Deploying a react app using pm2 and serve · LoginRadius Engineering

Yes, you have learned React and now you can develop a full-fledged front end application. The create-react-app helps you to set up and run a React project, including it code transpiling, basic linting, testing, and build systems. In short, you can start writing React code with minimal preparation. But once your application is done, it is time to deploy the same on the server, you are stuck and you will seek help from your backend or DevOps mates.

Wait!! Being a front end guy it seems to be difficult, but depolying your application on server is relatively easier than writing state management in redux.

In this post, we will learn how to deploy a React application on an Ubuntu 18.04 server using Node.jsserve, and pm2

pm2 is a process manager for the JavaScript runtime Node.js. This is an open-source daemon process manager that helps to manage and keep application 24/7

Prerequisites

To follow this tutorial, you’ll need the following:

  • Latest Node.js version installed on your computer or the Linux server where are you going to deploy the application.
    sudo apt-get update
    sudo apt-get install nodejs
    node -v or node –version
    npm -v or npm –version
  • create-react-app tool
    npm install -g create-react-app

    Deploying the app

    1.First of all, create the app using npx create-react-app

      npx create-react-app my-app

    2.Now you can run the app by running following command in the project directory root

      npm start

    3.The default react app will run in http://localhost:3000

    4.Now install serve and pm2 packages globally on the system/server

    npm install -g serve
    npm install -g pm2

    5.Since you are in the root directory of your project, run the following command to create a production build of your app. This will create a directory named build in the project root directory

    npm run build 

    6.Now we can run the following command to deploy the app

      pm2 serve <path> <port> --spa

    In our case, we can run the following command

    pm2 serve build 8082 --spa 

    PM2 can serve static files very easily with the pm2 serve feature. It supports serving raw files from a specified folder or you can serve a SPA (Single Page Application) with it.

Now you can see that your application is running on 8081 port while you have logged out from your ssh terminal of the browser.

  • Check the status of the application following command in the shell.
    pm2 list

Bonus: Below are some utility commands to manage the pm2 process

  • Specify the app name pm2 --name <app-name>
  • Delete all pm2 process pm2 delete all
  • Delete specific process pm2 delete <app-name>
  • Check the CPU and memory usage pm2 monit

Reference links :-

  1. Quick start pm2
  2. Process management with pm2

Source: Deploying a react app using pm2 and serve · LoginRadius Engineering

Deploying a react app using pm2 and serve · LoginRadius Engineering was last modified: October 16th, 2020 by Jovan Stosic

Set Up a Node.js App for a Website With Apache on Ubuntu 16.04

Introduction

Node.js is a JavaScript runtime environment which lets you easily build server-side applications. This tutorial will explain how to set up a Cloud Server running Ubuntu 16.04 so that Node.js scripts run as a service, and configure the Apache server to make the script accessible from the web.

Although Node.js scripts can be run from the command line using screen, running the scripts as a service using the process manager PM2 gives the scripts a more robust set of behaviors. When run as a service this way, the scripts will automatically restart if the server is rebooted or the script crashes.

PM2 is a process manager for Node.js, with a wide range of features you can use to control and manage your Node.js scripts. Visit the official PM2 website for more information on using PM2.

Requirements

  • A Cloud Server running Linux (Ubuntu 16.04).
  • A working domain name which points to the server.
  • Apache web server installed and running.

Install Node.js

Update your server’s packages and install curl with the following commands:

sudo apt-get update
sudo apt-get install curl
mixed

Download the Node.js PPA:

curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
mixed

Run the nodesource_setup.sh command to add the PPA to your server’s package cache:

sudo bash nodesource_setup.sh
mixed

Note: This script will update the server automatically. There is no need to run apt-get update a second time.

Install Node.js:

sudo apt-get install nodejs
mixed

This will automatically install npm as well.

Finally, install the build-essential package for npm:

sudo apt-get install build-essential
mixed

Create a Sample Node.js Application

For this example we will begin by creating a separate directory in your website’s document root for housing Node.js applications:

sudo mkdir /var/www/html/nodejs
mixed

Create the file hello.js in this directory:

sudo nano /var/www/html/nodejs/hello.js
mixed

Put the following content into the file:

#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (request, response) {
   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end('Hello World! Node.js is working correctly.\n');
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
mixed

Save and exit the file.

Make the file executable:

sudo chmod 755 hello.js
mixed

Install PM2

Use npm to install PM2 with the command:

sudo npm install -g pm2
mixed

Start the hello.js example script with the command:

sudo pm2 start hello.js
mixed

As root add PM2 to the startup scripts, so that it will automatically restart if the server is rebooted:

sudo pm2 startup systemd
mixed

Configure Apache

To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands:

sudo a2enmod proxy
sudo a2enmod proxy_http
mixed

Once the installation is complete, restart Apache for the changes to take effect:

sudo service apache2 restart
mixed

Next, you will need to add the Apache proxy configurations. These directives need to be inserted into the VirtualHost command block in the site’s main Apache configuration file.

By common convention, this Apache configuration file is usually /etc/apache2/sites-available/example.com.conf on Ubuntu.

Note: The location and filename of a site’s Apache configuration file can vary.

Edit this file with your editor of choice, for example with the command:

sudo nano /etc/apache2/sites-available/example.com.conf
mixed

Scroll through the file until you find the VirtualHost command block, which will look like:

<VirtualHost *:80>
ServerName example.com
    <Directory "/var/www/example.com/html">
    AllowOverride All
    </Directory>
</VirtualHost>
mixed

Add the following to VirtualHost command block:

ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location /nodejs>
      ProxyPass http://127.0.0.1:8080
      ProxyPassReverse http://127.0.0.1:8080
   </Location>
mixed

Be sure to put these lines outside any Directory command blocks. For example:

<VirtualHost *:80>
ServerName example.com

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location /nodejs>
      ProxyPass http://127.0.0.1:8080
      ProxyPassReverse http://127.0.0.1:8080
   </Location>

    <Directory "/var/www/example.com/html">
    AllowOverride All
    </Directory>
</VirtualHost>
mixed

Save and exit the file, then restart Apache for the changes to take effect:

sudo services apache2 restart`
mixed

After Apache restarts, you can test the application by viewing it in a browser. You should see the message, “Hello World! Node.js is working correctly.”

 

Source: Set Up a Node.js App for a Website With Apache on Ubuntu 16.04 – IONOS

Set Up a Node.js App for a Website With Apache on Ubuntu 16.04 was last modified: October 15th, 2020 by Jovan Stosic