You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do not need cloud hosting or paid services. PHP works well on the Pi and uses very little memory. You will learn how to install PHP Raspberry Pi step by step.
Table of Content
You can turn your Raspberry Pi into a small web server that runs PHP. This works well for small websites or tools that run on your local network.
Prerequisites
Make sure you have these before you begin:
- A Raspberry Pi 3, 4, or newer
- A microSD card with at least 8GB space
- Raspberry Pi OS (Lite or Desktop)
- An internet connection
- Access to the terminal or remote SSH
Once your Pi is ready and connected, you can move to the first setup step.
Understand What Raspberry Pi Is
Raspberry Pi is a credit-card-sized single-board computer that costs little money and fits in a shirt pocket.
The team in Cambridge formed the Raspberry Pi Foundation in 2009 and set a clear mission to boost computer science skills in schools.
Here is the use case:
- Set up a REST API that works over your network
- Build a local web server
- Create dashboards for IoT projects
Raspberry Pi works like a small desktop computer. It uses a system-on-chip that combines a CPU, GPU, and memory on one board. You plug in power storage and input devices.
It runs an operating system from a microSD card and loads programs like any other computer.
The microSD card holds the operating system files. When you turn on the Raspberry Pi, it loads the firmware and boots into Raspberry Pi OS or another Linux system. You see a desktop or command line, and you can run code, open apps, or manage files.
Update and Upgrade the System
Run the following to update your Pi:
sudo apt update
sudo apt upgradeThis checks for the latest software packages. You should always update before installing new tools. It avoids bugs and missing dependencies.
Now you can install your web server.
Install Apache or Nginx Web Server
You have two options. Choose one of the Apache or Nginx. Here is the setup of Apache:
sudo apt install apache2To use Nginx:
sudo apt install nginxNginx uses fewer system resources, and some developers choose it for its speed. You can follow this guide to install PHP.
Run this to install PHP:
sudo apt install phpYou may also need extra PHP modules. Here are some useful ones:
sudo apt install php-mysql php-cli php-curl php-xml php-mbstringTo check that PHP is installed correctly:
php -vLet’s move on to the following section to understand how to configure PHP with the web server.
Configure PHP with Web Server
This configuration is for the Apache server:
Create a test file:
sudo nano /var/www/html/info.phpAdd this line:
<?php phpinfo(); ?>Save the file. You can now open your browser and go to:
http://<raspberrypi-ip>/info.php
It will show you a page with PHP details.
You need to install PHP-FPM if you are using the Nginx server:
sudo apt install php-fpmThen edit the Nginx default config:
sudo nano /etc/nginx/sites-available/default
Also, you have to make sure these lines exist in the right blocks:
index index.php index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.X-fpm.sock;
}Replace php7.X with your PHP version. Then restart Nginx:
sudo systemctl restart nginxYou can now test your PHP setup.
How to Get HTTPS Working with Apache with WSGI (Flask)
Use Let’s Encrypt
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d yourdomain.com
This automates certificate issuance and Apache config.
sudo a2ensite default-sslCreate or edit your Apache site config, e.g., /etc/apache2/sites-available/flaskapp.conf:
<VirtualHost *:80>
ServerName yourdomain.com
# Redirect all HTTP to HTTPS
Redirect "/" "https://yourdomain.com/"
</VirtualHost>
<VirtualHost *:443>
ServerName yourdomain.com
# SSL settings
SSLEngine On
SSLCertificateFile /etc/ssl/certs/flaskapp.crt
SSLCertificateKeyFile /etc/ssl/private/flaskapp.key
# WSGI configuration
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/flaskapp/flaskapp.wsgi
<Directory /var/www/flaskapp>
Require all granted
</Directory>
# Optional: static files
Alias /static /var/www/flaskapp/static
<Directory /var/www/flaskapp/static>
Require all granted
</Directory>
# Logging (optional)
ErrorLog ${APACHE_LOG_DIR}/flaskapp-error.log
CustomLog ${APACHE_LOG_DIR}/flaskapp-access.log combined
</VirtualHost>
Ensure you have your WSGI entry point file /var/www/flaskapp/flaskapp.wsgi.
Restart Apache:
sudo systemctl restart apache2Wrapping Up
You installed PHP on a Raspberry Pi. You also set up a web server and tested PHP.
Your next steps:
- Build a simple PHP site
- Connect it to a database
- Try Laravel or create an API
Similar Reads
The PHP “while” loop is a simple tool that repeatedly runs a block of code as long as a certain…
The array_rand() is a helper function in PHP that makes it easier when dealing with arrays; you can just pull…
PHP comparison operators allow you to compare values in many ways, and this simplifies the process of checking whether values…
At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…
The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…
The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…
The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…
The increment and decrement are operators that can be used to increase or decrease the variable values which have a…
PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…
PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…