How to Install Nginx on the Raspberry Pi: Raspberry Pi Nginx Web Server

Running a Raspberry Pi web server is among my favorite projects for the ultra-popular single-board computer. Installing WordPress or Drupal on a Raspberry Pi remains simple yet convenient. Balancing modest computing power with energy efficiency, the Raspberry Pi handles server purposes with ease. Free, open-source HTTP server software Nginx is a top choice for a high-performance server. Learn how to make a Raspberry Pi Nginx web server!

What is Nginx?

Nginx is a free, high performance, open-source, reverse proxy and HTTP server option. Additionally, Nginx touts POP3 and IMAP proxy server functionality. With an intuitive set up, easy configuration, and low resource consumption, Nginx works flawlessly on the Raspberry Pi.

What You'll Need to Run Nginx on the Raspberry Pi

An Nginx Raspberry Pi web server requires a few items. First, you'll need a Raspberry Pi board. I recommend the Raspberry Pi 4 with either 4GB or 8GB of RAM. Next up, you'll need a microSD card with a minimum capacity of 8GB, though 16GB or larger is preferred. Additionally, you'll need a compatible power supply unit (PSU), case, and peripherals such as a keyboard and mouse. On the software side, you'll need Nginx plus a Linux operating system running on the Pi. Since this is a web server, an active Internet connection is required.

Raspberry Pi Nginx parts list:

Install Nginx on Raspberry Pi 

Begin by installing the Nginx package. In a terminal, run:



sudo apt-get install nginx

Next, start the server using:



sudo /etc/init.d/nginx start

NGINX places an HTML file under the web folder. As such, you can test if NGINX installed properly by navigating to http://localhost/ or http://[YOUR RASPBERRY PI'S IP ADDRESS]/. To view your IP address, you may run the command hostname -I. 

If installing NGINX on the Raspberry Pi went as planned, you'll see a friendly "Welcome to nginx" message when you navigate to http://localhost/ in a browser. 

By default, NGINX stores its web page location at /var/www/html. Here, you may edit the index.nginx-debian.html to change the default web page. You can check the default page at /etc/nginx/sites-available

When you install Nginx, you may wish to install PHP as well. This scripting language is often used server-side, so for a Raspberry Pi NGINX web server, I suggest installing PHP. 

In a terminal, run:



sudo apt-get install php-fpm

Next, enable PHP within NGINX with:



cd /etc/nginx
sudo nano sites-enabled/default

Locate the line:



index index.html index.htm;

After index, add index.php. Your finished line should read:



index index.php index.html index.htm;

Find the following section:



# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# location ~ \.php$ {

Remove the pound sign (or hashtags if you will) for this snippet:



location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}

The finished product should look like:



# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}

Now, reload the configuration file by running:



sudo /etc/init.d/nginx reload

When that's complete, ensure that PHP installed properly. Rename index.nginx-debian.html as index.php:



cd /var/www/html/
sudo mv index.nginx-debian.html index.php

Then, open index.php:



sudo nano index.php

Now, add dynamic PHP content:



<?php echo phpinfo(); ?> 

When you finish, save your work, then refresh your http://localhost/ web page. If all went as planned, you should see your refreshed page complete with dynamic PHP.

How to Build a Raspberry Pi Web Server With NGINX: Final Thoughts

Ultimately, installing NGINX and PHP on a Raspberry Pi is pretty simple. Because NGINX provides a high performance, low resource consumption web server option, it's ideal for use on a Raspberry Pi. You may even use WordPress on the Raspberry Pi with NGINX and MySQL for a robust web server configuration. Aside from use as a web server, the Raspberry Pi makes an excellent media server

What are you running on your Raspberry Pi?

Leave your feedback...