Skip to content

Instantly share code, notes, and snippets.

@i-am-mani
Created May 14, 2022 13:42
Show Gist options
  • Select an option

  • Save i-am-mani/65b348e0bffb97af2159a1f0b513c5b9 to your computer and use it in GitHub Desktop.

Select an option

Save i-am-mani/65b348e0bffb97af2159a1f0b513c5b9 to your computer and use it in GitHub Desktop.
A deployment script to automate strapi deployment in for test/production. Postgress for DB, PM2 for process manager, Caddy or Nginx for reverse-proxy.
localhost # your server_name
reverse_proxy 127.0.0.1:1337 # assuming your strapi instance in running on 1337
#!/bin/bash
# Create either Caddyfile in / or nginx.conf in /config
#
#
APPLICATION_PATH=XXXX
# https://<YOUR_GITHUB_TOKEN>@github.com/<PROFILE/<PROJECT.git
GIT_LINK=XXXX
AWS_S3_REGION=XXX
AWS_S3_BUCKET=XXX
AWS_CLOUDFRONT=XXX
DATABASE_PORT=5432
DATABASE_USERNAME=XXX
DATABASE_PASSWORD=XXX
DATABASE_NAME=XXX
HOST=0.0.0.0
PORT=1337
APP_KEYS=XXX
API_TOKEN_SALT=XXX
ADMIN_JWT_SECRET=XXX
JWT_SECRET=XXX
install_postgres() {
echo "Checking if postgres is installed"
if ! [[ -x "$(command -v psql)" ]]; then
echo "Postgres is not installed. Installing postgres"
sudo apt-get install postgresql postgresql-contrib
echo "CREATE USER $DATABASE_USERNAME WITH PASSWORD '$DATABASE_PASSWORD';" | sudo -i -u postgres psql
echo "CREATE DATABASE $DATABASE_NAME;" | sudo -i -u postgres psql
else
echo "Postgres is already installed"
fi
}
create_envs() {
cat <<EOF >$APPLICATION_PATH/.env
AWS_S3_REGION=$AWS_S3_REGION
AWS_S3_BUCKET=$AWS_S3_BUCKET
AWS_CLOUDFRONT=$AWS_CLOUDFRONT
DATABASE_PORT=$DATABASE_PORT
DATABASE_USERNAME=$DATABASE_USERNAME
DATABASE_PASSWORD=$DATABASE_PASSWORD
DATABASE_NAME=$DATABASE_NAME
HOST=$HOST
PORT=$PORT
APP_KEYS=$APP_KEYS
API_TOKEN_SALT=$API_TOKEN_SALT
ADMIN_JWT_SECRET=$ADMIN_JWT_SECRET
JWT_SECRET=$JWT_SECRET
EOF
}
install_git() {
echo "Checking if git is installed"
if ! [[ -x "$(command -v git)" ]]; then
echo "Git is not installed. Installing git"
sudo apt-get install git
else
echo "Git is already installed"
fi
}
clone_repository() {
echo "Cloning repository"
if ! [[ -d "$APPLICATION_PATH" ]]; then
git clone $GIT_LINK $APPLICATION_PATH
else
echo "Repository already cloned"
fi
}
install_pm2() {
echo "Installing pm2"
sudo npm install -g pm2
}
build_strapi() {
echo "Initiating Strapi"
export NODE_ENV=production
cd $APPLICATION_PATH || exit
echo "Installing dependencies"
npm install
echo "Building Strapi"
npm run build
}
run_strapi_instance() {
echo "Starting Strapi"
# Create an ecosystem.config.js file
# https://pm2.keymetrics.io/docs/usage/application-declaration/
pm2 start $APPLICATION_PATH/config/ecosystem.config.js --name strapi
}
install_node() {
echo "Checking if node is installed"
if ! [ -x "$(command -v node)" ]; then
echo "Node is not installed. Installing node"
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
else
echo "Node is already installed"
fi
}
install_caddy() {
echo "Checking if caddy is installed"
if ! [ -x "$(command -v caddy)" ]; then
echo "Caddy is not installed. Installing caddy"
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
else
echo "Caddy is already installed"
fi
}
install_nginx() {
echo "Checking if nginx is installed"
if ! [ -x "$(command -v nginx)" ]; then
echo "Nginx is not installed. Installing nginx"
sudo apt install -y nginx
else
echo "Nginx is already installed"
fi
}
setup_nginx_reverse_proxy() {
echo "Setting up nginx reverse proxy"
sudo rm -rf /etc/nginx/sites-enabled/default
sudo cp $APPLICATION_PATH/config/nginx.conf /etc/nginx/sites-available/cms.conf
sudo ln -s /etc/nginx/sites-available/cms.conf /etc/nginx/sites-enabled/
sudo service nginx restart
}
setup_caddy_reverse_proxy() {
echo "Setting up reverse proxy"
cd $APPLICATION_PATH || exit
caddy run
echo "Caddy is running"
}
main() {
install_postgres
install_git
install_node
clone_repository
install_pm2
create_envs
build_strapi
run_strapi_instance
install_nginx
setup_nginx_reverse_proxy
# install_caddy
# setup_caddy_reverse_proxy
}
main
module.exports = {
apps: [
{
name: 'cms',
script: 'npm run develop', # or build and start
env: {
NODE_ENV: 'develop',
},
exp_backoff_restart_delay: 100,
},
],
};
upstream strapi {
server 127.0.0.1:1337;
keepalive 64;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://strapi;
}
}
# For HTTPS configuration refer => https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/optional-software/nginx-proxy.html#nginx-virtual-host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment