nginx-tor-start.sh (3877B)
1 #!/bin/bash 2 set -e 3 4 echo "Starting Nginx + Tor services..." 5 6 # Ensure Tor directories have correct permissions 7 echo "Setting up Tor directory permissions..." 8 chown -R debian-tor:debian-tor /var/lib/tor 9 chmod 700 /var/lib/tor 10 chmod 700 /var/lib/tor/hidden_service 11 chmod 755 /var/log/tor 12 chown -R debian-tor:debian-tor /var/log/tor 13 14 # Ensure web directory permissions 15 echo "Setting up web directory permissions..." 16 chown -R www-data:www-data /var/www/html 17 find /var/www/html -type d -exec chmod 755 {} \; 18 find /var/www/html -type f -exec chmod 644 {} \; 19 20 # Ensure SQLite directory permissions if it exists 21 if [ -d /var/lib/sqlite ]; then 22 echo "Setting up SQLite directory permissions..." 23 chown -R www-data:www-data /var/lib/sqlite 24 chmod 755 /var/lib/sqlite 25 find /var/lib/sqlite -name "*.sqlite*" -exec chmod 664 {} \; 2>/dev/null || true 26 fi 27 28 # Install process monitoring tools 29 if ! command -v ps >/dev/null 2>&1; then 30 apt-get update && apt-get install -y procps && rm -rf /var/lib/apt/lists/* 31 fi 32 33 echo "Creating log file and fixing permissions..." 34 touch /var/log/tor/tor.log 35 chown debian-tor:debian-tor /var/log/tor/tor.log 36 chmod 640 /var/log/tor/tor.log 37 38 echo "Testing Tor configuration..." 39 # Run Tor configuration test and capture output 40 TOR_TEST_OUTPUT=$(su debian-tor -c "tor --verify-config -f /etc/tor/torrc" 2>&1) || { 41 echo "Tor configuration error: $TOR_TEST_OUTPUT" 42 echo "Attempting to start anyway with basic config..." 43 } 44 45 echo "Starting Tor hidden service..." 46 # Start Tor and capture any immediate errors 47 su debian-tor -c "tor -f /etc/tor/torrc" & 48 TOR_PID=$! 49 echo "Tor started with PID: $TOR_PID" 50 51 # Give it a moment to initialize 52 sleep 3 53 54 # Check if the process is still running 55 if ! kill -0 $TOR_PID 2>/dev/null; then 56 echo "ERROR: Tor process died immediately! Checking logs..." 57 cat /var/log/tor/tor.log 2>/dev/null || echo "No log file found" 58 echo "Trying to run Tor in foreground to see error:" 59 su debian-tor -c "tor -f /etc/tor/torrc" || true 60 fi 61 62 # Wait for Tor to initialize and generate hostname 63 echo "Waiting up to 3 minutes for Tor to generate hidden service hostname..." 64 WAIT_COUNT=0 65 while [ ! -f /var/lib/tor/hidden_service/hostname ] && [ $WAIT_COUNT -lt 90 ]; do 66 sleep 2 67 WAIT_COUNT=$((WAIT_COUNT + 1)) 68 if [ $((WAIT_COUNT % 15)) -eq 0 ]; then 69 echo "Still waiting for Tor... ($WAIT_COUNT/90)" 70 # Check if Tor process is still running 71 if ! kill -0 $TOR_PID 2>/dev/null; then 72 echo "ERROR: Tor process died! Attempting restart with debug..." 73 cat /var/log/tor/tor.log 2>/dev/null || echo "No log file found" 74 su debian-tor -c "tor -f /etc/tor/torrc" & 75 TOR_PID=$! 76 fi 77 fi 78 done 79 80 # Extract and save hostname 81 if [ -f /var/lib/tor/hidden_service/hostname ]; then 82 HOSTNAME=$(cat /var/lib/tor/hidden_service/hostname) 83 echo "✅ Hidden service hostname: $HOSTNAME" 84 # Save to unique file based on container hostname 85 CONTAINER_NAME=$(hostname) 86 echo "$HOSTNAME" > /var/www/html/hostname_${CONTAINER_NAME}.txt 87 chown www-data:www-data /var/www/html/hostname_${CONTAINER_NAME}.txt 88 chmod 644 /var/www/html/hostname_${CONTAINER_NAME}.txt 89 echo "Hostname saved to /var/www/html/hostname_${CONTAINER_NAME}.txt" 90 else 91 echo "❌ Warning: Could not find Tor hostname file after 3 minutes" 92 CONTAINER_NAME=$(hostname) 93 echo "Failed to generate hostname" > /var/www/html/hostname_${CONTAINER_NAME}.txt 94 echo "Final log check:" 95 cat /var/log/tor/tor.log 2>/dev/null || echo "No log file found" 96 fi 97 98 # Function to handle shutdown 99 shutdown() { 100 echo "Shutting down services..." 101 kill $TOR_PID 2>/dev/null || true 102 nginx -s quit 2>/dev/null || true 103 exit 0 104 } 105 106 # Trap shutdown signals 107 trap shutdown SIGTERM SIGINT 108 109 echo "Starting Nginx..." 110 # Start Nginx in foreground 111 exec nginx -g 'daemon off;'