Skip to content

Instantly share code, notes, and snippets.

@b2z
Last active November 25, 2018 18:50
Show Gist options
  • Save b2z/320fa5b154bf8a1c6ea39380db5734fc to your computer and use it in GitHub Desktop.
Save b2z/320fa5b154bf8a1c6ea39380db5734fc to your computer and use it in GitHub Desktop.
Virtual host script for apache2 on Ubuntu
#!/bin/bash
# Author: Ivlev E
# Modified by voland
# Modified by Dmitry Rekun
echo -n "Enter host name: "
read newHost
newPath="/var/www/$newHost"
mkdir $newPath
newPath="$newPath/public_html"
mkdir $newPath
# Add record to hosts
file="/etc/hosts"
b=$(cat $file)
newContent="127.0.0.1 ${newHost}"$'\n'"127.0.0.1 ${newHost}"$'\n'$b
sudo bash -c "echo '${newContent}' > $file"
# Add site to sites-available and set directives
sap=/etc/apache2/sites-available/$newHost.conf
sudo touch $sap
sudo chmod 644 $sap
directives="<VirtualHost *:80>
ServerName ${newHost}
ServerAlias ${newHost} www.${newHost}
DocumentRoot ${newPath}
<Directory ${newPath:1}/>
AllowOverride All
</Directory>
</VirtualHost>"
sudo bash -c "echo '$directives' > $sap"
# Enable virtual host
sudo a2ensite $newHost
# Enable rewrite module
#sudo a2enmod rewrite
# Reload the server
sudo systemctl reload apache2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment