Pi Web Server v3

Local web server: nginx, php, SQLite3, GD, curl, xml, mbstring & custom error pages

Board version: Pi 3 B+ Rev 1.3
Memory: 1 GB
RPi OS version: Lite 32-bit



Basic installation OS


Installing NGINX Web server

We should also run the following command to uninstall Apache2 since there is a chance that it is pre-installed on your system: sudo apt remove apache2

Install NGINX onto our Raspberry Pi by running the following command on your Raspberry Pi: sudo apt install nginx -y

Now with NGINX installed, we can now start up the software. Type the following command into terminal to start up the web server on your Raspberry Pi: sudo systemctl start nginx

With the NGINX web server now started up we should now grab our local IP address.
Just go to the local IP Address that you obtained using hostname -I.
Once you browse to the address you should see something like Welcome to nginx!


Configuring NGINX for PHP

Before we get started with configuring PHP for NGINX, we need to go ahead and install PHP it and some recommended PHP modules that will make it easier when dealing with more extensive PHP scripts. You can install everything by running the command below: sudo apt install php8.2-fpm php8.2-mbstring php8.2-curl php8.2-gd php8.2-xml php8.2-sqlite3 -y

With PHP-FPM now installed we can make the required modifications to the default NGINX configuration file. To begin editing the default configuration file run the following command on your Raspberry Pi: sudo nano /etc/nginx/sites-enabled/default

Within this file, find and replace the following lines.
Find: index index.html index.htm; Replace With: index index.php index.html index.htm;

Here we need to add index.php to the index line, and this tells NGINX to recognize the index.php file as a possible index, adding it first in the list means it will be selected over an index.html file.
Find: #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php5-cgi alone: # fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: # fastcgi_pass unix:/var/run/php5-fpm.sock; #} Replace With: location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; } This code sets up NGINX to process .php files by passing them through to PHP-FPM.
Once done, you are able to save and exit by pressing CTRL + X and then pressing Y and then ENTER.

Next, we will need to tell NGINX to reload its configuration by running the following command: sudo systemctl reload nginx

Finally, let’s test the PHP setup by writing a very simple index.php file in our /var/www/html directory. Run the following command to create and begin editing our index.php file: sudo nano /var/www/html/index.php To this file, add the following line of code: <?php phpinfo(); ?> Once that is all done, we can save and exit by pressing CTRL + X then Y and lastly ENTER.

Now like earlier in the tutorial you can go to your Raspberry Pi’s IP address, this time however you should see a page showing all the information about your version of PHP and what modules are currently active. It is a good indication that your PHP installation is up and running correctly.


Manage your web pages

To manage your web pages, you should change the permissions for your /var/www/html/ folder.
To do this, run the following commands: ls -lh /var/www/ sudo chown -R pi:www-data /var/www/html/ sudo chmod -R 770 /var/www/html/ ls -lh /var/www/


Check the ftp server

There is no need to install a ftp server, because the SSH daemon has a build-in sftp server.
Check the ftp server with your favorite ftp client in sftp mode (port 22)

If you can not upload anything because of a permission denied $ sudo chown -R pi /var/www/html

If you can not upload anything because of a permission denied $ sudo chown -R pi /var/www


Set web server user to be owner of frontend (This one gives /var/www/html to www-data user/group): $ sudo chown -R www-data:www-data /var/www/html

If files want to do something then you must change the following.
You must change the owner of PHP from www-data to pi. $ cd /etc/php/8.2/fpm/pool.d $ sudo nano www.conf Search for the owner/group (somewhere at the top) and change the owner to pi. ; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. user = pi group = www-data Save en reboot.

All of this also has to do with the ftp server, after upload the files are from owner/group pi:pi.
This means that a file cannot execute anything, only static pages can be displayed.


Install SQLite3

$ sudo apt install sqlite3 Reboot


Testing SQLite

We can start using it by creating a new database: $ sqlite3 my.sqlite

This creates a new database (same name file in current directory) and prompts for further SQL statements like create table or other. SQLite version 3.40.1 2022-12-28 14:03:47 Enter ".help" for instructions sqlite>

Let's create a simple table temperature: sqlite> create table temperature (id integer, temp float, date text); And now we can start adding data to table: sqlite> insert into temperature values (1, 22.1, '2013-10-08'); That's it, we have stored our first record in to database. To check it's there lets run a select statement: sqlite> select * from temperature; Bellow we get the results: 1|22.1|2013-10-08

Terminate SQLite CLI Shell.
You can terminate the sqlite3 program by typing your system End-Of-File character (usually a CTRL D).

SQLite shell can also be terminated using .exit command: sqlite> sqlite> .exit pi@raspberrypi:~ $


Custom error pages

Edit: $ sudo nano /etc/nginx/sites-enabled/default Put the error codes in: server { listen 80 default_server; listen [::]:80 default_server; . . . # Custom error pages error_page 404 /e404.html; location = /e404.html { root /var/www/err_pages; internal; } error_page 403 /e403.html; location = /e403.html { root /var/www/err_pages; internal; } }