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 upgrade
This 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 apache2
To use Nginx:
sudo apt install nginx
Nginx 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 php
You may also need extra PHP modules. Here are some useful ones:
sudo apt install php-mysql php-cli php-curl php-xml php-mbstring
To check that PHP is installed correctly:
php -v
Let’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.php
Add 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-fpm
Then 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 nginx
You 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-ssl
Create 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 apache2
Wrapping 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