<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Production Archives - Linux Windows and android Tutorials</title>
	<atom:link href="https://www.osradar.com/tag/production/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.osradar.com</link>
	<description>tutorials and news and Seurity</description>
	<lastBuildDate>Sat, 01 May 2021 16:09:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.13</generator>
	<item>
		<title>How to set up your NodeJS application for production</title>
		<link>https://www.osradar.com/set-up-nodejs-application-production/</link>
					<comments>https://www.osradar.com/set-up-nodejs-application-production/#respond</comments>
		
		<dc:creator><![CDATA[angeloma]]></dc:creator>
		<pubDate>Tue, 04 May 2021 04:06:00 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[Production]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[Ubuntu 20.04 tutorials]]></category>
		<guid isPermaLink="false">https://www.osradar.com/?p=29777</guid>

					<description><![CDATA[<p>Hello friends. It comes to this point in life, you are a NodeJS developer and you want to deploy your application in production, how to do it? Well in this post you will learn how to set up a NodeJS application for production on an Ubuntu 20.04 server. We have talked a lot about NodeJS [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.osradar.com/set-up-nodejs-application-production/">How to set up your NodeJS application for production</a> appeared first on <a rel="nofollow" href="https://www.osradar.com">Linux  Windows and android  Tutorials</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Hello friends. It comes to this point in life, you are a NodeJS developer and you want to deploy your application in production, how to do it? Well in this post you will learn how to set up a NodeJS application for production on an Ubuntu 20.04 server.</p>



<p><a href="https://www.osradar.com/tag/nodejs" target="_blank" rel="noreferrer noopener">We have talked a lot about NodeJS</a> in this blog because it is a new technology that allows us to create many complex web applications quickly.</p>



<p class="has-drop-cap">NodeJS is a technology that allows running Javascript on the server-side using Google’s V8 engine. It is a real revolution in the way web applications are developed because it reduces the asymmetry of client and server performance.</p>



<p>Many important forms such as Linkedin or eBay were created using this technology. This shows us the power of NodeJS.</p>



<p>An advantage of NodeJS is that its syntax is similar to that of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" target="_blank" rel="noreferrer noopener">Javascript</a> and therefore its learning curve decreases a lot.</p>



<h2 id="set-up-your-nodejs-application-on-ubuntu-20.04"><a name="set-up-your-nodejs-application-on-ubuntu-20.04" href="#set-up-your-nodejs-application-on-ubuntu-20.04"></a>Set up your NodeJS application for production on Ubuntu 20.04</h2>



<p>The first thing we have to do is to install NodeJS on the server where we want our application to be. To do this, we will use the 14.x branch, which is one of the most modern ones.</p>



<p>To do this, connect via SSH to your server and update the distribution</p>



<pre class="wp-block-preformatted">sudo apt update
sudo apt upgrade</pre>



<p>Now, add the NodeJS repository for the 14.x branch by downloading and executing the official script</p>



<pre class="wp-block-preformatted">curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh</pre>



<p>This will start the whole process and at the end, you will be able to check the version of NodeJS installed by running this command:</p>



<pre class="wp-block-preformatted">node -v
v14.16.1</pre>



<p>It is important that we also check the installed version of <code>npm</code>.</p>



<pre class="wp-block-preformatted">npm -v
6.14.12</pre>



<p>Now we have the software installed, let’s see an example of how to deploy our application.</p>



<p>To do this I will create a simple NodeJS file called <code>exmaple.js</code> that displays a <code>hello world</code>.</p>



<pre class="wp-block-preformatted">nano example.js
const http = require('http');

const hostname = 'localhost';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World. Welcome to Osradar\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});</pre>



<p>Of course, in your case, you will have several files and all the necessary structures.</p>



<p>To run the application just use this command</p>



<pre class="wp-block-preformatted">node example.js</pre>



<p>And you will get an output screen similar to this one:</p>



<pre class="wp-block-preformatted">Server running at http://localhost:3000/</pre>



<p>To stop the execution of the program, just press <code>CTRL + C</code>.</p>



<p>Running a production NodeJS application in this way is not recommended. The normal way is to use <code>pm2</code> which allows us to run it as a system service.</p>



<p>To do this, let’s install it on the system.</p>



<pre class="wp-block-preformatted">sudo npm install pm2@latest -g</pre>



<p>You will get an output screen similar to this one</p>



<pre class="wp-block-preformatted">+ pm2@4.5.6
added 175 packages from 194 contributors in 13.798s</pre>



<p>And now you can start running the created program:</p>



<pre class="wp-block-preformatted">pm2 start example.js</pre>



<p>You will get an output screen similar to this.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="497" src="https://www.osradar.com/wp-content/uploads/2021/05/1-1024x497.png" alt="1.- Running the NodeJS application" class="wp-image-29840" srcset="https://www.osradar.com/wp-content/uploads/2021/05/1-1024x497.png 1024w, https://www.osradar.com/wp-content/uploads/2021/05/1-300x146.png 300w, https://www.osradar.com/wp-content/uploads/2021/05/1-768x373.png 768w, https://www.osradar.com/wp-content/uploads/2021/05/1-696x338.png 696w, https://www.osradar.com/wp-content/uploads/2021/05/1-1068x519.png 1068w, https://www.osradar.com/wp-content/uploads/2021/05/1.png 1229w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption>1.- Running the NodeJS application</figcaption></figure>



<p>Remember that this is an example and that you have to execute these commands from the folder where your project is and with the main execution file of your application.</p>



<h2 id="configuring-nginx-as-the-reverse-proxy"><a href="#configuring-nginx-as-the-reverse-proxy" name="configuring-nginx-as-the-reverse-proxy"></a>Configuring Nginx as the reverse proxy</h2>



<p>This step, although optional, is recommended so that our application can be accessed without any problems. To do this, install Nginx</p>



<pre class="wp-block-preformatted">sudo apt install nginx</pre>



<p>And then, create a new configuration file for our application. In this case, I will call the file <code>node</code> but you can name it whatever you want.</p>



<pre class="wp-block-preformatted">sudo nano /etc/nginx/sites-available/node</pre>



<p>Now add the following to the file.</p>



<pre class="wp-block-preformatted">server {
    listen 80;
    server_name node.osradar.test;
    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;
    }
}</pre>



<p>Remember to replace the <code>server_name</code> value with the domain you have.</p>



<p>Save your changes and close the text editor.</p>



<p>You can check if the Nginx syntax is correct by running</p>



<pre class="wp-block-preformatted">sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful</pre>



<p>The output on the screen indicates that the syntax is correct and you can continue.</p>



<p>To apply the changes, restart Nginx.</p>



<pre class="wp-block-preformatted">sudo systemctl restart nginx</pre>



<p>Now, open your web browser and log in with your domain name and you will see the application working.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="1366" height="669" src="https://www.osradar.com/wp-content/uploads/2021/05/2-1024x502.png" alt="2.- NodeJS application for Production" class="wp-image-29841" srcset="https://www.osradar.com/wp-content/uploads/2021/05/2-1024x502.png 1024w, https://www.osradar.com/wp-content/uploads/2021/05/2-300x147.png 300w, https://www.osradar.com/wp-content/uploads/2021/05/2-768x376.png 768w, https://www.osradar.com/wp-content/uploads/2021/05/2-696x341.png 696w, https://www.osradar.com/wp-content/uploads/2021/05/2-1068x523.png 1068w, https://www.osradar.com/wp-content/uploads/2021/05/2.png 1366w" sizes="(max-width: 1366px) 100vw, 1366px" /><figcaption>2.- NodeJS application for Production</figcaption></figure>



<h2 id="conclusion"><a href="#conclusion" name="conclusion"></a>Conclusion</h2>



<p>In this post, we have shown you how to deploy your NodeJS application on a server. At least you have the basics, from here you have to set up the security policies that your server needs.</p>
<p>The post <a rel="nofollow" href="https://www.osradar.com/set-up-nodejs-application-production/">How to set up your NodeJS application for production</a> appeared first on <a rel="nofollow" href="https://www.osradar.com">Linux  Windows and android  Tutorials</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.osradar.com/set-up-nodejs-application-production/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
