Ubuntu 16.04: Install Trac for project management

This article will describe installing Trac which manages user with digest authentication.

1 Install trac

Run the following command just once.

1.1 Install trac and libapache2-mod-wsgi

Install trac and libapache2-mod-wsgi.

$ sudo apt install -y trac libapache2-mod-wsgi

1.2 Enable auth_digest

Enable auth_digest.

$ sudo a2enmod auth_digest

1.3 Create project root directory

Create project root directory which has project directories.

$ sudo mkdir /var/lib/trac
$ sudo mkdir -p /var/www/html/trac
$ sudo chown www-data:www-data /var/www/html/trac

2 Create project

Run the following command for each project.

2.1 Create project directory

Create project directory.

$ sudo trac-admin /var/lib/trac/test initenv test sqlite:db/trac.db
$ sudo trac-admin /var/lib/trac/test deploy /var/www/html/trac/test
$ sudo chown -R www-data:www-data /var/lib/trac/test
$ sudo chown -R www-data:www-data /var/www/html/trac/test

2.2 Create user

Create admin user.

$ sudo htdigest -c /var/lib/trac/test/.htdigest "test" admin
Adding user admin in realm test
New password:
Re-type new password:
$ sudo trac-admin /var/lib/trac/test permission add admin TRAC_ADMIN

Create user. Run htdigest command without -c option.

$ sudo htdigest /var/lib/trac/test/.htdigest "test" hiroom2
Adding user hiroom2 in realm test
New password:
Re-type new password:

2.3 Create Apache2 conf

Create Apache2 conf for project.

$ sudo su -c '
cat <<EOF > /etc/apache2/sites-available/test.conf
WSGIScriptAlias /trac/test /var/www/html/trac/test/cgi-bin/trac.wsgi
<Location /trac/test>
  AuthType Digest
  AuthName "test"
  AuthUserFile /var/lib/trac/test/.htdigest
  Require valid-user
</Location>
EOF
'

Enable conf and restart Apache2.

$ sudo a2ensite test
$ sudo systemctl restart apache2

3 Access to Trac

Access the following URL with browser.

http://<server>/trac/test

The dialog of digest authentication is displayed.

0001_Digest-Auth.png

Trac page is displayed.

0002_Trac.png

4 Script for installing Trac

The following script will install Trac automatically.

#!/bin/sh

install_trac()
{
  sudo apt install -y trac libapache2-mod-wsgi

  sudo a2enmod auth_digest

  sudo mkdir /var/lib/trac
  sudo mkdir /var/www/html/trac
  sudo chown www-data:www-data /var/www/html/trac
}

TMP=`mktemp -t linux-trac.sh.XXXXXX`
trap "rm $TMP* 2>/dev/null" 0
sudo apt install -y expect

create_digest()
{
  filename=$1
  realm=$2
  username=$3
  password=$4
  options=

  if [ ! -f ${filename} ]; then
    options=-c
  fi

  cat <<EOF > ${TMP}
set timeout -1
spawn sudo htdigest ${options} ${filename} ${realm} ${username}
expect "New password: "
send "${password}\n"
expect "Re-type new password: "
send "${password}\n"
expect eof
EOF
  expect ${TMP}
}

ADMIN_PASSWORD="trac"
USER_PASSWORD="trac"

create_test_project()
{
  sudo trac-admin /var/lib/trac/test initenv test sqlite:db/trac.db
  sudo trac-admin /var/lib/trac/test deploy /var/www/html/trac/test
  sudo chown -R www-data:www-data /var/lib/trac/test
  sudo chown -R www-data:www-data /var/www/html/trac/test

  create_digest /var/lib/trac/test/.htdigest "test" admin ${ADMIN_PASSWORD}
  sudo trac-admin /var/lib/trac/test permission add admin TRAC_ADMIN
  create_digest /var/lib/trac/test/.htdigest "test" hiroom2 ${USER_PASSWORD}

  sudo su -c '
cat <<EOF > /etc/apache2/sites-available/test.conf
WSGIScriptAlias /trac/test /var/www/html/trac/test/cgi-bin/trac.wsgi
<Location /trac/test>
  AuthType Digest
  AuthName "test"
  AuthUserFile /var/lib/trac/test/.htdigest
  Require valid-user
</Location>
EOF
'
  sudo a2ensite test
  sudo systemctl restart apache2
}

install_trac
create_test_project