Skip to content

Instantly share code, notes, and snippets.

@mirhec
Created September 20, 2017 07:14
Show Gist options
  • Save mirhec/1c8af440aa4917d6ccd61ea628e5510c to your computer and use it in GitHub Desktop.
Save mirhec/1c8af440aa4917d6ccd61ea628e5510c to your computer and use it in GitHub Desktop.

Revisions

  1. mirhec created this gist Sep 20, 2017.
    200 changes: 200 additions & 0 deletions raspi-setup-gitea.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,200 @@
    # Setup Gitea on Raspberry Pi (3)

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

    Setup Raspberry Pi with minimal Raspbian image. You need to connect to the HDMI port and set the following:

    ```bash
    sudo raspi-config
    ```
    There you need to enable the SSH server and you should change the hostname.

    Then configure the correct timezone:

    ```bash
    sudo dpkg-reconfigure tzdata
    ```

    Now log in via ssh and execute the following commands:

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

    # Create a new user:
    sudo adduser --disabled-login --gecos 'Gitea' git

    # Install go 1.8.1
    wget https://storage.googleapis.com/golang/go1.8.1.linux-armv6l.tar.gz
    sudo tar -C /usr/local -xzf go1.8.1.linux-armv6l.tar.gz

    # Change to user git:
    sudo su - git

    # Set environment variables
    export PATH=/usr/local/go/bin:$PATH
    export GOPATH="$HOME/"

    # Download and build go-bindata
    go get github.com/jteeuwen/go-bindata
    cd $GOPATH/src/github.com/jteeuwen/go-bindata/go-bindata
    export PATH=$GOPATH/src/github.com/jteeuwen/go-bindata/go-bindata:$PATH

    # Download the latest gitea version:
    go get -d -u code.gitea.io/gitea
    cd $GOPATH/src/code.gitea.io/gitea

    # Build it
    TAGS="bindata sqlite" make generate build

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

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

    ```bash
    [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/src/code.gitea.io/gitea
    ExecStart=/home/git/src/code.gitea.io/gitea/gitea web web
    Restart=always
    Environment=USER=git HOME=/home/git

    [Install]
    WantedBy=multi-user.target
    ```

    ```bash
    # Start gitea:
    sudo systemctl enable gitea
    sudo systemctl start gitea
    ```
    ## Install and setup nginx with ssl
    ```bash
    # 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 gitea <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 gitea <your-domain>;
    return 301 https://$host$request_uri;
    }
    ```

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

    ```bash
    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:
    ```bash
    0 1 2 * * sudo service nginx stop && sudo /home/pi/certbot-auto renew --dry-run && sudo service nginx start
    ```

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

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

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

    ## Create backup-script
    Now we want to create a backup script that is run by crontab every day. To do so, create a new file called `gitea-backup.sh` and insert the following content (replace YOUR_SERVER/YOUR/DIR as well as USER and PASSWORD):

    ```bash
    #!/bin/bash

    # Create directory if it does not exist
    mkdir -p git

    # Mount NAS directory
    sudo mount -t cifs //<your-nas>/Git git -o user=<your-nas-user>,pass=<your-password>

    # Create backup directory if it does not exist and cd into it
    mkdir -p git/gitea-backups

    # Run the backup job
    sudo zip -r "git/gitea-backups/gitea-backup-$(date +"%Y-%m-%d %H-%M-%S").zip" /home/git/src/code.gitea.io/gitea/custom /home/git/src/code.gitea.io/gitea/data /home/git/gitea-repositories

    # Remove all files, keeping the last recent one and cd back
    cd git/gitea-backups
    ls -t1 . | tail -n +3 | xargs -d '\n' rm -f
    cd ../..

    # Allow changing the backup dir and files
    sudo chmod -R 777 git/gitea-backups

    # Clean up everything
    sudo umount git
    rmdir git
    ```

    Now `chmod +x gitea-backup.sh` and create a new crontab entry:

    ```bash
    crontab -e

    # Now add the following line:
    0 0 * * * /home/pi/gitea-backup.sh
    ```

    This calls our script everyday at 0:00 AM, creates a gogs dump and copies it to our network share.

    ## Update Gitea

    ```bash
    sudo su - git
    export GOPATH="$HOME/"
    export PATH=$GOPATH/src/github.com/jteeuwen/go-bindata/go-bindata:$PATH
    export PATH=/usr/local/go/bin:$PATH
    cd $GOPATH/src/code.gitea.io/gitea/
    git checkout HEAD -- public/swagger.v1.json
    go get -d -u code.gitea.io/gitea
    TAGS="bindata sqlite" make generate build
    exit
    sudo systemctl restart gitea
    ```