Install phpMyAdmin on Ubuntu 22.04

Posted
Comments 0

Install phpMyAdmin on Ubuntu 22.04

phpMyAdmin is probably the most widely used database management software. With phpMyAdmin you can edit, create, drop, or alter any aspect of a MySQL database. You can also easily import and export your databases. Today, I’ll show you how to install phpMyAdmin on Ubuntu 22.04 and also further secure it using Apache’s built-in security system .htpasswd.

This article is part of my series The Ultimate Web Server.

Contents

Prerequisites

REQUIRED

RECOMMENDED

Install phpMyAdmin

Before we start installing packages, let’s update the package cache:

sudo apt update

phpMyAdmin has certain package dependencies (mbstring, zip, gd, json and curl) so let’s install them with the phpMyAdmin package:

sudo apt install phpmyadmin php-mbstring php-zip php-gd php-json php-curl -y

NOTE: You need to press the SPACE bar to select “apache2” otherwise the installer will not install the necessary files. Hit SPACE, TAB then ENTER.

  • Select Yes when asked whether to use dbconfig-common
  • You will then be asked for a phpMyAdmin password

If you received Error 1819, keep reading. Otherwise skip to the next section: Enabling PHP Modules.

If you followed my guide How to install a LAMP stack on Ubuntu 22.04 and chose to enable the Validate Password Component, you may get error 1819 (HY000) when entering the password for phpMyAdmin. If so, you’ll need to temporarily disable this feature. Select abort and OK, then follow these instructions.

Open MySQL:

sudo mysql -uroot -p

Uninstall the Validate Password Component:

UNINSTALL COMPONENT "file://component_validate_password";

Then exit MySQL:

exit;

Install phpMyAdmin:

sudo apt install phpmyadmin

Once phpMyAdmin has finished installing, enable the Validate Password Component:

INSTALL COMPONENT "file://component_validate_password";

Enabling PHP Modules

We need to enable the mbstring PHP module for phpMyAdmin to work:

sudo phpenmod mbstring

The restart Apache/PHP:

sudo systemctl restart apache2

Now head to your web browser and enter the following URL replacing ricbre.com with your domain name:

https://www.ricbre.com/phpmyadmin

For username enter phpmyadmin then enter the password you used when installing the phpMyAdmin package:

Login to phpMyAdmin

Create a Dedicated phpMyAdmin User (Optional)

Rather than use the default phpMyAdmin user, you can also create a dedicated user. This step is optional.

Login to MySQL:

mysql -uroot -p

Create the new user (replace richie with your preferred username, and password with a strong password):

CREATE USER 'richie'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Grant your new user with all privileges (replace richie with your new username) then flush privileges and exit:

GRANT ALL PRIVILEGES ON *.* TO 'richie'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;

Secure phpMyAdmin (Optional)

phpMyAdmin is one of the top targets for hackers due to it’s popularity, so it’s a good idea to further secure it using Apache’s .htaccess and .htpasswd system. This step is optional, but highly recommended.

Open phpMyAdmin’s .conf file:

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

We need to add AllowOverride All directive under the DirectoryIndex index.php line:

<Directory /usr/share/phpmyadmin>
    Options SymLinksIfOwnerMatch
    DirectoryIndex index.php
    AllowOverride All

Then save ctrl + o, ENTER and exit nano ctrl + x

Restart Apache:

sudo systemctl restart apache2

Next, we create an .htaccess file inside phpMyAdmin’s directory:

sudo nano /usr/share/phpmyadmin/.htaccess

Then copy and past the following code inside the file:

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

Then save ctrl + o, ENTER and exit nano ctrl + x.

Now we create a .htpasswd file to store your password and username (replace username with a new username):

sudo htpasswd -c /etc/phpmyadmin/.htpasswd username

You will need to enter a password for your new user. Apache will then create a hashed password inside the .htpasswd file. Then we restart Apache:

sudo systemctl restart apache2

Now when you visit phpMyAdmin you’ll be initially asked for your username and password, then you will login to phpMyAdmin with your normal password you created earlier.

Login using htpasswd and htaccess Login to phpMyAdmin

Conclusion

I hope everything went smoothly and you now have a secure phpMyAdmin installed to manage all your MySQL databases. If you had any trouble, hit me up in the comments, socials, or email.

See Also

Further Reading

Author
Categories Ubuntu

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.