← All posts

Moving Trac to Nginx and tracd

Trac and Nginx logos

I once wrote an article called Installing and Configuring Trac on FreeBSD. Now it’s time to drop Apache in favor of Nginx+PHP/FastCGI, and I need somewhere to move Trac.

There are several options besides mod_python. I chose to use tracd (Trac’s own web server). But if at some point I decide I don’t like this approach, I’ll definitely write an article about setting it up with FastCGI.

Tracd

Start tracd like this:


tracd --daemonize --pidfile=/var/run/trac/tracd.3050 --port=3050 --protocol=http --env-parent-dir=/var/trac \
    --basic-auth=project1,/var/svn/svn-auth-file,svn --basic-auth=project2,/var/svn/svn-auth-file,svn

Here,

  • --daemonize - runs tracd in the background as a daemon;
  • --pidfile - path to the PID file;
  • --port - TCP port to listen on;
  • --protocol - protocol (options: http, scgi, ajp. In our case, http);
  • --env-parent-dir - multi-project mode; specify the projects’ parent directory (there is also “–single-env”, which is single-project mode and requires the full path to the project);
  • --basic-auth - authentication (format: “projectdir,htpasswd_file,realm”; if you use –env-parent-dir, you can specify just the project directory in “projectdir”, rather than the full path).

Type “–help” and read the help for all the options.

Nginx

Now let’s add a new backend to Nginx:


upstream trac {
    server 127.0.0.1:3050;
}

And rewrite the host configuration:


server {
    listen 80;
    server_name trac.example.com;

    location / {
        #auth_basic            "Password, please!";
        #auth_basic_user_file  /var/svn/svn-auth-file;

        proxy_pass      http://trac;
        proxy_set_header Host $host;
    }
}

Well, that’s it—all that’s left is to restart nginx:


/usr/local/etc/rc.d/nginx restart

A Script to Manage Our Trac

As a bonus, let’s make a script to manage tracd:


#!/bin/sh
TRACD="/usr/local/bin/tracd"
TRAC="/var/trac"
USER="www"
PIDFILE="/var/run/trac/tracd.3050"
PORT="3050"
PYTHON_EGG_CACHE="/tmp/egg_cache"

start()
{
    export PYTHON_EGG_CACHE
    su $USER -c "$TRACD --daemonize --pidfile=$PIDFILE --port=$PORT --protocol=http --env-parent-dir=$TRAC \
                --basic-auth=project1,/var/svn/svn-auth-file,svn --basic-auth=project2,/var/svn/svn-auth-file,svn"
}

stop()
{
    su $USER -c "kill `cat $PIDFILE`"
}

restart()
{
    stop
    sleep 1
    start
}

$1

Since we’ll be running it as www, we need to grant write permissions for the /var/run/trac/ directory.
Save the script in “/usr/local/etc/rc.d/tracd” and run it:


/usr/local/etc/rc.d/tracd start

If you see the error “This account is currently not available.”, you need to set a shell for the user you are running tracd as. You can do this with

chsh имя_пользователя

Further Reading

Comments are closed. Read the original discussion in Russian →