|
| 1 | +# Production Deployment Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `--production-ready` flag creates a deployment-ready Laravel bundle with obfuscated source code. This guide shows you how to create and deploy the bundle to your production server. |
| 6 | + |
| 7 | +## What's Included/Excluded |
| 8 | + |
| 9 | +### ✅ Included in Bundle |
| 10 | + |
| 11 | +- **Source code** (obfuscated based on `--source` flags) |
| 12 | +- **Application structure**: `app/`, `routes/`, `config/`, `database/`, `resources/`, `public/` |
| 13 | +- **Dependency manifests**: `composer.json`, `package.json` |
| 14 | +- **Entry points**: `artisan`, `public/index.php`, `server.php` |
| 15 | +- **Public assets**: CSS, JS, images in `public/` |
| 16 | +- **Views and templates**: Blade files, React/Vue components |
| 17 | + |
| 18 | +### ❌ Excluded from Bundle |
| 19 | + |
| 20 | +- **Dependencies**: `vendor/`, `node_modules/` (install on server) |
| 21 | +- **Environment files**: `.env*` (configure on server) |
| 22 | +- **Version control**: `.git/`, `.github/` |
| 23 | +- **Development tools**: `tests/`, `phpunit.xml`, `.editorconfig` |
| 24 | +- **Lock files**: `composer.lock`, `package-lock.json` (generated during install) |
| 25 | +- **Cache/logs**: `storage/logs/`, `bootstrap/cache/` |
| 26 | + |
| 27 | +## Step 1: Create Production Bundle |
| 28 | + |
| 29 | +### Basic Usage |
| 30 | + |
| 31 | +```bash |
| 32 | +# Obfuscate all configured paths (from config/obfuscator.php) |
| 33 | +php artisan obfuscate:run --production-ready |
| 34 | + |
| 35 | +# Obfuscate specific directories only |
| 36 | +php artisan obfuscate:run --production-ready --source=app --source=routes |
| 37 | + |
| 38 | +# Custom destination |
| 39 | +php artisan obfuscate:run --production-ready --destination=deploy/production |
| 40 | + |
| 41 | +# Non-interactive (for CI/CD) |
| 42 | +php artisan obfuscate:run --production-ready --force |
| 43 | +``` |
| 44 | + |
| 45 | +### Example: Full Production Build |
| 46 | + |
| 47 | +```bash |
| 48 | +# Create production bundle with obfuscated app and routes |
| 49 | +php artisan obfuscate:run \ |
| 50 | + --production-ready \ |
| 51 | + --destination=deploy/$(date +%Y%m%d-%H%M%S) \ |
| 52 | + --source=app \ |
| 53 | + --source=routes \ |
| 54 | + --force |
| 55 | + |
| 56 | +# Save the encryption key shown in the output! |
| 57 | +# Example: Encryption Key: ABC123XYZ |
| 58 | +``` |
| 59 | + |
| 60 | +**Important**: Save the encryption key! You'll need it in Step 4. |
| 61 | + |
| 62 | +## Step 2: Package the Bundle |
| 63 | + |
| 64 | +```bash |
| 65 | +# Navigate to your bundle directory |
| 66 | +cd deploy/20260107-150000 |
| 67 | + |
| 68 | +# Create a tarball (recommended for deployment) |
| 69 | +tar -czf ../production-bundle.tar.gz . |
| 70 | + |
| 71 | +# Or create a zip file |
| 72 | +zip -r ../production-bundle.zip . |
| 73 | +``` |
| 74 | + |
| 75 | +## Step 3: Upload to Server |
| 76 | + |
| 77 | +### Option A: Direct Upload |
| 78 | + |
| 79 | +```bash |
| 80 | +# Using SCP |
| 81 | +scp production-bundle.tar.gz user@server:/var/www/html/ |
| 82 | + |
| 83 | +# Using rsync |
| 84 | +rsync -avz --exclude='.git' deploy/20260107-150000/ user@server:/var/www/html/ |
| 85 | +``` |
| 86 | + |
| 87 | +### Option B: Via CI/CD |
| 88 | + |
| 89 | +See [CI-CD-EXAMPLES.md](./CI-CD-EXAMPLES.md) for GitHub Actions, GitLab CI, and Jenkins examples. |
| 90 | + |
| 91 | +## Step 4: Server Setup |
| 92 | + |
| 93 | +### 1. Extract Bundle |
| 94 | + |
| 95 | +```bash |
| 96 | +ssh user@server |
| 97 | + |
| 98 | +cd /var/www/html |
| 99 | +tar -xzf production-bundle.tar.gz |
| 100 | +rm production-bundle.tar.gz |
| 101 | +``` |
| 102 | + |
| 103 | +### 2. Install Dependencies |
| 104 | + |
| 105 | +```bash |
| 106 | +# Install PHP dependencies (production only, optimized) |
| 107 | +composer install --no-dev --optimize-autoloader --no-interaction |
| 108 | + |
| 109 | +# Install Node dependencies (if needed) |
| 110 | +npm install --production |
| 111 | + |
| 112 | +# Or using yarn |
| 113 | +yarn install --production |
| 114 | +``` |
| 115 | + |
| 116 | +### 3. Configure Environment |
| 117 | + |
| 118 | +```bash |
| 119 | +# Create environment file |
| 120 | +cp .env.example .env |
| 121 | + |
| 122 | +# Edit environment variables |
| 123 | +nano .env |
| 124 | +``` |
| 125 | + |
| 126 | +**Critical**: Set the encryption key from Step 1: |
| 127 | + |
| 128 | +```env |
| 129 | +# In .env file |
| 130 | +APP_ENV=production |
| 131 | +APP_DEBUG=false |
| 132 | +APP_KEY=base64:your-laravel-app-key-here |
| 133 | +
|
| 134 | +# PHPBolt encryption key (from obfuscation output) |
| 135 | +PHPBOLT_KEY=ABC123XYZ |
| 136 | +``` |
| 137 | + |
| 138 | +### 4. Set Up Encryption Key in Code |
| 139 | + |
| 140 | +The bolt extension needs the key defined as a constant. Add this to `public/index.php` **before** the Laravel bootstrap: |
| 141 | + |
| 142 | +```php |
| 143 | +<?php |
| 144 | + |
| 145 | +// Define PHPBolt decryption key (BEFORE Laravel bootstrap) |
| 146 | +define('PHP_BOLT_KEY', env('PHPBOLT_KEY', 'your-key-here')); |
| 147 | + |
| 148 | +// Laravel bootstrap continues... |
| 149 | +use Illuminate\Contracts\Http\Kernel; |
| 150 | +use Illuminate\Http\Request; |
| 151 | + |
| 152 | +define('LARAVEL_START', microtime(true)); |
| 153 | +// ... rest of index.php |
| 154 | +``` |
| 155 | + |
| 156 | +### 5. Set Permissions |
| 157 | + |
| 158 | +```bash |
| 159 | +# Set ownership |
| 160 | +chown -R www-data:www-data /var/www/html |
| 161 | + |
| 162 | +# Set directory permissions |
| 163 | +find /var/www/html -type d -exec chmod 755 {} \; |
| 164 | + |
| 165 | +# Set file permissions |
| 166 | +find /var/www/html -type f -exec chmod 644 {} \; |
| 167 | + |
| 168 | +# Set storage and cache permissions |
| 169 | +chmod -R 775 /var/www/html/storage |
| 170 | +chmod -R 775 /var/www/html/bootstrap/cache |
| 171 | +``` |
| 172 | + |
| 173 | +### 6. Optimize Laravel |
| 174 | + |
| 175 | +```bash |
| 176 | +# Generate application key (if not set) |
| 177 | +php artisan key:generate |
| 178 | + |
| 179 | +# Cache configuration |
| 180 | +php artisan config:cache |
| 181 | + |
| 182 | +# Cache routes |
| 183 | +php artisan route:cache |
| 184 | + |
| 185 | +# Cache views |
| 186 | +php artisan view:cache |
| 187 | + |
| 188 | +# Optimize autoloader |
| 189 | +composer dump-autoload --optimize |
| 190 | +``` |
| 191 | + |
| 192 | +### 7. Restart Services |
| 193 | + |
| 194 | +```bash |
| 195 | +# PHP-FPM |
| 196 | +sudo systemctl restart php8.2-fpm |
| 197 | + |
| 198 | +# Nginx |
| 199 | +sudo systemctl restart nginx |
| 200 | + |
| 201 | +# Or Apache |
| 202 | +sudo systemctl restart apache2 |
| 203 | +``` |
| 204 | + |
| 205 | +## Step 5: Verify Deployment |
| 206 | + |
| 207 | +### Check Application |
| 208 | + |
| 209 | +```bash |
| 210 | +# Test artisan commands |
| 211 | +php artisan about |
| 212 | + |
| 213 | +# Check routes |
| 214 | +php artisan route:list |
| 215 | + |
| 216 | +# Test queue workers (if used) |
| 217 | +php artisan queue:work --once |
| 218 | +``` |
| 219 | + |
| 220 | +### Check Web Access |
| 221 | + |
| 222 | +```bash |
| 223 | +# Test with curl |
| 224 | +curl -I https://your-domain.com |
| 225 | + |
| 226 | +# Or visit in browser |
| 227 | +https://your-domain.com |
| 228 | +``` |
| 229 | + |
| 230 | +## CI/CD Integration |
| 231 | + |
| 232 | +### GitHub Actions Example |
| 233 | + |
| 234 | +```yaml |
| 235 | +name: Deploy Production |
| 236 | + |
| 237 | +on: |
| 238 | + push: |
| 239 | + branches: [main] |
| 240 | + |
| 241 | +jobs: |
| 242 | + deploy: |
| 243 | + runs-on: ubuntu-latest |
| 244 | + steps: |
| 245 | + - uses: actions/checkout@v3 |
| 246 | + |
| 247 | + - name: Setup PHP |
| 248 | + uses: shivammathur/setup-php@v2 |
| 249 | + with: |
| 250 | + php-version: 8.2 |
| 251 | + extensions: bolt |
| 252 | + |
| 253 | + - name: Install dependencies |
| 254 | + run: composer install --no-dev |
| 255 | + |
| 256 | + - name: Create production bundle |
| 257 | + run: | |
| 258 | + php artisan obfuscate:run \ |
| 259 | + --production-ready \ |
| 260 | + --destination=deploy/prod \ |
| 261 | + --source=app \ |
| 262 | + --source=routes \ |
| 263 | + --force |
| 264 | + echo "BOLT_KEY=$(grep 'Encryption Key:' output.log | awk '{print $3}')" >> $GITHUB_ENV |
| 265 | + |
| 266 | + - name: Deploy to server |
| 267 | + run: | |
| 268 | + rsync -avz deploy/prod/ ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:/var/www/html/ |
| 269 | + |
| 270 | + - name: Setup production |
| 271 | + uses: appleboy/ssh-action@master |
| 272 | + with: |
| 273 | + host: ${{ secrets.DEPLOY_HOST }} |
| 274 | + username: ${{ secrets.DEPLOY_USER }} |
| 275 | + key: ${{ secrets.DEPLOY_KEY }} |
| 276 | + script: | |
| 277 | + cd /var/www/html |
| 278 | + composer install --no-dev --optimize-autoloader |
| 279 | + echo "PHPBOLT_KEY=${{ env.BOLT_KEY }}" >> .env |
| 280 | + php artisan config:cache |
| 281 | + php artisan route:cache |
| 282 | + sudo systemctl restart php8.2-fpm |
| 283 | +``` |
| 284 | +
|
| 285 | +## Troubleshooting |
| 286 | +
|
| 287 | +### Issue: "bolt_decrypt() function not found" |
| 288 | +
|
| 289 | +**Solution**: Install bolt.so extension on production server: |
| 290 | +
|
| 291 | +```bash |
| 292 | +# Download bolt extension |
| 293 | +wget https://github.com/arshidkv12/phpBolt/releases/latest/download/bolt.so |
| 294 | + |
| 295 | +# Install extension |
| 296 | +sudo cp bolt.so $(php-config --extension-dir)/ |
| 297 | +echo "extension=bolt.so" | sudo tee /etc/php/8.2/mods-available/bolt.ini |
| 298 | +sudo phpenmod bolt |
| 299 | + |
| 300 | +# Restart PHP |
| 301 | +sudo systemctl restart php8.2-fpm |
| 302 | + |
| 303 | +# Verify |
| 304 | +php -m | grep bolt |
| 305 | +``` |
| 306 | + |
| 307 | +### Issue: "Undefined constant PHP_BOLT_KEY" |
| 308 | + |
| 309 | +**Solution**: Define the constant in `public/index.php` before Laravel bootstrap (see Step 4.4 above). |
| 310 | + |
| 311 | +### Issue: "Class not found" errors |
| 312 | + |
| 313 | +**Solution**: Clear and regenerate autoloader: |
| 314 | + |
| 315 | +```bash |
| 316 | +composer dump-autoload --optimize |
| 317 | +php artisan config:clear |
| 318 | +php artisan cache:clear |
| 319 | +``` |
| 320 | + |
| 321 | +### Issue: Permission denied errors |
| 322 | + |
| 323 | +**Solution**: Fix file permissions (see Step 4.5 above). |
| 324 | + |
| 325 | +## Best Practices |
| 326 | + |
| 327 | +1. **Always test** the bundle in a staging environment first |
| 328 | +2. **Save encryption keys** securely (use secrets management) |
| 329 | +3. **Use --dry-run** to preview what will be obfuscated |
| 330 | +4. **Version your bundles** using timestamps or version numbers |
| 331 | +5. **Keep backups** of previous deployments |
| 332 | +6. **Monitor logs** after deployment for any decryption issues |
| 333 | +7. **Use --source** to obfuscate only what's needed (not views, not config) |
| 334 | + |
| 335 | +## Security Notes |
| 336 | + |
| 337 | +- **Never commit** `.env` files or encryption keys to version control |
| 338 | +- **Rotate keys** periodically in production |
| 339 | +- **Restrict access** to deployment scripts and keys |
| 340 | +- **Use HTTPS** for all deployment transfers |
| 341 | +- **Audit deployments** regularly |
| 342 | + |
| 343 | +## Support |
| 344 | + |
| 345 | +- **Documentation**: [README.md](../README.md) |
| 346 | +- **CI/CD Examples**: [CI-CD-EXAMPLES.md](./CI-CD-EXAMPLES.md) |
| 347 | +- **Issues**: https://github.com/aflorea4/laravel-source-obfuscator/issues |
| 348 | + |
0 commit comments