Search
Close this search box.

How To Install and Secure phpMyAdmin on Ubuntu 18.04

Table of Contents

How To Install and Secure phpMyAdmin on Ubuntu 18.04

Introduction

phpMyAdmin is a free and open-source administration tool for MySQL and MariaDB. As a portable web application written primarily in PHP, it has become one of the most popular MySQL administration tools, especially for web hosting services.

This article will explain how to install phpMyAdmin in Ubuntu 18.04.

Step 1 — Installing phpMyAdmin

To get started, we will install phpMyAdmin from the default Ubuntu repositories.

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:

sudo apt update
sudo apt install phpmyadmin php-mbstring php-gettext

This will ask you a few questions 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.

For the server selection, choose apache2

Select Yes when asked whether to use dbconfig-common to set up the database
You will then be asked to choose and confirm a MySQL application password for phpMyAdmin
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:

sudo phpenmod mbstring

Afterward, 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 must ensure that your users have the privileges required for interacting with the program.

Step 2 — Adjusting User Authentication and Privileges

Installing phpMyAdmin onto your server automatically creates a database user called phpmyadmin, which performs specific 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 greater security and usability in many cases. Still, it can also complicate things when you need to allow an external program — like phpMyAdmin — to access the user.

To log in to phpMyAdmin as your root MySQL user, you must switch its authentication method from auth_socket to mysql_native_password if you haven’t already done it 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 uses 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, you can see that the root user authentically uses the auth_socket plugin. Run the following ALTER USER command to configure the root account to show with a password. Be sure to change the 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)

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 here.

Configuring Password Access for a Dedicated MySQL User
Alternatively, some may find 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 password authentication is enabled, as described in the previous section, you must 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 'user1'@'localhost' IDENTIFIED BY 'password';

Then, grant your new user appropriate privileges. For example, you could present 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 'user1'@'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 as root or with the new username and password you just configured.

When you log in, you’ll see the user interface. Now that you can connect and interact with phpMyAdmin, all that’s left to do is harden your system’s security to protect it from attackers.

Step 3 — Securing Your phpMyAdmin Instance

To secure your phpMyAdmin instance from attackers, 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:

sudo nano /etc/apache2/conf-available/phpmyadmin.conf

Add an AllowOverride All directive within the section of the configuration file like this:

/etc/apache2/conf-available/phpmyadmin.conf

Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
. . .

When you have added this line, save and close the file.

To implement the changes you made, restart Apache:

sudo systemctl restart apache2

Now that you have enabled .htaccess for your application, you need to create one to implement some security.

For this to be successful, the file must be created within the application directory. You can make 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:

/usr/share/phpmyadmin/.htaccess
AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/phpmyadmin/.htpasswd
Require valid-user

Here is what each of these lines means:

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. It would be best to keep this generic so 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 stops unauthorized users from entering.
When you are finished, save and close the file.

The location you selected for your password file was /etc/phpmyadmin/.htpasswd. You can now create this file and pass it to 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. Afterward, the file is made 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 a layer of security, which is desirable since phpMyAdmin has previously suffered from vulnerabilities.

Tips and tricks

In this tutorial, you have successfully learned how to install and secure phpMyAdmin in Ubuntu 18.04. If you want to learn more about MySQL, checkout these documents:
https://www.phpmyadmin.net/docs/

If you want a server with Ubuntu 18.04 operating system, get started now with VPSie and get one month for free.

This tutorial will help you based on this original tutorial https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04; please use our tutorial for notes and tips.

fedora-epel-yum-repository-in-your-centos

FAQ

phpMyAdmin is a free and open-source web-based tool that provides a graphical interface for managing MySQL and MariaDB databases.

Installing phpMyAdmin can simplify the process of managing MySQL or MariaDB databases, especially for those who are not comfortable with using the command line.

You can install phpMyAdmin on Ubuntu by following these steps:

  1. Open the Terminal application on your Ubuntu system.
  2. Update the package index by running the command: sudo apt-get update
  3. Install phpMyAdmin by running the command: sudo apt-get install phpmyadmin
  4. During the installation process, you will be prompted to choose a web server to configure. Select “Apache2” and press “Enter”.
  5. You will also be prompted to create a password for the phpMyAdmin administrative account. Choose a strong password and remember it.

Once phpMyAdmin is installed, you can access it by opening a web browser and entering the URL: http://localhost/phpmyadmin. If you are accessing phpMyAdmin from a remote system, replace “localhost” with the IP address or hostname of your Ubuntu system.

To secure phpMyAdmin, follow these steps:

  1. Open the configuration file at /etc/phpmyadmin/config.inc.php.
  2. Find the line that begins with “$cfg[‘blowfish_secret’]” and generate a new blowfish secret by running the command: openssl rand -base64 32.
  3. Replace the existing blowfish secret with the new one in the configuration file.
  4. Add the following lines to the end of the configuration file:
bash
$cfg['Servers'][$i]['auth_type'] = 'cookie'; $cfg['Servers'][$i]['AllowNoPassword'] = false; $cfg['Servers'][$i]['controluser'] = 'phpmyadmin'; $cfg['Servers'][$i]['controlpass'] = 'your_control_password';
  1. Replace “your_control_password” with a strong password of your choice.

To configure phpMyAdmin to use HTTPS, follow these steps:

  1. Install an SSL certificate on your web server.
  2. Edit the Apache configuration file at /etc/apache2/sites-enabled/000-default.conf and add the following lines inside the <VirtualHost> block:
perl
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  1. Restart Apache by running the command: sudo service apache2 restart.

You can uninstall phpMyAdmin from your Ubuntu system by running the command: sudo apt-get remove phpmyadmin.

Make a Comment
Share on
Facebook
Twitter
LinkedIn
Print
VPSie Cloud service

Fast and Secure Cloud VPS Service

Try FREE
For a month

The First 1 orders gets free discount today! Try Sign up on VPSie to get a chance to get the discount.