1. blogging

Install Strapi on Amazon AWS with 1-Click

This is a step-by-step guide for deploying a Strapi project to Amazon AWS EC2. This guide will hook up with an Amazon AWS RDS for managing and hosting the database.Optionally, this guide will show you how to connect host and serve images on Amazon AWS S3.

Prior to starting this guide, you ought to have created a Strapi project, to use for deploying on AWS. And have read through the configuration section.

You must have an Amazon AWS account before doing these steps.Best practices for using AWS Amazon services state to not use your root account user and to use instead the IAM (AWS Identity and Access Management) service.

Your root user is therefore only used for a really few select tasks. For example, for billing, you create an Administrator user and Group for such things.And other, more routine tasks are done with a regular IAM User.

  1. creating your Administrator IAM Admin User and Group
  • Login as root.
  • Create Administrator role

2.create a regular user for the creation and management of your Strapi project

  • Copy your IAM Users sign-in link found here: IAM Console and then log out of your root user and log in to your administrator user you just created.
  • Return to the IAM Console by searching for IAM and clicking or going here: IAM Console
  • Click on Users, in the left hand menu, and then click Add User

IMPORTANT CREDENTIALS

If you do not do these steps you will have to reset your Access key ID and Secret access key later.

Download the .csv file and store it in a safe place. This contains the user name, login link, Access key ID and Secret access key.

Launch an EC2 virtual machine

Amazon calls a virtual private server, a virtual server or Amazon EC2 instance. To use this service you will Launch Instance.

In this section, you will establish IAM credentialslaunch a new instance and set-up primary security rules.

From your AWS Management Console and as your regular user

Find Services, search for ec2 and click on EC2, Virtual Servers in the Cloud

Click on the blue Launch Instance button

  • Select Ubuntu Server 18.04 LTS (HVM), SSD Volume Type
  • Ensure General purpose + t2.small is checked
  • Click the grey Next: Configure Instance Details and Next: Add Storage
  • In the Step 4: Add Storage verify the General Purpose SSD (gb2), then click Next: Add tags.
  • In the Step 5: Add Tags, add tags to suit your project or leave blank, then click Next: Configure Security Group.
  • In the Step 6: Configure Security Group, configure the security settings as follows:Assign a security group: Check as Create a new security group
  • Security group name: Name it, e.g. strapi
  • Description: Write a short description, e.g. strapi instance security settings
  • You should have a rule: Type: SSHProtocol: TCPPort Range 22Source: 0.0.0.0/0 (all IP addresses). If not, add it.
  • Click the grey Add rule to add each of these rules:Type: SSHProtocol: TCPPort Range 22Source: ::/0
  • Type: HTTPProtocol: TCPPort Range 80Source: 0.0.0.0/0, ::/0
  • Type: HTTPSProtocol: TCPPort Range 443Source: 0.0.0.0/0, ::/0
  • Type: Custom TCP RuleProtocol: TCPPort Range 1337Source: 0.0.0.0/0 Description: Strapi for Testing Port These rules are basic configuration and security rules. You may want to tighten and limit these rules based on your own project and organizational policies.
  • Click the blue Review and Launch button.
  • Review the details, in the Step 7: Review Instance Launch, then click the blue Launch button. Now, you need to select an existing key pair or create a new key pair. To create a new key pair, do the following:Select the dropdown option Create a new key pair.
  • Name your the key pair name, e.g. ec2-strapi-key-pair

Install a PostgreSQL database on AWS RDS

  1. Navigate to the AWS RDS Service

In the top menu, click on Services and do a search for rds, click on RDS, Managed Relational Database Service

2.Create the database

Click the orange Create database button. Follow these steps to complete installation of a PostgreSQL database:

  • Engine Options: Click on PostgreSQL, version PostgreSQL 10.x-R1
  • Templates: Click on Free Tier.
  • Settings
    • DB instance identifier Give a name to your database, e.g. strapi-database
    • Credential Settings: This is your psql database username and password.
      • Master username: Keep as postgres, or change (optional)
      • Uncheck Auto generate a password, and then type in a new secret password.
  • Connectivity, and Additional connectivity configuration: Set Publicly accessible to Yes.
  • OPTIONAL: Review any further options (DB Instance sizeStorageConnectivity), and modify to your project needs.
  • You need to give you Database a name. Under Additional configuration:
    • Additional configuration, and then Initial database name: Give your database a name, e.g. strapi.
  • Review the rest of the options and click the orange, Create database button.

Configure S3 for image hosting

Amazon calls cloud storage services S3. You create a bucket, which holds the files, images, folders, etc… which then can be accessed and served by your application. This guide will show you have to use Amazon S3 to host the images for your project.

1. Navigate to the Amazon S3

In the top menu, click on Services and do a search for s3, click on Scalable storage in the cloud.

2. Create the bucket

Click on the blue Create bucket button:

  • Give your bucket a unique name, under Bucket name, e.g. my-project-name-images.
  • Select the most appropriate region, under Region, e.g. EU (Paris) or US East (N. Virgina).
  • Click Next.
  • Configure any appropriate options for your project in the Configure Options page, and click next.
  • Under Block public access:
    • Uncheck Block all public access and set the permissions as follows:
      • Uncheck Block new public ACLs and uploading public objects (Recommended)
      • Uncheck Block public access to buckets and objects granted through any access control lists (ACLs)
      • Check Block public access to buckets and objects granted through new public bucket policies
      • Check Block public and cross-account access to buckets and objects through any public bucket policies
    • Select Do not grant Amazon S3 Log Delivery group write access to this bucket.
  • Click Next.
  • Review and click Create bucket.

Configure EC2 as a Node.js server

You will set-up your EC2 server as a Node.js server. Including basic configuration and Git.

You will need your EC2 ip address:

  • In the AWS Console, navigate to the AWS EC2. In the top menu, click on Services and do a search for ec2, click on Virtual Servers in the cloud.
  • Click on 1 Running Instance and note the IPv4 Public OP address. E.g. 34.182.83.134.

#1. Setup the .pem file

  • You downloaded, in a previous step, your User .pem file. e.g. ec2-strapi-key-pair.pem. This needs to be included in each attempt to SSH into your EC2 server. Move your .pem file to ~/.ssh/, follow these steps:
  • On your local machine, navigate to the folder that contains your .pem file. e.g. downloads
  • Move the .pem file to ~/.ssh/ and set file permissions: Path:./path-to/.pem-file/
mv ec2-strapi-key-pair.pem ~/.ssh/
chmod 400 ~/.ssh/ec2-strapi-key-pair.pem

Log in to your server as the default ubuntu user:

ssh -i ~/.ssh/ec2-strapi-key-pair.pem ubuntu@12.123.123.11

Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-1032-aws x86_64)

...

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@ip-12.123.123.11:~$

3. Install Node.js with npm:

Strapi currently supports Node.js v12.x.x. The following steps will install Node.js onto your EC2 server.

cd ~
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
...
sudo apt-get install nodejs
...
node -v && npm -v

The last command node -v && npm -v should output two versions numbers, eg. v12.x.x, 6.x.x.

Prepare and clone Strapi project to server

These instructions assume that you have already created a Strapi project, and have it in a GitHub repository.

You will need to update the database.json file to configure Strapi to connect to the RDS database.

And you will need to install an npm package called pg locally on your development server.

1. Install pg in your Strapi project.

On your development machine, navigate to your Strapi project root directory: Path: ./my-project/

npm install pg

2. Edit the database.json file.

Copy/paste the following:

Path: ./my-project/config/environments/production/database.json

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "bookshelf",
      "settings": {
        "client": "postgres",
        "host": "${process.env.DATABASE_HOST || '127.0.0.1'}",
        "port": "${process.env.DATABASE_PORT || 27017}",
        "database": "${process.env.DATABASE_NAME || 'strapi'}",
        "username": "${process.env.DATABASE_USERNAME || ''}",
        "password": "${process.env.DATABASE_PASSWORD || ''}"
      },
      "options": {
        "ssl": false
      }
    }
  }
}

3. Install the Strapi Provider Upload AWS S3 Plugin:

Path: ./my-project/.

This plugin will allow configurations for each active environment.

npm install strapi-provider-upload-aws-s3@beta

4. Push your local changes to your project’s GitHub repository.

git add .
git commit -m 'installed pg, aws-S3 provider plugin and updated the production/database.json file'
git push

5. Deploy from GitHub

You will next deploy your Strapi project to your EC2 instance by cloning it from GitHub.

From your terminal and logged into your EC2 instance as the ubuntu user:

cd ~
git clone https://github.com/your-name/your-project-repo.git

Next, navigate to the my-project folder, the root for Strapi. You will need to run npm install to install the packages for your project.

Path: ./my-project/

cd ./my-project/
npm install
NODE_ENV=production npm run build

Next, you need to install PM2 Runtime and configure the ecosystem.config.js file

6. Install PM2 Runtime

PM2 Runtime allows you to keep your Strapi project alive and to reload it without downtime.

Ensure you are logged in as a non-root user. You will install PM2 globally:

npm install pm2@latest -g
  • You will need to open your nano editor and then copy/paste the following:
cd ~
pm2 init
sudo nano ecosystem.config.js
  • Next, replace the boilerplate content in the file, with the following:
module.exports = {
  apps: [
    {
      name: 'your-app-name',
      cwd: '/home/ubuntu/my-strapi-project/my-project',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
        DATABASE_HOST: 'your-unique-url.rds.amazonaws.com', // database Endpoint under 'Connectivity & Security' tab
        DATABASE_PORT: '5432',
        DATABASE_NAME: 'strapi', // DB name under 'Configuration' tab
        DATABASE_USERNAME: 'postgres', // default username
        DATABASE_PASSWORD: 'Password',
      },
    },
  ],
};

Configure Strapi Provider AWS S3 plugin

You may now test the image upload, and you will find your images being uploaded to Amazon AWS S3

Set up a webhook

  • Check the status of the webhook:
sudo systemctl status webhook

Your Strapi project has been installed on an AWS EC2

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Install Strapi on Amazon AWS with 1-Click

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password