Skip to content

Instantly share code, notes, and snippets.

@pawel-dubiel
Last active October 11, 2015 04:27
Show Gist options
  • Save pawel-dubiel/3802709 to your computer and use it in GitHub Desktop.
Save pawel-dubiel/3802709 to your computer and use it in GitHub Desktop.
Setting up a new domain #script
#!/bin/bash
echo "Enter a host name (e.g. example.com):";
read domain;
echo ""
echo 'IP ADDRESSES'
ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'
echo "Enter IP ADDRESS:";
read ipaddress;
echo ""
### Check to see if $domain exists
if [[ ! -e /etc/apache2/sites-available/${domain} ]]; then
echo "Are you sure you want to create ${domain}? yes/no"
read q
if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then
if [[ ! -e /var/www/${domain} ]]; then mkdir /var/www/${domain}; fi
if [[ ! -e /var/www/${domain}/public ]]; then mkdir /var/www/${domain}/public; fi
if [[ ! -e /var/www/${domain}/public/htdocs ]]; then mkdir /var/www/${domain}/public/htdocs; fi
chown -R root:www-data /var/www/${domain};
chmod -R 0775 /var/www/${domain}/public;
chown root:root /var/www/${domain};
chmod 0755 /var/www/${domain};
echo "
############# ${domain} ##############
### domain: ${domain}
### ip: ${ipaddress}
######################################
<VirtualHost ${ipaddress}:80>
ServerName ${domain}
ServerAlias www.${domain}
DocumentRoot /var/www/${domain}/public/htdocs
<Directory /var/www/${domain}/public/htdocs>
AllowOverride All
Order allow,deny
Allow from all
</Directory>
LogLevel warn
CustomLog /var/log/apache2/${domain}.access.log combined
</VirtualHost>" >> /etc/apache2/sites-available/${domain}.conf
ln -s /etc/apache2/sites-available/${domain}.conf /etc/apache2/sites-enabled/${domain}.conf
echo "Testing configuration"
apachectl configtest
echo "Would you like me to restart the server?"
read q
if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then
/etc/init.d/apache2 reload
fi
fi
else
echo "${domain} already exists"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment