File: /var/www/create-vhost-sincerbot.sh
#!/bin/bash
# Script para crear vhosts automáticamente
# Uso: ./create-vhost.sh <dominio> <carpeta> [laravel|static]
if [ $# -lt 2 ]; then
echo "Uso: ./create-vhost.sh <dominio> <carpeta> [laravel|static]"
echo "Ejemplo: ./create-vhost.sh api-control-accesos.hitch.cl control_accesos_api laravel"
echo "Ejemplo: ./create-vhost.sh control-accesos.hitch.cl control_accesos static"
exit 1
fi
DOMAIN=$1
FOLDER=$2
TYPE=${3:-laravel}
WEBROOT="/var/www/$FOLDER"
if [ ! -d "$WEBROOT" ]; then
echo "Error: La carpeta $WEBROOT no existe"
exit 1
fi
if [ "$TYPE" = "laravel" ]; then
DOCROOT="$WEBROOT/public"
if [ ! -d "$DOCROOT" ]; then
echo "Error: La carpeta $DOCROOT no existe (proyecto Laravel debe tener /public)"
exit 1
fi
else
DOCROOT="$WEBROOT"
fi
echo "=== Creando VHost para $DOMAIN ==="
echo "Carpeta: $WEBROOT"
echo "DocumentRoot: $DOCROOT"
echo "Tipo: $TYPE"
echo ""
echo "[1/5] Creando configuración del vhost..."
cat > /etc/apache2/sites-available/$DOMAIN.conf << EOF
<VirtualHost *:80>
ServerAdmin webmaster@hitch.cl
ServerName $DOMAIN
ServerAlias $DOMAIN
DocumentRoot $DOCROOT
<Directory $DOCROOT>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-error.log
CustomLog \${APACHE_LOG_DIR}/$DOMAIN-access.log combined
</VirtualHost>
EOF
echo "[2/5] Configurando permisos..."
#chown -R root:www-data $WEBROOT
chown -R azureuser:www-data $WEBROOT
find $WEBROOT -type d -exec chmod 755 {} \;
find $WEBROOT -type f -exec chmod 644 {} \;
find $WEBROOT -type d -exec chmod g+s {} \;
if [ "$TYPE" = "laravel" ]; then
echo "[3/5] Configurando permisos de Laravel..."
[ -d "$WEBROOT/storage" ] && chown -R www-data:www-data $WEBROOT/storage && chmod -R 775 $WEBROOT/storage
[ -d "$WEBROOT/bootstrap/cache" ] && chown -R www-data:www-data $WEBROOT/bootstrap/cache && chmod -R 775 $WEBROOT/bootstrap/cache
else
echo "[3/5] Proyecto estático, omitiendo permisos especiales"
fi
echo "[4/5] Habilitando sitio..."
a2ensite $DOMAIN.conf > /dev/null 2>&1
echo "[5/5] Recargando Apache..."
systemctl reload apache2
if [ $? -ne 0 ]; then
echo "Advertencia: Apache tuvo warnings"
fi
echo ""
echo "=================================="
echo "✅ VHost creado exitosamente"
echo "=================================="
echo "Dominio: $DOMAIN"
echo "URL: http://$DOMAIN"
echo "Carpeta: $WEBROOT"
echo "DocumentRoot: $DOCROOT"