Last active
April 19, 2023 22:24
-
-
Save sh-sh-dev/9c310ea1868ae10794ef77b8f213d16a to your computer and use it in GitHub Desktop.
Apache subdomain creator bash script
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 | |
Domain='example.com' # root domain | |
Name='' | |
G='\033[0;32m' | |
N='\033[0m' | |
while [[ -z ${Name} ]]; do | |
echo "Enter Subdomain: (for example api)" | |
read Name | |
done | |
# create directory if not exists | |
echo -e "${G}Creating directory...\n${N}"; | |
cd /var/www/html | |
sudo mkdir -p ${Name} | |
# write config | |
echo -e "${G}Writing configs...\n${N}"; | |
sudo echo " | |
# domain: ${Name}.${Domain} | |
# public: /var/www/html/${Name}/ | |
<VirtualHost *:80> | |
ServerAdmin ${Name}@${Domain} | |
ServerName ${Name}.${Domain} | |
ServerAlias www.${Name}.${Domain} | |
DirectoryIndex index.html index.php | |
DocumentRoot /var/www/html/${Name}/ | |
LogLevel warn | |
ErrorLog /var/log/apache2/error-${Name}.${Domain}.log | |
CustomLog /var/log/apache2/access-${Name}.${Domain}.log combined | |
</VirtualHost>" > /etc/apache2/sites-available/${Name}.${Domain}.conf | |
# enable site | |
echo -e "${G}Enabling site...\n${N}"; | |
sudo a2ensite ${Name}.${Domain} | |
# restart apache | |
echo -e "${G}Restarting apache...\n${N}"; | |
sudo service apache2 reload | |
sudo service apache2 restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment