How To Install and Secure phpMyAdmin On Ubuntu 18
How To Install and Secure phpMyAdmin On Ubuntu 18
How To Install and Secure phpMyAdmin On Ubuntu 18
on Ubuntu 18.04
Introduction
While many users need the functionality of a database management system like
MySQL, they may not feel comfortable interacting with the system solely from the
MySQL prompt.
phpMyAdmin was created so that users can interact with MySQL through a web
interface. In this guide, we’ll discuss how to install and secure phpMyAdmin so that you
can safely use it to manage your databases on an Ubuntu 18.04 system.
Prerequisites
Before you get started with this guide, you need to have some basic steps completed.
First, we’ll assume that your server has a non-root user with sudo privileges, as well as
a firewall configured with ufw, as described in the initial server setup guide for Ubuntu
18.04.
We’re also going to assume that you’ve completed a LAMP (Linux, Apache, MySQL,
and PHP) installation on your Ubuntu 18.04 server. If this is not completed yet, you can
follow this guide on installing a LAMP stack on Ubuntu 18.04.
Finally, there are important security considerations when using software like
phpMyAdmin, since it:
Once you are finished with these steps, you’re ready to get started with this guide.
This is done by updating your server’s package index and then using the apt packaging
system to pull down the files and install them on your system:
This will ask you a few questions in order to configure your installation correctly.
Warning: When the prompt appears, “apache2” is highlighted, but not selected. If you do
not hit SPACE to select Apache, the installer will not move the necessary files during
installation. Hit SPACE, TAB, and then ENTER to select Apache.
The installation process adds the phpMyAdmin Apache configuration file into the
/etc/apache2/conf-enabled/ directory, where it is read automatically. The only
thing you need to do is explicitly enable the mbstring PHP extension, which you can
do by typing:
phpMyAdmin is now installed and configured. However, before you can log in and begin
interacting with your MySQL databases, you will need to ensure that your MySQL users
have the privileges required for interacting with the program.
In order to log in to phpMyAdmin as your root MySQL user, you will need to switch its
authentication method from auth_socket to mysql_native_password if you haven’t
already done so. To do this, open up the MySQL prompt from your terminal:
· sudo mysql
Next, check which authentication method each of your MySQL user accounts use with
the following command:
Output
+------------------+-------------------------------------------+----------
-------------+-----------+
+------------------+-------------------------------------------+----------
-------------+-----------+
| root | |
auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
mysql_native_password | localhost |
| debian-sys-maint | *8486437DE5F65ADC4A4B001CA591363B64746D4C |
mysql_native_password | localhost |
| phpmyadmin | *5FD2B7524254B7F81B32873B1EA6D681503A5CA9 |
mysql_native_password | localhost |
+------------------+-------------------------------------------+----------
-------------+-----------+
5 rows in set (0.00 sec)
In this example, you can see that the root user does in fact authenticate using the
auth_socket plugin. To configure the root account to authenticate with a password,
run the following ALTER USER command. Be sure to change password to a strong
password of your choosing:
Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put
your new changes into effect:
· FLUSH PRIVILEGES;
Check the authentication methods employed by each of your users again to confirm that
root no longer authenticates using the auth_socket plugin:
Output
+------------------+-------------------------------------------+----------
-------------+-----------+
+------------------+-------------------------------------------+----------
-------------+-----------+
| root | *DE06E242B88EFB1FE4B5083587C260BACB2A6158 |
mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
mysql_native_password | localhost |
| debian-sys-maint | *8486437DE5F65ADC4A4B001CA591363B64746D4C |
mysql_native_password | localhost |
| phpmyadmin | *5FD2B7524254B7F81B32873B1EA6D681503A5CA9 |
mysql_native_password | localhost |
+------------------+-------------------------------------------+----------
-------------+-----------+
You can see from this output that the root user will authenticate using a password. You
can now log in to the phpMyAdmin interface as your root user with the password you’ve
set for it here.
· sudo mysql
Note: If you have password authentication enabled, as described in the previous section,
you will need to use a different command to access the MySQL shell. The following will run
your MySQL client with regular user privileges, and you will only gain administrator
privileges within the database by authenticating:
· mysql -u root -p
·
From there, create a new user and give it a strong password:
Then, grant your new user appropriate privileges. For example, you could grant the user
privileges to all tables within the database, as well as the power to add, change, and
remove user privileges, with this command:
· exit
You can now access the web interface by visiting your server’s domain name or public
IP address followed by /phpmyadmin:
http://your_domain_or_IP/phpmyadmin
Log in to the interface, either as root or with the new username and password you just
configured.
When you log in, you’ll see the user interface, which will look something like this:
Now that you’re able to connect and interact with phpMyAdmin, all that’s left to do is
harden your systems security to protect it from attackers.
To do this, you must first enable the use of .htaccess file overrides by editing your
Apache configuration file.
Edit the linked file that has been placed in your Apache configuration directory:
/etc/apache2/conf-available/phpmyadmin.conf
<Directory /usr/share/phpmyadmin>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
. . .
When you have added this line, save and close the file.
Now that you have enabled .htaccess use for your application, you need to create one
to actually implement some security.
In order for this to be successful, the file must be created within the application
directory. You can create the necessary file and open it in your text editor with root
privileges by typing:
/usr/share/phpmyadmin/.htaccess
AuthType Basic
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user
● AuthType Basic: This line specifies the authentication type that you are
implementing. This type will implement password authentication using a
password file.
● AuthName: This sets the message for the authentication dialog box. You should
keep this generic so that unauthorized users won’t gain any information about
what is being protected.
● AuthUserFile: This sets the location of the password file that will be used for
authentication. This should be outside of the directories that are being served.
We will create this file shortly.
● Require valid-user: This specifies that only authenticated users should be
given access to this resource. This is what actually stops unauthorized users
from entering.
You will be prompted to select and confirm a password for the user you are creating.
Afterwards, the file is created with the hashed password that you entered.
If you want to enter an additional user, you need to do so without the -c flag, like this:
Now, when you access your phpMyAdmin subdirectory, you will be prompted for the
additional account name and password that you just configured:
https://domain_name_or_IP/phpmyadmin
After entering the Apache authentication, you’ll be taken to the regular phpMyAdmin
authentication page to enter your MySQL credentials. This setup adds an additional
layer of security, which is desireable since phpMyAdmin has suffered from
vulnerabilities in the past.
Conclusion
You should now have phpMyAdmin configured and ready to use on your Ubuntu 18.04
server. Using this interface, you can easily create databases, users, tables, etc., and
perform the usual operations like deleting and modifying structures and data.