Build a Raspberry Pi Web Server
So you’ve bought a Raspberry Pi and you are now struggling to find something to do with it after learning how to flash the
LEDs.
How about building a home web server to run one or more websites. It’s not as difficult as it sounds and is a great learning
process.
This site and others are being hosted on a Raspberry Pi 3B+ running WordPress on an NginX web server.
The first thing you will need is to do is install Raspberry Pi OS onto your SD card (I use Debian). Full instructions can be found on the official Raspberry Pi OS Downloads page.
Install NginX
Let’s begin by installing & setting up our web server. For this I will be using NginX (pronounced Engine-X) Open Source as the server (you could use another server such as Apache but I think NginXis better suited to the RPi). NginX can serve HTML pages over HTTP but we’re more interested in the dynamic content served up by WordPress via the PHP scripting language (more of this later).
Right so to install NginX onto your RPi enter the following command on your RPi Terminal.
sudo spt-get istall nginx
Now we start the server with the command
sudo service nginx start
You can now test your server by entering the IP address into a web browser. The IP address of my server is http://192.168.0.32, to find the IP address of your server, enter the following command into your terminal.
ifconfig
You will be presented with 1 or more blocks of text. Find the line that starts eth0: This is your ethernet connection and will contain a line beginning inet, this is your IP address and should start 192.168, Make a note of this address as you will need it again later.
When you enter your IP address into the browser you should see the following page.
Configuring NginX
With our server up and running, we need to do some configuration to optimise the server’s performance.
First lets open up the configuration file using the following command in the terminal.
sudo nano /etc/nginx/nginx.conf
Any line in this file beginning with a ‘#’ is commented out. When you see the word ‘uncomment’ in the instructions below, that means remove the ‘#’ from the start of the line.
- The maximum allowed connections is 1024 so we should change the worker_connections line to worker_connections 1024;
- Uncomment the multi_accept line & change it to multi_accept on;
- NginX will log every access to the server. If you don’t want huge log files you should turn this off. Change the access_log line to access_log off;
- Uncomment server_tokens and change it to server_tokens off; to stop the NginX version number being sent out in errors & headers.
- Change the number of seconds an idle connection will be closed after by changing the keep alive timeout to 10 like so keepalive_timeout 10; This will force an idle connection to close after 10 seconds.
- Add the line client_max_body_size 16m; to allow for pages with lots of images.
- Uncomment the gzip_vary line and change to gzip_vary on; to tell proxies to allow gzipped and regular files to be cached. This stops non-gzip capable clients trying to render gzipped files.
- Uncomment the gzip_proxied line and change it to gzip_proxied any; to force all gzip to compress all proxy responses.
- Uncomment the gzip_comp_level line and change it to gzip_comp_level 5; to give an optimal compression ratio while not adversely impacting CPU usage too much.
- Uncomment the gzip_http_version line and change it to gzip_http_version 1.1; to ensure both http/1.0 & http/1.1 files are compressed.
- Add the line gzip_min_length 256; next so we don’t try to compress files smaller than 256 bytes.
- Finally replace the gzip_tipes line with the following to add missing MIME types.
gzip_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/svg+xml image/x-icon text/css text/plain text/x-component text/javascript text/xml;
This will make sure all the listed file types are compressed along with the html types.
Save your changes by pressing <ctrl-s> then quit nano by pressing <ctrl-x>. Now type the following into the terminal to test the integrity of your changes.
sudo nginx -t
If everything is ok type the following into the terminal to restart your server.
sudo service nginx restart