Introduction
phpMyAdmin is a database administration tool for MySQL and MariaDB, written in PHP. It is mainly used to manage databases through a web interface, allowing you to perform tasks such as creating and deleting databases, creating and deleting tables, inserting and querying data, and so on (without having to use commands).
It offers many options, such as importing and exporting databases, optimizing tables, managing users and permissions, etc. This makes it a very useful tool for both database administrators and web developers who use MySQL as their database engine.
This article will explain how to install phpMyAdmin and configure it using the web server NGINX, along with a brief explanation of how to create a user to log in to the tool and access all databases. A LEMP stack previously configured for this scenario will be used. For more details on How to install NGINX MariaDB and PHP (LEMP), click the hyperlink.
Installing phpmyadmin
On the most common distributions, the package for installing phpMyAdmin is included in the official repository, and you only need to use the package management tool included with the operating system itself.
On Debian or distributions based on it such as Ubuntu, simply run the following:
# apt install phpmyadmin -y --no-install-recommends
It will be installed using the native "Advanced Package Tool" package manager.
On RHEL or distributions based on it such as CentOS, simply run the following:
# yum install phpmyadmin -y
It will be installed using the native "Yellowdog Updater (Modified)" package manager.
Configuring phpMyAdmin
By default, the installation path for phpmyadmin is in /usr/share, and you can add it with the location parameter to configure it in NGINX. You can also decide whether to deploy it for a specific site by modifying its configuration file or add it to the global configuration to access it using the address, by creating a configuration file.
To configure it for a specific site, simply add the following lines to its configuration file inside the server block. You can use an editor directly to do this:
# nano /etc/nginx/sites-enabled/ejemplo.conf
You must add the following content to the file in the server block, inside its braces:
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
To allow access to phpMyAdmin using the server's IP address, first check whether the directory conf.d is included in the NGINX configuration:
# grep conf.d /etc/nginx/nginx.conf
The following result should appear:
include /etc/nginx/conf.d/*.conf;
If no results appear, create the directory and add the include to the file nginx.conf.
Next, create the configuration file for phpMyAdmin inside the directory. You can use an editor directly to do this:
# nano /etc/nginx/conf.d/phpmyadmin.conf
You must add the following configuration to the file:
server {
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
However, remember to modify the value of the parameter fastcgi_pass if necessary. For example, for another version of PHP, change it to the FPM sock for the corresponding version:
fastcgi_pass unix:/run/php/php8.0-fpm.sock;Additionally, you can add more security by filtering by IP address in the NGINX configuration or even create redundancy with auth_basic. More information is available at: How to configure authentication for a web directory in NGINX.
Finally, you need to apply the configuration, and you can simply restart the service to do so:
# systemctl restart nginxOnce the configuration has been completed and implemented, the web server should display the phpMyAdmin login page when you add phpmyadmin to the root in your web browser.
Creating a user for phpMyAdmin
If you have not created any users, you can run the following from a MySQL client:
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'contraseña';You will create a user named "admin", and remember to change the example password, then grant permissions to the user. You can use the following command to grant all permissions on all databases:
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';Once the user has been created and granted permissions, you can use it to access phpMyAdmin. More information is available in the article How to manage users and permissions in MySQL.
We hope this tutorial has helped you 🙂. Remember, if you have questions about this or any other issue related to your servers on Clouding, feel free to write to soporte@clouding.io We're here for whatever you need!