These are my notes from setting up Gogs on Debian 8 Jessie with Apache2 and PostgreSQL.
This guide assumes you have a fresh copy of Debian Jessie and it's up to date sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove
.
Firewall
On my server access to all ports except 22, 80 and 443 are blocked by an external firewall. If you don't have this I'd recommend setting up iptables to block public access to port 3000 but that is beyond the scope of this post.
Install the prerequisites
sudo apt-get install apache2 git postgresql
Add a user for the service to run as
sudo useradd --system --home-dir /home/git --create-home git
create a log directory for gogs
sudo mkdir /var/log/gogs
sudo mkdir /opt/gogs
sudo chown git:git /var/log/gogs/
Create a PostgreSQL database
# Generate a password
dd if=/dev/random bs=1 count=18 2>/dev/null | base64
# Connect to psql
sudo su postgres -c psql
CREATE USER gogs_user WITH PASSWORD 'HNefjrdVmVQdzU4Ssso0FMCt';
CREATE DATABASE gogs_db;
GRANT ALL PRIVILEGES ON DATABASE gogs_db to gogs_user;
\q
Download gogs
cd /opt/gogs/
# This will obviously need to be updated to the latest version
sudo wget https://cdn.gogs.io/gogs_v0.9.13_linux_amd64.tar.gz
sudo tar xzfv gogs_v0.9.13_linux_amd64.tar.gz
sudo rm gogs_v0.9.13_linux_amd64.tar.gz
sudo chown -R git:git /opt/gogs
Make it a service
Copy the systmed template and edit it
sudo cp /opt/gogs/gogs/scripts/systemd/gogs.service /etc/systemd/system/gogs.service
sudo vim /etc/systemd/system/gogs.service
[Unit]
Description=Gogs (Go Git Service)
After=syslog.target
After=network.target
#After=mysqld.service
After=postgresql.service
#After=memcached.service
#After=redis.service
[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git
WorkingDirectory=/opt/gogs/gogs
ExecStart=/opt/gogs/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git
[Install]
WantedBy=multi-user.target
And start the service
sudo systemctl enable gogs && sudo systemctl start gogs
Install the gogs server
If you're doing this locally you can just browse to http://localhost:3000. In my case I've SSHed into the server and I'm forwarding the port ssh -L 3000:localhost:3000 gogs-server.example.com
- Change the Database Type to PostgreSQL
- Change the User to gogs_user
- Change the password to the one you set earlier
- Change the Database Name to gogs_db
- Change the Repository Root Path to /opt/gogs/
- Change the domain to the address of your server
- Change the application url to include https and the address of your server
- Change the log path to /var/log/gogs
The rest of the defaults are fine for now.
You should also create an account now, as the first account created will become the admin account.
Setup Apache
First we want to secure our connections
Let's Encrypt
cd
git clone https://github.com/certbot/certbot
cd certbot
./certbot-auto --apache -d gogs-server.example.com
Apache Proxy
sudo vim /etc/apache2/sites-enabled/000-default-le-ssl.conf
Under VirtualHost add
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
or in my case I've got a few other services running on this host so I've done
################################################################################
# Gogs
################################################################################
<Proxy /gogs>
Order allow,deny
Allow from all
</Proxy>
ProxyPass /gogs http://127.0.0.1:3000
ProxyPassReverse /gogs http://127.0.0.1:3000
I was having all soughts of problems with gogs login page giving a 404 error until I found an issue on GitHub and then I removed the trailing /
for the proxy pass command (it was ProxyPass /gogs http://127.0.0.1:3000/
) and that fixed it.