summaryrefslogtreecommitdiff
path: root/cgit-setup-auto.sh
blob: b4e6c53fce9af34190cb5516b2d2f2c59904dc26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash

# CGit + Nginx Setup Script with Certbot SSL
# Usage: ./cgit-setup.sh <domain> <email>
# Example: ./cgit-setup.sh sg2.0nom.ch hc@email.ch

set -e

# Check arguments
if [ $# -ne 2 ]; then
    echo "Usage: $0 <domain> <email>"
    echo "Example: $0 domain.com name@email.com"
    exit 1
fi

DOMAIN="$1"
EMAIL="$2"

echo "Starting CGit + Nginx setup for $DOMAIN with email $EMAIL..."

# Update system and install dependencies
echo "Installing dependencies..."
sudo dnf install -y epel-release
sudo dnf install -y tmux git nginx fcgiwrap git gcc make openssl-devel zlib-devel
sudo dnf install -y certbot python3-certbot-nginx

# Clone and build cgit
echo "Building cgit..."
if [ ! -d "cgit" ]; then
    git clone https://git.zx2c4.com/cgit
fi

cd cgit
git submodule init
git submodule update
make
sudo make install
cd ..

# Create directories and set permissions
echo "Setting up directories..."
sudo mkdir -p /git /var/www/htdocs/cgit
sudo chown -R nginx:nginx /var/www/htdocs/cgit
sudo chgrp -R nginx /git
sudo chmod -R g+s /git
sudo chmod -R 775 /git

# Configure cgit
echo "Configuring cgit..."
sudo tee /etc/cgitrc > /dev/null <<EOL
css=/cgit.css
logo=/cgit.png
virtual-root=/
cache-size=200
scan-path=/git
EOL

sudo chown nginx:nginx /etc/cgitrc
sudo chmod 664 /etc/cgitrc

# Configure nginx for cgit (certbot-compatible)
echo "Configuring nginx..."
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

sudo tee /etc/nginx/conf.d/cgit.conf > /dev/null <<EOL
server {
    listen 80;
    server_name $DOMAIN;
    root /var/www/htdocs/cgit/;
    
    location / {
        try_files \$uri @cgit;
    }
    
    location @cgit {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME \$document_root/cgit.cgi;
        fastcgi_param PATH_INFO \$uri;
        fastcgi_param QUERY_STRING \$args;
        fastcgi_param HTTP_HOST \$server_name;
        fastcgi_pass unix:/run/fcgiwrap/fcgiwrap-nginx.sock;
    }
}
EOL

# Start services
echo "Starting services..."
sudo systemctl enable --now fcgiwrap@nginx.socket
sudo systemctl enable --now nginx

# Wait for nginx to start
sleep 2

# Run certbot to enable SSL
echo "Setting up SSL with certbot for domain: $DOMAIN and email: $EMAIL"
sudo certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos --email "$EMAIL" --redirect

# Add HTTPS parameter to cgit location after certbot configuration
echo "Updating configuration for HTTPS..."
sudo sed -i '
/listen 443 ssl/,/^}/ {
    /@cgit/,/}/ {
        /fastcgi_param HTTP_HOST/a\        fastcgi_param HTTPS on;
    }
}' /etc/nginx/conf.d/cgit.conf

# Test and reload nginx
echo "Testing configuration..."
sudo nginx -t
sudo systemctl reload nginx

# Configure git
git config --global init.defaultBranch main

echo ""
echo "Setup complete!"
echo "CGit is now available at: https://$DOMAIN"
echo "SSL certificate obtained for: $EMAIL"
echo ""
echo "To create a new repository:"
echo "  cd /git"
echo "  sudo git init --bare myrepo.git"
echo "  sudo chown -R nginx:nginx myrepo.git"
echo ""
echo "The repository will be automatically visible in cgit."