Created as a hacky demo of cloudfront and ec2.
Designed to be run on EC2 with a cloudfront distribution in front of it.
The rest api uses json-server to make it easy.
- Install node js on the EC2 instance. This article suggests using nvm to install nodejs, and it's pretty straightforward. You'll need to log out and back in again to EC2 to get nvm in your path.
- Get the code from
./restapp
onto the server (git clone maybe) and install the node modules usingnpm install
- You may need to configure the server by modifying
json-server.json
- I have it listening on 0.0.0.0 and port 8000 (depends on what port you added to your ec2 security group) - you can run this using
npm start
now, but as soon as you close the session it will stop running. I used pm2 to manage running it persistently (npm install pm2 -g
). The command I used waspm2 start npm --name "backend" -- start
- And you're done (apart from cloudfront) - now you can visit your EC2 instances via IP and port to view your restAPI (e.g. http://34.249.119.149:8000/tasks - that IP is probably incorrect now).
- You'll need nginx installed. That should be possible via
sudo amazon-linux-extras install nginx1
and then starting it viasudo systemctl start nginx
- Your content root is
/usr/share/nginx/html
by default. Either put the website there, or modify your content root by editing the nginx conf at/etc/nginx/nginx.conf
- my server conf section is copied below for reference. You may need to modify directory permissions of your new content root to let nginx read those files.
server {
listen 80;
listen [::]:80;
server_name _;
root /home/ec2-user/webroot;
# This was the original
# root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}