Skip to content

Instantly share code, notes, and snippets.

@milanchheda
Created March 19, 2017 12:26
Show Gist options
  • Save milanchheda/39161a33104a43ffc72837a70b8d7cb5 to your computer and use it in GitHub Desktop.
Save milanchheda/39161a33104a43ffc72837a70b8d7cb5 to your computer and use it in GitHub Desktop.
A simple shell script to add a new virtual host on Ubuntu machine.
# Create configuration file for the new virtual host in `sites-available` folder
sudo cat <<EOF >/etc/apache2/sites-available/$1.conf
<VirtualHost $2:80>
DocumentRoot "/var/www/html/$1/"
ServerName $2
<Directory "/var/www/html/$1/">
allow from all
order allow,deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Other directives here
</VirtualHost>
EOF
# Enable the newly added configuration file for the new virtual host
sudo a2ensite $1.conf
# Reload apache2 service
sudo service apache2 reload
# Add the new virtual host URL to `/etc/hosts`
sudo echo "127.0.0.1 $2" >> /etc/hosts
# Restart apache2 service
sudo service apache2 restart
@milanchheda
Copy link
Author

milanchheda commented Mar 19, 2017

Usage:

bash adding_virtual_host.sh project-folder-name project-url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment