Yurdle/dockerserver.sh

324 lines
8.4 KiB
Bash
Raw Normal View History

2024-01-03 19:10:31 +03:00
#!/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
2024-01-06 13:27:14 +03:00
# NOTE remote installation methods removed, just use for local tests
2024-01-03 19:10:31 +03:00
# TODO Make docker compose file for running components under different dockers
2024-01-06 13:27:14 +03:00
CONTAINER_PREFIX="YURDLE" # nginx docker name prefix
2024-01-03 19:10:31 +03:00
MOUNT_PATH="/source" # binded mount path in nginx docker
SCRIPT_NAME="dockerserver.sh" # name of the *this* file
2024-01-06 13:27:14 +03:00
SCRIPT_DIR="Yurdle" # containing directory of the *this* file
2024-01-03 19:10:31 +03:00
BUILD_DIR="." # build results of local source code (insert a dot (.) if you do not need to build your application)
2024-01-07 00:37:32 +03:00
# DATABASE_TYPE="postgresql" # database type, either mysql or postgresql
2024-01-07 03:27:44 +03:00
MAIN_NAME="yurdle" # main program name
PORT1="8000" # TODO what is the difference between 8000 and 80
2024-01-03 19:10:31 +03:00
PORT2="80"
2024-01-06 13:27:14 +03:00
WEB_ROOT_DIR="/usr/share/nginx/html/" # public web root dir
WEB_SERVER_DIR="/usr/share/nginx/server/" # private server root dir
2024-01-07 00:37:32 +03:00
SECRET_SERVER="false" # add secret server to $WEB_ROOT_DIR/../$SECRET_SERVER_FOLDER
2024-01-06 13:27:14 +03:00
SECRET_SERVER_LOCAL_RELATIONAL_PATH="../YurdleBackend" # local path
SERVER_BUILD_DIR="." # server build dir
2024-01-07 00:37:32 +03:00
# __DB_NAME__="localdb"
# __DB_USER__="dbuser"
# __DB_PWD__="dbpass"
2024-01-06 13:27:14 +03:00
PHP_VERSION="8.2"
2024-01-07 00:37:32 +03:00
ACTIVE_SERVICES=("nginx" "php$PHP_VERSION-fpm") # services
2024-01-03 19:10:31 +03:00
############################
############################
# 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
reconf : reconfigure configs
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
2024-01-06 13:27:14 +03:00
restall: update + reconf + restart + root
"""
2024-01-03 19:10:31 +03:00
2024-01-06 13:27:14 +03:00
CONFIG_FILE_CONTENT="""
<?php
class Config
{
const DB_HOST = 'localhost';
const DB_NAME = '$__DB_NAME__';
const DB_USERNAME = '$__DB_USER__';
const DB_PASSWORD = '$__DB_PWD__';
2024-01-07 00:37:32 +03:00
const DATAACCESS_TOKEN = 'LOCAL';
2024-01-06 13:27:14 +03:00
}
2024-01-03 19:10:31 +03:00
"""
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)(/.*)\$;
2024-01-06 13:27:14 +03:00
fastcgi_pass unix:/var/run/php/php$PHP_VERSION-fpm-$MAIN_NAME.sock;
2024-01-03 19:10:31 +03:00
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
2024-01-06 13:27:14 +03:00
listen = /var/run/php/php$PHP_VERSION-fpm-$MAIN_NAME.sock
2024-01-03 19:10:31 +03:00
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
"""
2024-01-06 13:27:14 +03:00
# print command HEAD
_head() {
echo -e "\n\n----------------------------------"
echo -e "${_RED}$1 HEAD${_NC}"
echo -e "----------------------------------"
}
_end() {
echo -e "----------------------------------"
echo -e "${_RED}$1 END${_NC}"
echo -e "----------------------------------\n\n"
}
2024-01-03 19:10:31 +03:00
_exit() {
echo -e "${_RED}exiting...${_NC}"
exit
}
_status() {
2024-01-06 13:27:14 +03:00
_head "SYSTEM STATUS"
for var in "${ACTIVE_SERVICES[@]}"; do
service "$var" status
done
_end "SYSTEM STATUS"
}
_restart() {
_head "SYSTEM RESTART"
for var in "${ACTIVE_SERVICES[@]}"; do
service "$var" restart
sleep 2
done
_end "SYSTEM RESTART"
_status
}
_reconf() {
_head "RECONFIGURATION"
2024-01-07 00:37:32 +03:00
echo -e "$CONFIG_FILE_CONTENT" >"$WEB_ROOT_DIR/config.php"
2024-01-06 13:27:14 +03:00
echo -e "$NGINX_CONFIG_FILE" >"/etc/nginx/conf.d/default.conf"
echo -e "$PHP_FPM_CONFIG_FILE" >"/etc/php/$PHP_VERSION/fpm/pool.d/$MAIN_NAME.conf"
_end "RECONFIGURATION"
}
_update() {
_head "SOURCE UPDATE"
if [[ $SECRET_SERVER == "true" ]]; then
rsync -a --progress /source/$BUILD_DIR $WEB_ROOT_DIR
rsync -a --progress /server/$SERVER_BUILD_DIR $WEB_SERVER_DIR
else
rsync -a --progress /source/$BUILD_DIR $WEB_ROOT_DIR
fi
_end "SOURCE UPDATE"
}
_update_conf() {
if [[ $3 == "server" ]]; then
sed -i "s/\($1 * = *\).*/\1$2;/" "$WEB_SERVER_FOLDER/config.php"
else
sed -i "s/\($1 * = *\).*/\1$2;/" "$WEB_ROOT_FOLDER/config.php"
fi
}
_refresh_folders() {
rm -rf $WEB_ROOT_DIR
mkdir -p $WEB_ROOT_DIR
2024-01-07 00:37:32 +03:00
if [[ $SECRET_SERVER == "true" ]]; then
rm -rf $WEB_SERVER_DIR
mkdir -p $WEB_SERVER_DIR
fi
2024-01-06 13:27:14 +03:00
}
_setup_database() {
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
service postgresql start
su -c "createuser $__DB_USER__;createdb -h localhost -p $__DB_PWD__ -E UTF8 -O $__DB_USER__ $__DB_NAME__;" - postgres
fi
}
_folder_permissions() {
groupadd $MAIN_NAME
useradd -g $MAIN_NAME $MAIN_NAME
cd $WEB_ROOT_DIR || exit
chown $MAIN_NAME:nginx -R .
2024-01-07 03:27:44 +03:00
if [[ $SECRET_SERVER == "true" ]]; then
cd $WEB_SERVER_DIR || exit
chown $MAIN_NAME:nginx -R .
fi
2024-01-03 19:10:31 +03:00
}
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"
2024-01-06 13:27:14 +03:00
if [[ $SECRET_SERVER == "true" ]]; then
docker run -it -d --name "$CONTAINER_PREFIX" --mount type=bind,source=".",target=/source/,readonly --mount type=bind,source="./$SECRET_SERVER_LOCAL_RELATIONAL_PATH",target=/server/,readonly --publish $PORT1:$PORT2 nginx bash
else
docker run -it -d --name "$CONTAINER_PREFIX" --mount type=bind,source=".",target=/source/,readonly --publish $PORT1:$PORT2 nginx bash
fi
2024-01-03 19:10:31 +03:00
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
2024-01-06 13:27:14 +03:00
set +x
2024-01-03 19:10:31 +03:00
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 == "restart" ]]; then
2024-01-06 13:27:14 +03:00
_restart
2024-01-03 19:10:31 +03:00
elif [[ $2 == "reconf" ]]; then
2024-01-06 13:27:14 +03:00
_reconf
elif [[ $2 == "restall" ]]; then
_update
2024-01-03 19:10:31 +03:00
sleep 1
2024-01-06 13:27:14 +03:00
_reconf
sleep 1
_restart
root
2024-01-03 19:10:31 +03:00
elif [[ $2 == "update" ]]; then
2024-01-06 13:27:14 +03:00
_update
2024-01-03 19:10:31 +03:00
elif [[ $2 == "status" ]]; then
_status
elif [[ $2 == "install" ]]; then
sleep 1
2024-01-06 13:27:14 +03:00
set -x
_refresh_folders
2024-01-03 19:10:31 +03:00
apt update
apt install nano exa tree wget unzip less rsync -y
2024-01-06 13:27:14 +03:00
2024-01-03 19:10:31 +03:00
cd $WEB_ROOT_DIR || exit
2024-01-06 13:27:14 +03:00
cp -r /source/$BUILD_DIR/* $WEB_ROOT_DIR/
if [[ $SECRET_SERVER == "true" ]]; then
cp -r /server/$BUILD_DIR/* $WEB_SERVER_DIR/
2024-01-03 19:10:31 +03:00
fi
2024-01-07 03:27:44 +03:00
apt install php php-fpm php-cli -y
2024-01-07 00:37:32 +03:00
locale-gen tr_TR
2024-01-06 13:27:14 +03:00
2024-01-07 00:37:32 +03:00
# _setup_database
2024-01-06 13:27:14 +03:00
_reconf
2024-01-03 19:10:31 +03:00
2024-01-06 13:27:14 +03:00
_folder_permissions
set +x
_restart
sleep 1
2024-01-03 19:10:31 +03:00
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