# Starter Script or User Data (NGINX as Reverse Proxy for Node.js with PM2) [Amazon Linux 2] - Install Git - Install Nginx - Setup Nginx as a Reverse Proxy for your Node.js Application - Install Node using NVM - Install PM2 - Run a Dummy API Server Using express - Start the Server using PM2 - Auto Start PM2 after a server reboot. Login to Ec2 Instance - `ssh ec2-user@54.221.355.461 -i ~/.ssh/temp-personal.pem` > This script is run by root user. But some part (`subscript`) of it is run as `ec2-user` so it can install `node.js` without sudo. > Don't forget to allow HTTP Port 80 in your Security Group. > The following script will completely overwrite your EC2 server's NGINX configuration to setup a reverse proxy. > Don't forget to add a backslash **`\`** if you want to use **$** inside the `subscript`. > You can check the output of this script `cat /var/log/cloud-init-output.log` or `cat /var/log/cloud-init.log`. ```sh #!/bin/bash cd /home/ec2-user/ ## Updating Packages sudo yum update -y ## Installing Git Client sudo yum install git -y ## Installing Cronttab sudo yum install crontabs -y sudo chkconfig crond on sudo service crond start ## Install Python sudo yum install python3.x86_64 -y ## For system to be able to compile software, you need many development tools, such as make, gcc, and autoconf. sudo yum groupinstall "Development Tools" -y ## Required By MongoDB sudo yum install krb5-devel -y sudo yum install cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs libcurl libpcap net-snmp openldap openssl -y ## Installing Nginx sudo amazon-linux-extras install nginx1.12 -y ## Modifying Nginx Server Configuration sudo cat > /etc/nginx/nginx.conf < /tmp/subscript.sh << EOF ## Installing NVM curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-usr/.bashrc echo '[ -s "\$NVM_DIR/nvm.sh" ] && . "\$NVM_DIR/nvm.sh" # This loads nvm' >> /home/ec2-user/.bashrc ## Dot source the files to ensure that variables are available within the current shell . /home/ec2-user/.nvm/nvm.sh . /home/ec2-user/.bashrc ## Install Node.js nvm install v10.16.3 nvm use v10.16.3 nvm alias default v10.16.3 ## Installing Global PM2 package npm install -g pm2 ## Installing Yarn curl -o- -L https://yarnpkg.com/install.sh | bash export PATH="\$HOME/.yarn/bin:\$HOME/.config/yarn/global/node_modules/.bin:\$PATH" ## Creating API Directory mkdir api cd api ## You'd probably want to connect to your git repository here and ## pull the master branch, then build and run it. ## But we'll create a mini express server yarn init -y yarn add express cat > ./app.js <