Setup Gitea on Raspberry Pi

In this post I’ll explain how I’ve setup Gitea o nmy Raspberry Pi 3.

viagra sen ligne france

These instructions are based on this article: https://www.alexruf.net/2016/05/23/setup-gogs-git-service.html.

Setup Raspberry Pi with minimal Raspbian image.

Add an empty file named ssh to the boot directory of your image.

Create, in the same directory, a file called wpa_supplicant.conf.

The file should contain the following details:

For Raspbian Jessie:

network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

For Raspbian Stretch (and newer versions):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

Before putting your raspberry pi on the network, check all connected devices using arp -a command.

C:\Users\guest>arp -a

Interface: 192.168.0.33 --- 0x10
  Internet Address      Physical Address     Type
  192.168.0.240         b8-27-eb-58-65-86    dynamic
  192.168.0.254         00-07-cb-90-2e-52    dynamic
  192.168.0.255         ff-ff-ff-ff-ff-ff    static
  224.0.0.22            01-00-5e-00-00-16    static
  224.0.0.252           01-00-5e-00-00-fc    static
  224.0.0.253           01-00-5e-00-00-fd    static
  239.255.255.250       01-00-5e-7f-ff-fa    static

Plug your raspberry pi and wait for it to join your wifi network.

Then retype the arp -a command and compare with the previous result to find the IP given by the DHCP server.

C:\Users\guest>arp -a

Interface: 192.168.0.33 --- 0x10
  Internet Address      Physical Address     Type
  192.168.0.240         b8-27-eb-58-65-86    dynamic
  192.168.0.241         f8-1a-67-19-a4-01    dynamic
  192.168.0.254         00-07-cb-90-2e-52    dynamic
  192.168.0.255         ff-ff-ff-ff-ff-ff    static
  224.0.0.22            01-00-5e-00-00-16    static
  224.0.0.252           01-00-5e-00-00-fc    static
  224.0.0.253           01-00-5e-00-00-fd    static
  239.255.255.250       01-00-5e-7f-ff-fa    static

Open an SSH terminal (I use putty) to this IP address.

Default login and password are: pi/raspberry

Type the following:

sudo raspi-config

There you should change the current user password and the hostname.

I’ll use rpi-git as new hostname.

Then configure the correct timezone:

sudo dpkg-reconfigure tzdata

Now execute the following commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install zip git -y

# Install ntfs-3g
# only if your usb drive is NTFS formatted
sudo apt-get install ntfs-3g
# Create a new user:
sudo adduser --disabled-login --gecos 'Gitea' git

# Change to user git:
sudo su - git

# install gitea from binary
wget -O gitea https://dl.gitea.io/gitea/1.4.0/gitea-1.4.0-linux-arm-7
chmod +x gitea

# Now type `exit` to go back to pi user.

Mount external drive for git repositories and LFS root path

Create the directory where the USB drive will be mount. I choose /mnt/git

sudo mkdir /mnt/git
sudo chown -R git:git /mnt/git
sudo chmod -R 770 /mnt/git

Get the uid and gid of the git user:

pi@rpi-git:~ $ sudo -u git id
uid=1001(git) gid=1001(git) groups=1001(git)

Execute the sudo blkid command to find your USB uuid:

pi@rpi-git:~ $ sudo blkid
/dev/mmcblk0p1: LABEL="boot" UUID="5DB0-971B" TYPE="vfat" PARTUUID="bea18737-01"
/dev/mmcblk0p2: LABEL="rootfs" UUID="060b57a8-62bd-4d48-a471-0d28466d1fbb" TYPE="ext4" PARTUUID="bea18737-02"
/dev/mmcblk0: PTUUID="bea18737" PTTYPE="dos"
/dev/sda1: LABEL="GIT" UUID="91AA-3225" TYPE="ntfs"

Update the /etc/fstab file by adding the following line (modify this line with your USB drive uuid and type):

UUID=91AA-3225     /mnt/git     ntfs-3g      uid=1001,gid=1001,umask=007 0 0

Then, you can test your fstab file is correct with the mount -fav command:

pi@rpi-git:~ $ sudo mount -fav
/proc                         : already mounted
/boot                         : already mounted
/                             : ignored
/mnt/git                      : successfully mounted

If all entries are correct, you can mount your USB drive with the sudo mount -a command.

Create the file /etc/systemd/system/gitea.service and add the following content:

[Unit]
Description=Gitea
After=syslog.target
After=network.target
After=mariadb.service mysqld.service postgresql.service memcached.service 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=/home/git
ExecStart=/home/git/gitea web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target
# Start gitea:
sudo systemctl enable gitea
sudo systemctl start gitea

Install and setup nginx with ssl

# Install and configure nginx (see https://gogs.io/docs/advanced/configuration_for_source_builds#setting-up-nginx-sever):
sudo apt-get install nginx -y

Edit /etc/nginx/sites-available/gitea and insert the following:

server {
    listen 443 ssl;
    server_name rpi-git <your-domain>;
    ssl_certificate     /etc/letsencrypt/live/<your-domain>/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/<your-domain>/privkey.pem;

    location / {
        client_max_body_size 364M;
        proxy_pass http://localhost:3000;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

server {
    listen 80;
    server_name rpi-git <your-domain>;
    return 301 https://$host$request_uri;
}

Then enable the gitea nginx-site and restart the nginx server:

sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/gitea
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart

Don’t be afraid if nginx failed to start. We have to add ssl certificate first.

Now we need to create the ssl certificates for the domains.

cd ~
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
sudo ./certbot-auto certonly --standalone -d <your-domain>

In order to update the certificates automatically, we need to create a cron job via sudo crontab -e and insert the following line:

0 1 2 * * sudo service nginx stop && sudo /home/pi/certbot-auto renew && sudo service nginx start

This executes the three commands on every first day of a month at 2 AM.

Now we can start the nginx server.

sudo service nginx restart

Launch a browser and access the gitea website (https://<your-domain>), you’ll be redirected to the install page.

Update the following fields

Database type:        SQLite3
Root URL:             https://<your-domain>
Repository Root Path: /mnt/git/gitea-repositories 
LFS Root Path:        /mnt/git/data/lfs 
Domain:               <your-domain> 
Log Path:             /mnt/git/log

and click the ‘Install gitea’ button.

Then, cick the ‘Register’ button and create the first account, it will be your admin account.

Your gitea server is now up and running!

Posted in

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.