It is usually recommended to use distribution packages when possible - they usually provide integration to your distribution and you will automatically get security updates from your distribution.

You can find a collection of step by step tutorials on how to do that here:

https://www.digitalocean.com/community/tutorial_collections/how-to-install-and-secure-phpmyadmin-with-apache

However in some cases if you have a custom environment or if the distribution packages do not work for any reason, you might want to install phpMyAdmin manually.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

There are a few ways of installing phpMyAdmin as described in the official documentation here:

https://docs.phpmyadmin.net/en/latest/setup.html

However I’ve been using another method which is a bit more convenient for some cases, so I’ve decided to put a short tutorial on how to do that!

Prerequisites

Before you get started with this guide, you need to have some basic steps completed.

  • First, we’ll assume that you are using a non-root user with sudo privileges, as described in steps 1-4 in the initial server setup:

https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-18-04

  • We’re also going to assume that you’ve completed a LAMP (Linux, Apache, MySQL, and PHP) installation on your Ubuntu 16.04 server. If this is not completed yet, you can follow this guide on installing a LAMP stack:

https://digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu-16-04

Step 1 - Install the additional PHP modules

Make sure that you have the additional PHP modules required by phpMyAdmin installed:

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

Also, 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

Step 2 - Download the source files

You can visit the following link and copy the link of the latest stable phpMyAdmin version:

https://www.phpmyadmin.net/files/

As of the time of writing this post, the latest phpMyAdmin was 5.0.2. So in order to download the files, you need to run the following commands:

  • First go to the /tmp directory:
  • cd /tmp
  • Then download the zip file:
  • wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.zip
  • Unzip the archive

Note: if you don’t have upzip already installed you can install it with:

  • sudo apt install unzip

After that unzip the archive:

  • unzip phpMyAdmin-5.0.2-all-languages.zip
  • Rename the folder
  • mv phpMyAdmin-5.0.2-all-languages phpmyadmin
  • Move the phpMyAdmin folder to your document root:
  • sudo mv phpmyadmin /var/www/html

Note: change the /var/www/html path in case that you are using a custom virtual host with a non-standard document root

Step 3 - Configure phpMyadmin

First you need to go to the /var/www/html/phpmyadmin directory and then update your phpMyAdmin config:

  • cd /var/www/html/phpmyadmin

Then with your favorite text editor open the config.sample.inc.php file:

  • sudo nano config.sample.inc.php

And then update the following line to the correct database host, as we are running MySQL on the same Droplet just put localhost:

  • $cfg['Servers'][$i]['host'] = localhost

Finally, rename the config:

  • sudo mv config.sample.inc.php config.inc.php

Finally, in order to log in to phpMyAdmin as your root MySQL user, you will need to switch its authentication method from authsocket to mysqlnative_password if you haven’t already done so. To do this, open up the MySQL prompt from your terminal:

  • sudo mysql

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;

After that you should be able to visit http://yourdomainor_IP/phpmyadmin and login with your root user.

Alternatively, some may find that it better suits their workflow to connect to phpMyAdmin with a dedicated MySQL user rather than the root user. To do this, open up the MySQL shell once again:

  • sudo mysql

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;

After that, you would be able to use the new user to login via phpMyAdmin and manage your databases.

Step 4 - Securing your phpMyAdmin Instance

It is strongly recommended to configure .htaccess authentication in order to protect your phpMyAdmin instance from brute-force attacks and just as a second layer of security.

Edit the linked file that has been placed in your Apache configuration directory:

  • sudo nano /etc/apache2/sites-available/000-default.conf

Add an AllowOverride All directive within the <Directory /var/www/html/phpmyadmin> section of the configuration file, like this:

<Directory /var/www/html/phpmyadmin>
    Options FollowSymLinks
    DirectoryIndex index.php
    AllowOverride All
</Directory>
    . . .

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

  • sudo nano /var/www/html/phpmyadmin/.htaccess

Within this file, enter the following information:

AuthType Basic
AuthName "Restricted Files"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

You can now create this file and pass it an initial user with the htpasswd utility:

  • sudo htpasswd -c /etc/apache2/.htpasswd username

Now, when you access your phpMyAdmin subdirectory, you will be prompted for the additional account name and password that you just configured.

Conclusion

You have successfully manually installed phpMyAdmin and you should now have phpMyAdmin configured and ready to use on your Ubuntu server.

Hope that this helps!
Regards,
Bobby

by Brennen Bearnes
A "LAMP" stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is...
Submit an Answer