├── .gitignore ├── .github └── FUNDING.yml ├── package.json ├── app.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: saasscaleup 4 | ko_fi: scaleupsaas 5 | custom: buymeacoffee.com/scaleupsaas 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-ssl-server", 3 | "version": "1.0.0", 4 | "description": "Show you how to deploy nodejs server on AWS EC2 Ubuntu server with Free SSL and Nginx", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/saasscaleup/nodejs-ssl-server.git" 12 | }, 13 | "keywords": [ 14 | "nodejs", 15 | "AWS", 16 | "EC@", 17 | "instanace", 18 | "Free", 19 | "SSL", 20 | "Nginx", 21 | "Reverse", 22 | "Proxy", 23 | "Expressjs" 24 | ], 25 | "author": "Scale-Up SaaS", 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/saasscaleup/nodejs-ssl-server/issues" 29 | }, 30 | "homepage": "https://github.com/saasscaleup/nodejs-ssl-server#readme", 31 | "dependencies": { 32 | "express": "^4.18.2" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | const hostname = '127.0.0.1'; // Your server ip address 4 | const port = 3000; 5 | 6 | const version = '1.0.0'; 7 | 8 | app.get('/', (req, res) => { 9 | // set response content 10 | res.send(` 11 | 12 |

[Version ${version}]: This is AMAZING!!! Like & Subscribe!

13 |
14 | 15 |
16 | 17 | `); 18 | 19 | console.log(`[Version ${version}]: New request => http://${hostname}:${port}`+req.url); 20 | 21 | }) 22 | 23 | app.listen(port, () => { 24 | console.log(`[Version ${version}]: Server running at http://${hostname}:${port}/`); 25 | }) 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nodejs-ssl-server 2 | 3 | How to deploy nodejs app to AWS EC2 Ubuntu 22 Server with free SSL and Nginx reverse proxy 4 | 5 | 6 | 7 | ## Installation instructions 8 | 9 | ### 1. Launch amazon ubuntu server in aws + Attach Elastic IP to the new instance 10 | 11 | ### 2. ssh to ubuntu to install packages 12 | 13 | ```sh 14 | ssh -i ubuntu@ -v 15 | ``` 16 | 17 | ### 3. Update and Upgrade linux machine and install node and nvm 18 | 19 | ```sh 20 | sudo apt update 21 | ``` 22 | 23 | ```sh 24 | sudo apt upgrade 25 | ``` 26 | 27 | ```sh 28 | sudo apt install -y git htop wget 29 | ``` 30 | 31 | #### 3.1 install node 32 | 33 | To **install** or **update** nvm, you should run the [install script][2]. To do that, you may either download and run the script manually, or use the following cURL or Wget command: 34 | ```sh 35 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 36 | ``` 37 | Or 38 | ```sh 39 | wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 40 | ``` 41 | 42 | Running either of the above commands downloads a script and runs it. The script clones the nvm repository to `~/.nvm`, and attempts to add the source lines from the snippet below to the correct profile file (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`). 43 | 44 | #### 3.2 Copy & Past (each line separately) 45 | 46 | ```sh 47 | export NVM_DIR="$HOME/.nvm" 48 | [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm 49 | [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion 50 | ``` 51 | 52 | #### 3.3 Verify that nvm has been installed 53 | 54 | ```sh 55 | nvm --version 56 | ``` 57 | 58 | #### 3.4 Install node 59 | 60 | ```sh 61 | nvm install --lts # Latest stable node js server version 62 | ``` 63 | 64 | #### 3.5 Check nodejs installed 65 | ```sh 66 | node --version 67 | ``` 68 | 69 | #### 3.6 Check npm installed 70 | ```sh 71 | npm -v 72 | ``` 73 | 74 | ### 4. Clone nodejs-ssl-server repository 75 | 76 | ```sh 77 | cd /home/ubuntu 78 | ``` 79 | 80 | ```sh 81 | git clone https://github.com/saasscaleup/nodejs-ssl-server.git 82 | ``` 83 | 84 | ### 5. Run node app.js (Make sure everything working) 85 | 86 | ```sh 87 | cd nodejs-ssl-server 88 | ``` 89 | 90 | ```sh 91 | npm install 92 | ``` 93 | 94 | ```sh 95 | node app.js 96 | ``` 97 | 98 | ### 6. Install pm2 99 | ```sh 100 | npm install -g pm2 # may require sudo 101 | ``` 102 | 103 | ### 7. Starting the app with pm2 (Run nodejs in background and when server restart) 104 | ```sh 105 | pm2 start app.js --name=nodejs-ssl-server 106 | ``` 107 | ```sh 108 | pm2 save # saves the running processes 109 | # if not saved, pm2 will forget 110 | # the running apps on next boot 111 | ``` 112 | 113 | #### 7.1 IMPORTANT: If you want pm2 to start on system boot 114 | ```sh 115 | pm2 startup # starts pm2 on computer boot 116 | ``` 117 | 118 | ### 8. FREE SSL - Install Nginx web server 119 | 120 | ```sh 121 | sudo apt install nginx 122 | ``` 123 | 124 | ```sh 125 | sudo nano /etc/nginx/sites-available/default 126 | ``` 127 | 128 | #### Add the following to the location part of the server block 129 | 130 | ```sh 131 | server_name yourdomain.com www.yourdomain.com; 132 | 133 | location / { 134 | proxy_pass http://localhost:5000; #whatever port your app runs on 135 | proxy_http_version 1.1; 136 | proxy_set_header Upgrade $http_upgrade; 137 | proxy_set_header Connection 'upgrade'; 138 | proxy_set_header Host $host; 139 | proxy_cache_bypass $http_upgrade; 140 | } 141 | ``` 142 | 143 | ##### Check NGINX config 144 | ```sh 145 | sudo nginx -t 146 | ``` 147 | 148 | ##### Restart NGINX 149 | ```sh 150 | sudo service nginx restart 151 | ``` 152 | 153 | #### You should now be able to visit your IP with no port (port 80) and see your app. Now let's add a domain 154 | 155 | ### 9 Add domain in goDaddy.com 156 | If you have domain, you can add A record to your EC2 instance IP with a new subdomain as I'm going to show you next 157 | 158 | #### 9.1 Check that Port 80 redirect to Nodejs server 159 | 160 | ### 10 Installing Free SSL 161 | 162 | #### 10.1 Installing Certbot 163 | 164 | ```sh 165 | sudo snap install core; sudo snap refresh core 166 | ``` 167 | 168 | ```sh 169 | sudo apt remove certbot 170 | ``` 171 | 172 | ```sh 173 | sudo snap install --classic certbot 174 | ``` 175 | 176 | ```sh 177 | sudo ln -s /snap/bin/certbot /usr/bin/certbot 178 | ``` 179 | 180 | #### 10.2 Confirming Nginx’s Configuration 181 | ```sh 182 | sudo nano /etc/nginx/sites-available/default 183 | ``` 184 | 185 | let edit this line: 186 | ```sh 187 | ... 188 | server_name example.com www.example.com; 189 | ... 190 | ``` 191 | 192 | ```sh 193 | sudo nginx -t 194 | ``` 195 | 196 | ```sh 197 | sudo systemctl reload nginx 198 | ``` 199 | 200 | #### 10.3 Obtaining an FREE SSL Certificate 201 | ```sh 202 | sudo certbot --nginx -d app.example.com 203 | ``` 204 | 205 | Output: 206 | ``` 207 | IMPORTANT NOTES: 208 | Successfully received certificate. 209 | Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem 210 | Key is saved at: /etc/letsencrypt/live/your_domain/privkey.pem 211 | This certificate expires on 2022-06-01. 212 | These files will be updated when the certificate renews. 213 | Certbot has set up a scheduled task to automatically renew this certificate in the background. 214 | 215 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 216 | If you like Certbot, please consider supporting our work by: 217 | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate 218 | * Donating to EFF: https://eff.org/donate-le 219 | ``` 220 | 221 | #### 10.4 Verifying Certbot Auto-Renewal 222 | ```sh 223 | sudo systemctl status snap.certbot.renew.service 224 | ``` 225 | Output: 226 | ``` 227 | ○ snap.certbot.renew.service - Service for snap application certbot.renew 228 | Loaded: loaded (/etc/systemd/system/snap.certbot.renew.service; static) 229 | Active: inactive (dead) 230 | TriggeredBy: ● snap.certbot.renew.timer 231 | ``` 232 | 233 | To test the renewal process, you can do a dry run with certbot: 234 | 235 | ```sh 236 | sudo certbot renew --dry-run 237 | ``` 238 | 239 | ### 11. Visit your website HTTPS:// 240 | Enjoy Your free Nodejs server with Free SSL :) 241 | 242 | 243 | ## Support 🙏😃 244 | 245 | If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you: 246 | 247 | 1. Subscribe to My youtube channel and leave a comment: http://www.youtube.com/@ScaleUpSaaS?sub_confirmation=1 248 | 2. Buy me A coffee ❤️ : https://www.buymeacoffee.com/scaleupsaas 249 | 250 | Thanks for your support :) 251 | 252 | 253 | 254 | --------------------------------------------------------------------------------