Setup a Raspberry Pi as a Apache/PHP/SQLite Server

4 minute read

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:

  1. Create the appropriate image for the microSD card
  2. Setup the webserver software, Apache, PHP and SQLite
  3. 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:

  1. 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

    Pick the correct image as shown

Large Version to see detail

  1. Click on the gear in the lower right corner and set the following:
    1. Image customization…”to always use”
    2. Enable SSH -> Use password authentication
    3. Set username and password (be sure to write down both)
    4. Set locale settings -> America/Los_Angeles or as appropriate
    5. Keyboard layout -> us or as appropriate
      Fill out the image options and click save

      Fill out the image options and click save

Large Version to see detail

  1. Storage -> Be sure to select your microSD card, NOT YOUR MAIN DRIVE!
  2. Click on Write, you will need to enter your computer (not the RPi) password
    Confirm you are writing to the correct disk!

    Confirm you are writing to the correct disk!

Large Version to see detail

Start up RPi 3

  1. Remove the microSD card from the carrier
  2. Place in the RPi3,
  3. Plug in ethernet than power.
  4. 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:

  1. This is the easiest method of setting up server, its mainly a copy and paste activity
  2. When you need to do this with a server in the cloud, this is the only way it can be performed.
  3. 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)

  1. 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

GUI to configure RPi3

Large Version to see detail

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

  1. Go down to 5. “Localisation…” type return
  2. With “L1 Locale” highlighted, type return
  3. Uncheck en_GB.UTF-8 UTF-8 (The * will disappear when you hit spacebar)
  4. Check en_US.UTF-8 UTF-8 (This time hit spacebar to make an * appear)
  5. Tab to and type return
  6. Configuring locales Press Down Key to highlight en_US.UTF-8 UTF-8
  7. Tab to and type return “Generating locales (this might take a while)…” then main window will show

  1. Arrow key down to 6 Advanced Options and type return
  2. With A1 Expand Filesystem highlighted, Tab to
  3. Type return
  4. With message “Root Partion…” type return
  5. In Main Window, tab to Finish and type return
  6. 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

Step 2: Server setup

Comments powered by Talkyard.