# curl https://devtools.krishanchawla.com/raw/github-webhook-php.txt GITHUB WEBHOOKS - PHP PHP native implementation for GitHub webhooks with signature verification and deployment Category: Git · Type: snippet Official docs: https://docs.github.com/en/webhooks ──────────────────────────────────────────────────────────── ## What is GitHub Webhooks? **GitHub Webhooks** allow you to receive real-time HTTP notifications when events occur in your repositories. This guide shows PHP implementation with HMAC signature verification for secure automated deployments on Apache and Nginx servers. --- ## 🐘 PHP - Native Handler PHP webhook handler using native functions. ```php ``` ## 🔐 PHP - Verify Signature Signature verification function. ```php ``` ## 📖 PHP - Parse JSON Payload Parse and process webhook payload. ```php ``` ## ✅ PHP - Complete Example Full working PHP webhook handler with error handling. ```php &1"); file_put_contents($log_file, "Deploy output: $output\n", FILE_APPEND); } http_response_code(200); echo "OK"; ?> ``` ## ▶️ Run - Development Run PHP development server. ```bash # Start built-in server php -S 0.0.0.0:8000 # Test webhook curl -X POST http://localhost:8000/webhook.php \ -H "X-GitHub-Event: push" \ -d '{"ref":"refs/heads/main"}' ``` ## 🪶 Run - Apache Apache configuration for PHP webhook. ```apache ServerName webhook.example.com DocumentRoot /var/www/webhook AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/webhook_error.log CustomLog ${APACHE_LOG_DIR}/webhook_access.log combined ``` ## Run - Nginx + PHP-FPM Nginx configuration with PHP-FPM. ```nginx server { listen 80; server_name webhook.example.com; root /var/www/webhook; location /webhook.php { fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` ## 🔑 Environment Variables Store secret in environment file. **Apache** - Add to `.htaccess`: ```apache SetEnv WEBHOOK_SECRET your_secret_here ``` **Nginx** - Add to site config: ```nginx fastcgi_param WEBHOOK_SECRET your_secret_here; ``` **PHP-FPM** - Add to pool config `/etc/php/8.1/fpm/pool.d/www.conf`: ```ini env[WEBHOOK_SECRET] = your_secret_here ``` ## 🐳 Docker Deployment Containerize PHP webhook handler. ```dockerfile FROM php:8.1-apache COPY webhook.php /var/www/html/ RUN chown -R www-data:www-data /var/www/html EXPOSE 80 ``` Build and run: ```bash docker build -t github-webhook-php . docker run -p 8000:80 -e WEBHOOK_SECRET=your_secret github-webhook-php ``` ## 🔒 File Permissions Set proper permissions for deployment script. ```bash # Make deploy script executable chmod +x /path/to/deploy.sh # Give PHP user permission (if needed) sudo chown www-data:www-data /path/to/deploy.sh # Or add www-data to appropriate group sudo usermod -aG deployers www-data ``` ## 📝 Logging Enhanced logging for debugging. ```php ``` View logs: ```bash tail -f /var/log/github-webhook.log ``` ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/github-webhook-php More tools: https://devtools.krishanchawla.com