Apache Subversion SSL & Virtual Hosts on Ubuntu

Friday September 28thUbuntu Category

Subversion - Version Control System
Configuring Subversion and Apache on Ubuntu for virtual hosts with (SSL) Secure Socket Layer is fairly straight forward but if you’ve installed Apache 2 with synaptic you will find that some of the tools have different names or are completely missing, here I will walk through each step of the installation and configuration of each part explaining what does what and the various options that you can put in place to limit access. This way we will have access to the subversion repository at https://svn.domain.com.

Once you have followed the tutorial I would recommend using RapidSvn and Meld or KDiff as they are two of the tool I regularly use with my repositories. RapidSvn is a GUI subversion tools so you can commit etc. It doesn’t have a visual diff tool which is unfortunate but Meld is the best visual tool I have found for Gnome, these in the Ubuntu repositories or if you are using Kbuntu you can use KDiff, you can install these two tools easily using synaptic or apt-get.In this tutorial we will cover installing Apache2, Subversion and configuring the SSL creation of the required certificate using the apache tools and the configuration of the Virtual Host so you can access the repository via a subdomain. Finally we will set the access so you can controll who will have acces to your repositories.With 11 steps you will have a subversion respository up an running with SSL and Virtual hosts and each step explains what is happening so you can easily understand the full process

1. Install Apache
In the terminal type:


sudo apt-get install apache2 apache2.2-common apache2-utils

		

This installs the apache 2 server common modules and utilities

2. Install Subversion
In the terminal type:


sudo apt-get install subversion subversion-tools

		

3. Install Apache Subversion Modules
In the terminal :


sudo apt-get install libapache2-svn

		

4. Restart Apache
In the terminal type:


sudo apache2ctl restart

		

5. Enable SSL Apache Module
In the terminal type:


sudo a2enmod ssl

		

6. Enable Apache to listen to the correct port for ssl (443)
In the terminal type:


sudo gedit /etc/apache2/ports.conf

		

Add the line: Listen 443

7. Create a certificate for SSL use.
Unfortunately apache is missing the tool (apache2-ssl-certificate) required to create the certificate but this can be easily downloaded from apache2-ssl.tar.gz , download this file and extract the package. There are two files ssleay.cnf and apache2-ssl-certificate. In the terminal navigate to the directory two files have been extracted and type:


sudo mkdir /etc/apache2/sslsudo cp ./ssleany.cnf  /etc/apache2/ssl/sudo cp ./apache2-ssl-certificate /usr/sbin/

		

Now create your certificate with and follow the instructions


sudo apache2-ssl-certificate

		

8. Create a Subversion repository

Here make a directory where you want to store one or more subversion repositories, in this example I’m using /srv/svn/repos/


sudo mkdir  /srv
sudo mkdir  /srv/svn
sudo mkdir  /srv/svn/repos

		

Now make the repository directory accessible to apache (www-data)


chown www-data:www-data /srv/svn/repos

		

Now we will make the first repository using the super user www-data


cd /srv/svn/repos
su -u www-data -s
svnadmin create projectname

We now have our first subversion repository remember when creating other repositories always use the su www-data to ensure apache can access the repository.

9. Creating the virtual host

Create the virtul host file for Apache


sudo cp /etc/apache2/sites-available/default  /etc/apache2/sites-available/svn.domain.comsudo gedit  /etc/apache2/sites-available/svn.domain.com

Copy and paste the following code edit the domain name and repository path if different:


NameVirtualHost *:443
<VirtualHost *:443>
  ServerAdmin yourname@domain.com
  ServerName svn.domain.com
  SSLEngine On
  SSLCertificateFile /etc/apache2/ssl/apache.pem
  SSLProtocol all
  SSLCipherSuite HIGH:MEDIUM
  <Location />
    Order allow,deny
    Allow from all
    DAV svn
    SVNPath /srv/svn/repos/projectname
    AuthType Basic
    AuthName "domain.com Subversion Repository"
    AuthUserFile /etc/apache2/dav_svn.passwd
    Require valid-user
  </Location>
  ErrorLog /var/log/apache2/error.log

  # Possible values include: debug, info, notice, warn, error, crit, alert, emerg.
  LogLevel warn CustomLog /var/log/apache2/access.log combined
</VirtualHost>

If you have many projects that you will be using the repository change


SVNPath /srv/svn/repos/project

to


SVNParentPath /srv/svn/repos

10. Create user(s) to access subversion repository


sudo htpasswd -c /etc/apache2/dav_svn.passwd username

to add more users use -m (the -c creates a new file)


sudo htpasswd -m /etc/apache2/dav_svn.passwd username2

11. Restart Apache and test


sudo /etc/init.d/apache2 restart

Now test your repostory with https://svn.domain.com and enjoy, if I’ve missed steps out please let me know in the comments.

Using the Code Igniter Validation Class to set default form values

Monday July 23rdCodeIgniter Category

Quite often when your using CodeIgniter Validation Class you require forms that have validation and default values for example a users profile. Unfortunately this is cannot be handled by this class but with extending the validation class can resolve this issue simply and effectively.

By extending the the validation class it is possible to set the default values. The Validation class takes the values to display from the $_POST variable but for the inital page load these are empty. Also dependingt on the form input type the values are collected at different times. For example an input field is taken at the call to set_field but checkboxes etc have their values reread at the call to set_checkbox. To easily account for both these cases the $_POST variables must have the values to use as the default values set after the call to set_fields.

So now to the practical part. Sub classing the Code Igniter Validation class. Create a file application/libraries/My_Validation.php if the folder is not present create it. Then copy the following code:


01.   <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
02.
03.   class MY_Validation extends CI_Validation {
04.
05.       function My_Validation()
06.       {
07.           parent::CI_Validation();
08.       }
09.
10.       function set_default_values($data, $value = null)
11.       {
12.           if (is_array($data) == TRUE)
13.           {
14.               foreach($data as $field => $value)
15.               {
16.                   $this->$field   = $value;
17.                   $_POST[$field]  = $value;
18.               }
19.           }
20.           else
21.           {
22.               $this->$data    = $value;
23.               $_POST[$data]   = $value;
24.           }
25.       }
26.   }
27.   ?>

This new function can be called with:


01.  $this->validation->set_default_values('fieldname', 'value');

or

01.  $data = array ('fieldname'=>'value');
02.  $this->validation->set_default_values($data);

or using a model

01.  $query = $this->some_model->read();
02.  if($query->num_rows() > 0)
03.  {
04.       $this->validation->set_default_values($query->row_query());
05.  }

That’s basically it, remember to call teh set_default_values after the call to $this->validation->set_fields() if there are no values stored in $_POST set the default values required. An example has been included to show the functionality.

HTML:

01.  <?=form_open('/form');?>
02.  <div>
03.    Text:
04.    <input type="text" name="text" value="<?=$this->validation->text;?>" id="text" />
05.  </div>
06.  <div>
07.    Selection:
08.    <select id="select" name="select">
08.      <option value="select1" <?= $this->validation->set_select('select', 'select1');?> >select1</option>
10.      <option value="select2" <?= $this->validation->set_select('select', 'select2');?> >select2</option>
11.      <option value="select3" <?= $this->validation->set_select('select', 'select3');?> >select3</option>
12.    </select>
13.  </div>
14.  <div>
15.    <input type="checkbox" name="check" value="check1" <?= $this->validation->set_checkbox('check', 'check1'); ?> />
16.    <input type="checkbox" name="check" value="check2" <?= $this->validation->set_checkbox('check', 'check2'); ?> />
17.    <input type="checkbox" name="check" value="check3" <?= $this->validation->set_checkbox('check', 'check3'); ?> />
18.  </div>
19.  <div>
20.    <input type="radio" name="radio" value="1" <?= $this->validation->set_radio('radio', 'radio1'); ?> />
21.    <input type="radio" name="radio" value="2" <?= $this->validation->set_radio('radio', 'radio2'); ?> />
22.    <input type="radio" name="radio" value="3" <?= $this->validation->set_radio('radio', 'radio3'); ?> />
23.  </div>
24.  <div>
25.    <button type="submit">Update</button>
26.  </div>
27.  </form>

Controller

01.  <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
02.
03.  class Form extends Controller {
04.
05.      function index()
06.      {
07.         $this->load->library('validation');
08.         $this->load->helper('form');
09.
10.         $rules['text'] = ‘required’;
11.  	    $rules['option'] = ‘required’;
12.  	    $rules['select'] = ‘required’;
13.  	    $rules['radio'] = ‘required’;
14.  	    $this->validation->set_rules($rules);
15.
16.         $fields['text'] = ‘text field’;
17.  	    $fields['select'] = ’select field’;
18.  	    $fields['check'] = ‘check box field’;
19.  	    $fields['radio'] = ‘radio field’;
20.  	    $this->validation->set_fields($fields);
21.
22.  	    if (count($_POST) == 0)
23.  	    {  // For initial load set defualt values
24.  		$this->validation->set_default_values(’text’, ‘hello world’);
25.  		$this->validation->set_default_values(’select’, ’select2′);
26.  		$this->validation->set_default_values(’check’, ‘check2′);
27.  		$this->validation->set_default_values(’radio’, ‘radio3′);
28.  	    }
29.
30.         if ($this->validation->run() == FALSE)
31.  	    {
32.  		$this->load->view(’form’);
33.  	    }
34.  	    else
35.  	    {
36.  		$this->load->view(’form’);
37.  	    }
38.      }
39.
40.  }
41.  ?>

Code Igniter Release 1.5.4

Monday July 16thCodeIgniter Category

EllisLabs has released a new version CodeIgniter 1.5.4, this is mainly another maintenance release with a large focus on improving the security. Great to see a new version ready to use, congrats to the Ellis Labs team for this release of Code Igniter and look forward to other more feature based releases as the new version of Expression Engine was released last month..

For a full list of the changes see the Change Log.

Size

Colors