|
| 1 | +#!/bin/bash |
| 2 | +### Set Language |
| 3 | +TEXTDOMAIN=virtualhost |
| 4 | + |
| 5 | +### Set default parameters |
| 6 | +action=$1 |
| 7 | +domain=$2 |
| 8 | +rootDir=$3 |
| 9 | +owner=$(who am i | awk '{print $1}') |
| 10 | +email='webmaster@localhost' |
| 11 | +sitesEnable='/etc/httpd/sites-enabled/' |
| 12 | +sitesAvailable='/etc/httpd/sites-available/' |
| 13 | +userDir='/var/www/' |
| 14 | +sitesAvailabledomain=$sitesAvailable$domain.conf |
| 15 | + |
| 16 | +### don't modify from here unless you know what you are doing #### |
| 17 | + |
| 18 | +if [ "$(whoami)" != 'root' ]; then |
| 19 | + echo $"You have no permission to run $0 as non-root user. Use sudo" |
| 20 | + exit 1; |
| 21 | +fi |
| 22 | + |
| 23 | +if [ "$action" != 'create' ] && [ "$action" != 'delete' ] |
| 24 | + then |
| 25 | + echo $"You need to prompt for action (create or delete) -- Lower-case only" |
| 26 | + exit 1; |
| 27 | +fi |
| 28 | + |
| 29 | +while [ "$domain" == "" ] |
| 30 | +do |
| 31 | + echo -e $"Please provide domain. e.g.dev,staging" |
| 32 | + read domain |
| 33 | +done |
| 34 | + |
| 35 | +if [ "$rootDir" == "" ]; then |
| 36 | + rootDir=${domain//./} |
| 37 | +fi |
| 38 | + |
| 39 | +### if root dir starts with '/', don't use /var/www as default starting point |
| 40 | +if [[ "$rootDir" =~ ^/ ]]; then |
| 41 | + userDir='' |
| 42 | +fi |
| 43 | + |
| 44 | +rootDir=$userDir$rootDir |
| 45 | + |
| 46 | +if [ "$action" == 'create' ] |
| 47 | + then |
| 48 | + ### check if domain already exists |
| 49 | + if [ -e $sitesAvailabledomain ]; then |
| 50 | + echo -e $"This domain already exists.\nPlease Try Another one" |
| 51 | + exit; |
| 52 | + fi |
| 53 | + |
| 54 | + ### check if directory exists or not |
| 55 | + if ! [ -d $rootDir ]; then |
| 56 | + ### create the directory |
| 57 | + mkdir $rootDir |
| 58 | + ### give permission to root dir |
| 59 | + chmod 755 $rootDir |
| 60 | + ### write test file in the new domain dir |
| 61 | + if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php |
| 62 | + then |
| 63 | + echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions" |
| 64 | + exit; |
| 65 | + else |
| 66 | + echo $"Added content to $rootDir/phpinfo.php" |
| 67 | + fi |
| 68 | + fi |
| 69 | + |
| 70 | + ### create virtual host rules file |
| 71 | + if ! echo " |
| 72 | + <VirtualHost *:80> |
| 73 | + ServerAdmin $email |
| 74 | + ServerName $domain |
| 75 | + ServerAlias $domain |
| 76 | + DocumentRoot $rootDir |
| 77 | + <Directory /> |
| 78 | + AllowOverride All |
| 79 | + </Directory> |
| 80 | + <Directory $rootDir> |
| 81 | + Options Indexes FollowSymLinks MultiViews |
| 82 | + AllowOverride all |
| 83 | + Require all granted |
| 84 | + </Directory> |
| 85 | + ErrorLog /var/log/httpd/$domain-error.log |
| 86 | + LogLevel error |
| 87 | + CustomLog /var/log/httpd/$domain-access.log combined |
| 88 | + </VirtualHost>" > $sitesAvailabledomain |
| 89 | + then |
| 90 | + echo -e $"There is an ERROR creating $domain file" |
| 91 | + exit; |
| 92 | + else |
| 93 | + echo -e $"\nNew Virtual Host Created\n" |
| 94 | + fi |
| 95 | + |
| 96 | + ### Add domain in /etc/hosts |
| 97 | + if ! echo "127.0.0.1 $domain" >> /etc/hosts |
| 98 | + then |
| 99 | + echo $"ERROR: Not able to write in /etc/hosts" |
| 100 | + exit; |
| 101 | + else |
| 102 | + echo -e $"Host added to /etc/hosts file \n" |
| 103 | + fi |
| 104 | + |
| 105 | + if [ "$owner" == "" ]; then |
| 106 | + chown -R $(whoami):$(whoami) $rootDir |
| 107 | + else |
| 108 | + chown -R $owner:$owner $rootDir |
| 109 | + fi |
| 110 | + |
| 111 | + ### enable website |
| 112 | + cp $sitesAvailabledomain $sitesEnable/$domain.conf |
| 113 | + |
| 114 | + ### restart Apache |
| 115 | + systemctl restart httpd |
| 116 | + |
| 117 | + ### show the finished message |
| 118 | + echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir" |
| 119 | + exit; |
| 120 | + else |
| 121 | + ### check whether domain already exists |
| 122 | + if ! [ -e $sitesAvailabledomain ]; then |
| 123 | + echo -e $"This domain does not exist.\nPlease try another one" |
| 124 | + exit; |
| 125 | + else |
| 126 | + ### Delete domain in /etc/hosts |
| 127 | + newhost=${domain//./\\.} |
| 128 | + sed -i "/$newhost/d" /etc/hosts |
| 129 | + |
| 130 | + ### disable website |
| 131 | + rm -f $sitesEnable/$domain.conf |
| 132 | + |
| 133 | + ### restart Apache |
| 134 | + systemctl restart httpd |
| 135 | + |
| 136 | + ### Delete virtual host rules files |
| 137 | + rm $sitesAvailabledomain |
| 138 | + fi |
| 139 | + |
| 140 | + ### check if directory exists or not |
| 141 | + if [ -d $rootDir ]; then |
| 142 | + echo -e $"Delete host root directory ? (y/n)" |
| 143 | + read deldir |
| 144 | + |
| 145 | + if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then |
| 146 | + ### Delete the directory |
| 147 | + rm -rf $rootDir |
| 148 | + echo -e $"Directory deleted" |
| 149 | + else |
| 150 | + echo -e $"Host directory conserved" |
| 151 | + fi |
| 152 | + else |
| 153 | + echo -e $"Host directory not found. Ignored" |
| 154 | + fi |
| 155 | + |
| 156 | + ### show the finished message |
| 157 | + echo -e $"Complete!\nYou just removed Virtual Host $domain" |
| 158 | + exit 0; |
| 159 | +fi |
0 commit comments