PhpMyAdmin
------
phpMyAdmin on Ubuntu 18.04
sudo apt-get install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php
Once the repository is added, install phpMyAdmin by running the following command:
sudo apt-get install phpmyadmin php-mbstring php-gettext -y
sudo phpenmod mbstring
sudo systemctl restart apache2
sudo mysql
Next, check the authentication method of MySQL user with the following command:
mysql> 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 | *AC18DF5CAE34BF4796EF975702F038A566B48B42 | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.01 sec)
In the above output, you should see that root user uses auth_socket plugin.
You can configure the root account to authenticate with a password by running the following command:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxxxxxx';
Next, flush the privileges with the following command:
mysql> FLUSH PRIVILEGES;
Now, check the authentication methods by running the following command:
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
Output:
+------------------+-------------------------------------------+-----------------------+-----------+ | user | authentication_string | plugin | host | +------------------+-------------------------------------------+-----------------------+-----------+ | root | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | mysql_native_password | localhost | | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost | | debian-sys-maint | *AC18DF5CAE34BF4796EF975702F038A566B48B42 | mysql_native_password | localhost | +------------------+-------------------------------------------+-----------------------+-----------+ 4 rows in set (0.01 sec)
Next, create a separate user with name phpmyadminuser to connect to phpMyAdmin.
First, log in to MySQL shell:
mysql -u root -p
Enter your root password, then create a user with the following command:
MariaDB [(none)]> CREATE USER 'phpmyadminuser'@'localhost' IDENTIFIED BY 'xxxxxx';
Next, grant appropriate privileges to phpmyadminuser with the following command:
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'phpmyadminuser'@'localhost' WITH GRANT OPTION;
Finally, exit from the MySQL shell with the following command:
MariaDB [(none)]> exit;
Secure PhpMyAdmin-Passwd
phpMyAdmin is now installed and configured. But, it is recommended to secure your phpMyAdmin instance to prevent unauthorized access You can secure phpMyAdmin by using Apache's built-in .htaccess authentication and authorization functionalities.
To do so, first enable the use of .htaccess file overrides by editing phpmyadmin.conf file:
sudo vim /etc/apache2/conf-available/phpmyadmin.conf
Make the following changes:
<Directory /usr/share/phpmyadmin> Options SymLinksIfOwnerMatch DirectoryIndex index.php AllowOverride All
Save and close the file. Then, restart Apache service to apply the chnages:
sudo systemctl restart apache2
Next, create a .htaccess file inside phpmyadmin directory:
sudo vim /usr/share/phpmyadmin/.htaccess
Add the following lines:
AuthType Basic AuthName "Restricted Files" AuthUserFile /etc/phpmyadmin/.htpasswd Require valid-user
Save and close the file. Then, create a user named admin with the htpasswd utility:
sudo htpasswd -c /etc/phpmyadmin/.htpasswd admin
Output:
New password: Re-type new password: Adding password for user admin
Your phpMyAdmin instance is now secured with an additional layer of authentication.
Now, open your web browser and type the URL http://your-server-ip/phpmyadmin.
Know your IP, you can find out with
sudo ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
phpMyAdmin URL
# sudo vim /etc/apache2/conf-available/phpmyadmin.conf
Alias /phpadmin /usr/share/phpmyadmin
sudo systemctl restart apache2
--------------------------------X------------------------------------------
apt-get update -y
apt-get upgrade -y
apt-get install apache2 mariadb-server apt-transport-https libapache2-mod-php7.2 php7.2-cli php7.2-fpm php7.2-json php7.2-mysql php7.2-zip php7.2-gd php7.2-mbstring php7.2-curl php7.2-xml php-pear php7.2-bcmath php7.2-intl php7.2-opcache mariadb-server php7.2-xml postfix wget unzip -y
Once all the packages are installed, open php.ini file and make some changes:
php -v
vim /etc/php/7.2/apache2/php.ini
Make the following changes:
upload_max_filesize = 1G post_max_size = 1G output_buffering = Off Save and close the file. Then, open another php.ini file:
vim /etc/php/7.2/cli/php.ini
Make the following changes:
upload_max_filesize = 1G post_max_size = 1G output_buffering = Off
Save and close the file, when you are finished.
Configure MariaDB
By default, MariaDB installation is not secured, so you will need to secure it first. You can secure it with the following command:
mysql_secure_installation
Enter current password for root (enter for none): Set root password? [Y/n]: N Remove anonymous users? [Y/n]: Y Disallow root login remotely? [Y/n]: Y Remove test database and access to it? [Y/n]: Y Reload privilege tables now? [Y/n]: Y
Once MariaDB is secured, log in to MariaDB shell with the following command:
mysql -u root -p
sudo apt-get update sudo apt-get install apache2 php mysql-server
Step 2 – Install phpMyAdmin
After installing LAMP stack on your system, let’s install phpMyAdmin using the command in your Ubuntu system.
sudo apt-get install phpmyadmin
During installation it will prompt for selecting web server, you have installed on your system. Select appropriate web server you used.
Step 3 – Configure Apache2 for phpMyAdmin
The installer creates and an Apache configuration file /etc/phpmyadmin/apache.conf for phpMyAdmin. Also, create a soft link to be used by Apache. Make sure a soft link is created at /etc/apache2/conf-available/phpmyadmin.conf. In case a soft link is not created and enable phpMyAdmin.
sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf sudo a2enconf phpmyadmin
After updating the above entry, restart Apache2 service using the following command
sudo service apache2 restart
Step 4 – Access phpMyAdmin
You phpMyAdmin installation has been completed successfully. Open you favourite web browser and access following url to open phpMyAdmin, change the localhost with servers hostname or ip address.
http://localhost/phpmyadmin/
Secure phpMyAdmin
Since phpMyAdmin is an easy target for attackers, the next recommended task is to secure phpyMyAdmin. The most common way to protect phpMyAdmin from unauthorized access is by using Apache’s built-in .htaccess authentication and authorization functionalities.
First of all, enable the use of .htaccess file overrides by editing the /phpmyadmin.conf file:
$ sudo nano /etc/apache2/conf-available/phpmyadmin.conf
Add “AllowOverride All” line within the <Directory /usr/share/phpmyadmin> section as shown below.
<Directory /usr/share/phpmyadmin> Options SymLinksIfOwnerMatch DirectoryIndex index.php AllowOverride All
$ sudo systemctl restart apache2
Next, create a file named .htaccess within /usr/share/phpmyadmin/ directory using command:
$ sudo nano /usr/share/phpmyadmin/.htaccess
Add the following lines in it:
AuthType Basic AuthName "Restricted Files" AuthUserFile /etc/phpmyadmin/.htpasswd Require valid-user
Save and close the file.
Now, run the following command to create a new user, for example ostechnix:
$ sudo htpasswd -c /etc/phpmyadmin/.htpasswd ostechnix
Replace ‘ostechnix’ with any username of your choice. You will asked to provide a password for the new user. Enter it twice.
Sample output:
New password: Re-type new password: Adding password for user ostechnix
---------------
Securing Your phpMyAdmin Instance
Because of its ubiquity, phpMyAdmin is a popular target for attackers, and you should take extra care to prevent unauthorized access. One of the easiest ways of doing this is to place a gateway in front of the entire application by using Apache's built-in .htaccess authentication and authorization functionalities.
To do this, 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 <Directory /usr/share/phpmyadmin> section of the configuration file, like this:
/etc/apache2/conf-available/phpmyadmin.conf
<Directory /usr/share/phpmyadmin> 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 use for your application, you need to create one to actually implement some security.
In order for this to be successful, the file must be created within the application directory. You can create 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 mean:
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. You should 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 shortly.
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 you are finished, save and close the file.
The location that you selected for your password file was /etc/phpmyadmin/.htpasswd. You can now create this file and pass it 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. Afterwards, the file is created 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
---