Installing Nginx on Raspberry Pi
NGINX (pronounced engine x) is a popular lightweight web server application you can install on the Raspberry Pi to allow it to serve web content.
Like Apache, NGINX can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP, .net core, JavaScript or any other popular web development programming language.
In this tutorial we will investigate how we can use the Raspberry Pi to become a lightweight web server, by installing Nginx on it.
Installing the Essentials
In an open Terminal session window, type the following command to install Nginx:
{% highlight ruby linenos %} sudo apt-get update sudo apt-get install nginx {% endhighlight %}
Once the installation has completed, you can start the Nginx web service with:
{% highlight ruby linenos %} sudo /etc/init.d/nginx start {% endhighlight %}
You can now test that the Nginx web server is working as expected by starting up a local Chromium browser instance on your Raspberry Pi.
You should see the default nginx test page when you open http://localhost
Installing PHP
Now that Nginx is running, it's just another trivial package install to enable PHP support! In an open Terminal session
window, just type the following to install the latest php-fpm
connector.
{% highlight ruby linenos %} sudo apt-get install php-fpm {% endhighlight %}
Like most web servers, Nginx allows for a set of "default" supported web documents. In other words, when you make a browser
request to the root of a folder on your web server without any specific page, eg. https://yoursite.com/
vs. https://yoursite.com/page1.html
,
Nginx will check it's list of supported default documents. If your folder has such a named default document file, then
it will return that automatically to the browser.
Let's add support for PHP.
{% highlight ruby linenos %} sudo nano /etc/nginx/sites-enabled/default {% endhighlight %}
Find the following line:
{% highlight ruby linenos %} index index.html index.htm; {% endhighlight %}
and add index.php
to the list:
{% highlight ruby linenos %} index index.php index.html index.htm; {% endhighlight %}
Then look for a section which is commented out (with #
)
Then update
Reload Nginx to pick up the recent configuration changes:
{% highlight ruby linenos %} sudo /etc/init.d/nginx reload {% endhighlight %}
Create a test default PHP page
To help test (and demonstrate) our newly added PHP support, let's create a default PHP page.
{% highlight ruby linenos %} cd /var/www/html/ sudo mv index.nginx-debian.html index.php {% endhighlight %}
Now open the index.php
file with nano
{% highlight ruby linenos %} sudo nano index.php {% endhighlight %}
After the <body>
tag in the page, but before the closing </body>
tag, add the following:
{% highlight ruby linenos %}
{% endhighlight %}Save and close up nano
. Now open Chromium browser and navigate to http://localhost
. You should
see the familiar PHP info output.
Conclusion
Hopefully this was a simple walkthrough to show you how to setup the latest version of Nginx on the Raspberry Pi. As an additional bonus, I also included some easy steps to enable a fast and lightweight PHP connector for Nginx.