Skip to content

Strapi application setup

Jeffrey Arts edited this page Aug 17, 2024 · 12 revisions

For a Strapi application, we use pm2 for deployment. This will create a node instance on the server. The create_strapi_site command will add the required configuration to nginx. In the current set-up there is no support yet for multiple Strapi running on the same server. But this can easily be achieved by modifying the create_strapi_site so it will prompt for the right port number. This port number should then be equal to the PORT number in the .env file.

Setup

Remote environment preparations

  1. Login to the remote server ssh <user>@<host>. Make sure that you are using the same user as you define in your local .env file.

  2. Create an ssh key

cd ~/.ssh 
ssh-keygen -f github_JeffreyArts_jeff_payload -t ecdsa -b 521

Replace <github_username> with the username that the repo belongs to, and replace <repo> with the name of the repo.

  1. Update (or create) ~/.ssh/config file
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_<github_username>_<repo>

If this code already exists, you can copy the IdentityFile line, and use your newly created keyfile

  1. Add ssh key to the list of deploy keys of your repo Go to your repo, and go to settings > deploy keys > add deploy key. For the title use @, and paste the contents of ~/.ssh/github_<github_username>_<repo>.pub here. Leave allow write access unchecked!

These are all the required steps needed for pm2 to be able to setup the deployment flow.

Local environment

  1. Download the ecosystem.config.js file and place it in the root of your project.
  2. Run pm2 deploy production setup
DEPLOYMENT_USER=<user>
DEPLOYMENT_HOST=<host>
DEPLOYMENT_PATH=/var/www/strapi/<project>
DEPLOYMENT_REPO=git@github.com:<github_username>/<repo>.git
  • Replace the user with the username required to login to the remote server.
  • Replace host with the name of the server (no www, https or other prefixes, ip address could be fine as well)
  • Replace project with the name of the directory that you want to install the project
  • Replace github_username & repo with the same properties of the repo where you added the deploy key to
  1. Copy .env file to remote server scp .env <user>@<host>:/var/www/strapi/<project>/source

  2. Test if deployment works export NODE_ENV=production; pm2 deploy ecosystem.config.js production --force

  3. Update package.json If deployment works, add "deploy": "pm2 deploy ecosystem.config.js production" to the package.json file

Finishing steps

Now that the deployment flow is working, we need to complete the nginx configuration.

  1. Log in to the server via ssh, and call create_strapi_server www.example.com, this will prompt you for the location of your Strapi application (/var/www/strapi//source) and set-up the NGINX configuration, including SSL via certbot.
  2. Configure Strapi Go to https://www.example.com/admin and create a admin account, setting up a new Strapi environment will not take over any data. So make sure that you manually go through your configuration settings to validate if everything is configured as it should.

Deploy

You can now deploy the application with yarn deploy.

Clone this wiki locally