ArchLinux 2018.10.01: Install cgit for Git web frontend

This article will describe installing cgit.

1 Install cgit

Install cgit package.

  • SCAN_PATH is used to scan-path in /etc/cgitrc.
#!/bin/sh -e

[ -z "${SCAN_PATH}" ] && SCAN_PATH=/srv/git

cgit_install()
{
  sudo pacman -Sy --noconfirm cgit
  cat <<EOF | sudo tee -a /etc/cgitrc
scan-path=${SCAN_PATH}
css=/cgit-css/cgit.css
logo=/cgit-css/cgit.png
EOF
}

apache_install()
{
  sudo pacman -Sy --noconfirm apache mod_wsgi
  sudo systemctl enable httpd

  # ssl configuration.
  # Country Name (2 letter code) [AU]:
  # State or Province Name (full name) [Some-State]:
  # Locality Name (eg, city) []:
  # Organization Name (eg, company) [Internet Widgits Pty Ltd]:
  # Organizational Unit Name (eg, section) []:
  # Common Name (e.g. server FQDN or YOUR name) []:
  # Email Address []:
  cat <<EOF | sudo openssl req -new -x509 -nodes -newkey rsa:4096 -days 1095 \
                   -keyout /etc/httpd/conf/server.key \
                   -out /etc/httpd/conf/server.crt
AU
Some-State
city
company
section
${WEBDAV_SERVER_FQDN}

EOF
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule ssl_module/LoadModule ssl_module/g' \
       -e 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/g'
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/httpd-ssl.conf
EOF

  # cgi configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/#LoadModule cgid_module/LoadModule cgid_module/g'

  # rewrite configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule rewrite_module/LoadModule rewrite_module/g'
  cat << EOF | sudo tee /etc/httpd/conf/extra/redirect-to-https.conf
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
EOF
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/redirect-to-https.conf
EOF

  # cgit configuration.
  cat <<EOF | sudo tee /etc/httpd/conf/extra/cgit.conf
ScriptAlias /cgit/ "/usr/lib/cgit/cgit.cgi/"
Alias /cgit-css "/usr/share/webapps/cgit/"

<Directory "/usr/share/webapps/cgit">
   AllowOverride None
   Options None
   Require all granted
</Directory>

<Directory "/usr/lib/cgit/">
   AllowOverride None
   Options ExecCGI FollowSymlinks
   Require all granted
</Directory>
EOF
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/cgit.conf
EOF

  sudo systemctl restart httpd
}

create_repo()
{
  sudo mkdir -p "${SCAN_PATH}"
  sudo chmod 777 "${SCAN_PATH}"
  sudo mkdir -p "${SCAN_PATH}"/test.git
  sudo chmod 777 "${SCAN_PATH}"/test.git
  cd "${SCAN_PATH}"/test.git
  git init --bare
}

cgit_main()
{
  cgit_install
  apache_install
  create_repo
}

cgit_main

2 Access to cgit

Access to the following URL.

https://<server>/cgit

Created repository is displayed.

0001_cgit.png