|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright (c) 2021-2025 community-scripts ORG |
| 4 | +# Author: MickLesk (CanbiZ) |
| 5 | +# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE |
| 6 | + |
| 7 | +function header_info { |
| 8 | + clear |
| 9 | + cat <<"EOF" |
| 10 | + ____ __ __ ___ ___ __ _ |
| 11 | + / __ \/ /_ ____ / |/ /_ __/ | ____/ /___ ___ (_)___ |
| 12 | + / /_/ / __ \/ __ \/ /|_/ / / / / /| |/ __ / __ `__ \/ / __ \ |
| 13 | + / ____/ / / / /_/ / / / / /_/ / ___ / /_/ / / / / / / / / / / |
| 14 | +/_/ /_/ /_/ .___/_/ /_/\__, /_/ |_\__,_/_/ /_/ /_/_/_/ /_/ |
| 15 | + /_/ /____/ |
| 16 | +EOF |
| 17 | +} |
| 18 | + |
| 19 | +YW=$(echo "\033[33m") |
| 20 | +GN=$(echo "\033[1;92m") |
| 21 | +RD=$(echo "\033[01;31m") |
| 22 | +BL=$(echo "\033[36m") |
| 23 | +CL=$(echo "\033[m") |
| 24 | +CM="${GN}✔️${CL}" |
| 25 | +CROSS="${RD}✖️${CL}" |
| 26 | +INFO="${BL}ℹ️${CL}" |
| 27 | + |
| 28 | +APP="phpMyAdmin" |
| 29 | +INSTALL_DIR_DEBIAN="/var/www/html/phpMyAdmin" |
| 30 | +INSTALL_DIR_ALPINE="/usr/share/phpmyadmin" |
| 31 | + |
| 32 | +IFACE=$(ip -4 route | awk '/default/ {print $5; exit}') |
| 33 | +IP=$(ip -4 addr show "$IFACE" | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1) |
| 34 | +[[ -z "$IP" ]] && IP=$(hostname -I | awk '{print $1}') |
| 35 | +[[ -z "$IP" ]] && IP="127.0.0.1" |
| 36 | + |
| 37 | +# Detect OS |
| 38 | +if [[ -f "/etc/alpine-release" ]]; then |
| 39 | + OS="Alpine" |
| 40 | + PKG_MANAGER_INSTALL="apk add --no-cache" |
| 41 | + PKG_QUERY="apk info -e" |
| 42 | + INSTALL_DIR="$INSTALL_DIR_ALPINE" |
| 43 | +elif [[ -f "/etc/debian_version" ]]; then |
| 44 | + OS="Debian" |
| 45 | + PKG_MANAGER_INSTALL="apt-get install -y" |
| 46 | + PKG_QUERY="dpkg -l" |
| 47 | + INSTALL_DIR="$INSTALL_DIR_DEBIAN" |
| 48 | +else |
| 49 | + echo -e "${CROSS} Unsupported OS detected. Exiting." |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +header_info |
| 54 | + |
| 55 | +function msg_info() { echo -e "${INFO} ${YW}${1}...${CL}"; } |
| 56 | +function msg_ok() { echo -e "${CM} ${GN}${1}${CL}"; } |
| 57 | +function msg_error() { echo -e "${CROSS} ${RD}${1}${CL}"; } |
| 58 | + |
| 59 | +function check_internet() { |
| 60 | + msg_info "Checking Internet connectivity to GitHub" |
| 61 | + HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://github.com) |
| 62 | + if [[ "$HTTP_CODE" -ge 200 && "$HTTP_CODE" -lt 400 ]]; then |
| 63 | + msg_ok "Internet connectivity OK" |
| 64 | + else |
| 65 | + msg_error "Internet connectivity or GitHub unreachable (Status $HTTP_CODE). Exiting." |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +} |
| 69 | + |
| 70 | +function is_phpmyadmin_installed() { |
| 71 | + if [[ "$OS" == "Debian" ]]; then |
| 72 | + [[ -f "$INSTALL_DIR/config.inc.php" ]] |
| 73 | + else |
| 74 | + [[ -d "$INSTALL_DIR_ALPINE" ]] && rc-service lighttpd status &>/dev/null |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +function install_php_and_modules() { |
| 79 | + msg_info "Checking existing PHP installation" |
| 80 | + if command -v php >/dev/null 2>&1; then |
| 81 | + PHP_VERSION=$(php -r 'echo PHP_VERSION;') |
| 82 | + msg_ok "Found PHP version $PHP_VERSION" |
| 83 | + else |
| 84 | + msg_info "PHP not found, will install PHP core" |
| 85 | + fi |
| 86 | + |
| 87 | + if [[ "$OS" == "Debian" ]]; then |
| 88 | + PHP_MODULES=("php" "php-mysqli" "php-mbstring" "php-zip" "php-gd" "php-json" "php-curl") |
| 89 | + MISSING_PACKAGES=() |
| 90 | + for pkg in "${PHP_MODULES[@]}"; do |
| 91 | + if ! dpkg -l | grep -qw "$pkg"; then |
| 92 | + MISSING_PACKAGES+=("$pkg") |
| 93 | + fi |
| 94 | + done |
| 95 | + if [[ ${#MISSING_PACKAGES[@]} -gt 0 ]]; then |
| 96 | + msg_info "Installing missing PHP packages: ${MISSING_PACKAGES[*]}" |
| 97 | + if ! apt-get update &>/dev/null || ! apt-get install -y "${MISSING_PACKAGES[@]}" &>/dev/null; then |
| 98 | + msg_error "Failed to install required PHP modules. Exiting." |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | + msg_ok "Installed missing PHP packages" |
| 102 | + else |
| 103 | + msg_ok "All required PHP modules are already installed" |
| 104 | + fi |
| 105 | + else |
| 106 | + msg_info "Installing Lighttpd and PHP for Alpine" |
| 107 | + $PKG_MANAGER_INSTALL lighttpd php php-fpm php-session php-json php-mysqli curl tar openssl &>/dev/null |
| 108 | + msg_ok "Installed Lighttpd and PHP" |
| 109 | + fi |
| 110 | +} |
| 111 | + |
| 112 | +function install_phpmyadmin() { |
| 113 | + msg_info "Fetching latest phpMyAdmin release from GitHub" |
| 114 | + LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4) |
| 115 | + LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g') |
| 116 | + if [[ -z "$LATEST_VERSION" ]]; then |
| 117 | + msg_error "Could not determine latest phpMyAdmin version from GitHub – falling back to 5.2.2" |
| 118 | + LATEST_VERSION="RELEASE_5_2_2" |
| 119 | + fi |
| 120 | + msg_ok "Latest version: $LATEST_VERSION" |
| 121 | + |
| 122 | + TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" |
| 123 | + msg_info "Downloading ${TARBALL_URL}" |
| 124 | + if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then |
| 125 | + msg_error "Download failed: $TARBALL_URL" |
| 126 | + exit 1 |
| 127 | + fi |
| 128 | + |
| 129 | + mkdir -p "$INSTALL_DIR" |
| 130 | + tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR" |
| 131 | +} |
| 132 | + |
| 133 | +function configure_phpmyadmin() { |
| 134 | + if [[ "$OS" == "Debian" ]]; then |
| 135 | + cp "$INSTALL_DIR/config.sample.inc.php" "$INSTALL_DIR/config.inc.php" |
| 136 | + SECRET=$(openssl rand -base64 24) |
| 137 | + sed -i "s#\$cfg\['blowfish_secret'\] = '';#\$cfg['blowfish_secret'] = '${SECRET}';#" "$INSTALL_DIR/config.inc.php" |
| 138 | + chmod 660 "$INSTALL_DIR/config.inc.php" |
| 139 | + chown -R www-data:www-data "$INSTALL_DIR" |
| 140 | + systemctl restart apache2 |
| 141 | + msg_ok "Configured phpMyAdmin with Apache" |
| 142 | + else |
| 143 | + msg_info "Configuring Lighttpd for phpMyAdmin (Alpine detected)" |
| 144 | + |
| 145 | + mkdir -p /etc/lighttpd |
| 146 | + cat <<EOF >/etc/lighttpd/lighttpd.conf |
| 147 | +server.modules = ( |
| 148 | + "mod_access", |
| 149 | + "mod_alias", |
| 150 | + "mod_accesslog", |
| 151 | + "mod_fastcgi" |
| 152 | +) |
| 153 | +
|
| 154 | +server.document-root = "${INSTALL_DIR}" |
| 155 | +server.port = 80 |
| 156 | +
|
| 157 | +index-file.names = ( "index.php", "index.html" ) |
| 158 | +
|
| 159 | +fastcgi.server = ( ".php" => |
| 160 | + (( |
| 161 | + "host" => "127.0.0.1", |
| 162 | + "port" => 9000, |
| 163 | + "check-local" => "disable" |
| 164 | + )) |
| 165 | +) |
| 166 | +
|
| 167 | +alias.url = ( "/phpMyAdmin/" => "${INSTALL_DIR}/" ) |
| 168 | +
|
| 169 | +accesslog.filename = "/var/log/lighttpd/access.log" |
| 170 | +server.errorlog = "/var/log/lighttpd/error.log" |
| 171 | +EOF |
| 172 | + |
| 173 | + msg_info "Starting PHP-FPM and Lighttpd" |
| 174 | + |
| 175 | + PHP_VERSION=$(php -r 'echo PHP_MAJOR_VERSION . PHP_MINOR_VERSION;') |
| 176 | + PHP_FPM_SERVICE="php-fpm${PHP_VERSION}" |
| 177 | + |
| 178 | + if $STD rc-service "$PHP_FPM_SERVICE" start && $STD rc-update add "$PHP_FPM_SERVICE" default; then |
| 179 | + msg_ok "Started PHP-FPM service: $PHP_FPM_SERVICE" |
| 180 | + else |
| 181 | + msg_error "Failed to start PHP-FPM service: $PHP_FPM_SERVICE" |
| 182 | + exit 1 |
| 183 | + fi |
| 184 | + |
| 185 | + $STD rc-service lighttpd start |
| 186 | + $STD rc-update add lighttpd default |
| 187 | + msg_ok "Configured and started Lighttpd successfully" |
| 188 | + |
| 189 | + fi |
| 190 | +} |
| 191 | + |
| 192 | +function uninstall_phpmyadmin() { |
| 193 | + msg_info "Stopping Webserver" |
| 194 | + if [[ "$OS" == "Debian" ]]; then |
| 195 | + systemctl stop apache2 |
| 196 | + else |
| 197 | + $STD rc-service lighttpd stop |
| 198 | + $STD rc-service php-fpm stop |
| 199 | + fi |
| 200 | + |
| 201 | + msg_info "Removing phpMyAdmin directory" |
| 202 | + rm -rf "$INSTALL_DIR" |
| 203 | + |
| 204 | + if [[ "$OS" == "Alpine" ]]; then |
| 205 | + msg_info "Removing Lighttpd config" |
| 206 | + rm -f /etc/lighttpd/lighttpd.conf |
| 207 | + $STD rc-service php-fpm restart |
| 208 | + $STD rc-service lighttpd restart |
| 209 | + else |
| 210 | + $STD systemctl restart apache2 |
| 211 | + fi |
| 212 | + msg_ok "Uninstalled phpMyAdmin" |
| 213 | +} |
| 214 | + |
| 215 | +function update_phpmyadmin() { |
| 216 | + msg_info "Fetching latest phpMyAdmin release from GitHub" |
| 217 | + LATEST_VERSION_RAW=$(curl -s https://api.github.com/repos/phpmyadmin/phpmyadmin/releases/latest | grep tag_name | cut -d '"' -f4) |
| 218 | + LATEST_VERSION=$(echo "$LATEST_VERSION_RAW" | sed -e 's/^RELEASE_//' -e 's/_/./g') |
| 219 | + |
| 220 | + if [[ -z "$LATEST_VERSION" ]]; then |
| 221 | + msg_error "Could not determine latest phpMyAdmin version from GitHub – falling back to 5.2.2" |
| 222 | + LATEST_VERSION="5.2.2" |
| 223 | + fi |
| 224 | + msg_ok "Latest version: $LATEST_VERSION" |
| 225 | + |
| 226 | + TARBALL_URL="https://files.phpmyadmin.net/phpMyAdmin/${LATEST_VERSION}/phpMyAdmin-${LATEST_VERSION}-all-languages.tar.gz" |
| 227 | + msg_info "Downloading ${TARBALL_URL}" |
| 228 | + |
| 229 | + if ! curl -fsSL "$TARBALL_URL" -o /tmp/phpmyadmin.tar.gz; then |
| 230 | + msg_error "Download failed: $TARBALL_URL" |
| 231 | + exit 1 |
| 232 | + fi |
| 233 | + |
| 234 | + BACKUP_DIR="/tmp/phpmyadmin-backup-$(date +%Y%m%d-%H%M%S)" |
| 235 | + mkdir -p "$BACKUP_DIR" |
| 236 | + BACKUP_ITEMS=("config.inc.php" "upload" "save" "tmp" "themes") |
| 237 | + |
| 238 | + msg_info "Backing up existing phpMyAdmin data" |
| 239 | + for item in "${BACKUP_ITEMS[@]}"; do |
| 240 | + [[ -e "$INSTALL_DIR/$item" ]] && cp -a "$INSTALL_DIR/$item" "$BACKUP_DIR/" && echo " ↪︎ $item" |
| 241 | + done |
| 242 | + msg_ok "Backup completed: $BACKUP_DIR" |
| 243 | + |
| 244 | + tar xf /tmp/phpmyadmin.tar.gz --strip-components=1 -C "$INSTALL_DIR" |
| 245 | + msg_ok "Extracted phpMyAdmin $LATEST_VERSION" |
| 246 | + |
| 247 | + msg_info "Restoring preserved files" |
| 248 | + for item in "${BACKUP_ITEMS[@]}"; do |
| 249 | + [[ -e "$BACKUP_DIR/$item" ]] && cp -a "$BACKUP_DIR/$item" "$INSTALL_DIR/" && echo " ↪︎ $item restored" |
| 250 | + done |
| 251 | + msg_ok "Restoration completed" |
| 252 | + |
| 253 | + configure_phpmyadmin |
| 254 | +} |
| 255 | + |
| 256 | +if is_phpmyadmin_installed; then |
| 257 | + echo -e "${YW}⚠️ ${APP} is already installed at ${INSTALL_DIR}.${CL}" |
| 258 | + read -r -p "Would you like to Update (1), Uninstall (2) or Cancel (3)? [1/2/3]: " action |
| 259 | + action="${action//[[:space:]]/}" |
| 260 | + case "$action" in |
| 261 | + 1) |
| 262 | + check_internet |
| 263 | + update_phpmyadmin |
| 264 | + ;; |
| 265 | + 2) |
| 266 | + uninstall_phpmyadmin |
| 267 | + ;; |
| 268 | + 3) |
| 269 | + echo -e "${YW}⚠️ Action cancelled. Exiting.${CL}" |
| 270 | + exit 0 |
| 271 | + ;; |
| 272 | + *) |
| 273 | + echo -e "${YW}⚠️ Invalid input. Exiting.${CL}" |
| 274 | + exit 1 |
| 275 | + ;; |
| 276 | + esac |
| 277 | +else |
| 278 | + read -r -p "Would you like to install ${APP}? (y/n): " install_prompt |
| 279 | + install_prompt="${install_prompt//[[:space:]]/}" |
| 280 | + if [[ "${install_prompt,,}" =~ ^(y|yes)$ ]]; then |
| 281 | + check_internet |
| 282 | + install_php_and_modules |
| 283 | + install_phpmyadmin |
| 284 | + configure_phpmyadmin |
| 285 | + if [[ "$OS" == "Debian" ]]; then |
| 286 | + echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/phpMyAdmin${CL}" |
| 287 | + else |
| 288 | + echo -e "${CM} ${GN}${APP} is reachable at: ${BL}http://${IP}/${CL}" |
| 289 | + fi |
| 290 | + else |
| 291 | + echo -e "${YW}⚠️ Installation skipped. Exiting.${CL}" |
| 292 | + exit 0 |
| 293 | + fi |
| 294 | +fi |
0 commit comments