Introduction
This guide explains how to install Trac in your server to manage your personal projects, using Nginx as webserver and Git as code repository.
Trac is an open source, web-based project management and bug-tracking tool. The program is inspired by CVSTrac, and was originally named svntrac due to its ability to interface with Subversion. It is developed and maintained by Edgewall Software. Trac is written in the Python programming language. Until mid-2005, it was available under the GNU General Public License; since version 0.9, it has been released under a modified BSD license. Both are free software licenses.
Trac allows hyperlinking information between a computer bug database, revision control and wiki content. It also serves as a web interface to a version control system like Subversion, Git, Mercurial, Bazaar and Darcs. Prior to version 0.11 the web front end presentation of Trac was handled by the ClearSilver template system. Starting with 0.11 an in-house template system called Genshi is used, although compatibility with ClearSilver based plugins will remain for several versions.
Nginx (pronounced "engine X") is a lightweight web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license.Originally, nginx was developed to fill the needs of various websites run by Rambler. According to the December 2008 Netcraft survey, nginx is now used on 3,354,329 domains, making it the fourth most popular web server, being used at sites like
Wordpress,
Penny-Arcade or
YouPorn.
Git is a free distributed revision control, or software source code management project with an emphasis on being fast. Git was initially created by Linus Torvalds for Linux kernel development. Every Git working directory is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on network access or a central server. Several high-profile software projects now use Git for revision control, most notably the Linux kernel, Perl 5, Samba, X.org Server, Qt (toolkit), One Laptop per Child (OLPC) core development, Ruby on Rails web framework, VLC, Merb, Wine, SWI Prolog, DragonFly BSD and the Android mobile platform.
Requirements
This document assumes you are using an
Ubuntu 8.04 server. This guide can be probably used with newer versions of Ubuntu and Debian, but some minor changes may be required. We will install
Trac 0.11. It requires:
- Python 2.5
- PostgreSQL as database (Trac 0.11 has some bugs when using MySQL).
- Nginx as HTTP-proxy (Apache could be used but this configuration is simpler to setup and runs faster)
It is recommended you apply all available patches (security and upgrade) to your server before proceeding.
Postgres
If you don't have Postgres installed in your server you can follow these instructions before proceeding with the installation. To install Postgres and all required packages run:
$ sudo apt-get install postgresql postgresql-client postgresql-contrib python-psycopg2
This will deploy the database, create the link so it runs as daemon and configure the "postgres" user (root user of the database). It will also add the Python libraries so we can access the database from Trac. Once installed, we need to change the default "postgres" password. To do that execute:
$ sudo su postgres -c psql template1
This will log you into the PostgreSQL console. There run:
template1=# ALTER USER postgres WITH PASSWORD 'new_password';
Now we need to set the password for the Ubuntu postgresql user. Execute:
$ sudo passwd -d postgres
$ sudo su postgres -c passwd
Now restart the server.
$ sudo /etc/init.d/postgresql-8.3 restart
The server should be running, try to log again using:
$ sudo su postgres -c psql template1
Be aware this has created a database server which only has localhost access, so it won't be available from other servers. If you want to allow connection from an external ip, you have to edit
pg_hba.conf.
Installation steps
Those steps assume you have an user with "sudo" privileges. You only need a console, no UI required.
Installing Trac
First of all we need some packages before we install Trac. Run:
$ sudo apt-get install build-essential graphviz apache2-utils htmldoc enscript
Once those components are installed, you can install Trac using "easy_install", the Python installer. Run:
$ sudo easy_install Trac
$ sudo easy_install http://svn.edgewall.org/repos/genshi/trunk/ #A required component, install after Trac to avoid problems
Now you can configure the database and create the Trac user. Log into PostgreSQL:
$ su postgres -c psql
and run:
template1=# create user
with password 'user_password';template1=# create database
with owner=user
encoding = 'utf8';
Now you have a user for Trac connectivity and a database to be used by one of your projects. Next step is to create the Trac environment. To this end, choose a folder (available to Nginx) as root folder for all your Trac projects. Inside this folder we will create several subfolders, one per project. This has to be done this way as Trac only allows one project per "environment", so if you want to manage several projects you need to run several Trac instances in parallel in the same server. Luckily this is not a huge overhead as the embedded Trac server is quite efficient.
To create the first project I will assume you've choose "/public/" as you root folder and this folder is accessible by Nginx. To initialize your environment run:
$ trac-admin /public/projectname initenv
This will ask some questions about the project. When you are asked for the PostgreSQL connection string, write:
postgres://trac_db_user:password@localhost:5432/database
You are done, your Trac server is configured and ready to go. To test it, use the embedded Trac server:
$ tracd -p 3050 /public/projectname
Be aware this (probably) can't be accessed from outside your box as the port will be closed. Just check it works with
Lynx or a similar browser.
Use nginx as proxy
As said before, the server is not yet available from outside the box (if it is, you have a security issue here!). Setting Nginx as proxy is quite easy. Go to your "nginx/sites-available" folder and edit the file of the domain you will use to access the Trac server. You can use this configuration as an example:
upstream domain_trac {
server 127.0.0.1:3050;
}
server { listen 80;
server_name domain.com;
rewrite ^/(.*) http://www.domain.com/$1 permanent;
} server {
listen 80;
server_name www.domain.com; access_log /home/public_html/domain.com/log/access.log;
error_log /home/public_html/domain.com/log/error.log;
location / {
proxy_pass http://domain;
proxy_redirect default;
}
}
Restart Nginx and you are done. Nginx will be a simple proxy of your "tracd" server, redirecting all the requests received. This is a more flexible and fast configuration than the one using mod_cgi on Apache or a similar server. Also it allows you to kill an instance of Trac easily and without affecting other running Trac instances under the same proxy.
Enabling user authentication
Probably you want some privacy for your project, or simply avoiding anybody can edit the wiki. To this end you can enable user authentication. This section does a brief description, you will find more detail at your own Trac server, under
http://www.domain.com/ProjectName/wiki/TracStandalone. User authentication is based on basic HTTP authentication, using Apache password files. Go to your root ("/public/") folder and run:
$ sudo htpasswd -c /public/ProjectName/.htpasswd username
This will request the password of the user and save it in the .htpasswd file. You can select another path outside the project folder if you want, but it has to be accessible by nginx and tracd. Now you have to notify Trac that you own a password file and want to use it for authentication. To this end, you have to launch the Trac server with some parameters:
$ tracd -p 3050 --basic-auth=ProjectName,/public/ProjectName/.htpasswd,domain.com /public/ProjectName/
Add Admin user
Once you enable authentication, there is no default admin user. You need to add at least one user to the admin group to be able to configure Trac properly. Run:
$ trac-admin /public/ProjectName/ permission add TRAC_ADMIN
Be careful, <user> has to be an existing user with a valid password in the .htpasswd file. For more details, check your own Trac instance at
http://www.domain.com/ProjectName/wiki/TracPermissions
Running multiple projects at once
As said before, Trac only allows one project per environment (folder). If you want to manage more projects, you have to create new folders under the root directory (in this tutorial, "/public/") and initialize each one independently. Then you can tell tracd to run them all at once. If you want to share the access permissions between all the projects, run:
$ tracd -p 8080 --auth="*",/public/ProjectName/.htpasswd,domain.com /public/project1 /public/project2
For other configurations check your own Trac instance at
http://www.domain.com/ProjectName/wiki/TracStandalone
Integrate with Git
As it is now, our Track instance only provides a Wiki and a ticketing system. To use the full power of Trac we need to link some code repository to it. Our choice is Git for several reasons (performance, usage, etc). This assumes you have installed a Gitosis repository in your server that can be used to store the git code. The integration is done using the Git plugin for Trac, available at
http://trac-hacks.org/wiki/GitPlugin. This page also contains steps to install the plugin. You just need to download the zip file, unzip it and run:
$ cd gitplugin/0.11/
$ sudo easy_install .
Once the plugin is installed, our next step is to clone the Gitosis administration file and edit it:
$ git clone git@YOUR_SERVER_HOSTNAME:gitosis-admin.git
$ cd gitosis-admin
$ nano gitosis.conf
Add the new group and repository related to the project:
[group coreteam]
writable = projectname
members = username@desktop
and commit the changes to the Gitosis server:
$ git commit -a -m "allowing access to projectname to user username"
$ git push
Now create your initial repository:
$ mkdir projectname
$ cd projectname
$ git init
$ git remote add origin git@YOUR_SERVER_HOSTNAME:projectname.git
# do some work
$ git add new files
$ git commit -a -m "first commit"
$ git push origin master:refs/heads/master
You have created your repository and submitted the first files. Now log into your Gitosis server and edit the file "gitosis/repositories/projectname/description". Change the content as required and set its owner to "git:webmasters" (chown). This step is important to avoid issues when pushing new code to the repository. Now you just need to edit "/public/projectname/conf/trac.ini" and add the references to git:
[components]
tracext.git.* = enabled[git]
cached_repository = true
git_bin = /usr/bin/git
persistent_cache = true
shortrev_len = 7[trac]
repository_dir = /home/git/repositories/game.git
repository_type = git
Configuration
Now that we have a running Trac instance, we have to configure it. You should check your Trac documentation at
http://www.domain.com/ProjectName/wiki/TracIni to see what kind of customizations you can do using the "trac.ini" file (for example, sending emails to users).
Change reporting system
The default ticket reports are not very user-friendly. To fix that and use a better system, edit "trac.ini":
[components]
trac.ticket.report.* = disabled
Add Spam filter
If you allow anonymous users to edit your content, you will probably need a spam filter. The one we will use needs an Akismet key to filter spam. To install it run:
$ sudo easy_install TracSpamFilter
Once installed, edit "trac.ini" and add:
[akismet]
api_key = 1234567890
Enable XML-RPC
XML-RPC allows 3rd party applications to connect to Trac. This is required if you will use Netbeans or Eclipse to interact with Trac. The offical plugin is located at
http://trac-hacks.org/wiki/XmlRpcPlugin, but it has some issues with custom workflows, which causes problems with the Cube'N plugin of Netbeans. As we use Netbeans, we need to use the fixed pluguin from
http://code.google.com/p/cubeon/downloads/list, TracXMLRPC-1.5.0-py2.5.egg. Download the file and install it using:
$ sudo easy_install racXMLRPC-1.5.0-py2.5.egg
Then edit trac.ini and add:
[components]
tracrpc.* = enabled
Restart Trac.
Running Trac as daemon
There are some scripts to launch Trac (using tracd) as a Linux daemon, but the few I've tested have issues when running under Ubuntu. The easiest solution is to launch tracd using “nohup”. This should suffice for most user. When I find a working init.d script I will copy it here.
Static content
For performance reasons, static content (like non-code files) should be put in the $TRAC_ENV/htdocs folder, and is accessed by URLs like <project_URL>/chrome/site/.... There's a wiki label for them: "
htdocs:file_name".