Installing GitBucket with PostgreSQL and Tomcat on CentOS 7

     

Table of Contents

Preamble

When it comes to on-premises Git hosting, GitBucket is definitely my current favorite. It has monthly updates, LDAP authentication, email notifications, forks, wikis, issues, pull requests, inline commenting, public and private repos, plugins, skins, protected branches, HTTP and SSH transports… you name it, man.

I’ve already sent a few patches to its current maintainer, Naoki Takezoe, he and his team are very responsive and helpful, too. What more could you ask for? Install it already!

PostgreSQL

In this step we add the official PostgreSQL repo to have the latest version, install it, configure authentication, then also set up a user and a database for use by GitBucket.

Find the URL for the current CentOS 7 PostgreSQL RPM, then install it. E.g.:

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

Then open /etc/yum.repos.d/CentOS-Base.repo and add to the base and updates sections:

exclude=postgresql*

Install the server:

yum install postgresql10-server

Initialize Postgres:

/usr/pgsql-10/bin/postgresql10-setup initdb

Set up authentication via /var/lib/pgsql/10/data/pg_hba.conf. Use md5 to have password authentication on the local interfaces instead of ident:

host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

Finally, start and enable the engine:

systemctl start postgresql-10.service
systemctl enable postgresql-10.service

Then create the gitbucket user and database:

sudo -u postgres sh -c 'createuser --username=postgres --no-createrole --no-createdb --no-inherit --encrypted --login --pwprompt --no-superuser gitbucket'
sudo -u postgres sh -c 'createdb --encoding=UTF-8 --owner=gitbucket gitbucket'

There you go, Postgres is up and running.

Tomcat

Now let’s install and configure Tomcat, which will be the engine hosting our GitBucket instance.

yum install tomcat
systemctl enable tomcat.service

Now speed up random number generation. This can actually save you minutes every time Tomcat is restarted. Open /etc/tomcat/tomcat.conf and add the following line:

JAVA_OPTS="-Djava.security.egd=file:/dev/urandom"

Also edit /etc/tomcat/server.xml to have an ending similar to this:

<Context path="" docBase="gitbucket">
        <!-- Default set of monitored resources -->
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>

GitBucket

Time to get GitBucket itself. Make sure to change the version number to the latest:

export GITBUCKET_VERSION='4.20.0'
wget https://github.com/gitbucket/gitbucket/releases/download/${GITBUCKET_VERSION}/gitbucket.war
cp gitbucket.war /var/lib/tomcat/webapps/gitbucket.war
chown root.tomcat /var/lib/tomcat/webapps/gitbucket.war
restorecon -v /var/lib/tomcat/webapps/gitbucket.war
ln -s /usr/share/tomcat/.gitbucket /opt/gitbucket

Make sure SELinux won’t intervene, so install the GitBucket SELinux policy module and also fix file permissions, as explained on the link.

Now test if Tomcat can start GitBucket, then if succeeded, stop Tomcat:

systemctl restart tomcat.service && sleep 2 && tail -f /var/log/tomcat/*
systemctl stop tomcat.service

Now point GitBucket to PostgreSQL, edit /opt/gitbucket/database.conf:

db {
    url = "jdbc:postgresql://localhost/gitbucket"
    user = "gitbucket"
    password = "***"
}

Then start Tomcat again with systemctl start tomcat, and log in with root/root. Once logged in, the settings should be self-explanatory. Cheers!