Skip to content

Instantly share code, notes, and snippets.

@iamdual
Last active April 3, 2023 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdual/880c75eaca167e9250231a23548b1efb to your computer and use it in GitHub Desktop.
Save iamdual/880c75eaca167e9250231a23548b1efb to your computer and use it in GitHub Desktop.
Simple Control Panel
#!/bin/bash
set -e
TEMPLATE="${TEMPLATE:-addsite.tmpl}"
PHP_VERSION="${PHP_VERSION:-8.2}"
SUDO_SUPPORT="${SUDO_SUPPORT:-1}"
# Set prefix for sudo if it is necessary
SUDO=''
if [ $SUDO_SUPPORT == "1" ] && [ "$EUID" -ne 0 ]; then
SUDO='sudo'
fi
if [[ $# -eq 0 ]] ; then
echo "./addsite.sh <site-domain>"
exit 1
fi
SITE_DOMAIN=$1; SERVER_NAME=$SITE_DOMAIN
if [[ $SITE_DOMAIN == www.* ]]; then
SITE_DOMAIN="$(echo $SITE_DOMAIN | sed 's/^www\.//g' | sed 's/ //g')"
SERVER_NAME="$SITE_DOMAIN www.$SITE_DOMAIN"
fi
$SUDO touch /etc/nginx/conf.d/$SITE_DOMAIN.conf
cat ./$TEMPLATE \
| sed "s/{{serverName}}/$SERVER_NAME/g" \
| sed "s/{{siteDomain}}/$SITE_DOMAIN/g" \
| sed "s/{{phpVersion}}/$PHP_VERSION/g" \
| $SUDO tee /etc/nginx/conf.d/$SITE_DOMAIN.conf > /dev/null
$SUDO mkdir -p /var/www/$SITE_DOMAIN/
$SUDO chown root:root /var/www/$SITE_DOMAIN/
$SUDO chmod 755 /var/www/$SITE_DOMAIN/
# Reload nginx
$SUDO systemctl reload nginx.service
# Obtain a certificate from Let's Encrypt
LE_DOMAINS="$(echo $SERVER_NAME | sed 's/ /,/g')"
LE_EMAIL="${LE_EMAIL:-admin@$SITE_DOMAIN.com}"
$SUDO certbot run -n --nginx --agree-tos -d $LE_DOMAINS -m $LE_EMAIL --redirect
echo "$SITE_DOMAIN created successfully."
server {
server_name {{serverName}};
root /var/www/{{siteDomain}};
index index.php index.html;
access_log /var/log/nginx/{{siteDomain}}.access.log;
error_log /var/log/nginx/{{siteDomain}}.error.log;
location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php{{phpVersion}}-fpm.sock;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
#!/bin/bash
set -e
PHP_VERSION='8.2'
SUDO_SUPPORT=1
if [ $(apt --help > /dev/null 2>&1; echo $?) -ne 0 ]; then
echo "Invalid distribution. Please check the requirements."
exit 1
fi
# Set prefix for sudo if it is necessary
SUDO=''
if [ $SUDO_SUPPORT -ne 0 ] && [ "$EUID" -ne 0 ]; then
SUDO='sudo'
fi
# Install top requirements
$SUDO apt -y update
$SUDO apt -y --no-install-recommends install software-properties-common gnupg2 ca-certificates curl lsb-release debian-archive-keyring
if [ $(lsb_release -is) != "Debian" ] && [ [$(lsb_release -cs) != "buster"] || $(lsb_release -cs) != "bullseye" ]; then
echo "Only Debian 10 (buster) and 11 (bullseye) is supported."
exit 1
fi
# Remove Apache if pre-installed
$SUDO apt -y remove apache2
# Install nginx from the its official repository
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| $SUDO tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian/ `lsb_release -cs` nginx" \
| $SUDO tee /etc/apt/sources.list.d/nginx.list
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| $SUDO tee /etc/apt/preferences.d/99nginx
$SUDO apt -y update
$SUDO apt -y --no-install-recommends install nginx
# Install PHP from the sury.org's repository
curl https://packages.sury.org/php/apt.gpg \
| $SUDO tee /usr/share/keyrings/sury-php.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/sury-php.gpg] \
https://packages.sury.org/php/ `lsb_release -cs` main" \
| $SUDO tee /etc/apt/sources.list.d/sury-php.list
$SUDO apt -y update
$SUDO apt -y --no-install-recommends install php$PHP_VERSION
$SUDO apt -y --no-install-recommends install php$PHP_VERSION-{common,cli,fpm,mysql,pgsql,pdo,phar,curl,mbstring,opcache,redis,intl,gettext,zip,xml,imap,ldap}
# Install certbot from the official Debian repository
# https://packages.debian.org/bullseye/certbot
$SUDO apt -y --no-install-recommends install certbot python3-certbot-nginx
echo "Installation completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment