Install Gogs on Debian 9 with nginx and PostgreSQL
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Gogs is a self-hosted Git service, similar to GitLab. It is written in Go and aims to be the easiest and most painless way to set up self-hosted Git service. Gogs is one of the best choices if you need to set up a private Git repository, but don’t want to pay for the private plans on other Git services.



This tutorial shows you how to install and configure Gogs, using PostgreSQL for the database server and nginx for the reverse proxy server. We will use example.com as the domain name for the site. Hosting your own software projects could benefit from large amounts of disk space, so consider using our
Block Storage service with this setup.
sudo. If you’re not familiar with the sudo command, you can check our
Users and Groups guide.Before Installing Gogs
- Follow the Getting Started and Securing Your Server guides, and make sure your Linode’s hostname is set. 
- Update your system: - sudo apt update && sudo apt upgrade
- Install - git:- sudo apt-get install -y git
- When you install Go and Gogs, it will be under the user - git. So, create that user:- sudo adduser --disabled-login --gecos 'Gogs' git
Install Go
In this section we will download the latest version of Go (version 1.7 at the time of this writing) and install the package in the /home/git/local/go directory.
- Create the - /home/git/localdirectory:- sudo su - git mkdir $HOME/local && cd $_
- Download Go and extract the compressed file: - wget https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz tar -C /home/git/local -xvzf go1.7.linux-amd64.tar.gz
- Set the - GOPATHenvironment variable to specify the location of our workspace. We will set the variables in our- .bashrcfile so they will be available every time we enter the shell.- echo 'export GOROOT=$HOME/local/go' >> $HOME/.bashrc echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $HOME/.bashrc source $HOME/.bashrc- Note We need to specify the- GOROOTenvironment variable since we are installing Go to a custom location.
- Check that Go is properly installed: - go version- You’ll see output that resembles the following: - go version go1.7 linux/amd64
Install Gogs
- Download and install Gogs: - go get -u github.com/gogs/gogs- This may take a few minutes during which your console will appear unresponsive. 
- Build the Gogs binary: - cd $GOPATH/src/github.com/gogs/gogs go build
- It will produce a binary named - gogsin the current directory. Execute the binary:- ./gogs web- It will start the web server and listen for HTTP connections on port 3000 while outputting something like this: - 2015/10/09 15:41:41 [W] Custom config (/home/git/go/src/github.com/gogs/gogs/custom/conf/app.ini) not found, ignore this if you're running first time 2015/10/09 15:41:41 [T] Custom path: /home/git/go/src/github.com/gogs/gogs/custom 2015/10/09 15:41:41 [T] Log path: /home/git/go/src/github.com/gogs/gogs/log 2015/10/09 15:41:41 [I] Gogs: Go Git Service 0.6.16.1008 Beta 2015/10/09 15:41:41 [I] Log Mode: Console(Trace) 2015/10/09 15:41:41 [I] Cache Service Enabled 2015/10/09 15:41:41 [I] Session Service Enabled 2015/10/09 15:41:41 [I] Run Mode: Development 2015/10/09 15:41:41 [I] Listen: http://0.0.0.0:3000- Open - http://example.com:3000using your browser. Gogs will redirect the browser to the installation page. Since you haven’t installed the database server yet, close the browser and stop the web server by pressing CTRL+C. You will return to this installation page after installing PostgreSQL and Nginx.
- Exit from user - git:- exit
Install PostgreSQL
Using a database server for Gogs is completely optional. Feel free to skip this section if you don’t want to use a database server with Gogs.
- Install the required packages: - sudo apt-get install -y postgresql postgresql-client libpq-dev
- Open the PostgreSQL interactive terminal to create a new database and user for Gogs: - sudo -u postgres psql -d template1- The output looks like this: - psql (9.4.4) Type "help" for help. template1=#
- Create new user for Gogs: - 1- CREATE USER gogs CREATEDB;
- Set the password for user - gogs:- \password gogs- It will prompt you for the password and password confirmation. Take note of this password, you will need it later when configuring Gogs. 
- Create new database for Gogs: - 1- CREATE DATABASE gogs OWNER gogs;
- Exit the - psqlterminal:- \q
Install nginx
We will use nginx as the reverse proxy for Gogs, so we can access Gogs using our domain name rather than using our host’s IP address. In addition, we will let nginx handle the HTTPS connections for our Gogs site.
- Create a self-signed SSL certificate or buy a commercial SSL certificate from a certificate authority (CA). 
- Install nginx from the repository: - sudo apt-get install -y nginx
- Set nginx as the reverse proxy for Gogs. Using - sudo, create a new file named- /etc/nginx/sites-available/gogs, and set the content as shown below:- File: /etc/nginx/sites-available/gogs
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18- server { listen 80; server_name example.com; return 302 https://$server_name$request_uri; } server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/certificate_key.key; location / { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:3000; } }
 
- Activate the reverse proxy: - sudo ln -s /etc/nginx/sites-available/gogs /etc/nginx/sites-enabled/gogs
- Restart nginx: - sudo systemctl restart nginx
Automatic Startup with systemd
In this section we will set up Gogs to run automatically on boot by creating a systemd unit file.
- Using - sudo, create- /etc/systemd/system/gogs.service:- File: /etc/systemd/system/gogs.service
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18- [Unit] Description=Gogs (Go Git Service) After=syslog.target After=network.target After=postgresql.service After=nginx.service [Service] Type=simple User=git Group=git WorkingDirectory=/home/git/go/src/github.com/gogs/gogs ExecStart=/home/git/go/src/github.com/gogs/gogs/gogs web Restart=always Environment=USER=git HOME=/home/git [Install] WantedBy=multi-user.target
 
- Enable the systemd unit file: - sudo systemctl enable gogs
- Start the service: - sudo systemctl start gogs
- Check the status of the service: - sudo systemctl status gogs- It should display the output like this: - gogs.service - Gogs (Go Git Service) Loaded: loaded (/etc/systemd/system/gogs.service; enabled) Active: active (running) since Sat 2015-10-10 16:51:45 WIB; 34s ago Main PID: 818 (gogs) CGroup: /system.slice/gogs.service └─818 /home/git/go/src/github.com/gogs/gogs/gogs web Oct 10 16:51:45 debian gogs[818]: 2015/10/10 16:51:45 [W] Custom config (/home/git/go/src/github.com/g... time Oct 10 16:51:45 debian gogs[818]: 2015/10/10 16:51:45 [T] Custom path: /home/git/go/src/github.com/gog...ustom Oct 10 16:51:45 debian gogs[818]: 2015/10/10 16:51:45 [T] Log path: /home/git/go/src/github.com/gogits...s/log Oct 10 16:51:45 debian gogs[818]: 2015/10/10 16:51:45 [I] Gogs: Go Git Service 0.6.16.1008 Beta ...
Configure Gogs Using the Web Installer
Open https://example.com:3000 in your browser. It will redirect you to the installation page:

Modify the database settings to match with the PostgreSQL database we created earlier:
- Database type = PostgreSQL
- Host = 127.0.0.1:5432
- User = gogs
- Password = <password for user gogs>
- Database Name = gogs
Set your domain and URL under the General Settings section:
- Domain = example.com
- Application URL = https://example.com
Under Application General Settings, check the box for Enable Builtin SSH Server
Also, you might want to setup an admin account at the bottom of the installation page. Click the Install Gogs button when you’re ready to start the installation. When it’s finished, you will see the login page:

Congratulations! You have successfully installed Gogs on your server.
Disable HTTP Access to Gogs
The Gogs site is still accessible over unsecured HTTP via http://example.com:3000. This is because the Gogs web server listens to all network interfaces on port 3000. If you want your Gogs site to be secure, you need to configure Gogs to listen only to your loopback interface.
- Login as user - gitand go to the Gogs installation directory:- sudo su - git cd $GOPATH/src/github.com/gogs/gogs
- Open the configuration file - custom/conf/app.ini. Add a new configuration value- HTTP_ADDRunder the- [server]section. The section should look like this:- File: /home/git/go/src/github.com/gogs/gogs/custom/conf/app.ini
- 1 2 3 4 5 6 7 8- [server] DOMAIN = example.com HTTP_ADDR = 127.0.0.1 HTTP_PORT = 3000 ROOT_URL = https://example.com/ DISABLE_SSH = false SSH_PORT = 22 OFFLINE_MODE = false
 
- Logout from user - git:- exit
- Restart Gogs: - sudo systemctl restart gogs
- Open your browser. Confirm that - https://example.comis still running and- http://example.com:3000is not.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on
