Setup A Private Git Server on Debian 7

Introduction

This tutorial will show you how to configure a centralized Git server as an alternative to using third-party services, such as Github or Bitbucket. Running your own server is likely to be cheaper and safer. It is "safer" is because large sites, such as Github, are a bigger target for hackers. Also, you know exactly how your server is configured and where your data is stored.

Why Git?

It seems to me that the primary reason to use Git is that everyone else is using it. Thus, it is the most likely tool to be easily integrated into other tools/services as they arise such as composer/satis. The prevalence of git also means that it's very easy to start working with others with one less thing to agree on (people can get very passionate about which tools they use). In terms of version control itself, I think that most people are not investing enough time into studying the alternatives, such as SVN or Mercurial.

Why Debian?

I wanted to use a distro that I wouldn't need to update and reboot every other week, like I do with Ubuntu. I have stopped "supporting" CentOS ever since version 7, and Debian is incredibly easy to work with, coming from a heavy Ubuntu user's perspective.

Server Steps

    Add a git user to the server that will own the repositories. Every time that a developer wants to commit to this centralized repository, they will need to authenticate with this user and it's password. However, it will not be the name that appears on the git logs, that will be the name that the user set with
    git config user.name "Developers Name"
    sudo adduser $GIT_USER
    # fill in the details and use a strong password.
    su $GIT_USER
    cd $HOME
    
    A better system would be to have a user for each developer who is going to work on the project. Thus each developer has their own username and password to authenticate with. This also allows some users to only have read access, rather than write, which is useful for automated deployments. This also makes removing a developer from the project much easier.
    I will show you how to do this in another tutorial and link to it when it becomes available.
    Install Git
    sudo apt-get install git -y
    Optionally, create a folder we are going to store all our repositories inside of. If you created a user just for git in step 1, then I recommend that you don't bother doing this, and just stick all the repos in the git user's $HOME
    mkdir repos
    cd repos
    
    Create a repo
    mkdir $REPO_NAME
    cd $REPO_NAME
    git init --bare
    
    "A bare repository usually serves as a point of coordination for a centralized workflow, in which several people push and pull from that repository rather than directly among themselves; no one works with the bare copy directly." [Git Pocket Guide ]

Client Steps

    Navigate to where we want to store the code and clone the repository
    cd /path/to/stick/repo
    git clone $GIT_USER@$SERVER_IP/path/to/repos/$REPO_NAME
    
    Congratulations! You now have a local copy of the repository.
    Here is an example of how to add a file, commit it locally, and then push all the changes that have occurred to the centralized repository.
    touch README.txt
    git add README
    git commit . -m "My commit description goes here"
    git push $GIT_USER@$SERVER_IP:/path/to/repos/$REPO_NAME
    
    It is a good idea to "push" to the centralized repository frequently to avoid merging issues, but you don't have to perform a "push" after each commit. However, the changes are only available for others to use when they have been pushed.

Symfony2 - Creating a REST API - Part 1

Introduction

This tutorial aims to get someone who is completely new to Symfony2 setup with a very basic REST API with Symfony, from scratch, as quickly and as simply as possible.

The majority of this tutorial is based on the video below, which I found to be incredibly useful, but far too quick to follow which resulted in me creating this tutorial. This tutorial will also be kept up-to-date and fix some of the issues that have since arisen due to updates.

Steps

Defines

Firstly, register some variables in your shell that we will use throughout this tutorial

# Specify a name for your project
PROJECT_NAME="test_project"

# This should be the name of your organization, or your personal pseudonym
USERNAME="Programster"

# This must end with "Bundle" and use Camelcase
BUNDLE_NAME="DemoBundle"

Run the following commands to create a project

mkdir $PROJECT_NAME
cd $PROJECT_NAME
curl -s https://getcomposer.org/installer | php
sudo php composer.phar create-project symfony/framework-standard-edition ./$PROJECT_NAME '2.5.*'
We use sudo in the command above to removes the possible ErrorException
mkdir: permission denied

Answer the Questions

At this point it will ask you a series of questions. I elected to NOT install the Acme demo bundle and use the mysqli database_driver. When the secret option comes up, make sure to create something new and write it down for now

sudo chown -R $USER:$USER $PROJECT_NAME
cd ./$PROJECT_NAME

Run the following commands to include some relevant bundles

composer require jms/serializer-bundle @stable
composer require friendsofsymfony/rest-bundle @stable
Those commands will automatically add the corresponding lines to the composer.json file.

Create A New Bundle

Run the following command to create a new bundle

php app/console generate:bundle
$USERNAME/$BUNDLE_NAME
You will not be able to use the variable names directly, but have to manually convert them into the corresponding strings for entry!

Press return three times to use the defaults for the questions that come next.

Bundle Namespace:
Bundle name: 
Target directory:
For configuration format, I chose php. Then choose no to generating the whole directory structure and choose yes to confirm generation. Confirm automatic updating of your kernel and routing.

Register the bundles with Symfony.

editor app/AppKernel.php
Add the following two lines to the bundles array
            new JMS\SerializerBundle\JMSSerializerBundle(),
            new FOS\RestBundle\FOSRestBundle(),

Your file should now look similar to the one below:

Create A User Entity

cd src/$USERNAME/$BUNDLE_NAME
../../../app/console doctrine:generate:entity

Answer the Questions:

  • The Entity shortcut name: $USERNAME$BUNDLE_NAME:User
  • accept annotation as the configuration format
  • Add the following fields
  • username string 255
  • password string 255
  • email string 255
  • Press return to stop entering fields
  • Press return to use "no" for "Do you want to generate an empty repository class"
  • Press return to confirm generation.

Define Configurations

Run the following command to append to the end of your config.yml file.
echo "
sensio_framework_extra:
    view: { annotations: false }
    router: { annotations: true }

fos_rest:
    format_listener:
        rules:
            - prefer_extension: false
    view:
        view_response_listener: true
" >> ../../../app/config/config.yml
Please note that this is different from what is shown in the video. This was taken from a stack overflow post.

Define Our First Controller

editor ../../../app/config/routing.yml
Add the following:
users:
    type: rest
    resource: $USERNAME\$BUNDLE_NAME\Controller\UsersController
editor Controller/UsersController.php
<?php

namespace $USERNAME\$BUNDLE_NAME\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;

class UsersController extends Controller
{
    public function getUsersAction()
    {
        $users = $this->getDoctrine()->getRepository('$USERNAME$BUNDLE_NAME:User')
                    ->findAll();
        return array('users' => $users);
    }
}

Test It's Working

Run the following command to check that all of your routes/configurations are set up correctly with the following command:
../../../app/console router:debug
You should see output similar to below:

That concludes the first part of the tutorial. I will add a link to the second part as it becomes available.

References

Debian 7 - Add Sudo!

When creating a fresh installation of Debian 7, you may notice that your sudo commands wont work. Run the following steps to allow sudo to work from a non-root user.

Steps

    Login as root and install sudo capabilities:
    apt-get install sudo
    Now add the user you want to have sudo priveleges
    adduser $MY_SUBUSER sudo
    
    For example:
    adduser programster sudo
    

    You can now log in as that user and run updates etc by just entering your own password.