Last active
June 7, 2018 09:50
-
-
Save baorv/4f01f3e23f7fd5be67bc9ae695cdd67d to your computer and use it in GitHub Desktop.
Automatically create new vhost for apache2
This file contains hidden or 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
| #!/bin/bash | |
| # SHOW HELP | |
| if [ "$1" == "-h" ]; then | |
| echo "Usage: sudo ./`basename $0` domain.local /var/www/html/domain/public" | |
| exit | |
| fi | |
| # CHECK ROOT PERMISSION | |
| if [ "$(whoami)" != 'root' ]; then | |
| echo -e "\e[1;31mYou have no permission to run $0 as non-root user. Use sudo\e[0m" | |
| exit | |
| fi | |
| # CHECK ARGUMENT | |
| if [ -z "$1" ]; then | |
| echo -e "\e[1;31mYour domain is not specify\e[0m" | |
| exit | |
| fi | |
| if [ -z "$2" ]; then | |
| echo -e "\e[1;31mWeb path is not specify\e[0m" | |
| exit | |
| fi | |
| if [ -z "$3" ] && [ -z "$4" ]; then | |
| database=true | |
| databaseName=$3 | |
| databasePassword=$4 | |
| else | |
| database=false | |
| fi | |
| # SETUP BEFORE CREATING | |
| domain=$1 | |
| webpath=$2 | |
| vhostContent="<VirtualHost *:80>\n | |
| \tServerAdmin admin@localhost\n | |
| \tServerName $domain\n | |
| \tServerAlias $domain\n | |
| \tDocumentRoot $webpath\n | |
| \t<Directory $webpath>\n | |
| \t\tOptions Indexes FollowSymLinks MultiViews\n | |
| \t\tAllowOverride all\n | |
| \t\tRequire all granted\n | |
| \t</Directory>\n | |
| \tErrorLog /var/log/apache2/$domain-error.log\n | |
| \tLogLevel error\n | |
| \tCustomLog /var/log/apache2/$domain-access.log combined\n | |
| </VirtualHost>" | |
| vhostFile="/etc/apache2/sites-available/$domain.conf" | |
| # CHECK FILE IS EXISTS | |
| if [ -f $vhostFile ]; then | |
| echo -e "\e[1;31mPlease choose another domain because your domain is listed\e[0m" | |
| exit | |
| fi | |
| # PUT CONTENT TO VHOST FILE | |
| echo -e $vhostContent > $vhostFile | |
| # ENABLE VHOST | |
| a2ensite $domain | |
| # RESTART SERVICE | |
| /etc/init.d/apache2 reload | |
| # PUT HOST TO /etc/hosts | |
| echo "127.0.0.1 $domain" >> /etc/hosts | |
| # | |
| ## CREATE DATABASE | |
| #databaseCheck=`mysql -uroot -p$databasePassword -e "SHOW DATABASES" | grep $databaseName` | |
| #if [ "$databaseCheck" != "$databaseName" ]; then | |
| # mysql -uroot -p $databasePassword -e "CREATE DATABASE $databaseName" | |
| # | |
| # # RETURN RESULT FOR USER | |
| # echo -e "\033[0;32mYou have just created new vhost for domain $domain with information: (Domain: $domain, Path: $webpath, Database: $databaseName, Password: $databasePassword)\033[0m" | |
| # exit | |
| #fi | |
| # RETURN RESULT FOR USER | |
| echo -e "\033[0;32mYou have just created new vhost for domain $domain with information: (Domain: $domain, Path: $webpath). Open your application: http://$domain\033[0m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment