[TUTORIAL] Install & Configure Apache PHP MySQL PhpMyAdmin on Linux Mint 21

Write tutorials for Linux Mint here
More tutorials on https://github.com/orgs/linuxmint/discu ... /tutorials and (archive) on https://community.linuxmint.com/tutorial
Forum rules
Don't add support questions to tutorials; start your own topic in the appropriate sub-forum instead. Before you post read forum rules
Post Reply
mudassir
Level 3
Level 3
Posts: 118
Joined: Tue Dec 12, 2023 2:28 am

[TUTORIAL] Install & Configure Apache PHP MySQL PhpMyAdmin on Linux Mint 21

Post by mudassir »

Introduction:
This tutorial is written to assist absolute beginners (to Linux Mint) to install and configure Apache, PHP, MySQL server and PhpMyAdmin on Linux Mint 21. No prior knowledge of Linux Command Line is required. After looking at many different resources online, I feel that most of them are either out of date or not exactly related to LM. Also installation of PhpMyAdmin is excluded in most of the illustrations. So, I decided to write this tutorial to help others and enable them to use LM for coding php websites. Happy coding!

Note: We're going to install most packages using terminal. Terminal can be opened by the keyboard shortcut Ctrl + Alt + T or by accessing the shortcut from Menu.


* I request the Moderators and experienced forum members to please highlight any correction. Thank you.


What's in this tutorial
- How to Install Apache Web Server on Linux Mint
-- How to write to Apache Document Root /var/www (solution)
- How to Install PHP on Linux Mint
-- How to display php errors in Browser (solution)
- How to Install MySQL Server on Linux Mint
- How to Install PhpMyAdmin on Linux Mint
- How to Login to PhpMyAdmin (solution)
- How to change PhpMyAdmin Password (solution)
- Can't perform queries or operations in PhpMyAdmin (solution)
- How to solve incompatible with sql_mode=only_full_group_by error (solution)

Version Information:
The tutorial is written for Linux Mint version 21 and above. LM repositories support the following versions of concerned packages at the time of writing this tutorial:

- Apache ver 2.4.52
- PHP ver 8.1.2
- MySQL server ver 8.0.35
- PhpMyAdmin ver 5.1.1


Let's start:

First thing first... open the terminal first and run the following command:
[Hint:] copy / paste the commands to avoid typo (use shortcut Ctrl + Shift + V to paste)
[Hint:] keep your internet connection open during entire process. All packages will be downloaded to your system from Linux Mint repositories prior to installation.

Code: Select all

sudo apt-get update
What does it do: apt-get update command downloads the list of packages available in LM repositories and update them.


How to Install Apache 2 Web Server:

Run the following command to install Apache 2 Web Server:

Code: Select all

sudo apt install apache2
The system will display details of package(s) to be downloaded and their size. And asks if you want to proceed. Press Y.
Once the installation is done, you may check the version of installed package using apache2 -v command in terminal. Or open http://localhost in your browser.

Done? Not yet exactly, the default DocumentRoot directory of apache2 is /var/www/. The DocumentRoot is the folder where we store our php code / project files. The directory should be owned by root and you most probably not have permissions to write / save your php code files. Changing owner and permission of this directory is our last step setting up Apache for now.

Run these two commands (one by one) in the terminal to enable write permission and change ownership of the directory:

Code: Select all

sudo chmod -R 755 /var/www
sudo chown -R $USER:$USER /var/www

How to Install PHP:

Installing PHP is pretty straight forward. Run the following command:

Code: Select all

sudo apt install php
The system will show the package information and size, prompt you if you want to proceed. Press Y and wait for the installation process to complete.

To know the version of installed package, run [php -v] command from terminal.

One little tweak: I would suggest one little tweak before we complete the PHP section here. PHP by default does not show errors in the browser when we run some php file from localhost. Rather, the execution of code stops at line containing some error. This is because display_errors is set to Off in php configuration (php.ini) file. I (personally) prefer to display code errors in the browser to make it easier to trace. Maybe someone don't. So, this step can be optional depending on your preference.

Here is how we can edit this setting in our php.ini file:

Code: Select all

sudo nano /etc/php/8.1/apache2/php.ini
Every line starting with a ; is comment in php configuration file. Look for display_error = Off in the file, replace Off with On press Ctrl+X to exit and press Y to save changes.

Note:PHP configuration file is located in /etc/php/8.1/apache2 folder at the time of writing this tutorial. One may have to search for php.ini file in:
/etc >>php/ >> <php version> >> apache2 folder.

Restart apache service using this command:

Code: Select all

sudo service apache2 restart
Now, we're done with PHP installation. To test how it works, make test.php file in [/var/www/html] directory with following code:

Code: Select all

<?php phpinfo(); ?>
Save the file and test it in the browser by accessing the file from localhost:

Code: Select all

http://localhost/test.php
This should display information on installed version of PHP in the browser.

The process of installing and configuring PHP is complete. You may save your php projects / codes in /var/www/html folder to run from localhost.


How to Install MySQL Server:

Go with the command below to initiate MySQL Server installation:

Code: Select all

sudo apt install mysql-server
It will display package information and size with a prompt asking if you want to proceed... press Y to start installation. Wait for the installation process to complete and run this command to check MySQL service status when done:

Code: Select all

sudo systemctl status mysql
Upon successful installation the above command will show the service Active and Running.


How to Install PhpMyAdmin:

To install PhpMyAdmin, I would prefer to go with GUI method:
1. Open Software Manager from Menu
2. Search for phpmyadmin, click appropriate package from results to open and click Install
3. The installation process will prompt to choose Web Server... make sure to check apache and click Next
4. The other prompt will ask whether dbconfig-common should be used, remember to check this also and finish Installation.

Note: Enter password for phpmyadmin if prompted during installation.. don't panic if it don't ask.

You may have to restart apache once again... do it from terminal:

Code: Select all

sudo service apache2 restart
Upon successful installation you can access PhpMyAdmin from localhost in your browser like this:

Code: Select all

http://localhost/phpmyadmin

Can't login to PhpMyAdmin?

You may find login information to access phpmyadmin easily in case you forget it or if you're not asked to enter password for phpmyadmin during installation. Your login information is stored in /etc/dbconfig-common/phpmyadmin.conf file. To open it, run:

Code: Select all

sudo nano /etc/dbconfig-common/phpmyadmin.conf
Look for user name at dbc_dbuser='phpmyadmin' line and password at dbc_dbpass='Jer6xx3xxxXx' line. Don't make any change(s) in the file. Note down your login details and close it.


How to change password for PhpMyAdmin:

To change the password of PhpMyAdmin or any other user, I prefer to go for MySQL Shell. There may be some more ways for PhpMyAdmin IKD. It's what I find easiest:

Code: Select all

sudo mysql
or

Code: Select all

sudo mysql -u root -p

Code: Select all

use mysql;
select user,host,plugin from user;
Use command below to change password. Edit contents of command as per the query results e.g. user, host and plugin.

Code: Select all

ALTER USER 'phpmyadmin'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPassword';
Here, phpmyadmin is the user name, localhost is the host and mysql_native_password is the plugin. Exit MySQL terminal using exit; command.


Not able to perform queries or operations in PhpMyAdmin?

You may not be able to run queries or perform basic operations in PhpMyAdmin because MySQL PRIVILEGES are not granted to 'phpmyadmin'. Go through the following procedure to know what operations are allowed to phpmyadmin:

To access MySQL Shell, run sudo mysql or sudo mysql -u root -p in the terminal and...

Code: Select all

SHOW GRANTS FOR 'phpmyadmin'@'localhost';
The command will show what operations (PRIVILEGES) are allowed (GRANTED) to PhpMyAdmin. To allow privileges to 'phpmyadmin' or any other user, you can use:

Code: Select all

GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
Or you may opt to grant all privileges using:

Code: Select all

GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
Note: You have to be careful when you grant all privileges to some user coz a MySQL user with all privileges can perform any operation. Being the only user of a computer running localhost, one may opt to go for this option with caution.

Finally run the FLUSH PRIVILEGES command immediately after CREATE USER or GRANT statement in order to reload the grant tables to ensure that the new privileges are put into effect:

Code: Select all

FLUSH PRIVILEGES;
Review current permissions by running the SHOW GRANTS command:

Code: Select all

SHOW GRANTS FOR 'phpmyadmin'@'localhost';
Exit MySQL Shell using exit; command.


How to solve incompatible with sql_mode=only_full_group_by error in MySQL:

This is the last topic, I would like to cover in this tutorial. Many old MySQL queries are rejected new MySQL versions having incompatible group by clause. Like this one:

Code: Select all

SELECT cust_id, cust_name from myorders group by cust_id;
This is the explanation of it on MySQL website: If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them.

We can resolve this error by disabling ONLY_FULL_GROUP_BY sql mode from MySQL Shell. First login to MySQL Shell using sudo mysql or mysql -u root -p.

View current sql_mode:

Code: Select all

select @@sql_mode;
To disable ONLY_FULL_GROUP_BY for current session, one may opt to run:

Code: Select all

SET SESSION sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
To disable global ONLY_FULL_GROUP_BY SQL Mode (does not retain after restart), one may opt to go for:

Code: Select all

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Or to permanently disable ONLY_FULL_GROUP_BY sql_mode you can run the following command:

Code: Select all

SET PERSIST sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
If you opt to disable ONLY_FULL_GROUP_BY permanently, you may have to restart mysql service from terminal. Exit MySQL Shell using exit; and from terminal run:

Code: Select all

sudo service mysql restart
Hope I explained the basics regarding installation of Apache, MySQL, PHP and PhpMyAdmin on Linux Mint and to make then run. Will try to post another tutorial soon on how to change MySQL and Apache Data Directory.
Please SHARE Knowledge.. Let's not FLAUNT it...
FUN := 'Linux Mint 21 Vanessa Cinnamon' + 'Php, Apache, MySQL Server' + 'Lazarus' + 'Gambas';
const Lazarus : Pascal; const Gambas As BASIC
peterup
Level 1
Level 1
Posts: 4
Joined: Fri Nov 03, 2023 9:18 pm

Re: [TUTORIAL] Install & Configure Apache PHP MySQL PhpMyAdmin on Linux Mint 21

Post by peterup »

Every line starting with a ; is comment in php configuration file. Look for display_error = Off in the file, replace Off with On press Ctrl+X to exit and press Y to save changes.
Perhaps you should explain how to search for the line display_error = Off, as finding it manually is hard as the file has so many comments.

Perhaps it should read like,
Every line starting with a ; is comment in the php configuration file. Search for display_error = Off in the file. Press Ctrl+W and enter display_error into the search prompt. Press Alt+W to jump to the next found line, until you reach the line containing display_error = Off. Replace Off with On. Press Ctrl+X to exit and press Y to save changes.
Thank you very much for this tutorial.
accumulation
Level 1
Level 1
Posts: 8
Joined: Fri Dec 22, 2023 1:15 pm

Re: [TUTORIAL] Install & Configure Apache PHP MySQL PhpMyAdmin on Linux Mint 21

Post by accumulation »

Hi, thanks for this, but one small qualm.

Prior to being able to edit '.../apache2/php.ini', some may need to run the following first:

Code: Select all

sudo apt install libapache2-mod-php
An 'apache2' directory might not appear in '/etc/php/...' otherwise.
Post Reply

Return to “Tutorials”