<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Swapnil Abhimanyu Wagh</title>
    <description>The latest articles on DEV Community by Swapnil Abhimanyu Wagh (@meswapnilwagh).</description>
    <link>https://dev.to/meswapnilwagh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F273970%2F65b65881-3e70-432e-94b9-596ca7e1544a.png</url>
      <title>DEV Community: Swapnil Abhimanyu Wagh</title>
      <link>https://dev.to/meswapnilwagh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/meswapnilwagh"/>
    <language>en</language>
    <item>
      <title>How To Access S3 From Private Subnet</title>
      <dc:creator>Swapnil Abhimanyu Wagh</dc:creator>
      <pubDate>Tue, 18 Jan 2022 19:13:08 +0000</pubDate>
      <link>https://dev.to/meswapnilwagh/how-to-access-s3-from-private-subnet-f0l</link>
      <guid>https://dev.to/meswapnilwagh/how-to-access-s3-from-private-subnet-f0l</guid>
      <description>&lt;p&gt;Hello &lt;strong&gt;Dev's&lt;/strong&gt;, in this article, I will show you how can you use the &lt;strong&gt;S3 Gateway endpoint&lt;/strong&gt; to connect to S3 from a private subnet.&lt;/p&gt;

&lt;p&gt;You might be thinking what is the difference between accessing S3 from a public subnet vs accessing it from a private subnet.&lt;/p&gt;

&lt;p&gt;If you are using S3 you might already know that S3 is a public service and you can access it using the public endpoint, so the URL is publicly resolvable. What I exactly mean here, if you are in VPC which is a private environment and you want to connect to S3 you have to go out to the internet. So there will be an internet gateway your request will go through that internet gateway to access the S3. &lt;/p&gt;

&lt;p&gt;Hope you might get the answer to the above question, in the public subnet you can access the internet via the internet gateway, and in the private subnet, you don’t have internet so you cant reach out to the internet and request time out.&lt;/p&gt;

&lt;p&gt;Imagine due to some security policies you don’t want to go over the public internet to access the S3. Then how can you access the S3, here comes the “&lt;strong&gt;S3 Gateway Endpoint&lt;/strong&gt;” to rescue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wUyXUu5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642011396297/zl3osKEpIo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wUyXUu5G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642011396297/zl3osKEpIo.jpeg" alt="S3-Gateway-Endpoint.jpeg" width="826" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is “VPC Endpoint”?
&lt;/h2&gt;

&lt;p&gt;As per AWS official documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A VPC endpoint enables connections between a virtual private cloud (VPC) and supported services, without requiring that you use an internet gateway, NAT device, VPN connection, or AWS Direct Connect connection. Therefore, you control the specific API endpoints, sites, and services that are reachable from your VPC.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;AWS provides the following types of VPC Endpoint &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interface endpoints&lt;/li&gt;
&lt;li&gt;Gateway load balancer endpoints&lt;/li&gt;
&lt;li&gt;Gateway endpoints &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;S3 Gateway endpoint provides a way for the request to be routed through AWS Network to S3, without going out to the internet.&lt;/p&gt;

&lt;p&gt;Let's understand what problem exactly the S3 gateway solves for us by doing a small &lt;strong&gt;Lab.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AWS Account - Click &lt;a href="https://amzn.to/3cij5KJ"&gt;here&lt;/a&gt; to create one&lt;/li&gt;
&lt;li&gt;Basic understanding of EC2, VPC &amp;amp; S3&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's Setup a Lab
&lt;/h2&gt;

&lt;p&gt;To set up our lab we will create the below resources in our AWS account&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public &amp;amp; Private Subnet&lt;/li&gt;
&lt;li&gt;Public &amp;amp; Private route table&lt;/li&gt;
&lt;li&gt;EC2 instances in both subnets&lt;/li&gt;
&lt;li&gt;IAM Role &lt;code&gt;to access S3 from EC2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;S3 Gateway Endpoint&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Public &amp;amp; Private Subnets
&lt;/h3&gt;

&lt;p&gt;A public subnet is a subnet that has a route to the internet gateway, due to that the EC2 instances launched in a public subnet can access the internet. On the other hand, a private subnet is a subnet that does not have an internet gateway attached to it.&lt;/p&gt;

&lt;p&gt;To create a subnet go to VPC and from the left side menu select &lt;code&gt;Subnets&lt;/code&gt; and click on the &lt;code&gt;Create Subnet&lt;/code&gt; button, fill in the form and click on &lt;code&gt;Create Subnet&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1vdEhHfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642190805651/8L1loJ338.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1vdEhHfv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642190805651/8L1loJ338.png" alt="image.png" width="880" height="1152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow the same process to create a private subnet, once you created two subnets that will look like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cPl0BPuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191271461/A0UAEh-rr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cPl0BPuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191271461/A0UAEh-rr.png" alt="image.png" width="880" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Public &amp;amp; Private route table
&lt;/h3&gt;

&lt;p&gt;Before creating a route table we will create an &lt;code&gt;Internet Gateway&lt;/code&gt; to attach to our public route table&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uyn0BmcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191678150/ICgmYtIM3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uyn0BmcD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191678150/ICgmYtIM3.png" alt="image.png" width="880" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once our internet gateway is created we will attach it to our VPC&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CNy427R1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192064365/5i3ebZ-CT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CNy427R1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192064365/5i3ebZ-CT.png" alt="image.png" width="880" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wauxXJyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192353608/iSmZ10RBV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wauxXJyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192353608/iSmZ10RBV.png" alt="image.png" width="880" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To create a route table click on the &lt;code&gt;Route Tables&lt;/code&gt; menu from the left sidebar and click &lt;code&gt;Create route table&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XZMIohZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191853925/DTVJxfP6Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XZMIohZG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642191853925/DTVJxfP6Y.png" alt="image.png" width="880" height="728"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once our route table is created we will add a route to an internet gateway&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BUXdLWuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192666871/1cw1jxZVw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BUXdLWuL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192666871/1cw1jxZVw.png" alt="image.png" width="880" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and associate the public subnet to our public route table&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Tc_aHbH3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192723457/VxC6P3nm1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Tc_aHbH3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642192723457/VxC6P3nm1.png" alt="image.png" width="880" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Launch EC2 instances
&lt;/h3&gt;

&lt;p&gt;We will launch two EC2 instances each in a public and private subnet. Navigate to Services -&amp;gt; EC2 and click &lt;code&gt;Launch Instances&lt;/code&gt;, it will open the launch EC2 instance wizard,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6XmE8RcZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311475279/dIwIpqBSL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6XmE8RcZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311475279/dIwIpqBSL.png" alt="image.png" width="880" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once AMI is selected, on the next screen select the instance type, and then click Next&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ShWXhQz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311634286/5XvegAhCD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ShWXhQz9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311634286/5XvegAhCD.png" alt="image.png" width="880" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the &lt;code&gt;Configure Instance&lt;/code&gt; step select the VPC &amp;amp; Subnet, as we want to put this EC2 instance in the public subnet we will select the public subnet we created from the subnet dropdown&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yzqWeN1p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311937755/uGWi0t96S.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yzqWeN1p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642311937755/uGWi0t96S.png" alt="image.png" width="880" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we will stick with the default setting for other steps click &lt;code&gt;Review and Launch&lt;/code&gt; and then &lt;code&gt;Launch&lt;/code&gt;. The popup will show up, select the existing key pair or create a new key pair for your EC2 instance, this key will be used for SSH access&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fOByK5Y1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642312414399/wNub4QlRL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fOByK5Y1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642312414399/wNub4QlRL.png" alt="image.png" width="880" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow the same steps again to launch our second instance, just make sure you select the private subnet this time.&lt;/p&gt;

&lt;p&gt;Now you should have the two instances, as we launch one instance in the public subnet hence that instance got the public IP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uXxm_Ryb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642530031857/pGFDVHFaC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uXxm_Ryb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642530031857/pGFDVHFaC.png" alt="image.png" width="880" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Create IAM Role
&lt;/h3&gt;

&lt;p&gt;To access the S3 service securely from our EC2 instances, we will create one IAM role which we will then assign to EC2 instances. &lt;/p&gt;

&lt;p&gt;To create an IAM role go to IAM -&amp;gt; Roles and click the &lt;code&gt;Create Role&lt;/code&gt; button&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sF-fJToK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642196596933/3hm2_8pV1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sF-fJToK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642196596933/3hm2_8pV1.png" alt="image.png" width="880" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the type of trusted entity and select the service to which this role will be assigned, select AWS services and then EC2 from the use case&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PXrziZQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642196876389/UiMdf0zdC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PXrziZQ1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642196876389/UiMdf0zdC.png" alt="image.png" width="880" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we will select which permissions we want to assign to this new role, as we want to access the S3 service we will search for S3 and select &lt;code&gt;AmazonS3FulllAccess&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DZ0vSfcW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642197099287/d24AzpyC2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DZ0vSfcW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642197099287/d24AzpyC2.png" alt="image.png" width="880" height="529"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to the review step and give a name to the newly created role and click &lt;code&gt;Create Role&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Assign Role to EC2
&lt;/h4&gt;

&lt;p&gt;Now we will assign this role to our EC2 instances, (&lt;em&gt;you can create this role before launching the EC2 and then select the role from IAM Role dropdown in EC2 instance launch wizard&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;To assign the role go to EC2 and select one of the EC2 instances then click on&lt;br&gt;
Action -&amp;gt; Security -&amp;gt; Modify IAM role&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XnTCRoFH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642313095138/D4hJbeU2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XnTCRoFH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642313095138/D4hJbeU2u.png" alt="image.png" width="880" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will open a new screen, select the role that we created in the above step, and click save,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aj-QbQAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642313626107/SBCKRd_lc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aj-QbQAt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642313626107/SBCKRd_lc.png" alt="image.png" width="880" height="408"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Demo 1: Fall Down - Access S3 from the private subnet
&lt;/h3&gt;

&lt;p&gt;In this demonstration, we will test the scenario of what exactly happens if we don't have the S3 Gateway assigned to the private subnet.&lt;/p&gt;

&lt;p&gt;Let's SSH into our public instance, then from the public instance, we will SSH into our private instance using the ssh-agent forwarding.&lt;/p&gt;

&lt;p&gt;Use below command to add SSH key in ssh-agent,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ ssh-add -K SW-Demo-Key.pem 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we added the key, use the below command to ssh into the public EC2 instance,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ ssh -A ec2-user@13.127.220.246
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will ssh to our private instance using private IP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ ssh ec2-user@10.10.1.245
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we will try to access the S3 from a private EC2 instance using the S3 command-line tool,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ec2-user@ip-10-10-1-245 ~]$ aws s3 ls --region ap-south-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will not work because, the EC2 instance in the private subnet doesn't have access to the internet, therefore S3 CLI cant reach the S3 service.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create S3 Gateway Endpoint
&lt;/h3&gt;

&lt;p&gt;To create an S3 gateway endpoint, navigate to services -&amp;gt;VPC-&amp;gt;Endpoints and click the &lt;code&gt;Create Endpoint&lt;/code&gt; button,&lt;/p&gt;

&lt;p&gt;As we want to create an endpoint for S3 search for S3 in the search box and select the service of type &lt;code&gt;Gateway&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Ij_ZemN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316114903/ZqtzXD5m2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Ij_ZemN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316114903/ZqtzXD5m2.png" alt="image.png" width="880" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we choose which type of endpoint we want to create now we need to apply that endpoint to our private subnet, to do that select the VPC and then select the private route table. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rw6YeP9p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316313574/ZfPR7UdKa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rw6YeP9p--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316313574/ZfPR7UdKa.png" alt="image.png" width="880" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, click the Create Endpoint button to create the endpoint, &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cSlL5zlH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316578446/6WEGn8ROI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cSlL5zlH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642316578446/6WEGn8ROI.png" alt="image.png" width="880" height="74"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AWS will automatically add a route in the selected route table in the background.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L0VwT5Bn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642530604145/AGkOx0zE4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L0VwT5Bn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1642530604145/AGkOx0zE4.png" alt="image.png" width="880" height="205"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Demo 2:
&lt;/h3&gt;

&lt;p&gt;Now let's try to access the S3 one more time from our private EC2 instance, SSH into the private EC2 instance as shown above using the SSH agent forwarding, and re-run the S3 CLI command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ec2-user@ip-10-10-1-245 ~]$ aws s3 ls --region ap-south-1
2022-01-18 18:31:44 sw-s3-endpoint-demo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hurry !! this time we can successfully connect to the S3 using the S3 Gateway endpoint. &lt;/p&gt;

&lt;p&gt;And that’s it, guys, don't forget to delete all the resources created during this lab.&lt;/p&gt;

&lt;p&gt;Hope you find this tutorial helpful. Don't forget to share and leave a comment if it helps you.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ec2</category>
      <category>s3</category>
      <category>vpc</category>
    </item>
    <item>
      <title>How To Generate &amp; Install SSL Certificate In Apache Web Server On Ubuntu</title>
      <dc:creator>Swapnil Abhimanyu Wagh</dc:creator>
      <pubDate>Sat, 05 Dec 2020 14:44:31 +0000</pubDate>
      <link>https://dev.to/meswapnilwagh/how-to-generate-install-ssl-certificate-in-apache-web-server-on-ubuntu-1pbl</link>
      <guid>https://dev.to/meswapnilwagh/how-to-generate-install-ssl-certificate-in-apache-web-server-on-ubuntu-1pbl</guid>
      <description>&lt;p&gt;&lt;strong&gt;SSL&lt;/strong&gt;, Secure Socket Layer is a protocol created in order to place normal traffic between server and client in encrypted and protected wrapper, without any possibility of traffic being intercepted in between of transmission. SSL certificates encrypt a site information and create more secure and trusted connection. Certificates can show server's identification information to site visitors. Certificates Authorities like &lt;a href="http://www.digicert.com/"&gt;DigiCert&lt;/a&gt; , &lt;a href="http://godaddy.com/"&gt;GoDaddy&lt;/a&gt; can issue the self-signed SSL certificates that verify the server information. In this guide I will cover how to create a &lt;strong&gt;self-signed SSL certificates&lt;/strong&gt; for Apache on an Ubuntu Server which will encrypt traffic to your server. As we are generating SSL certificates are for testing purpose and not issued by any Certificates Authorities it will not provide third party validation of your server identity, but still it will help you to transfer information securely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;p&gt;You need a Ubuntu server and a user with root / sudo privileges. You also need a Apache web server installed if its not there you can use following command to install Apache.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update 
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Enable SSL module in Apache:
&lt;/h2&gt;

&lt;p&gt;In order to setup SSL certificates in Apache web server we need to enable the pre-installed Apache module. To enable SSL module in Apache use below command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;a2enmod ssl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you enable the module we need to restart the Apache server for changes to be applied. Use below command to restart Apache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service apache2 restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our server is ready to setup SSL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Generate a Self-Signed Certificates:
&lt;/h2&gt;

&lt;p&gt;We will create a new directory where we will store server key and certificates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; /etc/apache2/ssl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First you generate the keys for the Certificate Signing Request (CSR)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /etc/apache2/ssl 
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;openssl genrsa &lt;span class="nt"&gt;-des3&lt;/span&gt; &lt;span class="nt"&gt;-out&lt;/span&gt; yourdomain.pkey 2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will prompt you to enter passphrase. Its up to you to enter a passphrase or not. If you do, every time you restart the Apache service you will ask for this passphrase. We will enter a passphrase and then will create a 'insecure' key from 'secure' one. Create a insure key without a passphrase form a secure key &lt;em&gt;&lt;strong&gt;yourdomain.pkey.&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;openssl rsa &lt;span class="nt"&gt;-in&lt;/span&gt; yourdomain.pkey &lt;span class="nt"&gt;-out&lt;/span&gt; yourdomain.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we will create a CSR from the key file. With the CSR and key file a self-signed certificate can be generated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;openssl req &lt;span class="nt"&gt;-new&lt;/span&gt; &lt;span class="nt"&gt;-key&lt;/span&gt; yourdomain.key &lt;span class="nt"&gt;-out&lt;/span&gt; yourdomain.csr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once enter will ask you number of questions one most important is Common Name use your domain or IP of you server as a value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter &lt;span class="s1"&gt;'.'&lt;/span&gt;, the field will be left blank.
&lt;span class="nt"&gt;-----&lt;/span&gt;
Country Name &lt;span class="o"&gt;(&lt;/span&gt;2 letter code&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;AU]:US
State or Province Name &lt;span class="o"&gt;(&lt;/span&gt;full name&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;Some-State]:New York
Locality Name &lt;span class="o"&gt;(&lt;/span&gt;eg, city&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;:NYC
Organization Name &lt;span class="o"&gt;(&lt;/span&gt;eg, company&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;Internet Widgits Pty Ltd]:Organization Pvt. Ltd
Organizational Unit Name &lt;span class="o"&gt;(&lt;/span&gt;eg, section&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;:
Common Name &lt;span class="o"&gt;(&lt;/span&gt;e.g. server FQDN or YOUR name&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[]&lt;/span&gt;:yourdomain.com
Email Address &lt;span class="o"&gt;[]&lt;/span&gt;:webmaster@yourdomain.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have CSR and key file now we can generate a self-signed certificate using following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;openssl x509 &lt;span class="nt"&gt;-req&lt;/span&gt; &lt;span class="nt"&gt;-days&lt;/span&gt; 365 &lt;span class="nt"&gt;-in&lt;/span&gt; yourdomain.csr &lt;span class="nt"&gt;-signkey&lt;/span&gt; yourdomain.key &lt;span class="nt"&gt;-out&lt;/span&gt; yourdomain.crt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3 : Configure Apache to use the Signed SSL Certificate :
&lt;/h2&gt;

&lt;p&gt;Now that we have all our necessary certificates and key file available, now we can configure Apache to use these in VirtualHost. Next, add an entry to &lt;code&gt;/etc/apache2/ports.conf&lt;/code&gt; for the domain you'll be using to host your SSL-enabled site.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;NameVirtualHost yourdomain.com:443
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now create  a Apache configuration file with VirtualHost entry for your site. Create a file &lt;code&gt;yourdomain-ssl&lt;/code&gt; with below contain in &lt;code&gt;/etc/apache2/site-available/&lt;/code&gt;folder of Apache.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&amp;lt;&lt;span class="n"&gt;VirtualHost&lt;/span&gt; &lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;:&lt;span class="m"&gt;443&lt;/span&gt;&amp;gt;
    &lt;span class="n"&gt;SSLEngine&lt;/span&gt; &lt;span class="n"&gt;On&lt;/span&gt;
    &lt;span class="n"&gt;SSLCertificateFile&lt;/span&gt; /&lt;span class="n"&gt;etc&lt;/span&gt;/&lt;span class="n"&gt;apache2&lt;/span&gt;/&lt;span class="n"&gt;ssl&lt;/span&gt;/&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;crt&lt;/span&gt;
    &lt;span class="n"&gt;SSLCertificateKeyFile&lt;/span&gt; /&lt;span class="n"&gt;etc&lt;/span&gt;/&lt;span class="n"&gt;apache2&lt;/span&gt;/&lt;span class="n"&gt;ssl&lt;/span&gt;/&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;key&lt;/span&gt;

    &lt;span class="n"&gt;ServerName&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;://&lt;span class="n"&gt;www&lt;/span&gt;.&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;
    &lt;span class="n"&gt;ServerAlias&lt;/span&gt; &lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;
    &lt;span class="n"&gt;DocumentRoot&lt;/span&gt; /&lt;span class="n"&gt;var&lt;/span&gt;/&lt;span class="n"&gt;www&lt;/span&gt;/&lt;span class="n"&gt;yourdomain&lt;/span&gt;

    &amp;lt;&lt;span class="n"&gt;Directory&lt;/span&gt; /&lt;span class="n"&gt;var&lt;/span&gt;/&lt;span class="n"&gt;www&lt;/span&gt;/&lt;span class="n"&gt;yourdomain&lt;/span&gt;&amp;gt;
        &lt;span class="n"&gt;Options&lt;/span&gt; &lt;span class="n"&gt;Indexes&lt;/span&gt; &lt;span class="n"&gt;FollowSymLinks&lt;/span&gt; &lt;span class="n"&gt;MultiViews&lt;/span&gt;
        &lt;span class="n"&gt;AllowOverride&lt;/span&gt; &lt;span class="n"&gt;All&lt;/span&gt;
        &lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;allow&lt;/span&gt;,&lt;span class="n"&gt;deny&lt;/span&gt;
        &lt;span class="n"&gt;allow&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt;
    &amp;lt;/&lt;span class="n"&gt;Directory&lt;/span&gt;&amp;gt;
&amp;lt;/&lt;span class="n"&gt;VirtualHost&lt;/span&gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4 : Permanent Redirection to https :
&lt;/h2&gt;

&lt;p&gt;To redirect your site permanently to https we will edit the VirtualHost entry for for 80 port which is in a file &lt;code&gt;yourdomin&lt;/code&gt;in &lt;code&gt;/etc/apache2/site-available&lt;/code&gt; folder of Apache, or else create one with below contain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&amp;lt;&lt;span class="n"&gt;VirtualHost&lt;/span&gt; *:&lt;span class="m"&gt;80&lt;/span&gt;&amp;gt;
    &lt;span class="n"&gt;ServerName&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;://&lt;span class="n"&gt;www&lt;/span&gt;.&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;
    &lt;span class="n"&gt;ServerAlias&lt;/span&gt; &lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;
    &lt;span class="n"&gt;DocumentRoot&lt;/span&gt; /&lt;span class="n"&gt;var&lt;/span&gt;/&lt;span class="n"&gt;www&lt;/span&gt;/&lt;span class="n"&gt;yourdomain&lt;/span&gt;
    &lt;span class="n"&gt;Redirect&lt;/span&gt; / &lt;span class="n"&gt;https&lt;/span&gt;://&lt;span class="n"&gt;www&lt;/span&gt;.&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;
&amp;lt;/&lt;span class="n"&gt;VirtualHost&lt;/span&gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or else you can create &lt;code&gt;.htaccess&lt;/code&gt; file in your site root folder for permanent redirection to https with below contain&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="n"&gt;RewriteEngine&lt;/span&gt; &lt;span class="n"&gt;On&lt;/span&gt;
&lt;span class="n"&gt;RewriteCond&lt;/span&gt; %{&lt;span class="n"&gt;HTTPS&lt;/span&gt;} &lt;span class="n"&gt;off&lt;/span&gt; [&lt;span class="n"&gt;OR&lt;/span&gt;]
&lt;span class="n"&gt;RewriteCond&lt;/span&gt; %{&lt;span class="n"&gt;HTTP_HOST&lt;/span&gt;} ^&lt;span class="n"&gt;yourdomain&lt;/span&gt;\.&lt;span class="n"&gt;com&lt;/span&gt;$ [&lt;span class="n"&gt;NC&lt;/span&gt;]
&lt;span class="n"&gt;RewriteRule&lt;/span&gt; ^ &lt;span class="n"&gt;https&lt;/span&gt;://&lt;span class="n"&gt;www&lt;/span&gt;.&lt;span class="n"&gt;yourdomain&lt;/span&gt;.&lt;span class="n"&gt;com&lt;/span&gt;%{&lt;span class="n"&gt;REQUEST_URI&lt;/span&gt;} [&lt;span class="n"&gt;L&lt;/span&gt;,&lt;span class="n"&gt;R&lt;/span&gt;=&lt;span class="m"&gt;301&lt;/span&gt;]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5 : Test your Apache config and activate the VirtualHost :
&lt;/h2&gt;

&lt;p&gt;It is always a good practice to check your Apache configuration for any error before we activate the VirtualHost and restart the Apache service, cause Apache will not start if your configuration file has some errors. Run below command to test configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;apachectl configtest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your configuration is right then activate the VirtualHost using below command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;a2ensite yourdomain      // Simple http configuration file 
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;a2ensite yourdomain-ssl     // SSL configuration file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, restart the Apache service for changes to take effect&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service apache2 restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5 : Test your Setup :
&lt;/h2&gt;

&lt;p&gt;Now, that we have everything prepared and done you can try visiting your server domain name or public IP using the https protocol like this &lt;code&gt;https://yourdomin.com.&lt;/code&gt; You will get a security warning from browser regarding your server identity because we are using self-signed certificates and it has been not signed by any certificate authority that browser trust.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://opensourceholic.files.wordpress.com/2014/05/ssl_warning.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HdodcsGD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2014/05/ssl_warning.png%3Fw%3D646" alt="ssl_warning"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this is expected you can move ahead, hit 'Proceed Anyway' button. Once Added Security Exception for your browser you will taken to the your site this time with encrypted traffic, check the SSL configuration by clicking on lock icon in address bar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://opensourceholic.files.wordpress.com/2014/05/encrypted.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wy_nYLID--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://opensourceholic.files.wordpress.com/2014/05/encrypted.png" alt="encrypted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you find this tutorial helpful. Feel free to ask questions! Don't forget to like or to leave a comment if its really help you.&lt;/p&gt;

</description>
      <category>ssl</category>
      <category>apache</category>
      <category>security</category>
      <category>web</category>
    </item>
    <item>
      <title>How to Password Protect a Website or Web SubDirectory With .htaccess &amp; .htpasswd</title>
      <dc:creator>Swapnil Abhimanyu Wagh</dc:creator>
      <pubDate>Sat, 05 Dec 2020 14:40:43 +0000</pubDate>
      <link>https://dev.to/meswapnilwagh/how-to-password-protect-a-website-or-web-subdirectory-with-htaccess-htpasswd-1e2f</link>
      <guid>https://dev.to/meswapnilwagh/how-to-password-protect-a-website-or-web-subdirectory-with-htaccess-htpasswd-1e2f</guid>
      <description>&lt;p&gt;Working on a website that you need others to see, but not the whole world? Password protecting a website (or a sub directory within a website).&lt;/p&gt;

&lt;p&gt;Protecting files on your website from unauthorized users can be very important. You can use PHP or any language to listen for login authorization information on each page, but that doesn’t protect your images, documents, and other media and it is not proper way to do so.&lt;/p&gt;

&lt;p&gt;That’s why I’ve found the new method of protecting files and directories the most reliable and is actually a pretty easy thing to do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O0DQNcRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/60/0%2AbSZQq62XuA0dosm9.png%3Fq%3D20" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O0DQNcRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/60/0%2AbSZQq62XuA0dosm9.png%3Fq%3D20" alt="Image for post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HY1KKyHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/672/0%2AbSZQq62XuA0dosm9.png" class="article-body-image-wrapper"&gt;&lt;img alt="Image for post" src="https://res.cloudinary.com/practicaldev/image/fetch/s--HY1KKyHo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/672/0%2AbSZQq62XuA0dosm9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To &lt;span id="rmm"&gt;&lt;span id="rmm"&gt;p&lt;/span&gt;&lt;/span&gt;assword protect we will use .htaccess and .htpasswd method.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step1: Basic Coniguration
&lt;/h1&gt;

&lt;p&gt;To make .htaccess files work as expected, you need to have below line in your site Apache configuration,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;AllowOverride All&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So your file will look like&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ServerName password-protected.com DocumentRoot /var/www/PasswordProtected  # This relaxes Apache security settings. AllowOverride all &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This tells Apache that it’s okay to allow .htaccess files to over-ride previous directives. You must reload Apache before this change will have an effect&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;sudo service apache2 reload&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Step2: Create .htaccess and .htpasswd files
&lt;/h1&gt;

&lt;p&gt;Create a file called .htaccess in the directory that you want to password-protect (in my case I am using /var/www/PasswordProtected directory) with the following content&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;AuthUserFile /var/www/PasswordProtected/.htpasswd AuthName “Authorization Required” AuthType Basic require valid-user&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then create the file /var/www/PasswordProtected/.htpasswd which contains the users that are allowed to login and their passwords.&lt;/p&gt;

&lt;p&gt;We do that with the htpasswd command, to use this command make sure apache2-utils package is install.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;htpasswd -c /var/www/PasswordProtected/.htpasswd USER1&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The -c flag is used only when you are creating a new file. After the first time, you will omit the -c flag, when you are adding new users to an already-existing password file. Otherwise you will overwrite the file!!&lt;/p&gt;

&lt;p&gt;And that’s it you are done !! Your website is password protected now.&lt;/p&gt;

</description>
      <category>web</category>
      <category>security</category>
      <category>htaccess</category>
    </item>
    <item>
      <title>How To Replace a lost Key Pair with new one on an EC2 instance</title>
      <dc:creator>Swapnil Abhimanyu Wagh</dc:creator>
      <pubDate>Sat, 05 Dec 2020 05:58:34 +0000</pubDate>
      <link>https://dev.to/meswapnilwagh/how-to-replace-a-lost-key-pair-with-new-one-on-an-ec2-instance-2f30</link>
      <guid>https://dev.to/meswapnilwagh/how-to-replace-a-lost-key-pair-with-new-one-on-an-ec2-instance-2f30</guid>
      <description>&lt;p&gt;Access to Amazon &lt;a href="http://aws.amazon.com/ec2/"&gt;EC2&lt;/a&gt; instances specially the Linux instances are always protected by a private key pair. Private key is only the way to get access to the instance, what if you lost the key? Is your servers lost in black hole? Thankfully nothing is lost you still can access your server, let me show you how can you solve this problem.&lt;br&gt;
            &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dt97MyP2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/30f249d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dt97MyP2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/30f249d.jpg" alt="AmazonEC2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we proceed please not that to do this we need to stop our running instance that means what ever app or website running on the serer will be offline for that period. In order to do this process we need the EC2 root volume to be an &lt;a href="http://aws.amazon.com/ebs/"&gt;EBS&lt;/a&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  Quick Start
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Launch New Instance
&lt;/h3&gt;

&lt;p&gt;First thing we need is to start and one instance with a key pair that we can access we will use this instance for recovering the old EBS. New instance must be created in same Availability Zone of the original instance. Firstly we will note down the original instance AZ.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EXD55fsf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446188775565.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EXD55fsf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446188775565.jpg" alt="Check Zone"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we will launch the new instance using the Launch Instance wizard, make sure that we are launching the instance same AZ in which our original instance is. You can chose AZ from **subnet **as shown in below image. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--atZdQnzL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446188985851.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--atZdQnzL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446188985851.jpg" alt="Launch Wizard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we actually launch the instance we will see the key pair section from there we will create and download a new key pair and then we will launch the instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lnYnT63O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189267596.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lnYnT63O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189267596.jpg" alt="Create New Key Pair"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Stop Original Instance
&lt;/h3&gt;

&lt;p&gt;When the new instance is ready, now we will detach the volume of original instance and attach it to new instance, in order to do that we need to stop the original instance. To stop the instance right click on the instance go to Instance State and chose Stop option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8PJiGVWg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189440828.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8PJiGVWg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189440828.jpg" alt="Stop Instance"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Detach Volume
&lt;/h3&gt;

&lt;p&gt;Now our instance is stop then we go to the volume section in EC2 console. Here we will look for the original instance root volume, using the &lt;code&gt;Attachment Information&lt;/code&gt;.This column shows to which instance the volume is attach and its device path usually the root drive path is &lt;code&gt;/dev/sad1.&lt;/code&gt;Now we will Detach the volume from original instance, in order to do that right click on volume and chose Detach Volume option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_XIhKr5o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189973622.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_XIhKr5o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/1446189973622.jpg" alt="Detach Volume"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Attach Volume
&lt;/h3&gt;

&lt;p&gt;Once we detach the volume, now we need to attach the volume to the Recovery instance, right click on the volume and chose Attach Volume option in dialog we will select the Recovery instance and also add a device path as &lt;code&gt;/dev/xvdf&lt;/code&gt; (or &lt;code&gt;/dev/sdf&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3oFkduAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/selection_002.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3oFkduAa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/selection_002.png" alt="Attach Volume"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we attach the volume to recovery instance we are now ready to mount that in same instance. We can access the instance using the key pair generated during the instance launch. To access instance use below command&lt;/p&gt;

&lt;p&gt;Setting the rigth permissions for the .pem file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod &lt;/span&gt;600 NewKeyPair.pem 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connecting to the instance using the certificate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nv"&gt;$ &lt;/span&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; NewKeyPair.pem ubuntu@public_ip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Copy Key
&lt;/h3&gt;

&lt;p&gt;Once you are connected to the instance now we have to mount the attach volume and copy the &lt;code&gt;authorized_keys&lt;/code&gt; file from new instance to the mounted drive at same location. Use below command to mount and copy the file.&lt;/p&gt;

&lt;p&gt;Becoming root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating the mount point:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; /mnt/original
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mounting the volume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;mount /dev/xvdf /mnt/original
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace the authorized_keys file with the new one. Now access to the Old instance will be possible only using the new pem. Path and name of file depends on the Linux Flavor you are using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /home/ubuntu/.ssh/authorized_keys &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /mnt/original/home/ubuntu/.ssh/authorized_keys 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unmunt the volume:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;umount /mnt/original/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we are done with copying the keys, now we will detach the volume from recovery instance by right clicking on volume and choosing the detach volume option as we do it earlier. After that we will re-attach it to the Original instance, make sure when you are re-attaching the volume that should on on root path so enter &lt;code&gt;/dev/sda1&lt;/code&gt; in device path option.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--75aR2EaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/selection_003.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--75aR2EaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://opensourceholic.files.wordpress.com/2015/10/selection_003.png" alt="Originalvolume"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Access Using New Key
&lt;/h3&gt;

&lt;p&gt;Now we can start the original instance back, and now you can access it using the new key pair.&lt;/p&gt;

&lt;p&gt;You can use same steps in case your machine is not accessible and you want access to your data in EBS.&lt;/p&gt;

&lt;p&gt;Hope you find this tutorial helpful. Feel free to ask questions! Don’t forget to like or to leave a comment if its really help you.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>ec2</category>
      <category>privatekey</category>
      <category>ssh</category>
    </item>
    <item>
      <title>How To Deploy NodeJS Apps With PM2 &amp; NGINX In Cluster Mode On Production Environment</title>
      <dc:creator>Swapnil Abhimanyu Wagh</dc:creator>
      <pubDate>Wed, 20 May 2020 20:36:23 +0000</pubDate>
      <link>https://dev.to/meswapnilwagh/how-to-deploy-nodejs-apps-with-pm2-nginx-in-cluster-mode-on-production-environment-5e96</link>
      <guid>https://dev.to/meswapnilwagh/how-to-deploy-nodejs-apps-with-pm2-nginx-in-cluster-mode-on-production-environment-5e96</guid>
      <description>&lt;h2&gt;
  
  
  What is Process Manager?
&lt;/h2&gt;

&lt;p&gt;Process Manager is a special program design to effectively manage server process and take benefit of server resources. It's useful to keep application online and restart on failure.&lt;/p&gt;

&lt;p&gt;Process Manager is also useful for clustering, logging and monitoring the application. Process Managers make it possible to demonize the application so it will be running in background as a service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;p&gt;In this tutorial we assume that you have following setup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ubuntu server and a user with root / sudo privileges&lt;/li&gt;
&lt;li&gt;All the necessary package installed to run simple NodeJS App&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Which Process Manager?
&lt;/h2&gt;

&lt;p&gt;There are multiple Process Manager available, as listed below but in this tutorial we will be focusing on &lt;strong&gt;PM2&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pm2.keymetrics.io/" rel="noopener noreferrer"&gt;PM2&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://strong-pm.io/" rel="noopener noreferrer"&gt;StrongLoop PM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/foreversd/forever" rel="noopener noreferrer"&gt;Forever&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why PM2?
&lt;/h2&gt;

&lt;p&gt;Following are the complete features set of PM2&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F4yXiyrt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2F4yXiyrt.png" alt="PM2 Features"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Process Manager:
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;npm&lt;/code&gt; to install the pm2 globally so it will available system-wide for use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;npm i pm2 &lt;span class="nt"&gt;-g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let start our NodeJS app using &lt;code&gt;pm2 start&lt;/code&gt; command&lt;/p&gt;

&lt;p&gt;First change the directory to our node application directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /opt/hello-pm2/
&lt;span class="nv"&gt;$ &lt;/span&gt;pm2 start app.js &lt;span class="nt"&gt;--name&lt;/span&gt; Hello &lt;span class="nt"&gt;-i&lt;/span&gt; 2 &lt;span class="nt"&gt;--watch&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will also register our app in the process list of PM2, which you can see in the output of the above command&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FOaLuQGO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FOaLuQGO.png" alt="Output of PM2 Start"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  PM2 as a service:
&lt;/h2&gt;

&lt;p&gt;PM2 will take care of all the application running under it and will restart automatically if the application killed or crash, but what if the system boot or reboot? PM2 has answer for this, PM2 provide an easy way to start PM2 as a system service in &lt;code&gt;systemd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;startup&lt;/code&gt; command generate and configure a PM2 startup script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;pm2 startup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now to setup the startup script copy/paste the last line from the output or earlier command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;PM2] Init System found: systemd
meswapnilwagh
&lt;span class="o"&gt;[&lt;/span&gt;PM2] To setup the Startup Script, copy/paste the following &lt;span class="nb"&gt;command&lt;/span&gt;:
&lt;span class="nb"&gt;sudo env &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd &lt;span class="nt"&gt;-u&lt;/span&gt; meswapnilwagh &lt;span class="nt"&gt;--hp&lt;/span&gt; /home/meswapnilwagh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the command to setup PM2 to start on boot/reboot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo env &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd &lt;span class="nt"&gt;-u&lt;/span&gt; meswapnilwagh &lt;span class="nt"&gt;--hp&lt;/span&gt; /home/meswapnilwagh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Basic PM2 Commands:
&lt;/h2&gt;

&lt;p&gt;Just like all other command line utility, PM2 also comes with bundle of subcommands which are helpful to manage application running under PM2&lt;/p&gt;

&lt;h3&gt;
  
  
  Start Application in cluster
&lt;/h3&gt;

&lt;p&gt;To start application in cluster mode you can use &lt;code&gt;-i&lt;/code&gt; flag and specify the number of instances you want to run you can also use &lt;code&gt;--name&lt;/code&gt; flag to name your process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pm2 start /opt/hello-pm2/app.js &lt;span class="nt"&gt;--name&lt;/span&gt; Hello &lt;span class="nt"&gt;-i&lt;/span&gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Stop Application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pm2 stop Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Restart Application
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pm2 restart Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  List Applications
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pm2 list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Monitor Application Process
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;pm2 monit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more usage of PM2 please refer &lt;a href="https://pm2.keymetrics.io/docs/usage/quick-start/" rel="noopener noreferrer"&gt;PM2 quick start&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://www.nginx.com" rel="noopener noreferrer"&gt;NGINX&lt;/a&gt; As Reverse Proxy :
&lt;/h2&gt;

&lt;p&gt;Till now we configure PM2 and running our node app in cluster seems all good, but are you still ready for production? How can you get rid of that port in your URL? Answer to all your question is Nginx (Engine-X).&lt;/p&gt;

&lt;h3&gt;
  
  
  What is NGINX?
&lt;/h3&gt;

&lt;p&gt;Officially, Nginx is a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. &lt;/p&gt;

&lt;p&gt;Best practice to deploy NodeJS application in production, is by using Nginx as reverse proxy to route the web client's requests to appropriate node process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install NGINX
&lt;/h3&gt;

&lt;p&gt;Use following command to install Nginx on Ubuntu&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get update
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure NGINX
&lt;/h3&gt;

&lt;p&gt;Open the nginx default site config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/sites-available/default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add below configuration in the file (You can take backup of original file for safer side)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;server &lt;span class="o"&gt;{&lt;/span&gt;
  listen       80&lt;span class="p"&gt;;&lt;/span&gt;
  server_name  mycooldomain.com&lt;span class="p"&gt;;&lt;/span&gt;

  location / &lt;span class="o"&gt;{&lt;/span&gt;
    proxy_pass http://localhost:4000&lt;span class="p"&gt;;&lt;/span&gt;
    proxy_http_version 1.1&lt;span class="p"&gt;;&lt;/span&gt;
    proxy_set_header Upgrade &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    proxy_set_header Connection &lt;span class="s1"&gt;'upgrade'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    proxy_set_header Host &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    proxy_cache_bypass &lt;span class="nv"&gt;$http_upgrade&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the nginx listening on &lt;a href="http://mycooldomain.com" rel="noopener noreferrer"&gt;http://mycooldomain.com&lt;/a&gt; and the &lt;code&gt;location /&lt;/code&gt; block take care of the incoming request and forwarding to NodeJS application.&lt;/p&gt;

&lt;p&gt;Save the file and restart nginx,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;service nginx restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open your browser and navigate to &lt;a href="http://mycooldomain.com" rel="noopener noreferrer"&gt;http://mycooldomain.com&lt;/a&gt;, you can see how node app is being served without using any port in URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations&lt;/strong&gt;!! You had successfully deployed NodeJS app on production using PM2 and Ngnix.&lt;/p&gt;

&lt;p&gt;Hope you find this tutorial helpful. Don't forget to share if its really help you. For any query please DM at &lt;a href="https://twitter.com/meswapnilwagh" rel="noopener noreferrer"&gt;Swapnil Wagh&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>nginx</category>
      <category>pm2</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
