{"id":23144012,"url":"https://github.com/shreyxnsh/apis-on-ec2","last_synced_at":"2026-04-20T13:07:08.731Z","repository":{"id":221099793,"uuid":"753175496","full_name":"shreyxnsh/apis-on-ec2","owner":"shreyxnsh","description":"Host NodeJS API's on AWS EC2 instance using these simple steps.","archived":false,"fork":false,"pushed_at":"2025-02-19T15:23:59.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T12:15:32.153Z","etag":null,"topics":["api","api-rest","aws","nodejs","nodejs-server","rest-api","route53"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shreyxnsh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-05T16:05:15.000Z","updated_at":"2025-02-19T15:24:15.000Z","dependencies_parsed_at":"2024-02-06T07:25:38.976Z","dependency_job_id":"bf646b06-542d-4421-8e6d-2a0564417118","html_url":"https://github.com/shreyxnsh/apis-on-ec2","commit_stats":null,"previous_names":["shreyxnsh/apis-on-ec2"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/shreyxnsh/apis-on-ec2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fapis-on-ec2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fapis-on-ec2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fapis-on-ec2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fapis-on-ec2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shreyxnsh","download_url":"https://codeload.github.com/shreyxnsh/apis-on-ec2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shreyxnsh%2Fapis-on-ec2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32048474,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T11:35:06.609Z","status":"ssl_error","status_checked_at":"2026-04-20T11:34:48.899Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-rest","aws","nodejs","nodejs-server","rest-api","route53"],"created_at":"2024-12-17T15:14:42.285Z","updated_at":"2026-04-20T13:07:08.715Z","avatar_url":"https://github.com/shreyxnsh.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Host NodeJs REST APIs on AWS EC2\nHost NodeJS API's on AWS EC2 instance using these simple steps.\n\n\n\n## Commands\n\nAfter connecting to your EC2 instance via a SSH client : \n\n\nRun these four commands to install all the required packages in your machine : NodeJS , NPM , Nginx\n\n```bash\n  sudo apt update\n```\n\n```bash\n  sudo apt install nodejs\n```\n\n```bash\n  sudo apt install npm\n```\n\n```bash\n  sudo apt install nginx\n```\n\n```bash\n  sudo apt update\n```\n\n\nClone your project's github repository into the EC2 machine in a folder (ex : api-folder)\n```bash\n  git clone \"https://github.com/your-repo-link\"\n```\n\n\nMove to the api-folder directory\n```bash\n  cd api-folder\n```\nNavigate to the .env file to paste your environment variables\n```bash\n  nano .env\n```\n\nInstall NVM (Node Version Manager)\n```bash\n  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash\n```\n\n```bash\n  export NVM_DIR=\"$([ -z \"${XDG_CONFIG_HOME-}\" ] \u0026\u0026 printf %s \"${HOME}/.nvm\" || printf %s \"${XDG_CONFIG_HOME}/nvm\")\"\n```\n\n```bash\n  [ -s \"$NVM_DIR/nvm.sh\" ] \u0026\u0026 \\. \"$NVM_DIR/nvm.sh\"\n```\n\n```bash\n  nvm install v18\n```\nInstall NPM packages of your project\n```bash\n  npm i\n```\n\nRun any of these as per your project's package.json\n```bash\n  npm run dev    /   npm start\n```\nCreate the server code file for your domain (ex : testapi.com)\n```bash\n  sudo nano /etc/nginx/sites-available/testapi.com\n```\nOnce you run the command above, you will enter an environment where you have to paste this code, MAKE SURE to change server_name and root folder directory\n```bash\n  server {\n        listen 80;\n        listen 443;\n        server_name testapi.com;\n        root /home/ubuntu/api-folder;\n        index index.html index.htm;\n\n        location / {\n                proxy_pass             http://127.0.0.1:3000;\n                proxy_read_timeout     60;\n                proxy_connect_timeout  60;\n                proxy_redirect         off;\n\n                # Allow the use of websockets\n                proxy_http_version 1.1;\n                proxy_set_header Upgrade $http_upgrade;\n                proxy_set_header Connection 'upgrade';\n                proxy_set_header Host $host;\n                proxy_cache_bypass $http_upgrade;\n        }\n         location /as {\n        access_log off;\n        add_header 'Content-Type' 'text/plain';\n    }\n\n}\n```\n\nAfter pasting this code save and exit it using : \n```bash\n  Ctrl + S  --\u003e Ctrl + C  --\u003e Ctrl + X\n```\nPaste these commands as it is (change your domain name)\n```bash\n  sudo ln -s /etc/nginx/sites-available/testapi.com /etc/nginx/sites-enabled/testapi.com\n```\n\n```bash\n  sudo rm -rf /etc/nginx/sites-enabled/default\n```\n\nRestart your nginx service after changing server code : \n```bash\n  sudo service nginx restart\n```\n\n\n```bash\n  systemctl status nginx.service\n```\n```bash\n  sudo service nginx restart\n```\nCheck if your nginx is completely healthy :\n```bash\n  sudo service nginx status\n```\nInstall pm2 (Process Manager) globally in this machine\n```bash\n  sudo npm i -g pm2\n```\n\nStart your server : (must have in package.json --\u003e  \"start\": \"nodemon index)\n```bash\n  pm2 start npm -- start\n```\nGet the logs of your running server to know what's happening\n```bash\n  pm2 logs 0\n```\n\nStop the server\n```bash\n  pm2 stop 0\n```\n\n\n# Get SSL Certificate for https protocol\nRun these commands and change the nginx server configuration to accept 443 ssl requests.\n\n\nUpdate and install certbot\n```bash\nsudo apt update\nsudo apt install certbot python3-certbot-nginx -y\n\n```\nRun the certbot\n```bash\nsudo certbot --nginx -d testapi.com\n```\nNano server configuration\n```bash\nsudo nano /etc/nginx/sites-enabled/testapi.com\n```\n\nUpdate Server configuration \n```bash\n  \nserver {\n    listen 443 ssl;\n    server_name testapi.com;\n\n    ssl_certificate /etc/letsencrypt/live/testapi.com/fullchain.pem;\n    ssl_certificate_key /etc/letsencrypt/live/testapi.com/privkey.pem;\n\n    location / {\n        proxy_pass http://127.0.0.1:5000;\n        proxy_read_timeout 60;\n        proxy_connect_timeout 60;\n        proxy_redirect off;\n\n        proxy_http_version 1.1;\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection 'upgrade';\n        proxy_set_header Host $host;\n        proxy_cache_bypass $http_upgrade;\n    }\n}\n\nserver {\n    listen 80;\n    server_name testapi.com;\n    return 301 https://$host$request_uri;\n}\n```\nCheck Server\n```bash\nsudo nginx -t\n```\nRestart nginx\n```bash\nsudo systemctl restart nginx\n```\nAuto renew certificate after 90 days\n```bash\nsudo certbot renew --dry-run\n```\nThe above commands will allow https request and route http to https in browser but if you want to accept http requests then update the server configuration to this \n```bash\nserver {\n    listen 80;\n    server_name testapi.com;\n\n    location / {\n        proxy_pass http://127.0.0.1:8000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n\nserver {\n    listen 443 ssl;\n    server_name testapi.com;\n\n    ssl_certificate /etc/letsencrypt/live/testapi.com/fullchain.pem;\n    ssl_certificate_key /etc/letsencrypt/live/testapi.com/privkey.pem;\n\n    location / {\n        proxy_pass http://127.0.0.1:8000;\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n\n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyxnsh%2Fapis-on-ec2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshreyxnsh%2Fapis-on-ec2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshreyxnsh%2Fapis-on-ec2/lists"}