Update phpmyadmin.sh

This commit is contained in:
CanbiZ 2025-04-29 10:39:08 +02:00
parent b349760a80
commit f26f33a0e2

View File

@ -26,25 +26,27 @@ CROSS="${RD}✖️${CL}"
INFO="${BL}${CL}" INFO="${BL}${CL}"
APP="phpMyAdmin" APP="phpMyAdmin"
PORT=8081 INSTALL_DIR="/usr/share/phpmyadmin"
WEBROOT="/usr/share/phpmyadmin" SERVICE_PATH_DEBIAN="/etc/systemd/system/phpmyadmin.service"
SERVICE_PATH_ALPINE="/etc/init.d/phpmyadmin"
DEFAULT_PORT=8081
IFACE=$(ip -4 route | awk '/default/ {print $5; exit}') IFACE=$(ip -4 route | awk '/default/ {print $5; exit}')
IP=$(ip -4 addr show "$IFACE" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1) IP=$(ip -4 addr show "$IFACE" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1)
[[ -z "$IP" ]] && IP=$(hostname -I | awk '{print $1}')
[[ -z "$IP" ]] && IP="127.0.0.1" [[ -z "$IP" ]] && IP="127.0.0.1"
# Detect OS
if [[ -f "/etc/alpine-release" ]]; then if [[ -f "/etc/alpine-release" ]]; then
OS="Alpine" OS="Alpine"
SERVICE_PATH="/etc/init.d/phpmyadmin"
PKG_MANAGER="apk add --no-cache" PKG_MANAGER="apk add --no-cache"
PHP_SERVICE="php81" SERVICE_PATH="$SERVICE_PATH_ALPINE"
elif [[ -f "/etc/debian_version" ]]; then elif [[ -f "/etc/debian_version" ]]; then
OS="Debian" OS="Debian"
SERVICE_PATH="/etc/systemd/system/phpmyadmin.service"
PKG_MANAGER="apt-get install -y" PKG_MANAGER="apt-get install -y"
PHP_SERVICE="php" SERVICE_PATH="$SERVICE_PATH_DEBIAN"
else else
echo -e "${CROSS} Unsupported OS" echo -e "${CROSS} Unsupported OS detected. Exiting."
exit 1 exit 1
fi fi
@ -54,57 +56,101 @@ function msg_info() { echo -e "${INFO} ${YW}${1}...${CL}"; }
function msg_ok() { echo -e "${CM} ${GN}${1}${CL}"; } function msg_ok() { echo -e "${CM} ${GN}${1}${CL}"; }
function msg_error() { echo -e "${CROSS} ${RD}${1}${CL}"; } function msg_error() { echo -e "${CROSS} ${RD}${1}${CL}"; }
read -r -p "Install ${APP}? (y/n): " prompt # Check for existing installation
if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then if [[ -d "$INSTALL_DIR" ]]; then
msg_info "Installing dependencies" echo -e "${YW}⚠️ ${APP} is already installed.${CL}"
$PKG_MANAGER lighttpd ${PHP_SERVICE} ${PHP_SERVICE}-session ${PHP_SERVICE}-mysqli ${PHP_SERVICE}-mbstring ${PHP_SERVICE}-gettext curl unzip &>/dev/null read -r -p "Would you like to uninstall ${APP}? (y/N): " uninstall_prompt
msg_ok "Dependencies installed" if [[ "${uninstall_prompt,,}" =~ ^(y|yes)$ ]]; then
msg_info "Uninstalling ${APP}"
if [[ "$OS" == "Debian" ]]; then
systemctl disable --now phpmyadmin.service &>/dev/null
rm -f "$SERVICE_PATH"
else
rc-service phpmyadmin stop &>/dev/null
rc-update del phpmyadmin &>/dev/null
rm -f "$SERVICE_PATH"
fi
rm -rf "$INSTALL_DIR"
msg_ok "${APP} has been uninstalled."
exit 0
fi
msg_info "Fetching latest phpMyAdmin" read -r -p "Would you like to update ${APP}? (y/N): " update_prompt
mkdir -p "$WEBROOT" if [[ "${update_prompt,,}" =~ ^(y|yes)$ ]]; then
curl -fsSL https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip -o /tmp/pma.zip msg_info "Updating ${APP}"
unzip -q /tmp/pma.zip -d /usr/share/ curl -fsSL "https://www.phpmyadmin.net/home_page/version.txt" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 | while read -r LATEST_VERSION; do
mv /usr/share/phpMyAdmin-* "$WEBROOT" curl -fsSL "https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" | tar -xz --strip-components=1 -C "$INSTALL_DIR"
rm -f /tmp/pma.zip done
msg_ok "phpMyAdmin installed to $WEBROOT" msg_ok "Updated ${APP}"
exit 0
else
echo -e "${YW}⚠️ Update skipped. Exiting.${CL}"
exit 0
fi
fi
msg_info "Creating Lighttpd config" echo -e "${YW}⚠️ ${APP} is not installed.${CL}"
cat <<EOF >/etc/lighttpd/lighttpd.conf read -r -p "Enter port number (Default: ${DEFAULT_PORT}): " PORT
server.modules = ("mod_access", "mod_alias", "mod_redirect") PORT=${PORT:-$DEFAULT_PORT}
server.document-root = "$WEBROOT"
server.port = $PORT read -r -p "Would you like to install ${APP}? (y/n): " install_prompt
index-file.names = ( "index.php", "index.html" ) if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then
alias.url = ( "/phpmyadmin/" => "$WEBROOT/" ) msg_info "Installing ${APP} on ${OS}"
mimetype.assign = ( ".html" => "text/html", ".php" => "text/html" ) $PKG_MANAGER nginx php-fpm php-mysqli php-json php-session curl tar &>/dev/null
server.modules += ( "mod_fastcgi" )
fastcgi.server = ( ".php" => (( mkdir -p "$INSTALL_DIR"
"bin-path" => "/usr/bin/${PHP_SERVICE}-cgi", curl -fsSL "https://www.phpmyadmin.net/home_page/version.txt" | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1 | while read -r LATEST_VERSION; do
"socket" => "/tmp/php-fastcgi.socket" curl -fsSL "https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" | tar -xz --strip-components=1 -C "$INSTALL_DIR"
))) done
chown -R root:root "$INSTALL_DIR"
msg_ok "Installed ${APP}"
msg_info "Configuring Nginx"
mkdir -p /etc/nginx/conf.d
cat <<EOF >/etc/nginx/conf.d/phpmyadmin.conf
server {
listen ${PORT};
server_name _;
root ${INSTALL_DIR};
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php\$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
}
EOF EOF
msg_ok "Config created" nginx -s reload || systemctl reload nginx || rc-service nginx reload
msg_ok "Nginx configured"
msg_info "Creating service" msg_info "Creating phpMyAdmin service"
if [[ "$OS" == "Debian" ]]; then if [[ "$OS" == "Debian" ]]; then
cat <<EOF >"$SERVICE_PATH" cat <<EOF >"$SERVICE_PATH"
[Unit] [Unit]
Description=phpMyAdmin Lighttpd Description=phpMyAdmin Service
After=network.target After=network.target
[Service] [Service]
ExecStart=/usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf Type=simple
ExecStart=/usr/sbin/nginx -g "daemon off;"
Restart=always Restart=always
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target
EOF EOF
systemctl enable --now phpmyadmin &>/dev/null systemctl enable --now phpmyadmin
else else
cat <<EOF >"$SERVICE_PATH" cat <<EOF >"$SERVICE_PATH"
#!/sbin/openrc-run #!/sbin/openrc-run
command="/usr/sbin/lighttpd" command="/usr/sbin/nginx"
command_args="-D -f /etc/lighttpd/lighttpd.conf" command_args="-g 'daemon off;'"
command_background=true command_background=true
depend() { depend() {
@ -112,11 +158,12 @@ depend() {
} }
EOF EOF
chmod +x "$SERVICE_PATH" chmod +x "$SERVICE_PATH"
rc-update add phpmyadmin default &>/dev/null rc-update add phpmyadmin default
rc-service phpmyadmin start &>/dev/null rc-service phpmyadmin start
fi fi
msg_ok "Service ready" msg_ok "Service created successfully"
echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://$IP:$PORT${CL}"
echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}:${PORT}${CL}"
else else
echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}" echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}"
exit 0 exit 0