• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SMASHING LAB

GUIDES | LINUX | GEEKY STUFF

  • Nginx
  • Linux
  • Guide
  • Ubuntu
  • WordPress
  • Drones, My Hobby

How to Install and Secure phpMyAdmin with Apache on Ubunut 18.04

By Imran Yousaf

phpMyAdmin is an open-source PHP application designed to handle MySQL and MariaDB server administration through a web-based interface.

phpMyAdmin lets you manage databases, user accounts, and MySQL privileges, execute SQL statements, import and export data in various data formats and much more through a web-based interface that is much easier than through the command line.

This tutorial covers the steps needed to install and secure phpMyAdmin with Apache on Ubuntu 18.04.

Requirements

Make sure you have fulfilled the following prerequisites before continuing with this tutorial:

  • Have LAMP (Linux, Apache, MySQL, and PHP) already installed on your Ubuntu server.
  • Log in as a user with sudo privileges. Read → How to Create a Sudo User and Sudo Group on Ubuntu

Although it is not necessary, it is recommended to access your phpMyAdmin installation via HTTPS. If your domain is not protected or secured with SSL, you can follow this guide and secure your Apache with Let’s Encrypt on Ubuntu 18.04.

How to Install phpMyAdmin on Ubuntu

To install phpMyAdmin on your Ubuntu 18.04 server, follow these steps:

Update package indexes and upgrade system packages to the latest versions:

$ sudo apt update && sudo apt upgrade

Install the phpMyAdmin package from the default Ubuntu repository with the following command:

$ sudo apt install phpmyadmin

The installation will ask you to choose a web server that must be configured automatically to run phpMyAdmin, select apache by pressing the Space key and then Enter.

 

Next, you will be asked whether to use dbconfig-commonto manage the database, select Yes and press Enter.

Enter the password for phpMyAdmin, this is used to register with the database, select OK and press Enter.

You will be asked to confirm the password, enter the same password, select OK and press Enter .

After the installation process is complete, restart Apache so that the changes you make are applied:

$ sudo systemctl restart apache2

Create a MySQL Administrative User

In Ubuntu systems running MySQL 5.7 (and later), root users are set to use the authentication method auth_socketby default.

The plugin auth_socketauthenticates users who are connected from localhost via a Unix socket file. This means you cannot authenticate as root by providing a password.

Instead of changing the authentication method for the MySQL user root, I will create a new administrative MySQL user. This user will have the same rights as the root user and will be set to use the authentication method mysql_native_password.

I will use this user to log in to the phpMyAdmin dashboard and perform previous administrative tasks on the MySQL or MariaDB server.

Start by logging in to the MySQL server as the root user:

sudo mysql

From within the MySQL shell, run the following command which will create a new administrative user and give the appropriate permissions:

1
2
mysql> CREATE USER 'padmin'@'localhost' IDENTIFIED BY 'super-strong-password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'padmin'@'localhost' WITH GRANT OPTION;

In my example, I named the administrative user with padmin. You can use whatever username you like, making sure to set a strong password.

Access phpMyAdmin

To access the phpMyAdmin interface open your favorite browser and type your server’s domain name or public IP address and followed by the url /phpmyadminbehind it:

https://domain_or_ip_address/phpmyadmin

Log in with the administrative user login credentials that you created earlier and click Go .

After logging in, you will see the phpMyAdmin dashboard, which will look like this:

Securing phpMyAdmin

To add an additional layer of security, I will protect the phpMyAdmin directory password or password by making basic authentication.

First we will create a password file with the user using the tool htpasswdthat came with the Apache package. I will save the file .htpasswdin the directory /etc/phpmyadmin:

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

In this example, I created a user or named user padmin. You can choose any username, it doesn’t have to be the same as the user name for administrative MySQL.

The above command will ask you to enter and confirm the user’s password.

1
2
3
4
Output
New password:
Re-type new password:
Adding password for user padmin

If you want to add additional users, you can use the same command without the sign -c:

$ sudo htpasswd /etc/phpmyadmin/.htpasswd padmin2

The next step is to configure Apache so that the password protects the phpMyAdmin directory and uses the file .htpasswd.

To do this, open the file phpmyadmin.confthat was automatically created during the phpMyAdmin installation:

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

Please edit/enter the following lines:

1
2
3
4
5
6
7
8
9
10
11
    Options  +FollowSymLinks +Multiviews +Indexes  # edit this line
    DirectoryIndex index.php
 
    AllowOverride None
    AuthType basic
    AuthName "Authentication Required"
    AuthUserFile /etc/phpmyadmin/.htpasswd
    Require valid-user
 
    
    ...

Save and Close file and Restart Apache so that the changes you make immediately apply, type the following command:

$ sudo systemctl restart apache2

Now, when accessing your phpMyAdmin, you will be asked to enter the user login credentials that you previously created:

https://domain_or_ip_address/phpmyadmin

After entering basic authentication, you will be taken to the phpMyAdmin login page where you must enter your MySQL administrative user login credentials.

It’s also a good idea to change aliases /phpmyadminto something more unique and secure.

Conclusion

Congratulations, you have successfully installed phpMyAdmin and secured phpMyAdmin with Apache on your Ubuntu 18.04 server. You can now start creating MySQL databases, user and database tables and perform various MySQL requests and operations via the web interface of phpMyAdmin.

Some more articles you might also be interested in …

  • Guide How to Install Apache in Ubuntu 18.04
  • Install and Configure Apache on Ubuntu Server
  • How to Install LAMP Stack (Linux, Apache, MySQL,…
  • Compress the Apache Server Output with GZIP
  • How to install LAMP on CentOS 8
  • Install Docker in CentOS 7
  • How to install Nextcloud on CentOS 8
  • How to install Zabbix on openSUSE Leap 15.1
  • How to Install Brackets on Debian / Ubuntu
  • How to Install LAMP on openSUSE Leap 15.1
Tweet
Pin
Share
0 Shares

Filed Under: Guide, Ubuntu

Primary Sidebar

  • Twitter
  • Facebook
  • Google+

Recent Posts

  • How to install Nextcloud on CentOS 8
  • How to install Zabbix on openSUSE Leap 15.1
  • How to Easily Configure Your Domain’s Email Account with Gmail?
  • Tails 4.4 OS Released with Tor Browser 9.0.6
  • How to Convert JPG to PDF in Linux

Subscribe to our Newsletter

Useful articles, tips and videos about creating and promoting websites to your mail



* required field


  • Contact Us
  • Privacy Policy
  • About Us

Copyright © 2021. Smashing Lab