|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Colors for output |
| 4 | +RED='\033[0;31m' |
| 5 | +GREEN='\033[0;32m' |
| 6 | +YELLOW='\033[1;33m' |
| 7 | +NC='\033[0m' |
| 8 | + |
| 9 | +echo -e "${GREEN}Starting container setup...${NC}" |
| 10 | + |
| 11 | +# Create necessary Caddy directories with proper permissions |
| 12 | +echo -e "${YELLOW}Creating Caddy directories...${NC}" |
| 13 | +mkdir -p /data/caddy/locks /config/caddy |
| 14 | +chown -R www-data:www-data /data /config |
| 15 | +chmod -R 755 /data /config |
| 16 | + |
| 17 | +# Create necessary Yii2 directories if they don't exist |
| 18 | +echo -e "${YELLOW}Creating Yii2 directories...${NC}" |
| 19 | +mkdir -p /app/runtime/cache |
| 20 | +mkdir -p /app/runtime/logs |
| 21 | +mkdir -p /app/web/assets |
| 22 | + |
| 23 | +# Configure permissions for Yii2 directories |
| 24 | +echo -e "${YELLOW}Setting up permissions...${NC}" |
| 25 | + |
| 26 | +# Try to set permissions and ownership - handle both mounted volumes and container-only scenarios |
| 27 | +if chown -R www-data:www-data /app/runtime 2>/dev/null; then |
| 28 | + chmod -R 775 /app/runtime |
| 29 | + echo -e "${GREEN}✓ Runtime directory configured correctly${NC}" |
| 30 | +else |
| 31 | + # If chown fails (mounted volume), try chmod only |
| 32 | + if chmod -R 777 /app/runtime 2>/dev/null; then |
| 33 | + echo -e "${YELLOW}⚠ Runtime directory permissions set to 777 (mounted volume)${NC}" |
| 34 | + else |
| 35 | + echo -e "${RED}✗ Error: Could not configure runtime directory${NC}" |
| 36 | + fi |
| 37 | +fi |
| 38 | + |
| 39 | +if chown -R www-data:www-data /app/web/assets 2>/dev/null; then |
| 40 | + chmod -R 775 /app/web/assets |
| 41 | + echo -e "${GREEN}✓ Assets directory configured correctly${NC}" |
| 42 | +else |
| 43 | + # If chown fails (mounted volume), try chmod only |
| 44 | + if chmod -R 777 /app/web/assets 2>/dev/null; then |
| 45 | + echo -e "${YELLOW}⚠ Assets directory permissions set to 777 (mounted volume)${NC}" |
| 46 | + else |
| 47 | + echo -e "${RED}✗ Error: Could not configure assets directory${NC}" |
| 48 | + fi |
| 49 | +fi |
| 50 | + |
| 51 | +echo -e "${GREEN}Setup completed.${NC}" |
| 52 | + |
| 53 | +# Check if composer.json exists and vendor directory doesn't exist |
| 54 | +if [ -f "/app/composer.json" ] && [ ! -d "/app/vendor" ]; then |
| 55 | + echo -e "${YELLOW}Installing Composer dependencies...${NC}" |
| 56 | + |
| 57 | + # Give www-data write access without exposing the tree to everyone |
| 58 | + chown -R www-data:www-data /app && \ |
| 59 | + chmod -R u+rwX,g+rwX /app |
| 60 | + |
| 61 | + # Create and configure npm cache directory for www-data |
| 62 | + mkdir -p /var/www/.npm |
| 63 | + chown -R www-data:www-data /var/www/.npm |
| 64 | + |
| 65 | + # Install dependencies with proper environment variables |
| 66 | + if [ "$YII_ENV" = "prod" ]; then |
| 67 | + # Production: exclude dev dependencies and optimize autoloader |
| 68 | + gosu www-data env \ |
| 69 | + HOME=/var/www \ |
| 70 | + COMPOSER_HOME=/var/www/.composer \ |
| 71 | + COMPOSER_CACHE_DIR=/var/www/.composer/cache \ |
| 72 | + npm_config_cache=/var/www/.npm \ |
| 73 | + composer install --no-dev --optimize-autoloader --no-interaction |
| 74 | + else |
| 75 | + # Development: include dev dependencies |
| 76 | + gosu www-data env \ |
| 77 | + HOME=/var/www \ |
| 78 | + COMPOSER_HOME=/var/www/.composer \ |
| 79 | + COMPOSER_CACHE_DIR=/var/www/.composer/cache \ |
| 80 | + npm_config_cache=/var/www/.npm \ |
| 81 | + composer install --optimize-autoloader --no-interaction |
| 82 | + fi |
| 83 | + |
| 84 | + echo -e "${GREEN}✓ Composer dependencies installed successfully${NC}" |
| 85 | +fi |
| 86 | + |
| 87 | +# Copy supervisor configuration |
| 88 | +echo -e "${YELLOW}Configuring supervisor...${NC}" |
| 89 | + |
| 90 | +if [ -f "/app/docker/supervisor/supervisord.conf" ]; then |
| 91 | + cp /app/docker/supervisor/supervisord.conf /etc/supervisor/supervisord.conf |
| 92 | + echo -e "${GREEN}✓ Supervisor configuration copied successfully${NC}" |
| 93 | +else |
| 94 | + echo -e "${RED}✗ Error: Supervisor configuration file not found${NC}" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +echo -e "${GREEN}Starting supervisor daemon...${NC}" |
| 99 | + |
| 100 | +# Start supervisor daemon |
| 101 | +exec /usr/bin/supervisord -c /etc/supervisor/supervisord.conf |
0 commit comments