Step-by-Step Guide to Installing Strapi with PostgreSQL on AWS

November 21, 2024
If you're looking to leverage AWS's robust cloud infrastructure for deploying Strapi with PostgreSQL, this guide will walk you through the entire process. From setting up your database to deploying Strapi on EC2, we've got you covered.
1. Initial Preparations
Before diving in, ensure you have the following:
- AWS Account: A valid and active account on AWS.
- Tools Installed:
2. Set Up PostgreSQL on AWS RDS
Create an RDS Instance
- Log in to the AWS Console and navigate to the RDS service.
- Click Create Database and select PostgreSQL as the engine.
- Choose "Free Tier" if applicable.
- Configure the database, including username and password.
- Under connectivity, enable public access if needed.
Configure Security Groups
- Navigate to EC2 Security Groups.
- Create or edit a group to allow inbound traffic on port 5432 for PostgreSQL.
3. Launch an EC2 Instance
Set Up the Instance
- Launch an EC2 instance from the AWS Console.
- Choose the Ubuntu 22.04 LTS AMI.
- Select a suitable instance type (e.g.,
t2.small). - Configure storage, tags, and networking.
Connect to EC2
- Use SSH to access the instance:
ssh -i "path/to/key.pem" ubuntu@your-public-ip
4. Install Dependencies
Update your EC2 instance and install required software:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
npm install -g yarn # optional
5. Deploy Strapi
Create a Strapi Project
npx create-strapi-app my-project --quickstart
cd my-project
npm install pg
Configure PostgreSQL
Edit config/database.js:
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: env('DATABASE_HOST', 'your-rds-endpoint'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'your-database-name'),
user: env('DATABASE_USERNAME', 'your-username'),
password: env('DATABASE_PASSWORD', 'your-password'),
ssl: env.bool('DATABASE_SSL', false),
},
debug: false,
},
});
Run Strapi
npm run develop
Access the admin panel at http://your-public-ip:1337/admin.
6. Set Up Nginx (Optional)
Install Nginx:
sudo apt install -y nginx
Configure a reverse proxy for Strapi on port 1337.
7. Enable SSL (Optional)
Install Certbot:
sudo apt install -y certbot python3-certbot-nginx
Run:
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
Conclusion
By following this guide, you can successfully deploy Strapi with PostgreSQL on AWS, leveraging AWS’s scalability and reliability. For production environments, consider adding monitoring and auto-scaling features to optimize performance further.