phpMyAdmin was created to enable users interact with MySQL through a web interface. This tutorial will discuss how to install and secure phpMyAdmin so that you can safely use it to manage your databases on Ubuntu 14.04.
Ensure you are using a non-root user with sudo privileges and that you’ve completed a LAMP (Linux, Apache, MySQL, and PHP) installation on your Ubuntu 14.04 server.
Step one — Install phpMyAdmin
Install phpMyAdmin from the default Ubuntu repositories. We first update our local package index and then using the apt packaging system to download the files and install them on our system.
sudo apt-get update
sudo apt-get install phpmyadmin
You will be asked some questions in order to configure your installation correctly.
- For the server selection, choose apache2. Note: 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.
- Select yes when asked whether to use dbconfig-common to set up the database.
- You will be prompted for your database administrator’s password.
- You will then be asked to choose and confirm a password for the phpMyAdmin application itself
The installation process adds the phpMyAdmin Apache configuration file into the /etc/apache2/conf-enabled/ directory, where it is automatically read.
The next thing you have to do is enable the php5-mcrypt extension, use the following command:
sudo php5enmod mcrypt
Then restart Apache for changes to be recognized:
sudo service apache2 restart
Access the web interface by visiting your server’s domain name or public IP address followed by /phpmyadmin:
Log into the interface using the root username and the administrative password you set up during the MySQL installation. After logging in, you will see the user interface.
Step Two — Secure your phpMyAdmin Instance
The next thing is to secure the application to help prevent unauthorized use , this is because phpMyAdmin is a popular target for attackers. We are going to do this is placing a gateway in front of the entire application. This can be done using the Apache’s built-in .htaccess authentication and authorization functionalities.
Configure Apache to Allow .htaccess Overrides
First enable the use of .htaccess file overrides by editing our Apache configuration file.
Next is edit the linked file that has been placed in the Apache configuration directory:
sudo nano /etc/apache2/conf-available/phpmyadmin.conf
We have to add an AllowOverride All directive in the <Directory /usr/share/phpmyadmin> section of the configuration file, like this:
. . .
After adding this file, save and close the file.
Restart Apache to implement the changes made:
sudo service apache2 restart
Create an .htaccess File
After enabling .htaccess use for our application, we now have to create an actual one to implement some security. For success, the file must be created within the application directory. Create the necessary file and open it in the text editor with root privileges by typing:
sudo nano /usr/share/phpmyadmin/.htaccess
Enter the following information in the file:
AuthName “Restricted Files”
These lines each has a meaning, let’s have a look:
- AuthType Basic: This line specifies the authentication type that we are implementing. This type will implement password authentication using a password file.
- AuthName: This sets the message for the authentication dialog box. 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 soon.
- 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 finished, save and close the file.
Create the .htpasswd file for Authentication
After specifying a location for our password file through the use of the AuthUserFile directive within the .htaccesss file, we now have to create the file.
We have to install it from the repositories:
sudo apt-get install apache2-utils
Then, we will have the htpasswd utility available.
Selected location for the password file is “/etc/phpmyadmin/.htpasswd”. Create this file and pass it an initial user by typing:
sudo htpasswd -c /etc/phpmyadmin/.htpasswd username
You will be prompted to select and confirm a password for the user you are creating. Then, the file is created with the hashed password that you entered. For adding an additional user, you need to do so without the -c flag, like this:
sudo htpasswd /etc/phpmyadmin/.htpasswd additionaluser
When you access your phpMyAdmin subdirectory, you will be prompted for the additional account name and password that you just configured:
After entering the Apache authentication, you’ll be taken to the regular phpMyAdmin authentication page to enter your other credentials. This will add an additional layer of security since phpMyAdmin has suffered from vulnerabilities in the past.
You should now have phpMyAdmin configured and ready to use on your Ubuntu 14.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.