Last active
July 11, 2021 01:07
-
-
Save selfagency/42b608c33871a43bbe4961712f9697ab to your computer and use it in GitHub Desktop.
[hosting management scripts]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DOMAIN=$1 | |
APP=$2 | |
PORT=$3 | |
## WordPress config | |
if [[ "$APP" == "wp" ]]; then | |
INSTALL="composer install --no-dev" | |
FIXPERMS="sudo find . -type d -exec chmod 755 {} \; | |
sudo find . -type f -exec chmod 644 {} \; | |
sudo chmod 660 ./config.php" | |
RESTART="" | |
SETOWNER="sudo chown -R caddy:caddy html" | |
CADDYFILE="$DOMAIN { | |
root * /var/www/$DOMAIN/html | |
php_fastcgi unix//run/php/php7.4-fpm.sock | |
@uploads { | |
path_regexp path /uploads\/(.*)\.php | |
} | |
rewrite @uploads / | |
@wp-admin { | |
path not ^\/wp-admin/* | |
} | |
rewrite @wp-admin {path}/index.php?{query} | |
encode gzip | |
file_server { | |
hide .env | |
} | |
log { | |
output file /var/log/caddy/$DOMAIN.log | |
level error | |
} | |
} | |
www.$DOMAIN { | |
redir * https://{http.request.host.labels.1}.{http.request.host.labels.0}{path} | |
}" | |
fi | |
## Node.js config | |
if [[ "$APP" == "node" ]]; then | |
INSTALL="yarn install --production" | |
FIXPERMS="" | |
RESTART="pm2 restart $DOMAIN" | |
SETOWNER="sudo chown -R selfagency:selfagency html" | |
CADDYFILE="$DOMAIN { | |
reverse_proxy localhost:$PORT | |
log { | |
output file /var/log/caddy/$DOMAIN.log | |
level error | |
} | |
} | |
www.$DOMAIN { | |
redir * https://{http.request.host.labels.1}.{http.request.host.labels.0}{path} | |
}" | |
fi | |
GITHOOK="#!/bin/sh | |
cd /var/www/$DOMAIN | |
git --work-tree=/var/www/$DOMAIN/html --git-dir=/repos/$DOMAIN.git checkout -f | |
git --work-tree=/var/www/$DOMAIN/html --git-dir=/repos/$DOMAIN.git pull | |
cd html | |
$INSTALL | |
$FIXPERMS | |
sudo cp ../.env . | |
sudo chmod 400 .env | |
cd .. | |
$SETOWNER | |
$RESTART | |
echo \"Successfully deployed.\"" | |
## Create Git repo | |
echo "Creating /repos/$DOMAIN.git..." | |
mkdir "/repos/$DOMAIN.git" | |
cd "/repos/$DOMAIN.git" || exit 1 | |
git init --bare . | |
cd .. | |
sudo chown -R selfagency:selfagency "$DOMAIN.git" | |
## Create post-receive hook | |
echo "Creating post-receive hook..." | |
printf '%s' "$GITHOOK" >> hooks/post-receive | |
chmod +x hooks/post-receive | |
## Create web root folder | |
echo "Creating /var/www/$DOMAIN..." | |
cd /var/www || exit 1 | |
mkdir -p "$DOMAIN/html" | |
chmod -R 775 "$DOMAIN" | |
cd "$DOMAIN" || exit 1 | |
touch .env | |
sudo chown -R selfagency:caddy . | |
## Update Caddyfile | |
echo "Creating /etc/caddy/sites/$DOMAIN.caddy..." | |
printf '%s' "$CADDYFILE" | sudo tee -a "/etc/caddy/sites/$DOMAIN.caddy" | |
sudo chown caddy:caddy "/etc/caddy/sites/$DOMAIN.caddy" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
DOMAIN=$1 | |
sudo pm2 delete $DOMAIN | |
sudo rm -rf /repos/$DOMAIN.git | |
sudo rm -rf /var/www/$DOMAIN | |
sudo rm -rf /etc/caddy/sites/$DOMAIN.caddy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
$PROJECT=selfagency | |
git --work-tree=/var/www/$PROJECT/live --git-dir=/var/www/$PROJECT/staging/$PROJECT.git checkout -f master | |
cd /var/www/$PROJECT/live | |
npm install | |
cd .. | |
pm2 restart $PROJECT --update-env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
$PROJECT=selfagency.dev | |
git --work-tree=/var/www/$PROJECT --git-dir=/repos/$PROJECT.git checkout -f | |
git --work-tree=/var/www/$PROJECT --git-dir=/repos/$PROJECT.git pull | |
cd /var/www/$PROJECT | |
composer install --no-dev | |
echo "Successfully deployed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment