Language: EN

como-instalar-php-sobre-apache-en-raspberry-pi

How to Install PHP on Apache on Raspberry Pi

In this post we are going to learn how to install PHP on a Debian-based operating system like Raspbian on Raspberry Pi.

PHP is a widely used language for serving dynamic content, and is a common component on a server. Along with Apache and MySQL/MariaDB, it is part of the “LAMP” stack (Linux, Apache, MySQL/MariaDB, PHP).

Installing PHP on Raspberry Pi is very simple through the APT package manager. Simply, in a command console we do,

sudo apt install php libapache2-mod-php php-mysql

That easy. Now, it is convenient to modify the Apache configuration since, by default, if it finds an ‘index.html’ file, it will prioritize it over an ‘index.php’. Normally we want the behavior to be the opposite, so we are going to modify it.

To do this, we do,

sudo nano /etc/apache2/mods-enabled/dir.conf

In the content of the file we will find the following,

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

We modify it, moving ‘index.php’ to the left,

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

We close the file and restart Apache by doing,

sudo systemctl restart apache2

To test that PHP is working correctly, we are going to create a simple file by doing,

sudo nano /var/www/html/info.php

Inside it, we simply put the following content,

Now we access the file’s local URL with the browser.

http://localhost/info.php

If you see the following, congratulations, PHP is working correctly.

raspberry-pi-php-resultado

Finally, we delete the test file we created

sudo rm /var/www/html/info.php

That’s how simple it is to install PHP on Raspberry Pi! In the next posts we will see how to install MySQL and MariaDB to configure our LAMP stack. See you soon!