Learn to set up a LAMP (Linux, Apache, MySQL, PHP) stack on your Raspberry Pi and configure it to work as a web server.
Type the following command into the terminal and press Enter to install apache2
sudo apt install apache2 -y
PHP is a preprocessor: it’s code that runs when the server receives a request for a web page via a web browser. It works out what needs to be shown on the page, and then sends that page to the browser.
Type the following command in the terminal to install PHP:
sudo apt install php -y
Type this command to delete the index.html file from earlier:
sudo rm index.html
Create a new file called index.php:
sudo nano index.php
In the nano code editor, type this PHP code into the file and save it.
<?php echo "hello world"; ?>
Refresh your browser. You should see “hello world”. This page is not dynamic, but it is still served by PHP.
On Debian/Ubuntu-based servers, edit:
sudo nano /etc/apache2/mods-enabled/dir.conf
On CentOS/RHEL, the file is usually located at:
sudo nano /etc/httpd/conf/httpd.conf
If you're using virtual host configurations, check files inside /etc/apache2/sites-available/ or /etc/httpd/conf.d/.
Modify the DirectoryIndex Directive
Look for a line like this:
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
Change it so that index.php comes first:
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
Save and Exit
If using nano, press CTRL + X, then Y, and hit Enter to save.
Restart Apache to Apply the Changes
On Debian/Ubuntu:
sudo systemctl restart apache2
On CentOS/RHEL:
sudo systemctl restart httpd
MariaDB is a popular database engine. Like PHP, it’s widely used on web servers, which is why projects like WordPress use it, and why those projects are so popular.
Type this command to install the MariaDB Server and PHP-MySQL packages:
sudo apt install mariadb-server php-mysql -y
Now restart Apache:
sudo service apache2 restart
Run the MySQL secure installation command in the terminal window.
sudo mysql_secure_installation
Go through the setup wizard :