├── LICENSE ├── README.md └── assets ├── all-instances.png ├── aws-launch-instance.png ├── aws.ss-1.png ├── aws.ss-2.png ├── aws.ss-3.png ├── aws.ss-4.png ├── aws.ss-5.png ├── aws.ss-6.png ├── aws.ss-7.png ├── aws.ss.png ├── connect-server-with-putty.png ├── git-install.png ├── instance-type.png ├── key-pair.png ├── launch-log.png ├── node-version-check.png ├── public_dns.png └── shut-down-behavior.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 axit vadi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deploy single/multiple Node.js apps on AWS EC2 instance with live domains, Nginx. 2 | 3 | ## Introduction 4 | 5 | ### Node.js 6 | 7 | Node.js is an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside a web browser. Node.js is built on open-source V8 JavaScript engine for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. 8 | 9 | ### AWS 10 | 11 | AWS is a platform that offers flexible, reliable, scalable, easy-to-use and cost-effective cloud computing solutions and is offered by Amazon. The platform is developed with a combination of infrastructure as a service (IaaS), platform as a service (PaaS) and packaged software as a service (SaaS) offerings. 12 | 13 | ### EC2 14 | 15 | EC2 or Elastic Compute Cloud is a web service that provides secure, resizable compute capacity in the cloud. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction. It provides you with complete control of your computing resources and lets you run on Amazon’s proven computing environment. 16 | 17 | ### Cloudflare 18 | 19 | Cloudflare is an American web-infrastructure and website-security company, providing content-delivery-network services, DDoS mitigation, Internet security, and distributed domain-name-server services. 20 | 21 | ### Git 22 | 23 | Git is a distributed version control system for tracking changes in source code during software development. 24 | 25 | 26 | ## What we are going to implement 27 | 28 | 1. What we are going to implement 29 | 2. Create an AWS account 30 | 3. Launch an Ubuntu EC2 instance 31 | 4. SSH into our instance 32 | 5. Install Node.js and Git 33 | 6. Clone repository from GitHub 34 | 7. Run the node.js app 35 | 8. Open different ports for the node instances 36 | 9. Make the app running using PM2 37 | 10. Configure Nginx for single and multiple node.js apps 38 | 11. Configure Nginx to autostart whenever instance will be rebooted 39 | 12. Purchase a domain from Godaddy, Namecheap, etc. 40 | 13. Configure domain name for our server 41 | 14. Configure subdomains for our server 42 | 15. Setup SSL with Cloudflare 43 | 44 | ### create an AWS Account 45 | 46 | We are using AWS EC2 server for hosting our multiple node.js apps. So, It is must to have an AWS account. Let’s start and create if you are not having one. For new users, there is an offer of 750 hours free per month up to a year. You must have a credit/debit card before start registering. 47 | 48 | ### Note: If you are already having an AWS account then you can skip this step and just login to your instance. 49 | 50 | 1. Open [AWS](https://aws.amazon.com) and Click on Create an AWS account. 51 | 2. After this, you will be asked to choose a support plan. Choose a basic plan which is free or choose another one if you want extra features. 52 | 3. Now you’ve successfully created your account. Now simply log in to the AWS console. 53 | 54 | ### Launch an Ubuntu EC2 instance 55 | 56 | Now to host our node.js apps we need an **EC2 virtual machine or instance**. So let’s get started to make our first EC2 instance up. 57 | 58 | 1. Log in to [AWS](https://aws.amazon.com) and click on the Sign in to the Console. Open your AWS console. 59 | 2. Click on EC2 in the All Services section. 60 | 61 | ![AWS console image](https://user-images.githubusercontent.com/98725622/221651498-c929493c-92b0-49d3-807e-c4ea7b948f33.png) 62 | 63 | 64 | 3. You will see a screen like below. Where all the currently running instances can be shown. Now click on the Running instances. 65 | 66 | ### Note: Right now I am having 1 instances and other services running so it is displaying there. But if you are new then there are 0 displaying. 67 | 68 | ![AWS EC2 instance image](https://user-images.githubusercontent.com/98725622/221651787-4f2b670f-2e53-4ec3-b840-bb2f153ffbd7.png) 69 | 70 | 4. After clicking on Running Instances you’ll see a Launch Instance button there. 71 | 72 | 5. Now, click on the Launch Instance button. After clicking you will see a page like this which shows the type of OS with the config you want in your machine e.i. We are going to use a Ubuntu Server 22.04 LTS 64-bit (x86). 73 | 74 | ![AWS Launch instance image](https://user-images.githubusercontent.com/98725622/221650590-5aa7487f-983a-4155-a48e-e453b7bd769a.png) 75 | 76 | ![instance-type](https://github.com/Axitvadi/aws-nginx-multiple-domain/assets/98725622/9448b3ce-3155-425a-b0a6-9ed5588e554f) 77 | 78 | 79 | 6. create new key pair if you are not already have. give a name of key and select format of key if you are windows user and connect your server using putty then select .ppk type or if you wish to use with open ssh then choose .pem type. 80 | 81 | ![key-pair](https://github.com/Axitvadi/aws-nginx-multiple-domain/assets/98725622/6471263e-8005-4b1f-8daa-8bb9a65f3891) 82 | 83 | 6. In advance details select Shutdown behavior to stop 84 | 85 | ![shut-down-behavior](https://github.com/Axitvadi/aws-nginx-multiple-domain/assets/98725622/a8c83b78-1ad2-49a7-99ca-a1ca14aad173) 86 | 87 | 7. click on launch instance button 88 | 89 | ![AWS console image](https://github.com/Axitvadi/aws-nginx-multiple-domain/assets/98725622/cce1b6ce-0fd3-49ec-930c-dbc592551e67) 90 | 91 | 8. Now we successfully launch our instance and click on view all instances. 92 | 93 | 9. After instance initialization, select the instance and click on connect. Then we can easily connect to our instance in browser, on Mac terminal by .pem file, and on Windows by using putty. 94 | 95 | ### Note: Keep your key in a secure place and don’t share your key with someone else not working with you on this. 96 | 97 | Your key must not be publicly viewable for SSH to work. Use the below command if needed. Always provide a correct path for the key. In below example, I am in the same directory in which my pen file or my key is present. 98 | 99 | chmod 400 path-for-key-here 100 | For Example: 101 | chmod 400 ./node-key.pem 102 | 103 | 10. 9. Now click on the instance id ( In my case i-0aaa83ad977631403 ) which appears in the green block above. You will be redirected to the instances page and now you can see your instance is up and running. 104 | 105 | ![AWS all instances](./assets/all-instances.png) 106 | 107 | ## SSH into our instance 108 | 109 | SSH means Secure Shell. The purpose of doing SSH to our instance is we want to access our server through a secure manner which you can access by using your terminal/cmd or using other Softwares like Putty. Now, depending upon your Operating System you can access your server in different manners. 110 | 111 | ### For Windows 112 | 113 | You can use Putty, etc. 114 | 115 | ## For Mac / Linux 116 | 117 | - Open your terminal 118 | - Go to AWS EC2 instances section and choose any instance. 119 | 120 | ![Public DNS](./assets/public_dns.png) 121 | 122 | - Copy the Public IP and write the below command in your terminal with your instance public IP which you have copied. 123 | 124 | Note: Please make sure your private key location is correct in your local PC. Which you have downloaded in the previous step. In below example, I am in the same directory in which the node-key.pem is present. 125 | 126 | ssh -i "private-key-location" ubuntu@public-ip 127 | For example 128 | ssh -i "./node-key.pem" ubuntu@ec2-54-161-91-174.compute-1.amazonaws.com 129 | 130 | ## In windows connect with Putty 131 | 132 | download and Install putty then open it. 133 | 134 | For generate .ppk file from .pem file that are downloaded from aws refer this link 135 | 136 | Please refer to the following link for instructions on generating a .ppk file from a .pem file that was downloaded from Amazon Web Services. 137 | 138 | https://asf.alaska.edu/how-to/data-recipes/connect-to-your-ec2-instance-using-putty-v1-1/ 139 | 140 | After generate .ppk file follow this instruction for connect your aws instance with putty 141 | 142 | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html 143 | 144 | ![Putty server connection ](./assets/connect-server-with-putty.png) 145 | 146 | ## Install Node.js and Git 147 | ### Installing Node : 148 | 149 | To run a Node.js app we require Node.js to be installed on our instance. So we will install all the required dependencies which can be used to install Node.js on our ubuntu instance. 150 | 151 | There are many ways to install node on our instance: 152 | 153 | Installing Node.js and npm from NodeSource 154 | 155 | First, of all Enable the NodeSource repository by running the following curl command as a sudo user on your instance using the terminal: 156 | 157 | Here is the full documentation of how to install node.js in Ubuntu. You can install node.js on your server by following these instructions. 158 | 159 | https://github.com/nodesource/distributions/blob/master/README.md 160 | 161 | OR 162 | 163 | https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04 164 | 165 | OR 166 | 167 | Run this command to install node.js 168 | 169 | curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ 170 | sudo apt-get install -y nodejs 171 | 172 | Here you can change node version by providing setup number like setup_14.x in above command 173 | 174 | Now, Verify that the Node.js and npm were successfully installed by printing their versions: 175 | 176 | $ node --version 177 | 178 | Output: v19.7.0 179 | 180 | $ npm --version 181 | 182 | Output: 9.5.0 183 | 184 | ![Node version check](./assets/node-version-check.png) 185 | 186 | ## Installing Git : 187 | 188 | To get our code on our EC2 Instance we need git. So, the below steps will define how to install Git on our EC2 instance. 189 | 190 | To refresh all the packages run 191 | 192 | $ sudo apt update -y 193 | 194 | ![Git install image](./assets/git-install.png) 195 | 196 | ### Clone repository from GitHub 197 | 198 | Now, we have Node.js and Git installed on our EC2 Instance. Now, we need the codebase on our ec2 instance. So, we will clone our repositories using the username and password from services like Github, Bitbucket, Gitlab, etc. I am using Github here. 199 | 200 | Go to Github. 201 | Navigate to your repository which you want to clone. 202 | Click on Code. 203 | -------------------------------------------------------------------------------- /assets/all-instances.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/all-instances.png -------------------------------------------------------------------------------- /assets/aws-launch-instance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws-launch-instance.png -------------------------------------------------------------------------------- /assets/aws.ss-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-1.png -------------------------------------------------------------------------------- /assets/aws.ss-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-2.png -------------------------------------------------------------------------------- /assets/aws.ss-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-3.png -------------------------------------------------------------------------------- /assets/aws.ss-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-4.png -------------------------------------------------------------------------------- /assets/aws.ss-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-5.png -------------------------------------------------------------------------------- /assets/aws.ss-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-6.png -------------------------------------------------------------------------------- /assets/aws.ss-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss-7.png -------------------------------------------------------------------------------- /assets/aws.ss.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/aws.ss.png -------------------------------------------------------------------------------- /assets/connect-server-with-putty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/connect-server-with-putty.png -------------------------------------------------------------------------------- /assets/git-install.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/git-install.png -------------------------------------------------------------------------------- /assets/instance-type.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/instance-type.png -------------------------------------------------------------------------------- /assets/key-pair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/key-pair.png -------------------------------------------------------------------------------- /assets/launch-log.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/launch-log.png -------------------------------------------------------------------------------- /assets/node-version-check.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/node-version-check.png -------------------------------------------------------------------------------- /assets/public_dns.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/public_dns.png -------------------------------------------------------------------------------- /assets/shut-down-behavior.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Axitvadi/aws-nginx-multiple-domain/2098b043e0f7b6a794a82574d2cd9363256a7aad/assets/shut-down-behavior.png --------------------------------------------------------------------------------