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 command line client.
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.
To complete this tutorial, you will need the following:
sudo
privileges, as well as a firewall configured with UFW. To set this up, follow our initial server setup guide for Ubuntu 18.04.Finally, there are important security considerations when using software like phpMyAdmin, since it:
For these reasons, and because it is a widely-deployed PHP application which is frequently targeted for attack, you should never run phpMyAdmin on remote systems over a plain HTTP connection. If you do not have an existing domain configured with an SSL/TLS certificate, you can follow this guide on securing Apache with Let’s Encrypt on Ubuntu 18.04. This will require you to register a domain name, create DNS records for your server, and set up an Apache Virtual Host.
Once you are finished with these steps, you’re ready to get started with this guide.
To get started, we will install phpMyAdmin from the default Ubuntu repositories.
First, update your server’s package index:
- sudo apt update
Then use apt
to pull down the files and install them on your system:
- sudo apt install phpmyadmin php-mbstring php-gettext
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. To select Apache, hit SPACE
, TAB
, then ENTER
.
If you do not hit SPACE
to select Apache, the installer will not move the necessary files during installation.
apache2
Yes
when asked whether to use dbconfig-common
to set up the databaseThe 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:
- sudo phpenmod mbstring
Afterwards, restart Apache for your changes to be recognized:
- sudo systemctl restart apache2
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.
When you installed phpMyAdmin onto your server, it automatically created a database user called phpmyadmin
which performs certain underlying processes for the program. Rather than logging in as this user with the administrative password you set during installation, it’s recommended that you log in as either your root MySQL user or as a user dedicated to managing databases through the phpMyAdmin interface.
In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket
plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program — like phpMyAdmin — to access the user.
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:
- SELECT user,authentication_string,plugin,host FROM mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| 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, 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:
- ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
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:
- SELECT user,authentication_string,plugin,host FROM mysql.user;
Output+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| 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 |
+------------------+-------------------------------------------+-----------------------+-----------+
5 rows in set (0.00 sec)
This output indicates 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.
Alternatively, some may find that it better suits their workflow to connect to phpMyAdmin with a dedicated user. To do this, open up the MySQL shell once again:
- 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:
- CREATE USER 'sammy'@'localhost' IDENTIFIED BY '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:
- GRANT ALL PRIVILEGES ON *.* TO 'sammy'@'localhost' WITH GRANT OPTION;
Following that, exit the MySQL shell:
- 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 be taken to the phpMyAdmin user interface:
Now that you’re able to connect and interact with phpMyAdmin, all that’s left to do is harden your system’s security to protect it from attackers.
Because of its ubiquity, phpMyAdmin is a popular target for attackers, and you should take extra care to prevent unauthorized access. One of the easiest ways of doing this is to place a gateway in front of the entire application by using Apache’s built-in .htaccess
authentication and authorization functionalities.
To do this, you must first enable the use of .htaccess
file overrides by editing your Apache configuration file.
Use your favorite text editor to edit the linked file that has been placed in your Apache configuration directory. This example uses nano
:
- sudo nano /etc/apache2/conf-available/phpmyadmin.conf
Add an AllowOverride All
directive within the <Directory /usr/share/phpmyadmin>
section of the configuration file, like this:
<Directory /usr/share/phpmyadmin>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
. . .
When you have added this line, save and close the file. If you used nano
to edit the file, do so by pressing CTRL + X
, Y
, and then ENTER
.
To implement the changes you made, restart Apache:
- sudo systemctl restart apache2
Now that you have enabled .htaccess
use for your application, you need to create an .htaccess
file 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:
- sudo nano /usr/share/phpmyadmin/.htaccess
Within this file, enter the following information:
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user
Here is what each of these lines mean:
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.When you are finished, save and close the file.
The location that you selected for your password file was /etc/phpmyadmin/.htpasswd
. You can now create this file and pass it an initial user with the htpasswd
utility:
- sudo htpasswd -c /etc/phpmyadmin/.htpasswd username
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:
- sudo htpasswd /etc/phpmyadmin/.htpasswd additionaluser
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.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
great tutorial. I need to put one step to access phpmyadmin. I had to create a link in my ubuntu(18.04) :
sudo ln -s /usr/share/phpmyadmin /var/www/html
after that everything works good!
thanks!
Yes you were correct when I completely closed firefox the cookies were flushed and I needed to login again. All is cool. Good tute.
For some reasons the interface was returning a 404 error:(server_ip/phpmyadmin)
Fixed with
Then add the following line to the end of the file:
Then restart apache:
/etc/init.d/apache2 restart
from this forum
I had noticed with ubuntu 18.04 that phpmyadmin v4.6.6deb5 keeps throwing these errors.
Warning in ./libraries/sql.lib.php#613 count(): Parameter must be an array or an object that implements Countable
How to fix that?
These instructions are good until the install phpmyadmin section, which does not work for me. All I get is the following when trying to load the http://###.###.###.###/phpmyadmin page.
Not Found
The requested URL /phpmyadmin was not found on this server.
Solution seems to be to put these 2 lines #—line 1 ServerName 127.0.0.1 #–line 2 Include /etc/phpmyadmin/apache.conf
at the bottom of the /etc/apache2/apache2.conf file, save then restart apache.
Works for me now.
Just a note or request, as the passwords to phpmyadmin are sent without encryption perhaps a quick tute on how to quickly SSL these pages could be included?
Hi I’m having these errors in phpmyadmin console
mysqli_real_connect(): (HY000/1045): Access denied for user ‘phpmyadmin’@‘localhost’ (using password: YES) Connection for controluser as defined in your configuration failed.
And I don’t see a phpmyadmin user when I run mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
What about Nginx , how can I choose nginx instead of apache2 ?
great tutorial - at least for a linux noob like me. nevertheless somethinbg went wrong - and I know what I did wrong. Maybe someone here can help:
In step 1 I didn’t read the warning (below) before hitting the return button.
Warning: When the prompt appears, “apache2” is highlighted, but not selected. To select Apache, hit
SPACE
,TAB
, thenENTER
.If you do not hit
SPACE
to select Apache, the installer will not move the necessary files during installation.So now the files for phpmyadmin are not where they should be. Is there a way to correct this? Simply uninstall and reinstall of phpmyadmin does not work. The selection screen does not appear again.
Thx in advance!
BR, Stefan
I have a proposed amendment to the initial command, ‘sudo apt install phpmyadmin php-mbstring php-gettext’
I was unable to install php-gettext due to it not being found, but was able to resolve by using ‘sudo apt-get install gettext’ instead.
Otherwise, excellent tutorial. Thank You!
doesn’t works with Ubuntu 20