Skip to content

Instantly share code, notes, and snippets.

@afragen
Forked from mirhec/raspi-setup-gitea.md
Last active December 20, 2023 16:47
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save afragen/e34e4b902a71a5550e04cb1ba7e0d711 to your computer and use it in GitHub Desktop.
Save afragen/e34e4b902a71a5550e04cb1ba7e0d711 to your computer and use it in GitHub Desktop.
Installing Gitea on Raspberry Pi 3 b+ with nginx and automatic backups

Setup Gitea on Raspberry Pi (3b+)

These instructions are based on this article: https://docs.gitea.io/en-us/install-from-source/.

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

Use Noobs to install Raspian.

Open menu Preferences > Raspberry Pi Settings

There you need to enable the SSH server and you should change the hostname.

Then configure the correct timezone:

Now log in via ssh and execute the following commands:

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

Install nginx

# Might need to update sources.list if nginx package isn't found.
sudo pico /etc/apt/sources.list
# uncomment last line
sudo apt-get update

sudo apt-get install nginx -y

Create a new user:

sudo adduser --disabled-login --gecos 'Gitea' git

Install latest release of Go

git clone https://github.com/udhos/update-golang
cd update-golang
sudo ./update-golang.sh

Updating version of Go

From the pi user issue the following command. sudo ./update-golang.sh

Then you will need to rebuild Gitea to use the updated version of Go.

Change to user git:

sudo su - git

Set environment variables

You will eventually need to add these environmental variables to each user pi, git, root

If the javascript heap is causing issues run the first command below.

export NODE_OPTIONS=--max_old_space_size=1024
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/git
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOPATH/src/gitea

Clone gitea and checkout latest tag

cd $GOPATH/src
git clone https://github.com/go-gitea/gitea
cd ./gitea

# Checkout the latest tag.
#git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
#git checkout $(git ls-remote --refs --tags https://github.com/go-gitea/gitea | awk '!/~|-dev|-rc/' | cut --delimiter='/' --fields=3 | sort --version-sort | tail --lines=1)
git checkout $(git ls-remote --refs --tags https://github.com/go-gitea/gitea | awk '!/~|-dev|-rc/' | awk '{print $2}' | awk -F '/' '{print $3}' | sort --version-sort | tail --lines=1)

Now type exit to go back to pi user

Build gitea under root user

sudo su - root

Reset environment variables above with root user

Install NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Now type exit and close the terminal window, then open a new terminal window

This is so the NVM path variables will work.

Now install node lts/fermium and build.

sudo su - root
cd $GOPATH/src/gitea

# Install Node
nvm ls
nvm install node --lts/fermium
nvm use lts/fermium

# Build can take 30-40 minutes or more
TAGS="bindata sqlite sqlite_unlock_notify" make build

Now type exit to go back to pi user.

Configure Gitea and Nginx

Create/Edit /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/src/gitea
ExecStart=/home/git/src/gitea/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

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

server {
    listen 80;
    server_name gitea;
    
    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;
    }
}

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 service nginx restart

Update Gitea

sudo su - git

# Load environmental variables to $PATH as above

cd $GOPATH/src/gitea/
git fetch --all --tags

# Checkout the latest tag.
# git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
# git checkout $(git ls-remote --refs --tags https://github.com/go-gitea/gitea | awk '!/~|-dev|-rc/' | cut --delimiter='/' --fields=3 | sort --version-sort | tail --lines=1)
git checkout $(git ls-remote --refs --tags https://github.com/go-gitea/gitea | awk '!/~|-dev|-rc/' | awk '{print $2}' | awk -F '/' '{print $3}' | sort --version-sort | tail --lines=1)

exit
sudo su - root

# Load environmental variables to $PATH as above

cd $GOPATH/src/gitea
nvm use lts/fermium

# Build can take up to 30 minutes or so.
TAGS="bindata sqlite sqlite_unlock_notify" make build

# If pre-built frontend files are present it is possible to only build the backend.
TAGS="bindata" make backend

exit
sudo systemctl restart gitea

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):

#!/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/gitea/custom /home/git/src/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:

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.

Virtual memory exhausted: Cannot allocate memory

https://www.bitpi.co/2015/02/11/how-to-change-raspberry-pis-swapfile-size-on-rasbian/

sudo nano /etc/dphys-swapfile

Default value in Raspbian is: CONF_SWAPSIZE=100

Change to: CONF_SWAPSIZE=2048

Restart swapfile service

sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start

Check memory+swap free -m

You should probably switch the swapsize back once the installation is complete to avoid potential disk issues.

You should find your instance of Gitea at http://localhost:3000, where localhost is the LAN IP of your Raspberry Pi. On your Raspberry Pi you should find it at http://gitea:3000

@lituss
Copy link

lituss commented May 21, 2019

it works , i needed to increase swap file to compile.
the binaries does't work for my raspberry.
Thank you!!!

@DanieBeets
Copy link

Thanks for this!

Copy link

ghost commented Dec 21, 2019

I need help !
I did everything and had no error but i don't know if it worked (localhost and the ip leads to nginx default message) so where to find gitea once installed ?

@afragen
Copy link
Author

afragen commented Dec 21, 2019

@Natendrtfm since it's running on your LAN, look at your router and find the local IP address. It should be running on port 3000.

So the address would be something like http://192.126.1.3:3000, essentially the http://localhost:3000 for your Raspberry Pi.

@erkenes
Copy link

erkenes commented Jan 20, 2020

@Natendrtfm since it's running on your LAN, look at your router and find the local IP address. It should be running on port 3000.

So the address would be something like http://192.126.1.3:3000, essentially the http://localhost:3000 for your Raspberry Pi.

I have the same issue.
At 192.168.178.91(IP-Adress of my PI 3B+) there is a message that nginx is running but at 192.178.168.91:3000 my browser says that this site is not reachable.

i used curl -s localhost:3000 >/dev/null && echo Success. || echo Fail. on my Pi via SSH to look if the site is available local but that didn`t worked.

@afragen
Copy link
Author

afragen commented Jan 20, 2020

@enese I'm really not certain what's going on on your Pi. You might need to reboot it and see if that helps.

Does it work when using the browser on the Raspberry Pi?

@erkenes
Copy link

erkenes commented Jan 21, 2020

@enese I'm really not certain what's going on on your Pi. You might need to reboot it and see if that helps.

Does it work when using the browser on the Raspberry Pi?

Reboot doesn't helped
And I can't use the Browser on my Pi because im using Raspbian Buster Lite
But I cant ping the Port via SSH so i think it wouldn't work anyway

@afragen
Copy link
Author

afragen commented Jan 21, 2020

It's possible that using Raspian Buster Lite is causing the issue. I'm using a simple Raspian image. I'm not really expert at setting up a Raspberry Pi.

@MunsKlo
Copy link

MunsKlo commented Jan 29, 2020

branch between the <> is a placeholder, right? But with what must i fill it?

@afragen
Copy link
Author

afragen commented Jan 29, 2020

@MunsKlo, yes that is a placeholder. I usually just use branch as in git checkout master

@djonmayer
Copy link

@afragen
What is the best way to restore the backup?

@afragen
Copy link
Author

afragen commented Jun 26, 2020

Updated instructions for latest versions of Gitea.

@unbraind
Copy link

i got the following error while building "node lts/dubnium":

FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
make: *** [Makefile:651: public/js/index.js]

I already increased the swap file but I still get the same error

@afragen
Copy link
Author

afragen commented Oct 15, 2020

I’m sorry but somehow increasing your memory allocation is needed. I don’t know how to tell you to do this. The instructions above currently work for me, at least they did at last update 😉

@unbraind
Copy link

I managed to get it working but I'm not sure, what exactly helped
I think it built successfully when I put the following in my .bashrc:
export NODE_OPTIONS=--max_old_space_size=1024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment