Sunday, May 22, 2016

Raspberry Pi Server

I've been using a company that offers subversion (SVN) hosting, and it has been working very well. Subversion is an excellent way to manage code/resources. It keeps records of every change, notes on each change, and who committed the changes. Git is another, probably more popular, code/resource management system. Although Git has some technical advantages, especially with larger remote teams, I find it overly complicated when I just want to mess with my own projects. If you have ongoing coding projects, set yourself up with some kind of revision control such as Subversion or Git. Trust me!

As my projects have grown in size, the cost to host them remotely is also increasing. On top of that, I just want to have my Raspberry Pi do some useful server stuff for me anyways. So, I moved all my SVN hosting to my local Raspberry Pi. Then I thought - shoot a local FTP server would come in handy to transfer files, let me get that setup. I've got some projects that could be hosted on a website in the future, let me get that setup. It would be nice to remotely login to the Raspberry Pi so it no longer needs a keyboard/screen, let me get that setup.


There are tons of tutorials for each individual task online already, but at this point writing about my snowball of requirements may be something useful to others. So I'll do my best to recap what I did! (I was basically done by the time I thought others may find this helpful.)

I'm working with a Raspberry Pi B+. At the time I bought it, it was the highest performance Pi on the market. I'll probably buy a Raspberry Pi 3 fairly soon and do all the instructions all over again. (So did I write this to help others online? Or just so I wouldn't forget all the steps when I do it again in a few months? ... Can it be both?)

Quick list of what will be done:

1. Setup Network
2. Shell Access (SSH)
3. Version Control (SVN)
4. File Transfer (SFTP)
5. Web Server


My steps...

1 Setup Network

You may have your Raspberry Pi using DHCP (Pi asks your router for the IP address), but in my case my Pi will not necessarily be connected to my router. Only do the following steps if you want a static IP! If you can access the Internet right now with your Pi and you don't have a reason to change it, then you're probably good - skip this step.

To configure the network from a terminal window:
sudo nano /etc/network/interfaces


The lines with "lo" are for configuring the loopback.

To configure the ethernet device for static IP:
iface eth0 inet static
My local network is 10.0.1.### so I configured the IP address to something memorable and out of the way (high):
address 10.0.1.200

All computers on my local network are 10.0.1.###, so I configured the subnet to:
netmask 255.255.255.0

Broadcast to my local network:
broadcast 10.0.1.255

And the gateway set to:
gateway 10.0.1.1


In nano, hit CTRL X to exit, Y to agree to save, and then push enter to confirm the filename.

And restart the networking:
sudo ifdown eth0 && sudo ifup eth0

To see how the network is configured, type:
ifconfig


2 Shell Access (SSH)

I have no plans on using X Windows on the Raspberry Pi, so remote shell access is more than enough for me. I downloaded PuTTY as a SSH Client on my Windows computer so that I could login to my Raspberry Pi remotely.

To enable shell access on the Raspberry Pi, type:
sudo raspi-config



Then select "8 Advanced Options"


Then select "A4 SSH"


Then select "Enable"


The SSH server should be good to go!

I set my Pi to a static IP address of 10.0.1.200.

When I run PuTTY on my Windows computer:


And clicking Open...


No more keyboard and monitor required for the Pi!

3 Version Control (SVN)

To download and install the SVN software:
sudo apt-get install subversion

Then to create the folder for all the repositories to be held:
mkdir -p /home/pi/repos

Then create each repository:
svnadmin create /home/pi/repos/projectname
Where projectname is what your project is. I created a half dozen, including "arduinoComputer", "gps", and "orbitalAero".


Finally download and install Apache SVN:
sudo apt-get install apache2 libapache2-svn

Edit dav_svn.conf:
sudo nano /etc/apache2/mods-available/dav_svn.conf


The file has a bunch of entries almost ready to go, they're just commented out. I modified mine to look like:
<Location /svn>

  # Uncomment this to enable the repository
  DAV svn

  #Alternatively, use SVNParentPath if you have multiple repositories under
  # a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...)
  # You need either SVNPath and SVNParentPath, but not both.
  SVNParentPath /home/pi/repos

  # Basic Authentication is repository-wide. It is not secure unless
  # you are using https.  See the 'htpasswd' command to create and
  # manage the password file - and the documentation for the
  # 'auth_basic' and 'authn_fiel' modules, which you will need for this
  # (enable them with 'a2enmod').
  AuthType Basic
  AuthName "Subversion Repository"
  AuthUserFile /etc/apache2/dav_svn.passwd

  # Require a valid account to checkout.
  Require valid-user

</Location>

In nano, hit CTRL X to exit, Y to agree to save, and then push enter to confirm the filename.

Restart Apache:
sudo /etc/init.d/apache2 restart

Then change the permissions of the repository folder:
sudo chown -R www-data:www-data /home/pi/repos

Then create the user:
sudo htpasswd -c /etc/apache2/dav_svn.passwd <username>
where <username> is your login name.

You can navigate to the repository using your web browser.
In my case: 10.0.1.200/svn/gps

(This screenshot was taken after I committed the GPS and Libraries folders in the following steps.)

However, viewing in the web browser isn't very helpful. At this point I went back to my Windows computer. I use TortoiseSVN so I can have a nice GUI to interact with my repos.

I did a checkout of each of the new projects from the Raspberry Pi to my Windows computer.


I copied the latest code of each project into their corresponding new repository, then committed them into the Raspberry Pi.


Unfortunately I lost all my change logs up to that point, but that's ok.

By the way - take a look at the Tortoise Log screenshot above. It has a record of every commit to the repository, who did it, the date, and all the files that have changed. If you double click on one of the files, it shows you all the changes that were made. If you went down a rabbit hole with your code and decided it's a disaster, you can revert back to any prevision revision. If you see a weird change to a file and are wondering who / what / when / why it happened, you can do that by looking at the log! You definitely want to use some kind of version control such as SVN or Git or similar!

4 File Transfer (SFTP)

I want easy file transfer access to my Pi for sharing files / backup purposes. SFTP (SSH File Transfer Protocol) is turn-key once you have SSH going!

Both WinSCP and FileZilla work very well for this purpose.

I created a Pi site with IP address 10.0.1.200


And connected!


Now I can drag files / folders between my workstation and the Pi server. To modify some files I have to log into the Pi as root (or change the folder's permissions).

5 Web Server

Apache was already installed in the SVN steps above, but if you skipped them, then from a terminal window type:
sudo apt-get install apache2

The default folder for the website is /var/www


And when opened in a web browser:


One of my projects will be taking advantage of this web server soon*!

* Soon is a very relative term. ;-)

A few handy functions when dealing with Apache...

To stop Apache (SVN and website):
sudo /etc/init.d/apache2 stop

To start Apache (SVN and website):
sudo /etc/init.d/apache2 start

To restart Apache (SVN and website):
sudo /etc/init.d/apache2 restart



No comments:

Post a Comment