Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- ##########################
- ####### Setup ############
- ##########################
- #
- # save this code to file ubuntu_virtualhost.sh or someyhing else as you wish
- # sudo sh ubuntu_setup_virtualhost.sh $USER project_folder_path project_name
- # Exemple: sudo sh ubuntu_setup_virtualhost.sh $USER Projects test_project
- # Path to files: /home/$USER/Projects/test_project
- #
- ##############################
- ####### Init Bash ############
- ##############################
- ## Set variables
- osusername=$1
- projectfolder=$2
- projectname=$3
- ## Check OS compatible
- unamestr=`gcc --version`
- SOURCE="Ubuntu"
- if echo "$unamestr" | grep -q "$SOURCE"; then
- echo "You use Ubuntu!";
- else
- echo "This bash script just use on Ubuntu!";
- exit
- fi
- ## Check params avaliable
- if id -u "$1" >/dev/null 2>&1; then
- echo "User $1 exists"
- else
- echo "User name $1 does not exist"
- echo "Example:"
- echo "sudo sh server_wordpress.sh $User"
- exit
- fi
- ## Make sure only root can run our script
- if [ "$(id -u)" != "0" ]; then
- echo "This script must be run as root! Example: sudo sh server_wordpress.sh" 1>&2
- exit 1
- fi
- ## Fix restart issue and change user apache owner
- read -r -p " Do you create DB relate for project (y/N)? " response
- case $response in [yY][eE][sS]|[yY])
- # Mysql init user and db
- echo -n " Enter new name for MySQL user (any name): "
- read dbuser
- echo -n " Enter password for this user: "
- read dbpass
- echo -n " Enter name for MySQL database: "
- read dbname
- echo -n " Please enter the password for MySQL root user: "
- read dbrootPass
- user=root
- BTICK='`'
- MYSQL=`which mysql`
- Q1="DROP DATABASE IF EXISTS $BTICK$dbname$BTICK;"
- Q2="CREATE DATABASE IF NOT EXISTS $BTICK$dbname$BTICK CHARACTER SET utf8 COLLATE utf8_general_ci;"
- Q3="GRANT USAGE ON *.* TO '$dbuser'@'localhost'; DROP USER '$dbuser'@'localhost';"
- Q4="CREATE USER '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';"
- Q5="GRANT ALL ON ${BTICK}$dbname${BTICK}.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';"
- Q6="FLUSH PRIVILEGES;"
- mysql --user="$user" --password="$dbrootPass" --execute="${Q1}${Q2}${Q3}${Q4}${Q5}${Q6}"
- ;;
- *)
- echo "No database changes"
- ;;
- esac
- #########################################
- ####### Setup Local Environment #########
- #########################################
- aliaspath="/home/"$osusername"/"$projectfolder
- if [ -d "$aliaspath" ]; then
- rm -rf $aliaspath"/"$projectname
- fi
- mkdir $aliaspath
- mkdir $aliaspath"/log"
- mkdir $aliaspath"/log/"$projectname
- mkdir $aliaspath"/"$projectname
- chmod -R 0777 $aliaspath
- chown -R $osusername:$osusername $aliaspath
- cd $aliaspath"./"$projectname"/"
- phpInfoFile=$(cat <<EOF
- <?php
- phpinfo();
- EOF
- )
- indexpath="/home/"$osusername"/"$projectfolder"/"$projectname"/index.php"
- echo "$phpInfoFile" >> $indexpath
- chmod -R 0777 "/home/"$osusername"/"$projectfolder
- chown -R $osusername:$osusername "/home/"$osusername"/"$projectfolder
- ##########################
- ####### Setup VH #########
- ##########################
- wpVHost=$(cat <<EOF
- <VirtualHost *:80>
- ServerAdmin name@mail.com
- DocumentRoot "/home/$osusername/$projectfolder/$projectname"
- DirectoryIndex index.php
- ServerName $projectname.local
- ServerAlias www.$projectname.local
- LimitRequestLine 40949999
- LimitRequestFieldSize 40949999
- SetEnv APPLICATION_ENV "development"
- <Directory "/home/$osusername/$projectfolder/$projectname">
- AllowOverride All
- Require all granted
- </Directory>
- ErrorLog /home/$osusername/$projectfolder/log/$projectname/error.log
- # Possible values include: debug, info, notice, warn, error, crit,
- # alert, emerg.
- LogLevel warn
- CustomLog /home/$osusername/$projectfolder/log/$projectname/access.log combined
- </VirtualHost>
- EOF
- )
- extconfig=".conf"
- configspath="/etc/apache2/sites-available/"
- hostsfile="/etc"
- vhpath=$configspath$projectname$extconfig
- ## Add localhost DNS domain
- cd $hostsfile
- echo "127.0.0.1 "$projectname".local www."$projectname".local" >> hosts
- ## Add VH config and enable
- if [ -f $vhpath ]; then
- rm $vhpath
- fi
- echo "$wpVHost" >> $vhpath
- cd $configspath
- a2ensite $projectname$extconfig
- service apache2 restart
- echo " All operation complete success!"
- echo " All operation complete success!" >> $indexpath
- echo " Use next credentials:"
- echo " Use next credentials:" >> $indexpath
- echo " Database name: "$dbname
- echo " Database name: "$dbname >> $indexpath
- echo " Database username: "$dbuser
- echo " Database username: "$dbuser >> $indexpath
- echo " Database user password: "$dbpass
- echo " Database user password: "$dbpass >> $indexpath
- echo " Path to files: /home/"$osusername"/"$projectfolder"/"$projectname
- echo " Path to files: /home/"$osusername"/"$projectfolder"/"$projectname >> $indexpath
- echo " Path to log files: /home/"$osusername"/"$projectfolder"/log/"$projectname
- echo " Path to log files: /home/"$osusername"/"$projectfolder"/log/"$projectname >> $indexpath
- echo " Web url: "$projectname".local/"
- echo " Web url: "$projectname".local/" >> $indexpath
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement