How to Host an API and a Website on an Amazon AWS EC2 Instance

How to Host an API and a Website on an Amazon AWS EC2 Instance
If you’re a data scientist or software engineer, you will likely encounter a scenario where you need to host an API and a website on an Amazon AWS EC2 instance. This post will walk you through the process, providing a step-by-step guide on how to set up your EC2 instance for this purpose.
Step 1: Configuring Your EC2 Instance
Firstly, you need to set up an Amazon AWS EC2 instance. Here is a brief rundown:
- Log into your AWS Management Console and navigate to the EC2 dashboard.
- Click on ‘Launch Instance’.
- Select your desired Amazon Machine Image (AMI). For this guide, we’ll use the Amazon Linux 2 AMI.
- Choose your instance type. For small projects, a t2.micro instance is usually sufficient.
- Review and launch your instance.
Remember to configure your instance’s security group to allow HTTP and HTTPS traffic.
Step 2: Installing the Necessary Software
SSH into your EC2 instance using your key pair. Once logged in, you need to install the necessary software. For this guide, we’ll assume you’re hosting a Node.js API and a static HTML website.
- Update your package manager:
sudo yum update -y
- Install Node.js:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
followed bynvm install node
- Install Nginx:
sudo amazon-linux-extras install nginx1.12 -y
Step 3: Setting Up Your API
Next, clone your API’s code onto your EC2 instance. You can use Git or simply copy the files over SCP. Install your project’s dependencies using npm install
.
To keep your API running even if you disconnect from SSH, use a process manager like PM2. Install it using npm install pm2 -g
, then start your API with pm2 start app.js
, replacing ‘app.js’ with your main file’s name.
Step 4: Setting Up Your Website
Place your website’s files in the Nginx default root directory, typically /usr/share/nginx/html/
.
Step 5: Configuring Nginx
Finally, you need to configure Nginx to serve both your API and website.
- Navigate to the Nginx configuration directory:
cd /etc/nginx/
- Open the Nginx configuration file in a text editor:
sudo nano nginx.conf
- In the ‘server’ block, add a ‘location’ block for your API:
location /api/ {
proxy_pass http://localhost:3000; # replace with your API's port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
This configuration forwards any requests with ‘/api/’ in the URL to your Node.js API.
- Save and exit the file, then restart Nginx:
sudo service nginx restart
That’s it! Your EC2 instance is now hosting both an API and a website. Remember to replace ‘/api/’ and ‘localhost:3000’ with your actual API’s route and port.
In conclusion, the Amazon AWS EC2 service offers a flexible and robust platform for hosting your APIs and websites. It may seem complex at first, but once you get the hang of it, the process becomes straightforward. Hopefully, this guide has provided you with a clear path to follow. Happy coding!
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.