From 23fabb5d89b61af7bd48db3bd7f4714580783854 Mon Sep 17 00:00:00 2001 From: asandikci Date: Wed, 3 Jan 2024 19:10:31 +0300 Subject: [PATCH] add docker server file --- dockerserver.sh | 283 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100755 dockerserver.sh diff --git a/dockerserver.sh b/dockerserver.sh new file mode 100755 index 0000000..7d355f9 --- /dev/null +++ b/dockerserver.sh @@ -0,0 +1,283 @@ +#!/bin/bash + +# PHP + Nginx + SQL, Docker Local Test Server Setup/Management File +# Based on nginx docker because I don't know how to use docker compose .d +# TODO Make docker compose file for running components under different dockers + +CONTAINER_PREFIX="YURTDLE" # nginx docker name prefix +MOUNT_PATH="/source" # binded mount path in nginx docker +SCRIPT_NAME="dockerserver.sh" # name of the *this* file +SCRIPT_DIR="Yurtdle" # containing directory of the *this* file +DEBUG="FALSE" # debug mode +BUILD_DIR="." # build results of local source code (insert a dot (.) if you do not need to build your application) +DATABASE_TYPE="postgresql" # database type, either mysql or postgresql +MAIN_NAME="yurtdle" # main program name +PORT1="8000" # TODO what is the difference between 8000 and 80 +PORT2="80" + +### REMOTE RELEASE ### +LATEST_RELEASE="-" +FILE_NAME="$MAIN_NAME-$LATEST_RELEASE.zip" +DOWNLOAD_LINK=https://git.aliberksandikci.com.tr/ifl/$MAIN_NAME/releases/download/$LATEST_RELEASE/$FILE_NAME +WEB_ROOT_DIR="/usr/share/nginx/html/" + +############################ +############################ + +# COLORS +_RED='\033[0;31m' +_NC='\033[0m \e[0m' # No Color, No Effect + +HELP=""" +Usage: + dock [command] [subcommand] + +COMMANDS: + ${_RED}ini:${_NC} initialize dock command + install: install $MAIN_NAME + local: get from local /source/ + remote: get from git service + reconf : reconfigure configs + debug: change application debug status [ON / OFF] + restart: restart services (nginx, $DATABASE_TYPE, php-fpm) + update: update source files with new build on /source/ + status: see current webserver status + help: see this help text + root: cd to web root folder + +BUNDLE COMMANDS: + complete-restart: update + reconf + restart + root +""" + +NGINX_CONFIG_FILE=""" +server { + listen $PORT2; + server_name localhost:$PORT1; + + location / { + alias $WEB_ROOT_DIR; + #root $WEB_ROOT_DIR; + index index.php; + + # Common parameter to increase upload size limit in conjunction with dedicated php-fpm file + client_max_body_size 50M; + + try_files \$uri \$uri/ index.php; + location ~ [^/]\.php(/|\$) { + fastcgi_split_path_info ^(.+?\.php)(/.*)\$; + fastcgi_pass unix:/var/run/php/php8.2-fpm-$MAIN_NAME.sock; + + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param REMOTE_USER \$remote_user; + fastcgi_param PATH_INFO \$fastcgi_path_info; + fastcgi_param SCRIPT_FILENAME \$request_filename; + } + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root $WEB_ROOT_DIR; + } +} +""" + +PHP_FPM_CONFIG_FILE=""" +[$MAIN_NAME] + +user = $MAIN_NAME +group = $MAIN_NAME + +chdir = /usr/share/nginx/html + +listen = /var/run/php/php8.2-fpm-$MAIN_NAME.sock +listen.owner = nginx +listen.group = nginx +listen.mode = 0660 + +pm = ondemand +pm.max_children = 8 +pm.max_requests = 500 +request_terminate_timeout = 1d + + +pm.process_idle_timeout = 10s + +; Additional php.ini defines, specific to this pool of workers. + +php_admin_value[upload_max_filesize] = 50M +php_admin_value[post_max_size] = 50M +""" + +_exit() { + echo + echo + echo -e "${_RED}exiting...${_NC}" + exit +} + +_status() { + set +x + echo + echo + echo "----------------------------------" + echo -e "${_RED}SYSTEM STATUS HEAD${_NC}" + echo "----------------------------------" + service nginx status + service mariadb status + service php8.2-fpm status + echo "----------------------------------" + echo -e "${_RED}SYSTEM STATUS HEAD${_NC}" + echo "----------------------------------" + echo + echo + set -x +} + +if [[ $# -eq 0 ]]; then + if [[ $(pwd) != *$SCRIPT_DIR ]]; then + echo "PLEASE CHANGE DIRECTORY TO \`$SCRIPT_DIR/\` BEFORE STARTING SCRIPT" + exit + fi + # TODO silmeden önce onay iste! + # TODO extra docker oluşturma ve istenilen docker'a attach + + echo -e "${_RED}CONTAINER WILL BE REMOVED !!!${_NC} (10)" + sleep 10 + docker stop $CONTAINER_PREFIX + docker rm $CONTAINER_PREFIX + echo "removed" + + docker run -it -d --name "$CONTAINER_PREFIX" --mount type=bind,source=".",target=/source/,readonly --publish $PORT1:$PORT2 nginx bash + echo -e "RUN SCRIPT: \`${_RED}bash $MOUNT_PATH/$SCRIPT_NAME docker ini${_NC}\`" + docker attach $CONTAINER_PREFIX --detach-keys="ctrl-k" + _exit + +elif [[ $1 == "attach" ]]; then + if [ -n "$(docker ps -f "name=$CONTAINER_PREFIX" -f "status=running" -q)" ]; then + echo "the container is already running!" + else + docker start $CONTAINER_PREFIX + fi + + docker attach $CONTAINER_PREFIX --detach-keys="ctrl-k" + +elif [[ $1 == "docker" ]]; then + set -x + + if [[ $2 == "ini" ]]; then + echo "alias dock=\"bash /source/$SCRIPT_NAME docker\"" >>/root/.bashrc + echo "alias root=\"cd $WEB_ROOT_DIR\"" >>/root/.bashrc + sleep 1 + exec bash + + elif [[ $2 == "debug" ]]; then + case "$3" in + ["oO"]["nN"] | ["tT"]["rR"]["uU"]["eE"] | 1) sed -i "s/\(DEBUG_MODE * = *\).*/\1TRUE;/" "$WEB_ROOT_DIR/config.php" ;; + ["oO"]["fF"]["fF"] | ["fF"]["aA"]["lL"]["sS"]["eE"] | 0) sed -i "s/\(DEBUG_MODE * = *\).*/\1FALSE;/" "$WEB_ROOT_DIR/config.php" ;; + *) echo -e "$_RED please input ON or OFF $_NC" ;; + esac + + elif [[ $2 == "restart" ]]; then + service mariadb restart + sleep 3 + service php8.2-fpm restart + sleep 3 + service nginx restart + + _status + + elif [[ $2 == "reconf" ]]; then + + echo -e "$CONFIG_FILE_CONTENT" >"$WEB_ROOT_DIR/config.php" + echo -e "$NGINX_CONFIG_FILE" >"/etc/nginx/conf.d/default.conf" + echo -e "$PHP_FPM_CONFIG_FILE" >"/etc/php/8.2/fpm/pool.d/$MAIN_NAME.conf" + echo RECONFIGURED + echo + _exit + elif [[ $2 == "complete-restart" ]]; then + echo "updating sources!" + rsync -a --progress /source/build/ $WEB_ROOT_DIR --exclude config.php --exclude storage + + sleep 1 + + echo -e "$CONFIG_FILE_CONTENT" >"$WEB_ROOT_DIR/config.php" + echo -e "$NGINX_CONFIG_FILE" >"/etc/nginx/conf.d/default.conf" + echo -e "$PHP_FPM_CONFIG_FILE" >"/etc/php/8.2/fpm/pool.d/$MAIN_NAME.conf" + echo RECONFIGURED + echo + + service mariadb restart + sleep 3 + service php8.2-fpm restart + sleep 3 + service nginx restart + + _status + + elif [[ $2 == "update" ]]; then + echo "updating sources!" + rsync -a --progress /source/build/ $WEB_ROOT_DIR --exclude config.php --exclude storage + elif [[ $2 == "status" ]]; then + _status + + elif [[ $2 == "install" ]]; then + if [[ $# -eq 2 || ! ($3 == "local" || $3 == "remote") ]]; then + echo -e "use $_RED dock install local $NC or $_RED dock install remote" + _exit + fi + sleep 1 + apt update + apt install nano exa tree wget unzip less rsync -y + rm -rf $WEB_ROOT_DIR + mkdir -p $WEB_ROOT_DIR + cd $WEB_ROOT_DIR || exit + if [[ $3 == "local" ]]; then + cp -r /source/$BUILD_DIR/* $WEB_ROOT_DIR/ + elif [[ $3 == "remote" ]]; then + wget "$DOWNLOAD_LINK" + sleep 3 + unzip $FILE_NAME + mv $FILE_NAME ../ + + else + echo -e "${_RED}use docker local or docker remote${_NC}" + fi + + ### CONFIGURATIONS ### + apt install php php-fpm -y + if [[ $DATABASE_TYPE == "mysql" ]]; then + apt install mariadb-server php-mysql -y + service mariadb start + mysql -u root -e "create database $MAIN_NAME" + mysql -u root -e "set password for root@localhost = password('root');" + elif [[ $DATABASE_TYPE == "postgresql" ]]; then + apt install postgresql php-pgsql -y + fi + + echo -e "$CONFIG_FILE_CONTENT" >"$WEB_ROOT_DIR/config.php" + echo -e "$NGINX_CONFIG_FILE" >"/etc/nginx/conf.d/default.conf" + echo -e "$PHP_FPM_CONFIG_FILE" >"/etc/php/8.2/fpm/pool.d/$MAIN_NAME.conf" + groupadd $MAIN_NAME + useradd -g $MAIN_NAME $MAIN_NAME + cd $WEB_ROOT_DIR || exit + chown $MAIN_NAME:nginx -R . + chmod 770 storage + + service php8.2-fpm start + service nginx start + service nginx status + service nginx restart + echo -e "${_RED}DONE! - OPEN localhost:$PORT1 TO TEST${_NC}" + elif [[ $2 == "help" ]]; then + echo -e "$HELP!" + else + echo -e "$_RED ! COMMAND NOT FOUND ! $_NC" + echo + echo -e "$HELP" + fi + +else + echo -e "$_RED ! COMMAND NOT FOUND ! $_NC" + +fi