NGINX Raspberry Pi Setup


NGINX Raspberry Pi Setup

Introduction

NGinx is a lightweight web server, ideal for Raspberry limited power. The question, why choose NGINX over the Apache? Performance.. As we mentioned, RaspPi has a relatively limited hardware capabilities, and having that in mind we should try to get more with the least effort. NGINX responds much faster than Apache, can support more users simultaneously and it consumes less RAM than Apache. Of course, all that is again relative. Some experts are I’m sure capable of squeezing the same/similar performance out of them both. All in all, we decided to go with NGINX Raspberry Pi Setup here.

NGINX Setup

Install:

# sudo apt-get install nginx

Start it:

# sudo /etc/init.d/nginx start

With that you’ll see NGINX Test page on http://localhost/ or on your RaspPi address http://<IP>/ if you’re accessing from another computer on the network. Use hostname -I or ifconfig to get your RaspPi IP address. Simply jump to your browser and check those urls:

NGINX Raspberry Pi Setup

Default page location is /var/www/html . You can change your index.html or you can alter default page location in /etc/nginx/sites-available/default

# nano /etc/nginx/sites-available/default

Look for root parameter. Next we should probably enable PHP. First, install php-fpm:

sudo apt-get install php-fpm

Find the line with:

index index.html index.htm index.nginx-debian.html;

and add index.php.  Next, adjust PHP scripts, depending on what you installed adjust the block appropriately (uncomment or alter):

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

Restart or Reload nginx:

# /etc/init.d/nginx reload

For test, make an index.php file in /var/www/html folder. In it, place any PHP code:

<?php echo phpinfo(); ?>

Now go to your browser and refresh, you should see PHP info there. You can adjust ownership for the sake of safety and easy management:

# sudo chown -R www-data:pi /var/www/html/

One more thing that might come in handy are logs:

/var/log/nginx/access.log
/var/log/nginx/error.log

You should use them when you encounter some problems with Nginx or maybe PHP. You’re most like find some clues there on what the problem might be.

Conclusion

That’s basically it. Nginx is up and running on your Raspi Beast, ready for new IoT endeavours. So, setting nginx web server up is no science, and it’s the same as for any other non-RaspPi server. On the other hand, if you plan to put a heavy load on it (significan number of users), making even more performant/optimal/compatible will probably take significant effort.