MySQL and MariaDB Installation: Basic Management Guide
Step-by-step MySQL and MariaDB installation on Linux, mysql_secure_installation, creating databases and users, basic SQL commands, remote access configuration, and performance tuning.
Step-by-step phpMyAdmin installation on Apache and Nginx, SSL configuration, user management, IP restrictions, and security hardening best practices.
phpMyAdmin is an open-source PHP application that lets you manage MySQL and MariaDB databases through a web browser. You can create databases, manage tables, run SQL queries, and handle user permissions through its graphical interface. This guide covers installation, Apache and Nginx configuration, SSL setup, and security hardening.
Before installing phpMyAdmin, ensure the following are ready:
# System update
apt update && apt upgrade -y
# PHP and required modules
apt install -y php php-mysql php-mbstring php-zip php-gd php-json php-curl
# Check PHP version
php -v
# Install phpMyAdmin
apt install -y phpmyadmin
During installation:
apache2 or lighttpd (skip if using Nginx)dbconfig-common: YesInstalling manually gives you the latest version and more control:
# Download the latest release
cd /tmp
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
# Extract archive
tar xzf phpMyAdmin-latest-all-languages.tar.gz
# Move to web directory
mv phpMyAdmin-*-all-languages /var/www/html/phpmyadmin
# Set ownership
chown -R www-data:www-data /var/www/html/phpmyadmin
chmod -R 755 /var/www/html/phpmyadmin
# Create config file
cp /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config.inc.php
# Generate a random 32-character key
openssl rand -base64 32
# Edit config.inc.php
nano /var/www/html/phpmyadmin/config.inc.php
// Paste the generated key here
$cfg['blowfish_secret'] = 'PASTE_YOUR_RANDOM_KEY_HERE';
// Set temp directory
$cfg['TempDir'] = '/tmp/phpmyadmin';
# Create temp directory
mkdir -p /tmp/phpmyadmin
chown www-data:www-data /tmp/phpmyadmin
nano /etc/apache2/sites-available/phpmyadmin.conf
<VirtualHost *:80>
ServerName pma.example.com
DocumentRoot /var/www/html/phpmyadmin
<Directory /var/www/html/phpmyadmin>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/phpmyadmin_error.log
CustomLog ${APACHE_LOG_DIR}/phpmyadmin_access.log combined
</VirtualHost>
# Enable site
a2ensite phpmyadmin.conf
a2enmod rewrite
systemctl reload apache2
# Create .htpasswd file
htpasswd -c /etc/apache2/.htpasswd admin
<Directory /var/www/html/phpmyadmin>
AuthType Basic
AuthName 'phpMyAdmin Access'
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
nano /etc/nginx/sites-available/phpmyadmin
server {
listen 80;
server_name pma.example.com;
root /var/www/html/phpmyadmin;
index index.php;
# HTTP Auth protection
auth_basic 'phpMyAdmin Access';
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
# Hide sensitive directories
location ~* /(libraries|setup/frames|setup/libs) {
deny all;
return 404;
}
}
# Create htpasswd for Nginx
apt install -y apache2-utils
htpasswd -c /etc/nginx/.htpasswd admin
# Enable site
ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
# Install Certbot
apt install -y certbot python3-certbot-apache
# or for Nginx:
apt install -y certbot python3-certbot-nginx
# Obtain SSL certificate for Apache
certbot --apache -d pma.example.com
# Obtain SSL certificate for Nginx
certbot --nginx -d pma.example.com
# Test auto-renewal
certbot renew --dry-run
server {
listen 443 ssl http2;
server_name pma.example.com;
root /var/www/html/phpmyadmin;
index index.php;
ssl_certificate /etc/letsencrypt/live/pma.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/pma.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# HSTS
add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains' always;
auth_basic 'phpMyAdmin Access';
auth_basic_user_file /etc/nginx/.htpasswd;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}
# Redirect HTTP to HTTPS
server {
listen 80;
server_name pma.example.com;
return 301 https://$host$request_uri;
}
-- Connect to MySQL/MariaDB
mysql -u root -p
-- Create admin user for phpMyAdmin
CREATE USER 'pma_admin'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON *.* TO 'pma_admin'@'localhost' WITH GRANT OPTION;
-- Create restricted app user
CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'AppPassword456!';
GRANT ALL PRIVILEGES ON myapp.* TO 'app_user'@'localhost';
FLUSH PRIVILEGES;
// Allow only specific users
$cfg['Servers'][$i]['AllowDeny']['order'] = 'deny,allow';
$cfg['Servers'][$i]['AllowDeny']['rules'] = [
'deny % from all',
'allow pma_admin from localhost',
'allow app_user from 192.168.1.0/24',
];
// Block root login
$cfg['Servers'][$i]['AllowRoot'] = false;
# Nginx - allow only specific IPs
location / {
allow 192.168.1.0/24;
allow 10.0.0.5;
deny all;
}
# Apache - IP restriction
<Directory /var/www/html/phpmyadmin>
Require ip 192.168.1.0/24
Require ip 10.0.0.5
</Directory>
# Rename the phpmyadmin directory
mv /var/www/html/phpmyadmin /var/www/html/db_manager_x7k2
# Update Nginx/Apache configuration accordingly
// config.inc.php - security settings
$cfg['LoginCookieValidity'] = 1800; // 30 minute session timeout
$cfg['LoginCookieStore'] = 0; // Delete session on browser close
$cfg['ForceSSL'] = true; // Enforce SSL
$cfg['Servers'][$i]['ssl'] = true; // MySQL SSL connection
$cfg['SendErrorReports'] = 'never';
# Remove setup directory after installation
rm -rf /var/www/html/phpmyadmin/setup
nano /etc/fail2ban/filter.d/phpmyadmin.conf
[Definition]
failregex = ^<HOST> .* 'POST /phpmyadmin/index.php.*' (401|403)
ignoreregex =
# /etc/fail2ban/jail.local
[phpmyadmin]
enabled = true
port = http,https
filter = phpmyadmin
logpath = /var/log/nginx/phpmyadmin_access.log
maxretry = 5
bantime = 3600
# Check PHP error logs
tail -f /var/log/php8.1-fpm.log
# Nginx error logs
tail -f /var/log/nginx/error.log
# Apache error logs
tail -f /var/log/apache2/phpmyadmin_error.log
# Check phpMyAdmin permissions
ls -la /var/www/html/phpmyadmin/
# Verify PHP modules
php -m | grep -E 'mysql|mbstring|zip'
phpMyAdmin is a powerful database management tool, but without proper security configuration it poses serious risks. Always use SSL, add HTTP authentication, restrict by IP, and change the default URL. With these measures in place, phpMyAdmin can be used safely on REXE servers.
Check three things: 1) Verify directory permissions with chown -R www-data:www-data /var/www/html/phpmyadmin, 2) Check that 'Require all granted' or allow rules are present in your Apache/Nginx config, 3) If IP restriction is enabled, make sure your IP address is in the allow list.
This is an intentional security measure. The setting $cfg['Servers'][$i]['AllowRoot'] = false; in config.inc.php blocks root login. For security, create a dedicated user instead: CREATE USER 'pma_admin'@'localhost' IDENTIFIED BY 'Password'; GRANT ALL PRIVILEGES ON *.* TO 'pma_admin'@'localhost';
Direct internet exposure is not recommended. For secure access: use SSL, add HTTP Basic Auth, change the default URL (use a random path instead of /phpmyadmin), apply IP restrictions, or access via VPN. Applying all these measures together provides the best security posture.
The $cfg['blowfish_secret'] value in config.inc.php is empty or too short. Generate a 32+ character random key with openssl rand -base64 32 and assign it to that setting. Refresh the page after making the change.
phpMyAdmin 5.x requires PHP 7.2 or higher. PHP 8.0+ is recommended for the latest phpMyAdmin versions. Check your current PHP version with php -v. PHP 8.1 or 8.2 provides the best compatibility and performance.
Step-by-step MySQL and MariaDB installation on Linux, mysql_secure_installation, creating databases and users, basic SQL commands, remote access configuration, and performance tuning.
PostgreSQL installation on Ubuntu/Debian and RHEL/CentOS, creating users and databases, basic SQL commands, remote connection setup, and backup strategies. Step-by-step guide.
Redis and Memcached installation on Ubuntu/Debian, basic commands, persistence settings, TTL configuration, Redis vs Memcached comparison, and application integration.