Last active
January 14, 2020 03:10
-
-
Save dccampbell/3b8bcdd2ae77f8e48e48 to your computer and use it in GitHub Desktop.
Install Apache, change user to current, enable macro and rewrite modules, set default server name, add a vhost macro, create a localdev site, add commands for easy add/removal of vhost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## Apache Setup | |
# Variables | |
BIN_DIR="/usr/local/bin" | |
# Install Apache | |
#sudo apt install -y apache2 | |
# Change Apache User/Group | |
sudo sed -i "s/www-data/$USER/" /etc/apache2/envvars | |
# Enable Built-In Modules | |
sudo a2enmod macro | |
sudo a2enmod rewrite | |
# Add Conf: Default Server Name | |
echo "ServerName localhost" | sudo tee "/etc/apache2/conf-available/servername.conf" >/dev/null | |
sudo a2enconf servername | |
# Add Conf: VHost Macro | |
sudo tee /etc/apache2/conf-available/vhost_macro.conf >/dev/null <<"EOF" | |
<Macro VHost $path $host $port> | |
<VirtualHost *:$port> | |
ServerName $host | |
DocumentRoot "$path" | |
<Directory "$path"> | |
Options All | |
AllowOverride All | |
Require all granted | |
</Directory> | |
</VirtualHost> | |
</Macro> | |
EOF | |
sudo a2enconf vhost_macro | |
# Add Site: localdev | |
echo "# Usage: Use VHost $path $host $port\n# Example: Use VHost /var/www/html localhost 80\n" |\ | |
sudo tee "/etc/apache2/sites-available/localdev.conf" >/dev/null | |
sudo a2ensite localdev | |
# Create shell script for easy adding of new vhosts | |
sudo tee $BIN_DIR/vhostAdd >/dev/null <<"EOF" | |
#!/usr/bin/env bash | |
[ $# -lt 2 ] && echo "Usage: $0 <path> <host> [port]" && exit 1; | |
path=$1; host=$2; port=${3:-80} | |
path_site=$(readlink -f $path) | |
path_conf=/etc/apache2/sites-available/localdev.conf | |
path_hosts=/etc/hosts | |
[ "$port" != 80 ] && echo "Notice: Make sure apache is set to listen on port $port" | |
grep -v " $host" $path_hosts | sudo tee $path_hosts >/dev/null # Remove host from hosts file | |
echo -e "\n127.0.0.1 $host" | sudo tee -a /etc/hosts >/dev/null # Append host to hosts file | |
grep -v "$host $port" $path_conf | sudo tee $path_conf >/dev/null # Remove host:port from apache conf | |
echo -e "\nUse VHost $path_site $host $port" | sudo tee -a $path_conf >/dev/null # Append host:port to apache conf | |
sudo service apache2 restart | |
EOF | |
sudo chmod +x $BIN_DIR/vhostAdd | |
# Create shell script for easy removal of vhosts | |
sudo tee $BIN_DIR/vhostRemove >/dev/null <<"EOF" | |
#!/usr/bin/env bash | |
[ $# -lt 1 ] && echo "Usage: $0 <host>" && exit 1; | |
host=$1; | |
path_conf=/etc/apache2/sites-available/localdev.conf | |
path_hosts=/etc/hosts | |
grep_conf=$(grep " $host " $path_conf 2>/dev/null) | |
grep_hosts=$(grep " $host" $path_hosts 2>/dev/null) | |
if [[ -n "$grep_conf" ]]; then | |
echo -e "Removing from $path_conf:\n$grep_conf\n" | |
grep -v " $host " $path_conf 2>/dev/null | sudo tee $path_conf >/dev/null # Remove host from apache conf | |
sudo service apache2 reload | |
else | |
echo "Host '$host' not found in $path_conf"; | |
fi | |
if [[ -n "$grep_hosts" ]]; then | |
echo -e "Removing from $path_hosts:\n$grep_hosts\n" | |
grep -v " $host" $path_hosts 2>/dev/null | sudo tee $path_hosts >/dev/null # Remove host from hosts file | |
sudo service apache2 reload | |
else | |
echo "Host '$host' not found in $path_hosts"; | |
fi | |
EOF | |
sudo chmod +x $BIN_DIR/vhostRemove | |
# Restart | |
sudo systemctl restart apache2 | |
# Complete! | |
echo "Setup complete! To add a new vhost, use 'vhostAdd <path> <host>'. To remove it, use 'vhostRemove <host>'" | |
echo "Example: vhostAdd ~/dev/mysite mysite.test" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment