# References
# https://randomnerdtutorials.com/raspberry-pi-apache-mysql-php-lamp-server/
# https://www.bennetrichter.de/en/tutorials/apache2-php8-mariadb-phpmyadmin/

#Install PHP8.1

sudo apt-get install php8.1-cli php8.1-common php8.1-curl php8.1-mbstring php8.1-intl php8.1-xml php8.1-mcrypt php8.1-zip php8.1-sqlite3 libapache2-mod-php8.1

#Check PHP Version
php -v

sudo apt install mariadb-server php8.1-mysql -y

sudo mysql_secure_installation

#You will be asked Enter current password for root (type a secure password): press Enter
#Type in Y and press Enter to Set root password
	# Example:- Set Password - root
#Type in a password at the New password: prompt, and press Enter. Important: remember this root password, as you will need it later
#Type in Y to Remove anonymous users
#Type in Y to Disallow root login remotely
#Type in Y to Remove test database and access to it
#Type in Y to Reload privilege tables now

#When the installation is completed, you’ll see the message: “Thanks for using MariaDB!”.

# Mysql login will be user=root & password=root

#If you experience any error login into phpMyAdmin, you might need to create a new user to login. Those commands will create a new user with name (admin) and password (your_password).
### Don't directly follow this step ####
sudo mysql --user=root --password=root
> create user admin@localhost identified by 'admin';
> grant all privileges on *.* to admin@localhost;
> FLUSH PRIVILEGES;
> exit;

#Install phpmyadmin

#Change directory
cd /usr/share

#Download phpMyAdmin, execute the command 
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.zip -O phpmyadmin.zip

unzip phpmyadmin.zip

rm phpmyadmin.zip

mv phpMyAdmin-*-all-languages phpmyadmin

chmod -R 0755 phpmyadmin

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

#Add the below content as it is.

	# phpMyAdmin Apache configuration

	Alias /phpmyadmin /usr/share/phpmyadmin

	<Directory /usr/share/phpmyadmin>
	    Options SymLinksIfOwnerMatch
	    DirectoryIndex index.php
	</Directory>

	# Disallow web access to directories that don't need it
	<Directory /usr/share/phpmyadmin/templates>
	    Require all denied
	</Directory>
	<Directory /usr/share/phpmyadmin/libraries>
	    Require all denied
	</Directory>
	<Directory /usr/share/phpmyadmin/setup/lib>
	    Require all denied
	</Directory>


#Activate apache2 configuration
sudo a2enconf phpmyadmin
sudo systemctl reload apache2

mkdir /usr/share/phpmyadmin/tmp/

chown -R www-data:www-data /usr/share/phpmyadmin/tmp/

#Now hit the below URL and check
http://use-ip-address/phpmyadmin

