Create Your Own PHP Website

In this page, I will shows on how to write PHP files and serve them as a website.

Install Latest PHP and Its libraries

To allow the Apache server to process our PHP files, we need to install the latest version of PHP and PHP module/library for Apache.

1. You can type the following command to install these:

sudo apt-get install php libapache2-mod-php -y

Note:

If you got an error like below:

pi@raspberrypi:~ $ sudo apt install php
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
alsa-base gstreamer0.10-alsa gstreamer0.10-plugins-base libdrm-etnaviv1
libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 libllvm6.0 libva-wayland2
libxfce4util-bin libxfce4util-common libxfce4util7 libxfconf-0-2 pimixer
point-rpi python3-pyperclip rpi.gpio-common xfconf
Use ‘sudo apt autoremove’ to remove them.
The following additional packages will be installed:
libapache2-mod-php7.3 php-common php7.3 php7.3-cli php7.3-common php7.3-json
php7.3-opcache php7.3-readline
Suggested packages:
php-pear
The following NEW packages will be installed:
libapache2-mod-php7.3 php php-common php7.3 php7.3-cli php7.3-common
php7.3-json php7.3-opcache php7.3-readline
0 upgraded, 9 newly installed, 0 to remove and 35 not upgraded.
Need to get 2,948 kB/2,969 kB of archives.
After this operation, 14.0 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirror.rise.ph/raspbian/raspbian buster/main armhf php7.3-common armhf 7.3.14-1~deb10u1 [520 kB]
Get:2 http://mirror.rise.ph/raspbian/raspbian buster/main armhf php7.3-json armhf 7.3.14-1~deb10u1 [16.2 kB]

Please type:

sudo apt update --fix-missing

And run the command before again.

2. Then, you can create the file name index.php:

sudo leafpad index.php

Note: Leafpad is a graphical editor or we can use sudo nano index.php 

3. Put some PHP content in it:

<?php echo "hello world"; ?>

4. Now save and refresh your browser.

Refresh your browser. You should see “hello world”. This page is not dynamic, but it is still served by PHP.

You should see “hello world”.

5. This is not dynamic but still served by PHP. Try something dynamic:

<?php echo date('Y-m-d H:i:s'); ?>

6. or show your PHP info:

<?php phpinfo(); ?>