{"id":552,"date":"2024-10-19T15:49:10","date_gmt":"2024-10-19T15:49:10","guid":{"rendered":"https:\/\/249host.com\/tutorials\/?p=552"},"modified":"2024-10-19T15:51:39","modified_gmt":"2024-10-19T15:51:39","slug":"install-node-js-on-and-host-application","status":"publish","type":"post","link":"https:\/\/249host.com\/tutorials\/install-node-js-on-and-host-application\/","title":{"rendered":"Install Node.js on and host application in Ubuntu latest version"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>Install Node.js is very easy in ubuntu server.Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.<\/p>\n<p>In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 server.Node.js is a powerful and versatile runtime environment that allows developers to execute JavaScript code on the server side.<\/p>\n<p>Installing Node.js on the latest version of Ubuntu is straightforward. You can use several methods, but here\u2019s a common and reliable way using the NodeSource repository. Follow these steps,<\/p>\n<h3>Step 1: Update Your Package Index<\/h3>\n<p>First, open a terminal and update your package index to ensure you have the latest information on available packages.<\/p>\n<pre>sudo apt update<\/pre>\n<h3>Step 2: Install Required Packages<\/h3>\n<p>You&#8217;ll need <code>curl<\/code> to fetch the installation script. If you don\u2019t have it installed, you can do so with below command<\/p>\n<pre>sudo apt install curl<\/pre>\n<h3>Step 3: Download and Install Node.js<\/h3>\n<p>NodeSource maintains a repository for Node.js. You can choose the version you want to install (e.g., Node.js 16 or 18). Here\u2019s how to install Node.js 18<\/p>\n<pre>curl -fsSL https:\/\/deb.nodesource.com\/setup_18.x | sudo -E bash -<\/pre>\n<p>Then, install Node.js:<\/p>\n<pre>sudo apt install -y nodejs<\/pre>\n<h3>Step 4: Verify the Installation<\/h3>\n<p>To check if Node.js and npm (Node Package Manager) are installed correctly, run<\/p>\n<pre>node -v\r\nnpm -v<\/pre>\n<h3>Step 5: Install Build Tools (Optional)<\/h3>\n<p>If you plan to compile and install native add-ons from npm, it\u2019s a good idea to install build tools<\/p>\n<pre>sudo apt install -y build-essential<\/pre>\n<p>To host your Node.js application on Ubuntu, you\u2019ll need to set up a few things beyond just installing Node.js. Here\u2019s a step-by-step guide to help you get started<\/p>\n<h3>Step 1: Prepare Your Application<\/h3>\n<p>Make sure your Node.js application is ready. If you haven&#8217;t created one yet, you can create a simple app as follows<\/p>\n<p>Create a project directory<\/p>\n<pre>mkdir myapp\r\ncd myapp<\/pre>\n<p>Initialize a new Node.js project<\/p>\n<pre>npm init -y<\/pre>\n<p>create a simple server in <code>index.js<\/code><\/p>\n<pre>const http = require('http');\r\n\r\nconst hostname = '0.0.0.0';\r\nconst port = 3000;\r\n\r\nconst server = http.createServer((req, res) =&gt; {\r\nres.statusCode = 200;\r\nres.setHeader('Content-Type', 'text\/plain');\r\nres.end('Hello World\\n');\r\n});\r\n\r\nserver.listen(port, hostname, () =&gt; {\r\nconsole.log(`Server running at http:\/\/${hostname}:${port}\/`);\r\n});<\/pre>\n<h3>Install PM2 (Process Manager)<\/h3>\n<p>PM2 is a popular process manager for Node.js applications that helps keep your app running. Install it globally<\/p>\n<pre>sudo npm install -g pm2<\/pre>\n<h3>Step 3: Start Your Application with PM2<\/h3>\n<p>Run your application using PM2:<\/p>\n<pre>pm2 start index.js --name myapp<\/pre>\n<h3>Step 4: Set Up PM2 to Run on Startup<\/h3>\n<p>To ensure PM2 starts your app on server reboot, run<\/p>\n<pre>pm2 startup\r\npm2 save<\/pre>\n<h3>Step 5: Configure a Reverse Proxy with Nginx<\/h3>\n<p>Using Nginx as a reverse proxy will help manage incoming traffic to your Node.js application.<\/p>\n<p><strong>Install Nginx:<\/strong><\/p>\n<pre>sudo apt install nginx<\/pre>\n<p><strong>Configure Nginx:<\/strong> Create a new configuration file for your application:<\/p>\n<pre>sudo nano \/etc\/nginx\/sites-available\/myapp<\/pre>\n<p>Add the following configuration, replacing <code>your_domain_or_IP<\/code> with your <a href=\"https:\/\/www.squarebrothers.com\/domain-name-prices\/\" target=\"_blank\" rel=\"noopener\">domain name<\/a> or server IP address:<\/p>\n<pre>server {\r\nlisten 80;\r\nserver_name your_domain_or_IP;\r\n\r\nlocation \/ {\r\nproxy_pass http:\/\/localhost:3000; # The port your app is running on\r\nproxy_http_version 1.1;\r\nproxy_set_header Upgrade $http_upgrade;\r\nproxy_set_header Connection 'upgrade';\r\nproxy_set_header Host $host;\r\nproxy_cache_bypass $http_upgrade;\r\n}\r\n}<\/pre>\n<p>Enable the configuration:<\/p>\n<pre>sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/<\/pre>\n<p>Test Nginx configuration:<\/p>\n<pre>sudo nginx -t<\/pre>\n<p>Restart Nginx:<\/p>\n<pre>sudo systemctl restart nginx<\/pre>\n<h3>Step 6: Open Your Firewall (If Applicable)<\/h3>\n<p>If you have a firewall running (like UFW), allow HTTP traffic<\/p>\n<pre>sudo ufw allow 'Nginx Full'<\/pre>\n<h3>Step 7: Access Your Application<\/h3>\n<p>Now, you should be able to access your application by navigating to <code>http:\/\/your_domain_or_IP<\/code> in your web browser.<\/p>\n<h3>Conclusion<\/h3>\n<p>Your Node.js application is now hosted on your Ubuntu server! If you have any further questions or need more details on specific steps, feel free to ask.<\/p>\n<p>To know how to install nodejs in windows for beginner&#8217;s <a href=\"https:\/\/249host.com\/tutorials\/install-node-js-on-windows\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Install Node.js is very easy in ubuntu server.Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development. In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu 20.04 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,10],"tags":[69,70],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-linux","category-general","tag-node-js","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":9,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":561,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/posts\/552\/revisions\/561"}],"wp:attachment":[{"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/249host.com\/tutorials\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}