Hosting a Git repository management tool on your own server offers numerous advantages: full control, data security, and independence from third-party providers. While GitLab is often seen as the all-purpose solution, Gitea provides a minimalist and efficient alternative – particularly well-suited for smaller teams or specialized use cases.
In this article, we will cover:
- Why Gitea is a smart choice, especially when compared to GitLab.
- How to install and configure Gitea on your own server without using Docker.
- Step-by-step integration of PostgreSQL, SMTP, and HTTPS.
- Practical tips for troubleshooting and optimization based on our experiences.
Why Choose Gitea Over GitLab?
Gitea strikes an excellent balance between functionality and resource efficiency. It caters to users who need a powerful Git management tool but prefer to avoid the overhead of larger platforms like GitLab.
Advantages of Gitea:
- Lightweight and Efficient: Gitea requires significantly less disk space and CPU power compared to GitLab, making it ideal for servers with limited resources.
- Full Control Without Containers: Gitea can be natively installed on your server without relying on Docker or other containerization methods, minimizing security risks.
- Open and Free: Gitea is fully open source and free, with no premium features locked behind a paywall.
- Simplicity and Flexibility: Its configuration is straightforward and direct, avoiding complex requirements like Kubernetes.
Why Not GitLab?
- Resource Intensive: GitLab requires extensive server resources and is often oversized for small to medium projects.
- Complexity: Installing and maintaining GitLab is more challenging and often relies on Docker or Kubernetes.
- Costs: The free edition of GitLab has limited functionality, and advanced features require paid licenses.
Preparation
To securely and efficiently install Gitea on your server, some preparatory steps are necessary. Below, we outline the key actions to lay the foundation for a seamless installation.
Server Operating System
We are using Ubuntu LTS 22.04.5, as this version provides long-term support and is stable for server applications.
Server Details
-
Hostname: The server is accessible at
example.com
. - Firewall: Ensure that the necessary ports (e.g., 22, 80, 443, 587) are open.
Database
We are using PostgreSQL as the database for Gitea. PostgreSQL offers scalability and is ideal for servers running multiple applications.
Step-by-Step Guide
In this section, we will guide you through the steps to set up Gitea on your server. The instructions are modular, allowing you to follow and customize each step as needed.
1. Prepare the System
- Update the system: Ensure your server is up to date:
sudo apt update && sudo apt upgrade -y
- Create a user for Gitea: Add a dedicated user for Gitea to enhance security:
sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /var/lib/gitea gitea
- Set up the directory structure for Gitea: Create the necessary directories and set permissions:
sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R gitea:gitea /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/
2. Install Gitea
- Download the Gitea binary: Fetch the latest version of Gitea:
wget -O gitea https://dl.gitea.io/gitea/latest/gitea-latest-linux-amd64
- Move the binary and set permissions: Move the file to the executable directory and set permissions:
sudo mv gitea /usr/local/bin/
sudo chmod +x /usr/local/bin/gitea
- Configure the systemd service: Create a new service file for Gitea:
sudo vim /etc/systemd/system/gitea.service
Add the following content:
[Unit] Description=Gitea - Git with a cup of tea After=network.target Requires=network.target [Service] User=gitea Group=gitea WorkingDirectory=/var/lib/gitea ExecStart=/usr/local/bin/gitea web Restart=always Environment=USER=gitea HOME=/var/lib/gitea GITEA_WORK_DIR=/var/lib/gitea NoNewPrivileges=true PrivateTmp=true [Install] WantedBy=multi-user.target
- Start and enable the service: Start the Gitea service and enable it to run at boot:
sudo systemctl enable gitea
sudo systemctl start gitea
Installing and Configuring Apache2
Apache2 is a powerful and flexible web server that can securely and efficiently serve Gitea. In this section, we’ll show you how to install and configure Apache2 to deliver Gitea over HTTPS.
Why Apache2 Over Nginx?
- Flexibility: Apache2 supports a wide range of dynamically loadable modules, offering greater configuration flexibility.
- Dynamic Configuration: Virtual hosts and other configuration changes can often be made without restarting the server, simplifying operations.
- Compatibility: Apache2 is compatible with a variety of applications and configurations, making it ideal for versatile server environments.
- Scalability: Apache2 is well-suited for servers hosting multiple services and applications simultaneously, such as Gitea and other web-based tools.
1. Installing Apache2
- Installation: Install Apache2 on your server:
sudo apt install apache2 -y
- Enable required modules: Activate the necessary modules:
sudo a2enmod proxy proxy_http proxy_balancer lbmethod_byrequests ssl headers
sudo systemctl restart apache2
2. Creating a Virtual Host for Gitea
- Create the configuration file: Create a new configuration file for the virtual host:
sudo vim /etc/apache2/sites-available/gitea.conf
- Add configuration: Insert the following content:
<VirtualHost *:80> ServerName example.com ProxyPreserveHost On ProxyPass / http://127.0.0.1:3000/ ProxyPassReverse / http://127.0.0.1:3000/ ErrorLog ${APACHE_LOG_DIR}/gitea_error.log CustomLog ${APACHE_LOG_DIR}/gitea_access.log combined </VirtualHost>
- Enable the virtual host: Activate the new configuration and restart Apache2:
sudo a2ensite gitea.conf
sudo systemctl restart apache2
3. Setting Up HTTPS with Let’s Encrypt
- Install Certbot: Install Certbot to obtain a free SSL certificate:
sudo apt install certbot python3-certbot-apache -y
- Configure SSL: Run Certbot to generate a certificate and automatically configure Apache2:
sudo certbot --apache -d example.com
- Test the certificate: Verify the SSL configuration:
sudo apache2ctl configtest
sudo systemctl reload apache2
4. Automating Certificate Renewal
- Test renewal: Ensure the certificate renews automatically:
sudo certbot renew --dry-run
Conclusion
With Apache2 and HTTPS, you have set up Gitea securely and efficiently. Thanks to Apache2’s flexibility and scalabilit
Initial Configuration and Integration of PostgreSQL
In this section, we will set up the PostgreSQL database and integrate it with Gitea. This step is essential to provide a stable and performant foundation for Git management.
1. Install PostgreSQL
- Install PostgreSQL: Install PostgreSQL using the following command:
sudo apt install postgresql postgresql-contrib -y
- Enable PostgreSQL service: Ensure the PostgreSQL service is active and enabled at system startup:
sudo systemctl enable postgresql
sudo systemctl start postgresql
2. Create Database and User
- Switch to PostgreSQL user: Switch to the PostgreSQL user to execute the following commands:
sudo -i -u postgres
- Create the database: Create the Gitea database:
createdb gitea
- Create a user: Create a user with a password to access the Gitea database:
createuser gitea_user
psql
ALTER USER gitea_user WITH ENCRYPTED PASSWORD 'strong-password';
GRANT ALL PRIVILEGES ON DATABASE gitea TO gitea_user;
\q
- Logout: Exit the PostgreSQL user session:
exit
3. Integration with Gitea
- Gitea initial setup: Open your browser and navigate to the Gitea setup page (e.g.,
http://example.com:3000
). - Database information: Enter the following details:
- Database Type: PostgreSQL
- Host: localhost
- Port: 5432
- Database Name: gitea
- Username: gitea_user
- Password: The password you set earlier
4. Test the Connection
- Test connection: Click “Test Connection” to verify that Gitea can successfully connect to the PostgreSQL database.
- Troubleshooting: If errors occur, check the following:
- Your server’s firewall settings
- PostgreSQL permissions
- Correct login credentials
Benefits of the Gitea-PostgreSQL Integration
The integration of PostgreSQL as the database for Gitea offers numerous advantages, especially for scalable and high-performance server environments. Below, we outline the key benefits.
1. High Performance
- Efficient Processing: PostgreSQL is renowned for its fast and efficient query processing, which makes a significant difference for large repositories or many simultaneous users.
- Indexes and Optimization: Extensive support for indexes improves query speed and overall Gitea performance.
2. Scalability
- Multiple Applications: PostgreSQL can be used for multiple applications simultaneously, making it ideal for server environments running various services.
- Handling Large Data Volumes: PostgreSQL efficiently manages large datasets and complex queries, ensuring future readiness.
3. Advanced Features
- JSON Support: PostgreSQL provides native support for JSON, simplifying work with modern applications.
- Stored Procedures and Triggers: Advanced features such as stored procedures and triggers enable complex automation directly within the database.
4. Security
- Encryption: PostgreSQL supports encryption for connections and at the database level, protecting sensitive information.
- Fine-Grained Permissions: User permissions can be precisely controlled to restrict access to sensitive data.
5. Open Source and Cost-Free
- Free of Charge: PostgreSQL is a fully open-source database with no hidden costs or licensing fees.
- Active Community: The large and active community ensures regular updates, new features, and extensive support.
Conclusion
PostgreSQL is the ideal choice for Gitea installations, offering a reliable, scalable, and secure foundation. By integrating PostgreSQL, you gain a high-performance and future-proof solution.
SMTP Configuration for Email Notifications
Email notifications are a crucial feature to stay informed about changes, pull requests, or other events in Gitea. In this section, we explain how to set up and test SMTP.
1. Preparation
- SMTP Server Details: Ensure you have the credentials for your SMTP server:
- SMTP Address: The URL or IP address of the server (e.g.,
mail.example.com
). - Port: 587 (for TLS) or 465 (for SSL).
- Username: Your email address (e.g.,
admin@example.com
). - Password: The associated password for authentication.
- SMTP Address: The URL or IP address of the server (e.g.,
2. SMTP Configuration in the app.ini
File
- Edit the File: Open the
app.ini
file with a text editor:sudo vim /var/lib/gitea/custom/conf/app.ini
- Update the [mailer] Section: Add or modify the following settings:
[mailer] ENABLED = true SMTP_ADDR = mail.example.com SMTP_PORT = 587 FROM = "User" <user@example.com> USER = user@example.com PASSWD = YourPassword PROTOCOL = starttls
3. Test the Configuration
- Restart Gitea: Restart Gitea to apply the changes:
sudo systemctl restart gitea
- Send a Test Email: Navigate to “Site Administration > Configuration > Test Mail” in Gitea to send a test email.
- Troubleshooting: If issues occur:
- Check the SMTP settings in the
app.ini
file. - Ensure the firewall allows the required port (e.g., 587).
- Review Gitea logs for error details.
- Check the SMTP settings in the
4. Important Notes
- SPF, DKIM, and DMARC: Ensure your DNS records are correctly configured to ensure successful email delivery.
- Test with OpenSSL: Verify the SMTP connection manually:
openssl s_client -starttls smtp -connect mail.example.com:587
Conclusion and Recommendations
With the successful installation and configuration of Gitea, you now have a lightweight, powerful Git management tool that is both flexible and secure. By choosing PostgreSQL as the database, you have also established a scalable and future-proof solution.
1. Key Benefits at a Glance
- Ease of Maintenance: Gitea is easy to manage and requires fewer resources compared to other platforms.
- Security: With role-based access and integration capabilities for security tools, Gitea meets professional requirements.
- Cost Efficiency: Gitea and PostgreSQL are fully open source and involve no licensing costs.
2. Recommendations
- Regular Updates: Keep Gitea and PostgreSQL up to date to benefit from new features and security updates.
- Backup Strategy: Implement regular backups to prevent data loss. PostgreSQL offers numerous tools for this purpose.
- Further Optimizations:
- Gitea Actions (CI/CD): Leverage the built-in CI/CD functionality to automate workflows for builds, tests, and deployments directly in Gitea.
- Webhooks: Use webhooks to integrate Gitea with other tools or services, such as triggering notifications or external processes.
If you need assistance with the installation or optimization of Gitea, don’t hesitate to reach out. Our team is happy to help you find the best solution for your requirements.
Visit our website for more information or book a workshop to take your infrastructure to the next level!
Further Reading
Here is a selection of helpful links to learn more about Gitea, PostgreSQL, and related topics.
- Gitea Documentation: Official Documentation
- Gitea Community: Community Forum
- PostgreSQL Documentation: Official PostgreSQL Documentation
- SPF, DKIM, and DMARC: Email Security Guide
- OpenSSL Guide: OpenSSL Documentation