Gitea is a lightweight, self-hosted Git repository management solution that provides a clean, easy-to-use interface similar to GitHub. In this guide, we'll walk you through installing Gitea on an Amazon Lightsail VPS.

## Prerequisites

Before you begin, ensure you have:

- An Amazon Lightsail account
- A Lightsail VPS running Ubuntu
- SSH access to your Lightsail instance
- Basic command-line knowledge

## Step 1: Prepare Your Lightsail Instance

### Create and Connect to Your Instance

1. Log in to the Amazon Lightsail console
2. Create a new Ubuntu instance
3. Choose an appropriate plan (minimum 1GB RAM recommended)
4. Connect via SSH through the Lightsail browser terminal or your local SSH client

### Configure Lightsail Firewall

Open the Networking tab and add the following firewall rules:

- HTTP (Port 80)
- HTTPS (Port 443)
- SSH (Port 22)
- Gitea (Port 3000)

## Step 2: Update Your System

Update system packages to ensure you have the latest versions:

```
sudo apt update
sudo apt upgrade -y
```

## Step 3: Install Dependencies

Install necessary dependencies for Gitea:

```
sudo apt install -y git sqlite3 wget nginx
```

## Step 4: Create Gitea User

Create a dedicated user for Gitea:

```
sudo adduser --system --group --disabled-password --shell /bin/bash --home /home/gitea gitea
```

## Step 5: Download Gitea

Download the latest Gitea binary:

```
wget -O gitea https://dl.gitea.io/gitea/latest/gitea-1.20.3-linux-amd64
sudo chmod +x gitea
sudo mv gitea /usr/local/bin/
```

## Step 6: Create Directory Structure

Set up the necessary directories:

```
sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R gitea:gitea /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/
```

## Step 7: Configure Systemd Service

Create a systemd service file for Gitea:

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

Paste the following configuration:

```
[Unit]
Description=Gitea
After=syslog.target
After=network.target

[Service]
RestartSec=2s
Type=simple
User=gitea
Group=gitea
WorkingDirectory=/var/lib/gitea/
ExecStart=/usr/local/bin/gitea web -c /etc/gitea/app.ini
Restart=always
Environment=USER=gitea HOME=/home/gitea

[Install]
WantedBy=multi-user.target
```

## Step 8: Configure Nginx Reverse Proxy

Create an Nginx configuration:

```
sudo nano /etc/nginx/sites-available/gitea
```

Add the following configuration:

```
server {
    listen 80;
    server_name gitea.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
```

Enable the site:

```
sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
```

## Step 9: Performance Optimization for Lightsail

Add a swap file to improve performance on smaller instances:

```
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo scp /swapfile /etc/fstab
sudo swapon /swapfile
```

## Step 10: Start and Enable Gitea

Start the Gitea service and enable it to run on boot:

```
sudo systemctl start gitea
sudo systemctl enable gitea
```

## Step 11: SSL Configuration with Let's Encrypt

Install Certbot:

```
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

# Generate SSL for your domain
sudo certbot --nginx -d gitea.yourdomain.com
```

## Security Recommendations

1. Use a strong, unique password
2. Keep Gitea and system updated
3. Configure two-factor authentication
4. Regularly backup your Gitea data

## Lightsail Cost-Saving Tips

- Choose an appropriate instance size
- Use Lightsail's snapshot feature
- Stop instances when not in use
- Monitor instance metrics

## Troubleshooting

If you encounter issues, check the logs:

```
# Gitea logs
sudo journalctl -u gitea

# Nginx logs
sudo tail -f /var/log/nginx/error.log
```

## Connecting Your Gitea Repository to DeployHQ

After setting up your Gitea instance, you can easily integrate it with [DeployHQ](https://www.deployhq.com) for streamlined deployments. Follow these steps:

### Manual Repository Addition

1- **Generate an Access Token in Gitea**

```
- Navigate to your Gitea user settings
- Go to "Applications"
- Create a new Personal Access Token
    - Select appropriate scopes (read:repository recommended)
    - Copy the generated token securely
```

2- **Add Repository in DeployHQ**

```
- Log in to your DeployHQ account
- Click "Add Repository"
- Choose "Git" as the repository type
- Select "Other Git Provider"
```

3- **Repository Connection Details**

```
- Repository URL: `https://your-gitea-domain.com/username/repository.git`
- Authentication Type: Token
- Username: Your Gitea username
- Password/Token: Paste the generated access token
```

### Important Configuration Notes

- Ensure the access token has sufficient permissions
- Use HTTPS URL for repository connection
- Verify firewall and network settings allow [DeployHQ](https://www.deployhq.com) to access your Gitea instance

### Deployment Setup

Once connected, you can:

- Configure deployment branches
- Set up automatic deployments
- Define custom deployment scripts
- Monitor deployment history

### Troubleshooting Repository Connection

- Double-check the repository URL
- Verify access token permissions
- Ensure Gitea is publicly accessible
- Check network connectivity

Pro Tip: [DeployHQ](https://www.deployhq.com) supports multiple deployment strategies, making it perfect for your self-hosted Gitea repositories!

## Conclusion

You've successfully installed Gitea on Amazon Lightsail! Enjoy your self-hosted Git repository management platform.

Pro Tip: Use [DeployHQ](https://www.deployhq.com) for seamless deployment of your projects directly to your Gitea repository!

* * *

## Additional Resources

- [Gitea Documentation](https://docs.gitea.com/)
- [Amazon Lightsail Documentation](https://docs.aws.amazon.com/lightsail/latest/userguide/what-is-amazon-lightsail.html)

