[EN] LAMP on Raspberry Pi

This article is a memoir about setting up the Raspberry Pi 3 B+ board as a LAMP-based web service or Linux + Apache + MySQL + PHP. We chose MariaDB instead of MySQL. We have discussed how to install and it was already set up in the previous article.

Objective

  1. Install Apache
  2. Install MariaDB
  3. Install PHP

Steps

Install Apache

To install Apache, run the following command and answer ‘Y’ when asked to confirm the installation.

sudo apt install apache2

Once the installation is complete, run the Apache modules with the following command.

sudo a2enmode rewrite

To be sure, reload the Apache2 service with the following command.

sudo systemctl restart apache2

when the web page is running, we need to change the web management permissions to the user named pi (or any other name added by the reader) by using the command to change the owner of web directory. /var/www/html and set the access rights to 770

sudo chown -R pi:www-data /var/www/html
chmod -R 770 /var/www/html

Apache is now ready and the database is installed. Next, let’s install PHP.

Install PHP

In addition to installing the PHP script interpreter, it must be configured to work with Apache. Later on, you can code a PHP extension to allow Apache to call the PHP interpreter to run and pass results between them.

The command to install PHP is run as follows:

sudo apt install php

The next step is to install the PHP module to Apache2 and then start Apache2 again with the following command.

sudo apt install libapache2-mod-php
sudo service apache2 restart

After that, delete the index.html file and create index.php using the following command to delete index.html and open the text editor to create index.php.

cd /var/www/html
rm index.html
nano index.php

The code for index.php is as follows: Once you’ve typed it, validate it, save it with Ctrl+O and command ‘Y’ to save, and exit the Text Editor with Ctrl+X and return to the Terminal’s Command Prompt.

<?php
echo "Hello, World!";
?>

The last step is for PHP to see MariaDB by running the following command:

sudo apt install php-mysql
sudo service apache2 restart

Conclusion

When following this article you will have a LAMP-ready Raspberry Pi board, enabling the creation of a web server that supports data storage in MariaDB database management systems, which is the foundation for future IoT and AI operations.

Finally, I hope the article will be very useful. and have fun with programming

(C) 2020, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-09-01