How to Install PHP, Apache, MySQL/MariaDB on Raspberry Pi

Install PHP

Connect to Raspberry Pi via SSH and execute command to download GPG key:

sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Add PHP repository:

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

Update the package lists:

sudo apt update

Next, install PHP 8.2 with command line interface (CLI):

sudo apt install -y php8.2-common php8.2-cli

Check PHP version when installation was finished:

php --version

There are various PHP extensions that provide additional functionality. PHP extensions can be installed using the following syntax:

sudo apt install -y php8.2-extension_name

Execute the following command to install commonly used PHP extensions:

sudo apt install -y php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip

We can use -m option to check what extensions are installed.

php -m

PHP integration with MySQL or MariaDB

To use PHP with MySQL or MariaDB database, we need to install the following extension:

sudo apt install -y php8.2-mysql

PHP integration with Apache

If we want to integrate PHP with Apache HTTP server, then install the following extension:

sudo apt install -y libapache2-mod-php8.2

Once installation was completed, restart Apache:

sudo service apache2 restart

Testing PHP

Create a new main.php file:

nano main.php

Add the following code:

<?php
echo 'Hello world';

Run the following command to test a script:

php main.php

Uninstall PHP

If you want to completely remove PHP anything related to it, execute the following command:

sudo apt purge --autoremove -y php-common mime-support

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/php.gpg
sudo rm -rf /etc/apt/sources.list.d/php.list

Remove PHP related file:

sudo rm -rf /var/lib/systemd/timers/stamp-phpsessionclean.timer

We will be happy to hear your thoughts

Leave a reply

Experience the Power of Preinstalled OS Drives and Booting Guides
Logo
Compare items
  • Total (0)
Compare
0
Ninja Silhouette 9 hours ago

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Joe Doe in London?

Joe Doe in London, England purchased a

Shopping cart