Home » Blog » Web Server

Category: Web Server

indigo-magician

How To Use Rsync With A Raspberry Pi Web Server

I spent the better part of an evening figuring out how to make this work and thought I would share a video I created detailing how you can use rsync with your Raspberry Pi.

I am using rsync to move files from my laptop to my Raspberry Pi web server.

I had to string together 3 tutorials and will detail all my steps in this video below.

Here is the quick version

You will need to change the Raspberry Pi web server’s folder permissions to belong to the user who is rysncing the files.

You can change those permissions by issuing the following command

sudo chown -R youruser:www-data /var/www

You must do this if you are running Raspbian (Jessie) because Pi, the default user, is locked down from SSH which is what rsync uses. It is advisable to great a NEW user and not use Pi when doing SSH commands. If you need help creating a new user check out the video above.

Once the file permissions are changed you can rsync from your local setup to your Raspberry Pi web server with this command.

rsync -a ~/Projects/ExampleSite raspibakery@192.180.90.3:/var/www/html

I ran into issues with trailing slashes so leave them off.

You will be prompted for your new user’s password and if everything works you can now check http://localhost and see your changes.

amber-code

How To Install Nginx On A Raspberry Pi Running Jessie

I have installed a web server on my Raspberry Pi before. But when I went to do it this last time I noticed a few things have changed.

This post will walk you through some of the hiccups I had when following the official Raspberry Pi documentation on how to install Nginx on a raspberry pi running Jessie.

Here is the official documentation. I would suggest following along with these steps.

First things first, installing Nginx

It’s pretty straightforward to install Nginx on a Raspberry Pi…simply type in the following command,

sudo apt-get install nginx

The first hiccup I had was apt-get returned an error suggesting I update my package manager.

So I simply typed

sudo apt-get update

After a few minutes, I was able to execute the first command with no problems (you might not have to do this).

To start the Nginx server on your Raspberry Pi simply type

sudo /etc/init.d/nginx start

You can now test that your server is installed by visiting http://localhost/ in your browser.

If you want to just run static HTML files you can stop here.

However, if you want to install PHP which gives you the ability to run a scripting language and write more dynamic websites, you will need to install a few more things.

Installing PHP on a Raspberry Pi webserver

The command you are looking for is…

sudo apt-get install php5-fpm

Time to configure some settings

Navigate to cd /etc/nginx

In this file, I notice some discrepancies. I notice that what I had as a default looked very different then what the documentation we telling me.

I tried a few times to get it to work but kept getting the following error.

Reloading nginx configuration (via systemctl): nginx.serviceJob for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.
 failed! 

Finally I just copied everything that was “suppose” to be uncommented in and reloaded the file and it worked.

So first things first.

We need to tell the server that we want the index file with the .php extension to take preference over those that are HTML.

Around line 25 you will need to add the index.php to the list so it looks like this

index index.php index.html index.htm;

Now, we need to uncomment some options. This is what I was talking about when I said it looked different. I just copied and replaced the section of options with what the documentation said I should have.

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }

This may not be a best practice but I was fed up with Googling error messages and looking in logs.

Finally we can reload this file

sudo /etc/init.d/nginx reload

Here is where things got fuzzy for me

The documentation tells us to change the index.html file to index.php to test if PHP files are being served correctly by the web server.

To do that we can navigate to the path below…

cd /usr/share/nginx/www/

And then change the file extension

sudo mv index.html index.php

I made a change to the file…went to http://localhost and didn’t see it.

I looked back in my terminal and saw that I didn’t have permissions to write to the /www folder.

I started Googling what I need to do to change the folder permissions but wasn’t sure of the right answer (by the way I am not using root as it’s advised to create a new user in Jessie).

However, I remember from a while ago that you can access the webserver files by going to the following path

/var/www/html

When I make a change to this index.php file I see the change on localhost as expected.

I am still looking into this but if someone could share thoughts on what I am missing it would be greatly apprecaited.

Here is my final test showing that PHP is running and the change to the file works.

hello-world