Archive for the 'Ubuntu How To' Category

How to install gitosis on Ubuntu

In the previous post we went over installing git on Ubuntu, we are now installing a package called gitosis so we can host git repositories.
First, make sure you have python-setuptools package installed since gitosis needs python. Open the terminal window and type:

sudo apt-get install python-setuptools

Now we need to get gitosis, type these commands one after another to create src folder in your user directory, clone the source and install it:


cd ~/
sudo mkdir src
cd src/
sudo git clone git://eagain.net/gitosis.git
cd gitosis/
sudo python setup.py install

Next, lets create a user that will own the repositories.


sudo adduser \
--system \
--shell /bin/sh \
--gecos 'git version control' \
--group \
--disabled-password \
--home /home/git \
git

Now we need a public ssh key, we’ll create a local one, it will ask you for a passphrase, enter the one you like:


ssh-keygen -t rsa

Now we need to let the git user use this key:


cp ~/.ssh/id_rsa.pub /tmp/
sudo -H -u git gitosis-init < /tmp/id_rsa.pub

Now lets change the permission on on post-update hook:


sudo chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update

Now lets clone gitosis-admin repository (you can get the hostname by typing hostname command in the terminal), navigate to the folder you want the repository to reside in and type:


sudo git clone git@YOUR_SERVER_NAME:gitosis-admin.git

That’s it, you got a local gitosis-admin repo with a config file and a keydir directory.

I will post how to use git and will try to install gitweb (convenient web interface) next.

Edit: I wrote another post on how to install gitweb in Ubuntu if you wish to move on to the next step. 🙂

How to install git on Ubuntu

As I finished installing svn server, I was given and advice to use git instead. So I started to look into it and decided to give it a try. This post is a start of a process.

You need git first. So, lets start installing it. Open the terminal and type:


sudo apt-get install git-core

Thats’s it. Short and up to the point. You can now check the version to make sure it is installed:


git --version

At the time of writing I have 1.7.1.4 🙂

In case you need a server, move on to the next point – installing gitosis on Ubuntu.

svn post-commit hook in Ubuntu

So, I’ve been messing around the scripts in Ubuntu. It’s fun… 2 am here and I finally got the post-commit hook to work the way I wanted it to. Had to learn quite a bit about bash scripts and php calling the bash scripts. 🙂

As I started in previous post – adding svn hooks in Ubuntu, this is a part 2 of that post. I am using SVNManager, and loading these hooks in the same /repo-templates folder I created in previous post.

To start, remove previous post-commit file from the folder if you created one as I made a new one. Then, lets create the files and make them executable:


sudo touch post-commit
sudo touch post-commit-email
sudo touch post-commit-send
sudo chmod +x post-commit post-commit-email post-commit-send

Lets start editing the post-commit file first:


sudo gedit post-commit

Here is what I got:

#!/bin/sh
 
REPOS="$1"
REV="$2"

$REPOS/hooks/post-commit-email $REPOS $REV

Next, I created post-commit-email file in php, change the path to php if needed (to get the path, type ‘which php’ in terminal). Also, make sure you get the right database connection and password:

sudo gedit post-commit-email

#!/usr/bin/php
 $val) {
  if($flag) {
	$emails .= ',';
  }
  $emails .= $val;
  $flag = 1;
}

mysql_close($con);

exec($repos.'/hooks/post-commit-send '.$repos.' '.$rev.' '.$repos_name.' '.$emails.'');

?>

At last, post-commit-send file:


sudo gedit post-commit-send

#!/bin/sh

REPOS="$1"
REV="$2"
REPOSNAME="$3"
SENDTO="$4"
SENDFROM=root@localhost

LIMITDIFF=200
CHANGELOG=`svnlook log -r $REV $REPOS`
AUTHOR=`svnlook author -r $REV $REPOS`
CHANGED=`svnlook changed -r $REV $REPOS`
DIFF=`svnlook diff -r $REV $REPOS | head --lines=$LIMITDIFF`
DATE=`date`
TMPFILE=/tmp/svn$REV-$RANDOM.message
	 
SUBJECT="New SVN Commit By $AUTHOR On $REPOSNAME, Revision #$REV"
echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOSNAME
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE
	 
-----------------------------------------------------------------
Log Message:
-----------------------------------------------------------------
$CHANGELOG
	 
-----------------------------------------------------------------
Changes:
-----------------------------------------------------------------
$CHANGED
 
-----------------------------------------------------------------
Diff: (only first $LIMITDIFF lines shown)
-----------------------------------------------------------------
$DIFF
" > $TMPFILE

#echo $SENDTO >> $REPOS/hooks/error_log 
	 
# Send email
cat $TMPFILE | mail -r "$SENDFROM" -s "$SUBJECT" "$SENDTO"
 
# Cleanup
rm $TMPFILE

Change the email if needed and make sure that the backtick character is correct as it may change when you copy from this page. I got the last file from this post and modified it a bit.

Let me know if this worked for you. 🙂

How to add svn hooks with SVNManager in Ubuntu

So, once we set up our svn and SVNManager in Ubuntu, it would probably be nice to get emails every time a user in the repository makes a commit.
Svnmanager supports the post create script, which will be fired every time a new repository gets created. Lets start:

sudo gedit /var/www/svnmanager/config.php

at the end of the config, uncomment and edit the line to:

$post_create_script = "/bin/bash /var/www/newrepo-script";

Now lets create the newrepo-script:


$post_create_script = "/bin/bash /var/www/newrepo-script";

And add these lines:

#!/bin/bash
 
cp -R /var/www/repo-templates/* $1/hooks/

which pretty much just tells to copy the content of the templates folder to the newly created repository’s /hooks/ directory. /hooks directory will execute the hooks that we place there on each event that we create. We will add post-commit hook for this example.


cd /var/www
sudo mkdir repo-templates

now the hook


sudo gedit post-commit

Add these lines and save:

#!/bin/sh
 
REPOS="$1"
REV="$2"

/usr/bin/svnnotify                    \
    --repos-path    "$REPOS"                \
    --revision      "$REV"                  \
    --subject-cx                            \
    --with-diff                             \
    --handler       HTML::ColorDiff         \
    --to            to_email@email.com   \
    --from          root@localhost

Feel free to edit the email addresses. Now we need to make the script executable:


sudo chmod +x post-commit

Make sure that you have svnnotify package. Type:

which svnnotify

and if it shows the path, make sure it is the same as we made in post-commit file, if not, edit post-commit file. If path doesn’t show, chances are you don’t have the package installed. Install it by:


sudo apt-get install libsvn-notify-perl

This should do it. If you want to send the emails to every user in the repository, you may have to edit the hook. I found another good tutorial about it here.

Update: I created the hook the way I wanted it, for complete code visit this page – svn post-commit hook in Ubuntu.

How to install postfix to relay mail through gmail in Ubuntu

Once I got my svnmanager working on Ubuntu, I still had problems sending mail. I pointed the domain mx record to the right direction, but for some reason my port 25 was refusing connections. So I decided to relay the mail through my gmail account.

First, install postfix if you haven’t already:


sudo apt-get install postfix

Now, to set up postfix, make sure that you have these files:

/etc/postfix/main.cf

/etc/postfix/generic

/etc/postfix/generic.db

/etc/postfix/sasl/passwd

/etc/postfix/sasl/passwd.db

which you can create via touch command like:

 

cd /etc/postfix
sudo touch generic

Next, edit /etc/postfix/main.cf (via command: sudo gedit /etc/postfix/main.cf), here is mine

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

#readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
#myorigin = /etc/mailname
mydestination = my-ubuntu, localhost.localdomain, localhost
relayhost = [smtp.gmail.com]:587
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
inet_protocols = all

##########################################
##### non debconf entries start here #####

##### client TLS parameters #####
smtp_tls_loglevel=1
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl/passwd
smtp_sasl_security_options = noanonymous

##### map username@localhost to username@gmail.com #####
smtp_generic_maps=hash:/etc/postfix/generic

Next, edit /etc/postfix/generic


sudo gedit /etc/postfix/generic

and have replace user_name and email_address with your credentials, add all users that may send email through gmail:


user_name@localhost email_address@gmail.com
root@localhost email_address@gmail.com
svnmanager@192.168.1.5 email_address@gmail.com

Now lets generate generic.db from this file:


cd /etc/postfix
sudo postmap generic

Next, lets add your gmail credentials to password file:


cd /etc/postfix/sasl
sudo gedit passwd

Copy this, replace email_address with your gmail email username and gmail_password with your gmail password:


[smtp.gmail.com]:587 email_address@gmail.com:gmail_password

Save the file, lets generate passwd.db now:


cd /etc/postfix/sasl
sudo postmap passwd
chown root.root passwd passwd.db
chmod 600 passwd passwd.db

Restast postfix:


/etc/init.d/postfix restart

Test (replace user_name with your user name):


echo 'test email' | mail -s 'just a test' email_address@gmail.com
sudo sendmail -bv user_name
sudo sendmail -bv email_address@gmail.com

You should receive the email now, sent from your localhost.

If there are any problems, check you /var/log/mail.log

How to install svn for apache and svnmanager on Ubuntu

This tutorial will guide through installation of subversion on Ubuntu to work with apache, and then installing svnmanager. Svnmanager is a handy tool that lets you manage the users and repositories through the web interface. Before we begin, make sure you have a lamp server setup on Ubuntu.

First, lets install subversion and apache library:


sudo apt-get install subversion libapache2-svn

Lets create the directories in which svn repositories and configurations will reside and give apache access to them:


cd /srv
sudo mkdir svn
sudo mkdir svnconfig
cd svn
sudo mkdir repos
sudo chown -R www-data.www-data /srv/svnconfig /srv/svn

Next, we need to set up apache and allow access to subversion repository:


sudo gedit /etc/apache2/mods-enabled/dav_svn.conf

Uncomment the lines like below, file names will be created at a later stage by svnmanager:

 


# Uncomment this to enable the repository
DAV svn

# Alternatively, use SVNParentPath if you have multiple repositories under
# under a single directory (/var/lib/svn/repo1, /var/lib/svn/repo2, ...).
# You need either SVNPath and SVNParentPath, but not both.
SVNParentPath /srv/svn/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_file' modules, which you will need for this
# (enable them with 'a2enmod').
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /srv/svn/htpasswd

# The following three lines allow anonymous read, but make
# committers authenticate themselves. It requires the 'authz_user'
# module (enable it with 'a2enmod').
Require valid-user

# To enable authorization via mod_authz_svn
AuthzSVNAccessFile /srv/svn/accessfile

 

Lets restart Apache:


sudo /etc/init.d/apache2 restart

Now lets create a new user in MySql for svnmanager. Get to mysql client using this command:


sudo mysql -u root -p

Type in your password and execute this mysql command:


CREATE DATABASE svnmanager;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER on svnmanager.* to svnmanager identified by 'password';
FLUSH PRIVILEGES;

Where password should be replaced by your password.

Next, if you followed my tutorial on installing lamp, you may need to add php-pear as it was not added previously. Got to the terminal and type:


sudo apt-get install php-pear

Next, lets install the pear VersionControl_SVN module, type:


sudo pear install -f -o VersionControl_SVN

Now we are ready to install SVNManager. Download the latest package, I’ll get mine from http://svnmanager.org, at the time of writing the latest is 1.08. The file itself is at http://prdownloads.sourceforge.net/svnmanager/svnmanager-1.08.tar.gz I am going to download the tar.gz one in my Apache web directory which is /var/www/ :


cd /var/www/
sudo wget http://prdownloads.sourceforge.net/svnmanager/svnmanager-1.08.tar.gz

Now lets untar (Ubuntu tar tutorial) it:


sudo tar xzf svnmanager-1.08.tar.gz

I will rename the folder for convenience:


sudo mv svnmanager-1.08 svnmanager

Now the fun part, lets copy and open the configuration file:


cd svnmanager
sudo cp config.php.linux config.php
sudo gedit config.php

Here you can start changing the language and htpasswd locations if you need. Since I have default installation and everything seems correct, I move to the next lines for svn locations:


$svn_config_dir = "/srv/svnconfig";
$svn_repos_loc = "/srv/svn/repos";
$svn_passwd_file = "/srv/svn/htpasswd";
$svn_access_file = "/srv/svn/accessfile";
$svn_trash_loc = "/srv/svn/trash";

Now if you have smtp server, you need to specify it in order for email notifications to function, which is a pretty nice feature in svnmanger. To intall smtp server just for the sake of experiment, you can just type ‘sudo apt-get install postfix’ and leave it with no configuration option. You can reconfigure it later with ‘sudo dpkg-reconfigure postfix’.


$smtp_server = "yoursmatpserverurl";

Next, database configuration, replace ‘yourpassword’ with password you made for svnmanager user:


$dsn = "mysqli://svnmanager:yourpassword@localhost/svnmanager";

Now save the file and restart Apache:


sudo /etc/init.d/apache2 restart

Go to http://localhost/svnmanager/ and you should see the message that the tables have been created. Reload the page and login with ‘admin’ ‘admin’, which we left untouched in the configuration file. Don’t worry, once you login and create admin user, config credentials will not work. Go to create your new user, set him as admin and type ‘admin’ in the lowest ‘password’ box, which is your current default password.

You are now set with SVNManager, which, I believe, significantly helps to manage multiple users and repositories.

Phew, took me forever to write this post as I was installing svn and svnmanager at the same time. Please let me know how it works for you. 🙂

Update: once you create a repository, try to access it in browser at http://localhost/svn/your_repository_name/ . If it shows 500 Internal Server Error, you may have to go back and edit /etc/apache2/mods-available/dav_svn.conf and change the line AuthUserFile to:

AuthUserFile /srv/svn/passwdfile

since the file htpasswd may not exist and cause this error to happen. Just checked my apache logs and this was the problem.

 

Another update: I did a postfix install on Ubuntu to send mail and updates to repositories to the needed users (handy feature). However, I had problems with svnmanager sending the mail. I checked my /var/log/mail.log and was getting:

Aug 26 13:02:00 my-ubuntu postfix/smtpd[13809]: connect from localhost[127.0.0.1]
Aug 26 13:02:00 my-ubuntu postfix/smtpd[13809]: warning: Illegal address syntax from localhost[127.0.0.1] in MAIL command: 
Aug 26 13:02:00 my-ubuntu postfix/smtpd[13809]: lost connection after RSET from localhost[127.0.0.1]
Aug 26 13:02:00 my-ubuntu postfix/smtpd[13809]: disconnect from localhost[127.0.0.1]

The problem seemed to be that my hostname was an ip address, which needs to be in square brackets like svnmanager@[192.168.1.5]. Then I edited the mail functionality:

/var/www/svnmanager/svnmanager/UserModule$ sudo gedit InvitePage.php

where you can edit $mail->From = “svnmanager@[$servername]”; by adding those square brackets. Also, you can edit the $message if needed.
Also, may need to edit InviteManagePage.php in the same folder to suit your needs.

How to install apache, php, mysql and phpmyadmin in Ubuntu

Installing LAMP (Linux, Apache, Mysql, Php) in Ubuntu is pretty easy. Follow the following steps.

 

Open the terminal window and type:


sudo apt-get install apache2

This would install apache, which you could test by typing http://localhost/ in the browser. It will give you a successful message if it works.


sudo apt-get install php5 libapache2-mod-php5

This would install the latest stable version of php5 and apache mod. Now lets create a sample php file to see if it works:


sudo gedit /var/www/phpinfo.php

Once the new gedit window opens, type:



Save the file and restart apache for php to kick in:


sudo /etc/init.d/apache2 restart

Now go to http://localhost/phpinfo.php in the browser and it should show all the php settings.

We still need mysql, these commands will install it:


sudo apt-get install mysql-server

Next, we need apache mode, php5-mysql and phpmyadmin (if you want, it gets pretty handy):


sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

Next, enable mysql in for php by editing php.ini file:


sudo gedit /etc/php5/apache2/php.ini

and uncomment the line


;extension=msql.so

by removing ‘;’ symbol in the beginning. Save the file and restart apache again:


sudo /etc/init.d/apache2 restart

Now navigate to http://localhost/phpmyadmin in your browser to start setting up the databases.

That’s it.

Using tar in Ubuntu

tar is pretty common command for archiving the files in Linux. It is pretty much the same across the distributions. You can type in the commands listed below in the Ubuntu terminal window.

Of course you can do:
man tar

and see all the flags and how to use, but hopefully this post will save you the effort.

To create an archive I usually do:
tar czf directory_name.tar.gz directory_name/

where “c” means create, “z” for gzip compression and “f” for file.

You can also do:
tar cjf directory_name.tar.bz2 directory_name/

for bzip compression or just:
tar cf directory_name.tar directory_name/

for a regular .tar

Basically, we specify the command with flags, type a file name that we want to create and pick a directory or file that we want to add to archive. These commands create an archive leaving the directory untouched.

 

Similarly, we extract these types of files using the “x” flag like this:
tar xf directory_name.tar
tar xzf directory_name.tar.gz
tar xjf directory_name.tar.bz2

That’s it.

How to use windows key to open Start menu in Ubuntu

The default short cut to open the “Start” panel in Ubuntu is “Alt+F1”, however you may wish to change it to the windows key. If you want to change it to something other than windows key, a combination of keys for instance, you would go to System > Preferences > Keyboard Shortcuts and click on “Show the panel’s main menu”, then, once selected just press they keys you want to use.

However, it doesn’t seem to do anything when you press the windows special key. In order to use it, you would have to open the terminal window or Run Application window (Alt+F2 by default) and execute this command:


gconftool-2 --set /apps/metacity/global_keybindings/panel_main_menu --type string "Super_L"

where Super L is the left “Super” key, which is left windows key.

How to open .rar files in Ubuntu

You need to install unrar to open .rar files in Ubuntu. It’s pretty easy, just open a terminal and type:


sudo apt-get install unrar

Now either double click the .rar file in GUI or right click -> Extract here. You can also type the following command in terminal:


unrar x /path_to_file/file_name.rar