All Projects → 61FINTECH → Deploy Strapi On Aws

61FINTECH / Deploy Strapi On Aws

Deploying a Strapi API on AWS (EC2 & RDS & S3)

Projects that are alternatives of or similar to Deploy Strapi On Aws

Awesome Aws
A curated list of awesome Amazon Web Services (AWS) libraries, open source repos, guides, blogs, and other resources. Featuring the Fiery Meter of AWSome.
Stars: ✭ 9,895 (+8077.69%)
Mutual labels:  aws, s3, ec2, rds
Awstaghelper
AWS bulk tagging tool
Stars: ✭ 98 (-19.01%)
Mutual labels:  aws, s3, ec2, rds
Hasura Backend Plus
🔑Auth and 📦Storage for Hasura. The quickest way to get Auth and Storage working for your next app based on Hasura.
Stars: ✭ 776 (+541.32%)
Mutual labels:  graphql, s3, pm2
Terraform Aws Alb
Terraform module to provision a standard ALB for HTTP/HTTP traffic
Stars: ✭ 53 (-56.2%)
Mutual labels:  aws, ec2, https
Check Reserved Instances
Compare instance reservations and running instances for AWS services
Stars: ✭ 102 (-15.7%)
Mutual labels:  aws, ec2, rds
Webiny Js
Enterprise open-source serverless CMS. Includes a headless CMS, page builder, form builder and file manager. Easy to customize and expand. Deploys to AWS.
Stars: ✭ 4,869 (+3923.97%)
Mutual labels:  graphql, aws, cms
Moto
A library that allows you to easily mock out tests based on AWS infrastructure.
Stars: ✭ 5,428 (+4385.95%)
Mutual labels:  aws, s3, ec2
Next
Directus is a real-time API and App dashboard for managing SQL database content. 🐰
Stars: ✭ 111 (-8.26%)
Mutual labels:  graphql, cms, postgresql
Aws Cost Saver
A tiny CLI tool to help save costs in development environments when you're asleep and don't need them!
Stars: ✭ 178 (+47.11%)
Mutual labels:  aws, ec2, rds
Udacity Data Engineering
Udacity Data Engineering Nano Degree (DEND)
Stars: ✭ 89 (-26.45%)
Mutual labels:  aws, s3, postgresql
Rpcheckup
rpCheckup is an AWS resource policy security checkup tool that identifies public, external account access, intra-org account access, and private resources.
Stars: ✭ 91 (-24.79%)
Mutual labels:  aws, s3, ec2
Ec2instances.info
Amazon EC2 instance comparison site
Stars: ✭ 3,619 (+2890.91%)
Mutual labels:  aws, ec2, rds
Directus
Open-Source Data Platform 🐰 — Directus wraps any SQL database with a real-time GraphQL+REST API and an intuitive app for non-technical users.
Stars: ✭ 13,190 (+10800.83%)
Mutual labels:  graphql, cms, postgresql
Aws
A collection of bash shell scripts for automating various tasks with Amazon Web Services using the AWS CLI and jq.
Stars: ✭ 493 (+307.44%)
Mutual labels:  aws, s3, ec2
Komiser
☁️ Cloud Environment Inspector 👮🔒 💰
Stars: ✭ 2,684 (+2118.18%)
Mutual labels:  aws, ec2, rds
Cloud Security Audit
A command line security audit tool for Amazon Web Services
Stars: ✭ 68 (-43.8%)
Mutual labels:  aws, s3, ec2
Aws Sdk Perl
A community AWS SDK for Perl Programmers
Stars: ✭ 153 (+26.45%)
Mutual labels:  aws, s3, ec2
Aws Inventory
Python script for AWS resources inventory (cheaper than AWS Config)
Stars: ✭ 69 (-42.98%)
Mutual labels:  aws, s3, ec2
Aws Cli Cheatsheet
☁️ AWS CLI + JQ = Make life easier
Stars: ✭ 94 (-22.31%)
Mutual labels:  aws, ec2, rds
Aws Workflows On Github
Workflows for automation of AWS services setup from Github CI/CD
Stars: ✭ 95 (-21.49%)
Mutual labels:  aws, s3, ec2

Deploying a Strapi API on AWS (EC2 & RDS & S3)

Goal: build a HTTPS-secured Strapi API with dev & staging & prod modes

Everything done in the following is AWS Free Tier eligible.
Make sure you have already skimmed the Strapi docs before you start.

§ Create EC2 instance / RDS instance / S3 bucket

You can skim this section if you are familiar with AWS

⊙ EC2

  1. Click Launch Instance button
  2. Choose AMI: Ubuntu Server 18.04 LTS (HVM), SSD Volume Type
  3. Choose Instance Type: General purpose, t2.micro
  4. Configure Instance: as you like
  5. Add Storage: General Purpose SSD (gp2), 8 GB
  6. Add Tags: as you like
  7. Configure Security Group:
    • SSH (22) - My IP or Anywhere or as you like
    • HTTP (80) - Anywhere
    • HTTPS (443) - Anywhere
  8. Review: click Launch button, then a modal pops up. If you are a:
    • Newbie: Choose Create a new key pair named strapi-cms, download it as strapi-cms.pem
    • Veteran: as you like
  9. Finally, click Launch Instances button

⊙ RDS

  1. Click Create database button
  2. Select engine: PostgreSQL
  3. Choose use case: as you like
  4. Specify DB details:
    • DB engine version: PostgreSQL 10.x-R1
    • DB instance class: db.t2.micro
    • Multi-AZ deployment: No
    • Storage: General Purpose (SSD), 20 GB
    • DB instance identifier: as you like
    • Master username: as you like
    • Password: as you like, recommend https://passwordsgenerator.net
  5. Configure advanced settings
    • Public accessibility: Yes (that's why you need a super strong password)
    • Database name: strapi
    • Monitoring & Maintenance window: choose an idle peroid in your timezone
    • Click Create database button
  6. Instance Details panel - Security groups
    • Edit inbound rules: PostgreSQL (5432) - Anywhere
  7. Create databases for dev & staging modes (GUI recommend: https://dbeaver.io)
    • Development mode: strapi_dev
    • Staging mode: strapi_staging
    • Production mode: strapi (already exists)

⊙ S3

  1. Click Create bucket button
  2. Name and region
    • Bucket name: as you like
  3. Configure options: as you like
  4. Set permissions
      • [ ] Block new public ACLs and uploading public objects (Recommended)
      • [ ] Remove public access granted through public ACLs (Recommended)
      • [x] Block new public bucket policies (Recommended)
      • [x] Block public and cross-account access if bucket has public policies (Recommended)
    • Do not grant Amazon S3 Log Delivery group write access to this bucket
  5. Review: click Create bucket button

§ Point your domain to EC2

Point the A / CNAME records to the EC2's IPv4 Public IP / Public DNS (IPv4), such as:

  • Development mode: dev-cms.yourdomain.com
  • Staging mode: staging-cms.yourdomain.com
  • Production mode: cms.yourdomain.com

§ Warm up EC2

⊙ Login to EC2 & update

Switch to the directory where the key pair strapi-cms.pem locates:

$ ssh -i strapi-cms.pem [email protected]<ec2-public-ip>
$ sudo apt update

⊙ Install Nginx

$ sudo apt install nginx

⊙ Install nvm & Node.js & PM2

  • Install nvm:
    $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
    
  • Install Node.js:
    $ nvm install 10
    
  • Install PM2:
    $ npm i pm2 -g
    

⊙ Install Certbot

According to https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx :

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install python-certbot-nginx

§ Create a local Strapi project

⊙ Installation and initialization

Firstly, install Strapi (refer to https://strapi.io/documentation/3.x.x/getting-started/installation.html):

$ npm i -g [email protected]

Secondary, create a new project (refer to https://strapi.io/documentation/3.x.x/cli/CLI.html#strapi-new):

$ strapi new strapi-cms \
    --dbclient=postgres \
    --dbport=5432 \
    --dbhost=<RDS endpoint> \
    --dbname=strapi_dev \
    --dbusername=<RDS master username> \
    --dbpassword=<RDS password>

Finally, DO NOT rush to strapi start for now.
There are some preparatory procedures to accomplish.
(By the way, you may be interested in https://strapi.io/documentation/3.x.x/advanced/usage-tracking.html)

⊙ Complete database configuration

If you prefer best practices like https://12factor.net/config and https://github.com/i0natan/nodebestpractices/blob/master/sections/projectstructre/configguide.md , you'd better use tools like dotenv instead.

strapi-cms/config/environments/development/database.json has been completed during the initialization.
Complete strapi-cms/config/environments/{staging|production}/database.json based on it. For example:

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "strapi-hook-bookshelf",
      "settings": {
        "client": "postgres",
        "host": "<RDS endpoint>",
        "port": 5432,
        "database": "<strapi_staging|strapi>",
        "username": "<RDS master username>",
        "password": "<RDS password>"
      },
      "options": {
        "ssl": false
      }
    }
  }
}

⊙ Fix listening ports

Modify port in strapi-cms/config/environments/{staging|production}/server.json:

  • development: 1337 (default)
  • staging: 1338
  • production: 1339

⊙ Install S3 upload plugin

Refer to https://strapi.io/documentation/3.x.x/guides/upload.html#install-providers

$ npm i -S [email protected]

⊙ Add npm scripts for staging & production mode

You can use PM2 - Ecosystem File instead if you'd like to

npm start is for development mode by default.
You can set NODE_ENV before it according to https://strapi.io/documentation/3.x.x/guides/deployment.html .
However, for the sake of compatibility, cross-env is introduced.

$ npm i -D cross-env

So strapi-cms/package.json may look like:

{
  ...
  "scripts": {
    "setup": "cd admin && npm run setup",
    "start": "node server.js",                         // for development mode
    "staging": "cross-env NODE_ENV=staging npm start", // for staging mode 
    "prod": "cross-env NODE_ENV=production npm start", // for production mode
    "strapi": "node_modules/strapi/bin/strapi.js",
    "lint": "node_modules/.bin/eslint api/**/*.js config/**/*.js plugins/**/*.js",
    "postinstall": "node node_modules/strapi/lib/utils/post-install.js"
  },
  ...
}

⊙ Add nginx.conf to the project root

If you prefer best practices using /etc/nginx/{sites-available|sites-enabled}, you may need help from https://nginxconfig.io or https://github.com/h5bp/server-configs-nginx . I prefer single file nginx.conf because of simplicity.

Don't forget to replace all yourdomain.com with yours.

⊙ Run

$ cd strapi-cms && strapi start

Your browser will open http://localhost:1337 automatically later.
Create an admin with a strong password.

⊙ Complete S3 settings

Visit PLUGINS > Files Upload to complete the S3 settings for all modes.

⊙ GraphQL

If you'd like to equip GraphQL, visit GENERAL > Marketplace to download.
Refer to https://strapi.io/documentation/3.x.x/guides/graphql.html for further info.

⊙ Configure response & security

Since Strapi will be running behind a well-tuned Nginx, you should:

For more details, please turn to https://strapi.io/documentation/3.x.x/configurations/configurations.html

⊙ Push to a Github free private repo (or Gitlab, etc)

$ git init
$ git add -A
$ git commit -m 'init'
$ git remote add origin <private-git-repo-url>
$ git push -u origin master

§ Deploy Strapi on EC2

⊙ Pull the repo and install npm dependencies

$ cd ~
$ git clone <private-git-repo-url>
$ cd strapi-cms
$ npm i

⊙ Run all modes with PM2

Thanks to this Stack Overflow comment, you can run npm scripts with PM2:

$ pm2 start npm --name "strapi-dev" -- start
$ pm2 start npm --name "strapi-staging" -- run staging
$ pm2 start npm --name "strapi-prod" -- run prod

Also, you need to set up the PM2 Startup Hook in case of reboot:

$ pm2 startup
$ pm2 save

⊙ Replace /etc/nginx/nginx.conf with yours

$ cd ~/strapi-cms
$ sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
$ sudo cp nginx.conf /etc/nginx/nginx.conf

⊙ Obtain SSL certificates

According to https://nginxconfig.io :

# HTTPS - certbot (before first run): create ACME-challenge common directory
$ sudo mkdir -p /var/www/_letsencrypt && chown www-data /var/www/_letsencrypt

# HTTPS - certbot (before first run): disable SSL directives
$ sudo sed -i -r 's/(listen .*443)/\1;#/g; s/(ssl_(certificate|certificate_key|trusted_certificate) )/#;#\1/g' /etc/nginx/nginx.conf

# Reload Nginx config
$ sudo systemctl reload nginx

# HTTPS - certbot: obtain certificates
$ sudo certbot certonly
    --webroot -n --agree-tos --force-renewal \
    -w /var/www/_letsencrypt \
    --email <your-email> \
    -d cms.<yourdomain.com> \
    -d staging-cms.<yourdomain.com> \
    -d dev-cms.<yourdomain.com>

# HTTPS - certbot (after first run): enable SSL directives
$ sudo sed -i -r 's/#?;#//g' /etc/nginx/nginx.conf

# Reload Nginx config again
$ sudo systemctl reload nginx

All done! Visit https://{cms|staging-cms|dev-cms}.yourdomain.com to see if it works.

⊙ Further optimizations

§ Summary

Now you have:

  • A Strapi API running on dev & staging & prod modes simultaneously
  • PM2-guarded processes with reboot startup hooks
  • Forced HTTPS-secured traffic for all
  • Auto-renew SSL certificates for free

PRs & issues are welcome! Sharing your experience will save others' time! tip

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].