Setting Up Gitea on a Custom Server with Docker Compose and Nginx

Photo by Yancy Min on Unsplash

Setting Up Gitea on a Custom Server with Docker Compose and Nginx

Prerequisites

Before we begin, ensure you have the following:

  • A server running Ubuntu with Docker and Docker Compose installed.

  • A domain name pointing to your server's IP address.

  • Basic understanding of Docker, Docker Compose, and Nginx configuration.

Step 1: Docker Compose Configuration

Create a docker-compose.yml file with the following contents:

version: "3"

networks:
  gitea:
    external: false

services:
  server:
    image: gitea/gitea:latest
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
      - DB_TYPE=mysql
      - DB_HOST=db:3306
      - DB_NAME=gitea
      - DB_USER=gitea
      - DB_PASSWD=gitea
    restart: always
    networks:
      - gitea
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "222:22"
    depends_on:
      - db

  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=gitea
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
    networks:
      - gitea
    volumes:
      - ./mysql:/var/lib/mysql

This configuration defines two services: server for Gitea and db for MySQL.

Step 2: Nginx Configuration

Next, configure Nginx as a reverse proxy for Gitea. Replace the contents of your Nginx configuration file (usually located at /etc/nginx/sites-available/default) with the following:

server {
    listen 443 ssl;
    server_name your_domain.com;

    ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://localhost:3000;
    }
}

Replace your_domain.com with your actual domain name.

Step 3: Bringing Up Gitea

Navigate to the directory containing your docker-compose.yml file and run the following command to start Gitea:

docker-compose up -d

This command will start the Gitea and MySQL containers in detached mode.

Step 4: Testing Gitea

Open your web browser and navigate to your domain (e.g., https://your_domain.com). You should see the Gitea login page. Follow the prompts to set up your admin account and start using Gitea to manage your Git repositories.

Step 5: Configuring Auto-Start

To ensure Gitea starts automatically when your server boots up, you can use systemd to create a service unit. Follow these steps:

  1. Create a systemd service unit file for Gitea:

     sudo nano /etc/systemd/system/gitea.service
    

    Paste the following configuration into the file:

  2.  [Unit]
     Description=Gitea (Git with a cup of tea)
     After=syslog.target
     After=network.target
     After=mysql.service # Make sure Gitea starts after MySQL
    
     [Service]
     RestartSec=2s
     Type=simple
     User=root
     WorkingDirectory=/path/to/your/gitea/directory
     ExecStart=/usr/local/bin/docker-compose up -d
     ExecStop=/usr/local/bin/docker-compose down
    
     [Install]
     WantedBy=multi-user.target
    

    Replace /path/to/your/gitea/directory with the absolute path to the directory containing your docker-compose.yml file.

  3. Save the file and exit the text editor.

  4. Reload systemd to load the new service unit:

     sudo systemctl daemon-reload
    
  5. Enable the Gitea service to start on boot:

     sudo systemctl enable gitea.service
    

    Now, Gitea will start automatically when your server boots up. You can control the service using systemd commands like start, stop, restart, and status.

    That's it! Gitea is now set up to start automatically with your server.

Did you find this article valuable?

Support Tarik Omercehajic by becoming a sponsor. Any amount is appreciated!