January 07, 2024

RPi install Apache/PHP WEB-server

Updated 2024-01-07 ! OK with Bookworm & PHP 8.2 !

As a part of one of my project, the ultimate Weather site ? Weather34, I needed a WEB-host with PHP.

I started up with a fresh Raspberry OS Lite SD Card with Samba installed.

Log on, with ssh/terminal to the RaspberryPi(RPi), 
in this case hostname "pws01", and update the OS with

sudo apt update
sudo apt full-upgrade -y
sudo apt clean

go on and install the Apache server with

sudo apt install apache2

Initial test setup
Check that the Apache server works with a WEB-browser and the URL

http://pws01/

And the default Apache2 page will show (See picture above)

Apache log files is available at /var/log/apache2 and documentation at Apache.org

Change permission, for the default Apache user, to change files within the Apache document root with

sudo chown -R www-data:www-data /var/www/html/

Install PHP 
Remove old PHP ?
Maybe You already have an old PHP installation ? Remove it with

sudo apt purge --autoremove '^php.*'

PHP on Raspberry Pi OS
(Select PHP version by your self ? Use PHP 8.1 hints below.)

Install, standard package, with

sudo apt install php libapache2-mod-php

Check the install

php -v 

And something like this will show


PHP 8.2.7 (cli) (built: Jun  9 2023 19:37:27) (NTS)

Copyright (c) The PHP Group

Zend Engine v4.2.7, Copyright (c) Zend Technologies

    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies


Curl is needed in many applications, including the mentioned Weather34 project, so install with 

sudo apt install php-curl

To check installed modules use php -m

PHP test in WEB-browser
Without SAMBA
Jump to "Test" below

With SAMBA
Samba is a fileserver application for Mac/Windows users and here you find installation instructions.

Create a Samba share for the default Apache document root which will make it very simple to add and edit files on the RPi from Your Mac or PC.

sudo nano /etc/samba/smb.conf

add this at the end of the file

[Apache_folder]
   comment = Apache folders   
   read only = no
   locking = no
   path = /var/www/html/    
   guest ok = no


and then add the default Apache user 

$ sudo smbpasswd -a www-data
New SMB password:
Retype new SMB password:
Added user www-data.


Finish with a restart of the Samba server

sudo systemctl restart smbd

Test
Create a file with PHP code

sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'

and then in Your browser use the URL

http://pws01/phpinfo.php

Information about PHP and the server will show.

Round up
If there is a need for restarting, after a module install?, the Apache server use 


sudo service apache2 restart

Now You can move on to install the Weather34 project or use the WEB-server to something else.

PHP 8.1 hints
$ sudo wget -qO /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
$ sudo apt update && sudo apt full-upgrade -y && sudo apt clean
$ sudo apt install php8.1 php8.1-curl libapache2-mod-php8.1
$ sudo systemctl restart apache2
Check => http://raspberrypi/
$ sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
Check => http://raspberrypi/phpinfo.php

2 comments:

  1. Before installation of php you first need to do an apt-get update to refresh.

    ReplyDelete

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !