Skip to content

Instantly share code, notes, and snippets.

@salodev
Created December 15, 2021 11:50
Show Gist options
  • Save salodev/db5e39adc507db8acdbee47aae78aa56 to your computer and use it in GitHub Desktop.
Save salodev/db5e39adc507db8acdbee47aae78aa56 to your computer and use it in GitHub Desktop.
Apache2 Local VirtualHost Generator
#!/bin/bash
templateFile="/etc/apache2/sites-available/template"
read -p "Nombre de host: " host
read -p "Ruta al public ej: /var/www/mi-proyecto/public : " documentRoot
if [[ ! -d $documentRoot ]]; then
echo "La ruta al public {$documentRoot} no existe.\nNo se ha realizado ninguna acción.\n"
exit 1;
fi
apacheFile="/etc/apache2/sites-available/$host.conf"
if [[ -f $apacheFile ]]; then
read -p "El archivo $apacheFile ya existe. Desea reescribirlo? (s/N): " reescribir
if [[ "$reescribir" != "s" && "$reescribir" != "S" ]]; then
echo "No se ha realizado ninguna acción\n"
exit 1;
fi
fi
echo "Creando archivo $apacheFile ..."
cat $templateFile > $apacheFile
echo "Configurando ..."
sed -i "s#__HOST__#$host#g" $apacheFile
sed -i "s#__DOCUMENT_ROOT__#$documentRoot#g" $apacheFile
read -p "Habilitar sitio en apache y reniciar? (S/n): " habilitar
if [[ "$habilitar" != "s" && "$habilitar" != "S" ]]; then
exit 0;
fi
echo "Habilitando archivo $host.conf ..."
a2ensite "$host.conf"
echo "Ejecuntando reload en apache ..."
service apache2 reload
read -p "Crear entrada en los DNS locales? (S/n): " configurarDns
if [[ "$configurarDns" != "s" && "$configurarDns" != "S" ]]; then
exit 0;
fi
echo "127.0.0.1 $host" >> /etc/hosts
echo "Navegue el siguiente link: http://$host/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment