Building a Web Server on Raspberry Pi Using Nginx

A Practical Guide for Engineers



This article provides a complete walkthrough of turning a Raspberry Pi into a lightweight, reliable web server using the Nginx platform. The steps include installation, firewall configuration, service management, and verification procedures suitable for engineering development environments.


1. Installing Nginx on Raspberry Pi

After powering on the Raspberry Pi and entering the terminal interface, first update the local package index—especially if it has been a long time since the last update:

sudo apt-get update

Once the package list is refreshed, you may upgrade existing software packages:

sudo apt-get upgrade

If you do not need to perform upgrades and simply want to install Nginx, run:

sudo apt-get install nginx

After installation completes, verify the installed version with:

nginx -v

A typical output might look like:

nginx version: nginx/1.14.2

If the version number appears, Nginx has been successfully installed.

Next, connect your PC to the Raspberry Pi via Ethernet. Ensure both devices are configured within the same subnet. Open a browser and enter the Raspberry Pi’s IP address (e.g., 192.168.0.81). You should see the Nginx welcome page.




2. Configuring the UFW Firewall

If your system uses UFW (Uncomplicated Firewall), begin by listing all available application profiles:

sudo ufw app list

Typical output on Ubuntu might include:

Nginx Full Nginx HTTP Nginx HTTPS OpenSSH

Profile Descriptions

  • Nginx Full: Opens both 80 (HTTP) and 443 (HTTPS)

  • Nginx HTTP: Opens only port 80

  • Nginx HTTPS: Opens only port 443

  • OpenSSH: Enables SSH service access

Enabling Profiles

To allow full access for both HTTP and HTTPS:

sudo ufw allow 'Nginx Full'

To allow only HTTPS:

sudo ufw allow 'Nginx HTTPS'

To remove a rule:

sudo ufw delete allow 'Nginx Full'

Verify current firewall rules:

sudo ufw status

[Illustration Placeholder – UFW Status Output]

For advanced UFW operations, refer to:
Configuration Guide for UFW Firewall in Ubuntu Linux Development.


3. Managing the Nginx Service

Starting and Stopping the Service

Stop the Nginx service:

sudo systemctl stop nginx

Start Nginx:

sudo systemctl start nginx

Restart the service:

sudo systemctl restart nginx

Reloading Configuration Seamlessly

When you modify only configuration files, Nginx supports live reload without interrupting existing connections:

sudo systemctl reload nginx

4. Enabling or Disabling Nginx at Startup

Nginx typically starts automatically at system boot.
To disable autostart:

sudo systemctl disable nginx

To re-enable autostart:

sudo systemctl enable nginx

5. Checking Nginx Service Status

To verify whether Nginx is running correctly:

sudo systemctl status nginx

If the output contains Active (running), the service is functioning properly.




6. Conclusion

With Nginx installed, firewall rules configured, and service management understood, your Raspberry Pi is now fully operational as a compact and efficient web server. This setup is ideal for prototyping, small-scale automation dashboards, IIoT gateways, and educational engineering projects.