Search This Blog

Setting up nginx on a Raspberry Pi

The Raspberry Pi can be a great small web server. I've hosted many sites on my Pi, and still would if my new ISP didn't block port 80 on my connection. It doesn't take much CPU power or RAM to host static sites and the many people even install WordPress on Raspberry Pis. In this post, I'll show you how to install and use the nginx web server on a Raspberry Pi running Raspbian.
Nginx is an efficient and lightweight web server that is gaining a lot of popularity, most notably taking a large part of the market share from the previously more popular Apache web server. Installing nginx on a Raspberry Pi is pretty simple. To get started, update your Pi's repositories so that you get the latest software:
sudo apt-get update
Now, install nginx on the Pi with this command:
sudo apt-get install nginx
Now that nginx is installed, we need to configure it to serve requests for your domain name. Nginx's configuration on a Raspberry Pi is located in the /etc/nginx directory. The file we're interested in is called default and is in the sites-enabled directory. While you can edit it from the sites-available directory as well, it's a link to sites-enabled, so we can do either one. So let's open that up for editing in a terminal:
sudo nano /etc/nginx/sites-enabled/default
We want to create a server block to define the site that nginx will serve from your Raspberry Pi. Do that by adding this to the bottom of the file, right before the last closing brace (}):
server {
    listen 80;
    root /usr/share/nginx/www/;
    index index.html index.htm;
That is one of the simplest server blocks for nginx, and will serve files and pages located inroot to people that request on port 80 at your IP address. Replace with your domain name. Remember that you'll need to make sure your domain registrar is pointing your domain name at your IP address and that your ISP allows serving files on port 80. Once you save the new default file, you'll need to reload nginx's config with this command:
sudo nginx -s reload
I suggest creating a directory for you domain in your home folder and then creating a symklink for it to nginx's default webroot. Here's how I would do that for this site:
mkdir ~/
sudo ln -s /home/myusername/ \
Then you can create your index files for a static site within ~/ And that's all you need to install nginx on your Raspberry Pi.