Setup a Raspberry Pi as a Apache/PHP/SQLite Server
The beginning of a short course on setting up a webserver on a Raspberry Pi.
Introduction
It is helpful to understand how to setup a webserver from scratch. As doing so, gives you complete control as to what that server can do. For example, using a low-cost Raspberry Pi, it provides a local cloud server for you to safely learn how to program a webserver. Using a public cloud can be problematic as it will be much easier for someone to hack into your cloud software.
Components of the Server
I’ll be using the following hardware and software:
- Raspberry Pi B+ Version 3 (RPi3)
- High quality microSD card
- Ethernet cable to connect the RPi3 to my network
- Raspberry Pi Debian Bullseye (lite) operating system
- Raspberry Pi Imager software
- Apache2 web server software
- PHP 8.1 server-side programming software
- SQLite 3 database software
My reason for using PHP is that I wish to use server-side control (as opposed to client-side as in Javascript), as its easier to integrate database operations using server-side. I am using SQLite instead of MySQL, as it too is easier to use. I could have used NGINX, instead of Apache, however, I understand Apache a bit more than NGINX.
There are three main steps to setting up the RPi3:
- Create the appropriate image for the microSD card
- Setup the webserver software, Apache, PHP and SQLite
- Fine tune specific aspects for greater efficiency, easier debugging etc
Step 1: RPi3 Setup
Use Raspberry Pi Imager to create an image for the Pi 3:
- Use Raspberry Pi OS (other) -> Raspberry Pi OS Lite (32-bit) “A port of Debian Bullseye with no desktop environment”
Pick the correct image as shown
- Click on the gear in the lower right corner and set the following:
- Image customization…”to always use”
- Enable SSH -> Use password authentication
- Set username and password (be sure to write down both)
- Set locale settings -> America/Los_Angeles or as appropriate
- Keyboard layout -> us or as appropriate
Fill out the image options and click save
- Storage -> Be sure to select your microSD card, NOT YOUR MAIN DRIVE!
- Click on Write, you will need to enter your computer (not the RPi) password
Confirm you are writing to the correct disk!
Start up RPi 3
- Remove the microSD card from the carrier
- Place in the RPi3,
- Plug in ethernet than power.
- Wait about a minute than attempt to connect
Connect to RPi 3
All of this work will be performed using a terminal program on your computer to interact with the RPi3. None of the work will require the RPi3 to be connected to a monitor and keyboard. The connection between the two computers will be a networking connection, via an ethernet cable.
I do this for several reasons:
- This is the easiest method of setting up server, its mainly a copy and paste activity
- When you need to do this with a server in the cloud, this is the only way it can be performed.
- It reduces the clutter of attaching multiple keyboards or a keyboard/monitor switch to both your computer and the RPi3.
If you have Windows and need a terminal program, use the Microsoft Terminal application. Its ideal for this and is downloadable from the Microsoft App Store. Both Linux(Konsole) and macOS (Terminal)have their own terminals which work great. Ultimately you will need to get to a terminal prompt…
TODO: (Need a way to confirm IP address, for this use 192.168.1.12)
- Open a terminal program and enter (for more context see Complete Listing)
ssh 192.168.1.12
yes
password
Complete Listing
ssh 192.168.1.12
The authenticity of host '192.168.1.12 (192.168.1.12)' can't be established.
ED25519 key fingerprint is SHA256:yJjnn0JUW3H97eXO+Ap9kqcKZPq2Q/zihS59YpS0sk4.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.12' (ED25519) to the list of known hosts.
lkoepsel@192.168.1.12's password:
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
We’ll fix the warnings as we go.
Configure RPi3
sudo raspi-config
This will bring up a limited GUI which looks like this:
GUI to configure RPi3
To move around you do the following:
- Use arrow keys to navigate
- Type return to enter
- To check or uncheck a selection, type spacebar
- Use tab to move between text in “< >” as in <Select> and <Finish>
Set Locale to US UTF-8
- Go down to 5. “Localisation…” type return
- With “L1 Locale” highlighted, type return
- Uncheck en_GB.UTF-8 UTF-8 (The * will disappear when you hit spacebar)
- Check en_US.UTF-8 UTF-8 (This time hit spacebar to make an * appear)
- Tab to and type return
- Configuring locales Press Down Key to highlight en_US.UTF-8 UTF-8
- Tab to and type return “Generating locales (this might take a while)…” then main window will show
- Arrow key down to 6 Advanced Options and type return
- With A1 Expand Filesystem highlighted, Tab to
- Type return
- With message “Root Partion…” type return
- In Main Window, tab to Finish and type return
- Type return with <Yes> selected and allow RPi 3 to reboot
Reconnect after Reboot
ssh 192.168.1.12
# Update Operating System
sudo apt update && sudo apt full-upgrade -y
Comments powered by Talkyard.