In this tutorial, we'll explain how to install phpMyAdmin on AlmaLinux 9 server.
You will learn how to install and secure phpMyAdmin on an AlmaLinux 9 server. phpMyAdmin is a popular web-based database management tool for managing MySQL/MariaDB databases. It's easy to use and provides a graphical interface to handle tasks such as creating databases, running SQL queries, and managing tables.
Prerequisites
- A dedicated server or KVM VPS running AlmaLinux 9.
- A non-root user with sudo privileges.
How to Install phpMyAdmin on AlmaLinux 9
Step 1: Installing the LAMP Stack
Update the AlmaLinux 9 system to ensure all existing packages are up to date.
sudo dnf update -y
Step 2: Install Apache Web Server
To install the Apache web server, use the following command:
sudo dnf install httpd -y
Once installed, enable Apache to start at boot and start the service:
sudo systemctl enable httpd
sudo systemctl start httpd
You can verify that Apache is running by visiting your server's IP address in a browser:
http://YOUR_SERVER_IP/
You should see the Apache default page.
Step 3: Install MariaDB (MySQL Alternative)
Install MariaDB server by running:
sudo dnf install mariadb-server -y
After the installation, enable and start the MariaDB service:
sudo systemctl enable mariadb
sudo systemctl start mariadb
To improve the security of your MariaDB installation, run the built-in security script:
sudo mysql_secure_installation
Follow the prompts to:
- Set a root password.
- Remove anonymous users.
- Disallow remote root login.
- Remove test databases.
- Reload privilege tables.
Create a MySQL User. Login into MySQL:
sudo mysql
or
mysql -u root -p
create a new user and give it a strong password:
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'password';
Note: Replace your_username
and password
with your values.
Grant your new user appropriate privileges.
GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'localhost' WITH GRANT OPTION;
Exit the MySQL session.
exit
Step 4: Install PHP
phpMyAdmin requires PHP to run. Install PHP and its modules using the following command:
sudo dnf install php php-mysqlnd php-fpm php-json php-xml php-mbstring php-gd -y
To ensure Apache loads the PHP modules, restart Apache:
sudo systemctl restart httpd
Step 5: Installing and Securing phpMyAdmin
phpMyAdmin is not included in AlmaLinux's default repositories, so you need to install the EPEL (Extra Packages for Enterprise Linux) repository:
sudo dnf install epel-release -y
After enabling EPEL, install phpMyAdmin using:
sudo dnf install phpmyadmin -y
Step 6: Configure Apache for phpMyAdmin
Configure Apache to allow access to phpMyAdmin. Open the configuration file:
sudo nano /etc/httpd/conf.d/phpMyAdmin.conf
Replace the lines that restrict access to localhost with:
<Directory /usr/share/phpMyAdmin/>
Require all granted
</Directory>
Tip: If you want to restrict access to a specific IP address, replace Require all granted with Require ip YOUR_IP_ADDRESS
.
Save and close the file.
Restart Apache to apply the changes:
sudo systemctl restart httpd
Now, access phpMyAdmin by visiting:
http://YOUR_SERVER_IP/phpmyadmin
Log in using the user we have created in step 3.
Step 7: Secure phpMyAdmin
To add another layer of protection, configure Apache to require a username and password. Create an authentication file:
sudo htpasswd -c /etc/httpd/.htpasswd username
Choose a strong password when prompted. Then, open the phpMyAdmin configuration file again:
sudo nano /etc/httpd/conf.d/phpMyAdmin.conf
Add these lines within the <Directory /usr/share/phpMyAdmin/>
section to enforce password authentication:
AuthType Basic
AuthName "Restricted Access"
AuthUserFile /etc/httpd/.htpasswd
Require valid-user
Save the file and restart Apache:
sudo systemctl restart httpd
Step 8: Restrict Access by IP Address
To allow only specific IPs to access phpMyAdmin, you can configure IP restrictions. Open the phpMyAdmin configuration file:
sudo nano /etc/httpd/conf.d/phpMyAdmin.conf
Replace:
<Directory /usr/share/phpMyAdmin/>
Require all granted
</Directory>
With:
<Directory /usr/share/phpMyAdmin/>
Require ip YOUR_IP_ADDRESS
</Directory>
Step 9: Enable HTTPS (SSL/TLS)
To secure your phpMyAdmin connection, enable HTTPS. First, install the mod_ssl
module:
sudo dnf install mod_ssl -y
Next, obtain an SSL certificate using Let's Encrypt:
Install Certbot:
sudo dnf install certbot python3-certbot-apache -y
Obtain an SSL certificate:
sudo certbot --apache -d your_domain_name
Follow the prompts to complete the SSL setup. After this, your phpMyAdmin will be accessible via HTTPS.
You might get Unable to find a virtual host listening on port 80 error. If you this error follow these steps:
Create a new configuration file for the virtual host. For example:
sudo nano /etc/httpd/conf.d/default.conf
In the file, add the following configuration to define a basic virtual host that listens on port 80:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain.com
DocumentRoot /var/www/html
ServerName your_domain.com
ServerAlias www.your_domain.com
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
</VirtualHost>
Make sure to replace your_domain.com
with your actual domain or IP address. The DocumentRoot points to /var/www/html
, which is the default location for serving web files.
To make sure the Apache configuration is valid, run the following command:
sudo apachectl configtest
You should see a message that says Syntax OK. If there are errors, address them as suggested by the output.
Finally, restart the Apache web server to apply the changes:
sudo systemctl restart httpd
Conclusion
In this tutorial, you learned how to install the LAMP stack (Apache, MariaDB, PHP) and phpMyAdmin on AlmaLinux 9, along with important security steps like enabling Apache authentication, restricting access by IP, disabling root login, and using HTTPS.
By following this guide, your users will be able to set up and manage their databases securely using phpMyAdmin.