TeamCity Server on Ubuntu

Last time, we set up a virtual machine template for an ubuntu server. Now that we have set up a clone of this machine, it is time to set up Teamcity on it.

Abstract

Teamcity on linux is meant to be run from its integrated Tomcat server. We will use the default Teamcity installation procedure in combination with the lightweight lighttpd to act as a front end server listening on port 80 and forwarding requests to Teamcity’s Tomcat installation. This setup is both, easier than configuring Tomcat on Port 80 (remember it requires root permissions to allocate) and we could add authentication or https access more easily later (though I will not do that for now).

Installing Teamcity

To install Teamcity, follow the instructions from JetBrains, which can be found here (takes less than 10 minutes). I chose to install mine at /var/TeamCity:
http://confluence.jetbrains.net/display/TCD65/Installing+and+Configuring+the+TeamCity+Server#InstallingandConfiguringtheTeamCityServer-installingWithTomcat 

Follow the instructions to set up your external database (recommended approach). I am using a SQL Server 2008 Installation that is already present and regularly backed up in my “private cloud”.  Edit your server.conf to configure a port for the Tomcat Server :

ubuntu@localhost: sudo vi /var/TeamCity/conf/server.xml

Permissions are a chore, but we don’t want the Teamcity Server directory to be owned by our admin user, so we change the owner of our Teamcity install directory to the default www-data user.

ubuntu@localhost: sudo chown -R www-data /var/TeamCity

Next, we want Teamcity to start automatically when the server is booted, so we add a small init script. Be sure to adjust the TEAMCITY_DATA_PATH environment variable to a static directory of your choice, otherwise TCs default will make end up in www-data’s home directory, which is, frankly, a very inconvenient location.

ubuntu@localhost:/var/TeamCity$ cat /etc/init.d/teamcity 
#!/bin/sh
# /etc/init.d/teamcity -  startup script for teamcity
export TEAMCITY_DATA_PATH="/var/TeamCity/.BuildServer"

case $1 in
start)
 start-stop-daemon --start  -c www-data --exec /var/TeamCity/bin/teamcity-server.sh start
;;

stop)
 start-stop-daemon --start -c www-data  --exec  /var/TeamCity/bin/teamcity-server.sh stop
;;

esac

exit 0

Now we need to register the startup script to run automatically:

ubuntu@localhost: sudo update-rc.d teamcity defaults

Next, we start the server manually (you can reboot too):

ubuntu@localhost: sudo /etc/init.d/teamcity start

Installing Lighttpd

Now we need to install lighttpd:

ubuntu@localhost: sudo apt-get install lighttpd

And configure it to forward requests from port 80 to the port we configured for Tomcat (8080 in my case).

ubuntu@localhost: sudo vi /etc/lighttpd/lighttpd.conf
server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_rewrite",
        "mod_proxy"
)

$HTTP["host"] =~ "teamcity.yourdomain.com" {
        proxy.server = (
                "" => (
                        "tomcat" => (
                                "host" => "127.0.0.1",
                                "port" => 8080,
                                "fix-redirects" => 1
                        )
                )
        )
}

Final Words

That’s it. By now you should have a running teamcity server. If something goes wrong, be sure to check the logs which can be found at /var/TeamCity/logs. To make the server available via DNS, you need to make sure the servers hostname is registered with your local DNS. In my case, I simply added a static record to it.

Advertisement
  1. djs10
    April 21, 2012 at 17:44

    Great article, thanks. Didn’t bother with the lighthttpd bit, but your script for running TC from init.d worked brilliantly.

  2. Ken
    July 11, 2012 at 23:46

    Enjoyed the article. Could you possibly provide same information for CentOS / Red Hat? We want teamcity to start as the teamcity user on reboot.

    Thanks!

    • July 12, 2012 at 15:07

      Sorry, no expert on CentOS. Think a quick google should help you here. Good chance the guys at serverfault.com can help you too.

    • abc123
      May 23, 2014 at 20:03

      I did this,
      #!/bin/bash
      # teamcity daemon
      # chkconfig: 345 20 80
      # description: teamcity daemon
      # processname: teamcity

      DAEMON_PATH=”/opt/TeamCity/bin/”

      DAEMON=teamcity
      DAEMONOPTS=””

      NAME=teamcity
      DESC=”teamcity service”
      PIDFILE=/opt/TeamCity/run/$NAME.pid
      SCRIPTNAME=/etc/init.d/$NAME

      export TEAMCITY_DATA_PATH=”/opt/TeamCity/.BuildServer”

      case “$1” in
      start)
      printf “%-50s” “Starting $NAME…”
      cd $DAEMON_PATH
      PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
      echo “Saving PID” $PID ” to ” $PIDFILE
      if [ -z $PID ]; then
      printf “%s\n” “Fail”
      else
      runuser -l teamcityuser -c ‘exec /opt/TeamCity/bin/teamcity-server.sh start’
      echo $PID > $PIDFILE
      printf “%s\n” “Ok”
      fi
      ;;
      status)
      printf “%-50s” “Checking $NAME…”
      if [ -f $PIDFILE ]; then
      PID=`cat $PIDFILE`
      if [ -z “`ps axf | grep ${PID} | grep -v grep`” ]; then
      printf “%s\n” “Process dead but pidfile exists”
      else
      echo “Running”
      fi
      else
      printf “%s\n” “Service not running”
      fi
      ;;
      stop)
      printf “%-50s” “Stopping $NAME”
      PID=`cat $PIDFILE`
      cd $DAEMON_PATH
      if [ -f $PIDFILE ]; then
      #kill -HUP $PID
      runuser -l teamcityuser -c ‘exec /opt/TeamCity/bin/teamcity-server.sh stop’
      printf “%s\n” “Ok”
      rm -f $PIDFILE
      else
      printf “%s\n” “pidfile not found”
      fi
      ;;

      restart)
      $0 stop
      $0 start
      ;;

      *)
      echo “Usage: $0 {status|start|stop|restart}”
      exit 1
      esac

  3. Todd Fischer
    August 15, 2012 at 20:06

    I also had to do a

    sudo chmod ugo+x /etc/init.d/teamcity

  1. March 9, 2012 at 07:29
  2. May 11, 2012 at 08:44

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: