In an earlier lesson we have installed an Apache web server. This is nice for serving static pages, but hardly any website consists only of static pages, so that is where a LAMP server comes in. LAMP is an abbreviation for Linux, Apache, MySQL/MariaDB, PHP/Python/Perl. So, the OS, the web server, the database and the scripting language.

OK, let’s get started: Before installing software, it is good practice to make sure the Pi is up to date:
sudo apt update && sudo apt upgrade -ySince we already installed Apache2, we can continue with PHP. At the moment of writing version 7.4 is the main version and version 8.1 is out as well. We’ll stick to version 7 for now:
sudo apt install php libapache2-mod-php -yTo check if the installation went well, we create our first php page with the use of the echo command, so no need for an editor this time:
sudo echo "<?php phpinfo ();?>" > /var/www/html/info.phpNow, open your web browser and go to: https://[yourname].duckdns.org/info.php and you should see a large page with a lot of information about your web server. 
This information is also interesting for potential hackers, so once you are done with it, it is good practice to remove the file again.
Next step is to install MariaDB, the open source version of MySQL, then we have to restart apache and we’ll secure the database server:
sudo apt install mariadb-server php-mysql -y
sudo systemctl reload apache2
sudo mysql_secure_installationYou will be asked enter current password for root (default is blank): press Enter.
Set root password, type Y and press Enter. 
Type in a new password and press Enter. Important: remember this root password. (So put the password in your password manager!)
Re-enter the new password and press Enter.
Type Y and press Enter to Remove anonymous users.
Type Y and press Enter to Disallow root login remotely.
Type Y and press Enter to Remove test database and access to it.
Type Y and press Enter to Reload privilege tables now.
When complete, you will see the message All done! and Thanks for using MariaDB!.
Some basic commands that you will have to use to create a database and grant privileges:
sudo mysql -uroot -p
* create database YOURDATABASENAME;
* GRANT ALL PRIVILEGES ON YOURDATABASENAME.* TO 'root'@'localhost' IDENTIFIED BY 'YOURROOTPASSWORD';
* FLUSH PRIVILEGES;
* EXITAs a bonus we’ll install phpmyadmin, a web frontend to administer your database:
sudo apt install phpmyadmin -ySelect Apache2 with the cursor keys and press the spacebar to highlight Apache2 > Tab > Enter.
Configure database for phpmyadmin with dbconfig-common? Select ‘No’ > Enter, we have already setup a database above with the MySQL installation.
To access phpmyadmin browse to https://[yourname].duckdns.org/phpmyadmin/ Username: root and [yourpassword]
Source:
https://www.instructables.com/Installing-LAMP-Linux-Apache-MySQL-PHP-on-a-Raspbe/
