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


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"

    external: false

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

    image: mysql:5.7
    restart: always
      - MYSQL_USER=gitea
      - MYSQL_PASSWORD=gitea
      - MYSQL_DATABASE=gitea
      - gitea
      - ./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;

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

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

Replace 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., 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=mysql.service # Make sure Gitea starts after MySQL
     ExecStart=/usr/local/bin/docker-compose up -d
     ExecStop=/usr/local/bin/docker-compose down

    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!