phpMyAdmin with NGINX – Installation and Configuration
In modern web development, server management is a vital part. Two popular tools that assist in this process are Nginx and phpMyAdmin. Nginx is a high-performance web server, while phpMyAdmin is a widely-used web-based interface for managing MySQL or MariaDB databases. When combined, these tools provide a strong platform for serving dynamic web applications and managing their databases efficiently.
What is Nginx?
NGINX (pronounced “engine-x”) is an open-source web server that also functions as a reverse proxy, load balancer, and HTTP cache. It is known for its speed, reliability and ability to handle a large number of simultaneous connections efficiently, making it ideal for high-traffic websites.
Key Features of Nginx
The key features of NGINX Web Server are –
- Event-Driven Architecture: Nginx handles connections asynchronously, which helps in managing multiple requests without consuming significant system resources.
- Reverse Proxy Capabilities: Nginx can be used to distribute traffic across multiple servers, balancing the load and preventing any one server from being overwhelmed.
- Static Content Delivery: Nginx is optimized for serving static files, such as images and stylesheets, efficiently, contributing to faster loading times.
- Security: Nginx provides security features like DDoS protection, SSL/TLS encryption, and HTTP/2 support.
- Scalability: Nginx is highly scalable and can easily handle growth in traffic by distributing the load across multiple servers.
What is phpMyAdmin?
phpMyAdmin is a free and open-source web application written in PHP that provides an easy-to-use interface to manage MySQL or MariaDB databases. It is a popular choice among developers and administrators for database management tasks, such as querying the database, exporting/importing data, and managing user permissions.
Key Features of phpMyAdmin
The key features of phpMyAdmin are –
- Web-Based Interface: phpMyAdmin allows database management through a web browser, making it accessible from anywhere and any system without needing to install additional client software.
- Database Management: It provides functionalities for querying, creating, modifying and deleting databases and tables.
- Query Execution: Users can write and execute SQL queries, enabling them to interact with the database directly using phpMyAdmin web interface.
- Data Export/Import: phpMyAdmin allows users to export data in various formats (e.g., CSV, SQL) and import it back into the database so easily.
- User Privileges Management: Administrators can easily assign or revoke permissions, ensuring secure access to the database.
Setting Up Nginx with phpMyAdmin
Install Nginx
First, we need to install Nginx on our server. If we are using a Debian-based distribution, we can install Nginx using the following commands.
- sudo apt update
- sudo apt install nginx
Once installed, we can start and enable Nginx to run at startup with the following command.
- sudo systemctl start nginx
- sudo systemctl enable nginx
We can check the status of Nginx using the following command.
- sudo systemctl status nginx
Install PHP and MariaDB
For phpMyAdmin to function, we need to have PHP and MariaDB installed on our system. To install PHP and MariaDB, we need to run the following command.
- sudo apt install php-fpm php-mysql mariadb-server
Make ensure that both PHP-FPM and MariaDB are configured and running.
MariaDB Configuration
For new MariaDB installations, we need to run security script. The script changes some of the less secure default options and makes it more secure.
To run the security script, we need to run the following command.
- sudo mysql_secure_installation
This will take us through a series of prompts where we can make some changes to our MariaDB installation’s security options. We can do the following actions for these options.
Enter current password for root (enter for none): Hit Enter because we have no initial password.
Set root password? [Y/n] N
We will now create a new account called dbadmin with the same capabilities as the root account, but configured for password authentication. So, we need to open up the MariaDB prompt from our terminal.
- sudo mariadb
We are now in MariaDB prompt. Do the following Database command to create a user and to change its privileges.
- MariaDB [(none)]> create user dbadmin;
- MariaDB [(none)]> GRANT ALL ON *.* TO ‘dbadmin’@’localhost’ IDENTIFIED BY ‘db@pawword’ WITH GRANT OPTION;
- MariaDB [(none)]> FLUSH PRIVILEGES;
- MariaDB [(none)]> exit;
MariaDB is now ready and we can install phpMyAdmin now.
Install phpMyAdmin
To install phpMyAdmin, we need to run the following command.
- sudo apt install phpmyadmin
During the installation, we will be prompted to choose the web server. Since we are using Nginx, we can skip this step by selecting “None” and manually configure it later.
Configure Nginx to Use phpMyAdmin
Once phpMyAdmin is installed, we need to configure Nginx to serve it. First, we need to create a symbolic link from phpMyAdmin’s installation directory to Nginx’s document root.
- sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
We can now create a subdomain for phpMyAdmin and a virtual host to access the phpMyAdmin directory or type http://server_ip/phpmyadmin to access phpmyadmin directory directly. Or we can edit the default Nginx configuration file to handle PHP files
- sudo vim /etc/nginx/sites-available/default
and can add the following block within the server block.
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
Save and close the file, then restart Nginx for the changes to take effect.
- sudo systemctl restart nginx
Now if we type our configured URL or http://server_ip/phpmyadmin we will get the following login page of phpMyAdmin.
Providing our configured username and password, we login to phpMyAdmin web interface and can manage our Databases with phpMyAdmin.
Secure phpMyAdmin (Optional but Recommended)
Securing phpMyAdmin is essential, as it is a common target for attacks. Here are a few steps we can take.
- Change the phpMyAdmin URL: We can modify the location block in the Nginx config to use a more obscure URL, making it harder to guess.
- Use HTTPS: Make ensure that all communications are encrypted using SSL certificates. We can obtain a free SSL certificate from Let’s Encrypt and configure Nginx to use HTTPS.
- Restrict Access: Limit access to phpMyAdmin based on IP addresses or configure a password authentication system.
Nginx and phpMyAdmin together offer a powerful solution for managing both web servers and databases. While Nginx takes care to handle web traffic efficiently, phpMyAdmin shortens database management, making it accessible through a web interface. Proper setup and security measures ensure a smooth and secure server environment. Whether we are hosting a small website or a large-scale application, this combination can help us manage our infrastructure effectively.
Why not a Cup of COFFEE if the solution?