This Bash script automates the process of installing or updating phpMyAdmin on a Debian/Ubuntu-based web server. It handles downloading the latest version, verifying checksums, configuring the web directory, setting up necessary MySQL database and user for phpMyAdmin's control features, and managing file permissions.
-
Automatic Latest Version Download: Fetches the latest stable phpMyAdmin release directly from
phpmyadmin.net. -
SHA256 Checksum Verification: Ensures the integrity and authenticity of the downloaded archive.
-
Atomic Installation/Update: Replaces the old phpMyAdmin installation with the new one safely by creating a temporary backup.
-
Automated MySQL Setup:
-
Creates a dedicated
phpmyadmindatabase if it doesn't exist. -
Creates or updates a specific control user (
pma_admin) with a strong, automatically generated password. -
Grants the
pma_adminuser the necessary privileges on thephpmyadmindatabase. -
Imports phpMyAdmin's control tables (
create_tables.sql) into thephpmyadmindatabase, but only if they don't already exist.
-
-
config.inc.phpConfiguration: Automatically setsblowfish_secret, MySQL host, and configures the control user (pma_admin) for phpMyAdmin's advanced features (e.g., bookmarks, history). -
Permission Management: Sets appropriate ownership (
www-data:www-data) and file permissions (755for directories,644for files) for the phpMyAdmin installation. -
Logging: All actions and errors are logged to
/var/log/update-phpmyadmin.log.
Before running this script, ensure the following are installed on your Debian/Ubuntu system:
-
wget: For downloading files. -
unzip: For extracting the phpMyAdmin archive. -
mysql-client: The MySQL client utilities (required formysqlcommand). -
openssl: For generating random passwords andblowfish_secret. -
php-mbstring(and other PHP extensions required by phpMyAdmin, e.g.,php-mysqli,php-json,php-gd,php-zip,php-curl,php-xml): Ensure your PHP installation has these extensions enabled. While the script doesn't install them, they are crucial for phpMyAdmin's functionality.
The script allows you to hardcode certain configuration parameters directly within the script file itself. Using these variables takes the highest precedence over values provided via command-line arguments or environment variables.
Important Security Note: If you hardcode sensitive information like passwords, ensure the script file has very strict permissions (e.g., chmod 600 /usr/local/bin/phpmyadmin.sh) to prevent unauthorized access. The script attempts to set these permissions automatically if MYSQL_ADMIN_PASSWORD_HARDCODED is used.
-
MYSQL_ADMIN_USER_HARDCODED:- Set your MySQL administrative username here (e.g.,
"my_mysql_admin"). - Leave empty (
"") if you prefer to provide the username via command-line argument or use the default (root).
- Set your MySQL administrative username here (e.g.,
-
MYSQL_ADMIN_PASSWORD_HARDCODED:- Set your MySQL administrative password here (e.g.,
"myStrongAdminPass"). - Leave empty (
"") if you prefer to provide the password via command-line argument, environment variable, or interactive prompt.
- Set your MySQL administrative password here (e.g.,
-
PMA_MYSQL_HOST_HARDCODED:- Set your MySQL host here (e.g.,
"192.168.1.100"or"db.example.com"). - This will override any host provided via the command-line argument.
- Leave empty (
"") if you prefer to provide the host via command-line argument or use the default (localhost).
- Set your MySQL host here (e.g.,
-
Save the Script: Save the script content to a file, for example,
/usr/local/bin/phpmyadmin.sh. -
Make Executable:
sudo chmod +x /usr/local/bin/phpmyadmin.sh
-
Run the Script:
The script accepts optional parameters:
sudo /usr/local/bin/phpmyadmin.sh [PMA_INSTALL_DIR] [MYSQL_HOST] [MYSQL_ADMIN_USER] [MYSQL_ADMIN_PASSWORD]-
PMA_INSTALL_DIR(Optional): The directory where phpMyAdmin will be installed.-
Default:
/var/www/phpmyadmin -
Example:
/usr/share/phpmyadmin
-
-
MYSQL_HOST(Optional): The MySQL host to configure in phpMyAdmin'sconfig.inc.php. This is also the host the script will attempt to connect to for MySQL administrative operations.- Note: This argument is superseded by
PMA_MYSQL_HOST_HARDCODEDif that variable is set in the script. - Default:
localhost(if not specified via hardcoded variable or this argument). - Example:
127.0.0.1ormy.remote.mysql.server
- Note: This argument is superseded by
-
MYSQL_ADMIN_USER(Optional): The username for the MySQL administrative user.- Note: This argument is superseded by
MYSQL_ADMIN_USER_HARDCODEDif that variable is set in the script. - Default:
root(if not specified via hardcoded variable or this argument).
- Note: This argument is superseded by
-
MYSQL_ADMIN_PASSWORD(Optional): The password for the MySQL administrative user (MYSQL_ADMIN_USER).- Note: This argument is superseded by
MYSQL_ADMIN_PASSWORD_HARDCODEDif that variable is set in the script. - See "MySQL Administrative User and Password Handling" below for detailed explanation of how the script obtains this password.
- Note: This argument is superseded by
-
-
Default installation (will prompt for MySQL root password):
sudo /usr/local/bin/phpmyadmin.sh
-
Specify installation directory:
sudo /usr/local/bin/phpmyadmin.sh /var/www/html/pma
-
Specify MySQL host (e.g., a remote container/VM):
sudo /usr/local/bin/phpmyadmin.sh /var/www/phpmyadmin 192.168.1.100
-
Provide MySQL administrative user and password via command-line arguments:
sudo /usr/local/bin/phpmyadmin.sh /var/www/phpmyadmin localhost my_admin_user MySecureAdminPass123
-
Provide MySQL administrative password via environment variable (using default
rootuser):export MYSQL_ADMIN_PASSWORD="MySecureAdminPass123" sudo /usr/local/bin/phpmyadmin.sh
(Remember to
unset MYSQL_ADMIN_PASSWORDafter execution for security.) -
Using hardcoded variables in the script: Refer to the "Hardcoded Configuration Variables" section above for how to set
MYSQL_ADMIN_USER_HARDCODED,MYSQL_ADMIN_PASSWORD_HARDCODED, orPMA_MYSQL_HOST_HARDCODEDdirectly in the script. When these are set, the corresponding command-line arguments can be omitted or left as empty strings.Example if
PMA_MYSQL_HOST_HARDCODEDis set in the script:sudo /usr/local/bin/phpmyadmin.sh /var/www/phpmyadmin "" root MySecureRootPass123(Note the empty string
""for theMYSQL_HOSTargument, as it's now overridden by the hardcoded variable.)
You can schedule this script to run automatically at regular intervals (e.g., weekly) using cron. This ensures your phpMyAdmin installation stays up-to-date.
-
Open Crontab: Open your
crontable for editing. If you are scheduling this script to run asroot(which is necessary for the MySQL operations), usesudo crontab -e.sudo crontab -e
-
Add a Cron Job Entry: Add the following line to the end of the file. This example schedules the script to run every Sunday at midnight (00:00).
0 0 * * 0 /usr/local/bin/phpmyadmin.sh >> /var/log/phpmyadmin_cron.log 2>&1
-
0: Minute (0-59) -
0: Hour (0-23) -
*: Day of month (1-31) -
*: Month (1-12) -
0: Day of week (0-7, where 0 and 7 are Sunday) -
/usr/local/bin/phpmyadmin.sh: Path to your script. -
>> /var/log/phpmyadmin_cron.log 2>&1: Redirects all standard output and errors to a separate cron log file, preventing email notifications from cron and allowing you to review the scheduled run's output.
Important Considerations for Cron and Passwords:
-
If your
MYSQL_ADMIN_PASSWORDis hardcoded in the script (see "Hardcoded Configuration Variables"), ensure the script's permissions are strictly set to600(chmod 600) as mentioned in that section. -
Do NOT include the MySQL administrative password directly in the cron entry. The script's internal password handling (hardcoded in script, environment variable, or interactive prompt) will manage this. Since cron jobs run non-interactively, if the password is not hardcoded or passed via environment variable, the script will fail to prompt for it and will abort. Therefore, for cron, either hardcode the password (with strict permissions) or ensure
MYSQL_ADMIN_PASSWORDis available in the cron job's environment (e.g., by sourcing a file that sets it, though hardcoding in the script is simpler for cron).
-
-
Save and Exit: Save the
crontabfile (usuallyCtrl+X, thenY, thenEnterinnano).
The script requires a MySQL user with administrative privileges to perform critical database operations:
-
Creating the
phpmyadmincontrol database. -
Creating or updating the
pma_admincontrol user. -
Granting privileges to the
pma_adminuser. -
Importing the necessary control tables.
The script attempts to obtain the MySQL administrative username and password in the following order of precedence:
For Username (MYSQL_ADMIN_USER):
- Hardcoded in the script: If
MYSQL_ADMIN_USER_HARDCODEDis set (see "Hardcoded Configuration Variables"). - Command-line argument: If provided as the third argument when executing the script.
- Default: If not provided by the above methods, it defaults to
root.
For Password (MYSQL_ADMIN_PASSWORD):
-
Hardcoded in the script: If
MYSQL_ADMIN_PASSWORD_HARDCODEDis set (see "Hardcoded Configuration Variables"). -
Command-line argument: If provided as the fourth argument when executing the script.
-
Environment variable: If the
MYSQL_ADMIN_PASSWORDenvironment variable is exported before running the script. -
Interactive prompt: If the password is not found through any of the above methods, the script will interactively prompt you to enter it.
- You will have up to 3 attempts to enter the correct password. This helps prevent accidental typos (e.g., wrong keyboard layout or CapsLock). If all attempts fail, the script will abort.
-
Avoid hardcoding in production: For maximum security, avoid hardcoding passwords directly in the script file. If you do, strictly control file permissions.
-
Use environment variables temporarily: If using environment variables,
unsetthem immediately after the script completes to clear them from your shell's environment. The script attempts tounsetthe variable if it sourced it from the environment or interactively. -
Restrict script permissions: Regardless of how you provide the password, always ensure your script file has strict permissions (
chmod 600) if it contains sensitive information or you are running it asroot.
This script creates or updates a dedicated MySQL user named pma_admin (with localhost host) and a database named phpmyadmin. This user and database are essential for phpMyAdmin's advanced features, such as:
-
User preferences storage: Saving custom settings for each phpMyAdmin user.
-
Bookmark queries: Storing frequently used SQL queries.
-
Relation transformations: Defining relationships between tables.
-
History: Keeping a record of executed SQL commands.
The pma_admin user's password is automatically generated by the script and securely stored within phpMyAdmin's config.inc.php file. If the pma_admin user already exists (e.g., from a previous manual setup or migration), the script will update its password to the newly generated one and configure config.inc.php accordingly to ensure consistency. The control tables (create_tables.sql) are imported only if the phpmyadmin database is empty.
All script output, including actions performed and any errors encountered, is logged to:
/var/log/update-phpmyadmin.log
This file is useful for debugging and reviewing the script's execution history. л