How to Set Up a Secure Self-Hosted Git Server Using Gitea

For developers and teams who want full control over their code repositories, a self-hosted Git server is a great alternative to services like GitHub and GitLab. Gitea is a lightweight, open-source Git hosting solution that offers a fast and simple way to manage repositories. In this guide, we’ll walk you through setting up a secure self-hosted Git server using Gitea.

1. Prerequisites

  • A server (VPS or on-premises) with at least 1 CPU core and 512MB RAM.
  • A domain or static IP address.
  • Basic knowledge of Linux command-line operations.
  • Docker installed on the server (recommended for easy setup).

2. Install Gitea with Docker

Using Docker simplifies deployment and keeps Gitea isolated from other services on your server. Run the following commands to set up Gitea:

mkdir -p /opt/gitea/{data,config}
docker run -d --name gitea \
  -p 3000:3000 -p 2222:22 \
  -v /opt/gitea/data:/data \
  -v /opt/gitea/config:/config \
  --restart=always \
  gitea/gitea:latest

This will download and start the Gitea container, exposing the web interface on port 3000 and SSH access on port 2222.

3. Configure Gitea

Once the container is running, open your browser and navigate to http://your-server-ip:3000. Follow the setup wizard:

  • Choose SQLite, PostgreSQL, or MySQL as your database (PostgreSQL recommended).
  • Set the repository root path.
  • Configure SSH and HTTP settings.
  • Create an administrator account.

4. Secure Your Git Server

To enhance security, consider these measures:

  • Enable HTTPS using a reverse proxy like Nginx with Let’s Encrypt SSL certificates.
  • Restrict SSH access to only trusted users by modifying the SSH daemon settings.
  • Regularly update Gitea to patch security vulnerabilities.

5. Using Gitea

Now that Gitea is up and running, you can create repositories, manage users, and integrate CI/CD tools for automated workflows. Clone a repository using:

git clone ssh://git@your-server-ip:2222/username/repository.git

Conclusion

A self-hosted Git server with Gitea provides flexibility and security while allowing complete control over your code. With proper configuration and security measures, you can ensure a robust development workflow without relying on third-party services.

Illustration of Gitea Git Server Setup:

Comments