summaryrefslogtreecommitdiff
path: root/cgit-setup-auto.sh
diff options
context:
space:
mode:
authorhc <hc@email.ch>2025-05-19 19:45:55 +0800
committerhc <hc@email.ch>2025-05-19 19:45:55 +0800
commit35321b6c3884491f4f559b3f4199e4563178f874 (patch)
tree41c7cb232d11fcb335e43964d87aa7fd759e69ab /cgit-setup-auto.sh
Diffstat (limited to 'cgit-setup-auto.sh')
-rw-r--r--cgit-setup-auto.sh125
1 files changed, 125 insertions, 0 deletions
diff --git a/cgit-setup-auto.sh b/cgit-setup-auto.sh
new file mode 100644
index 0000000..b4e6c53
--- /dev/null
+++ b/cgit-setup-auto.sh
@@ -0,0 +1,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."