Click here to Skip to main content
15,891,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am looking for someone that can assist me with my .sh script.
Please see script below:
The commented section is where help is required when un-commented it sets anything below it as a "string"

If any other issues are noticed in main script dump please do let me know :)

ISSUE
# setup hosts file
  # VHOST=$(cat <<EOF
  # <VirtualHost *:80>
  #     DocumentRoot "/var/www/html/${PROJECTFOLDER}/public"
  #     <Directory "/var/www/html/${PROJECTFOLDER}/public">
  #         AllowOverride All
  #         Require all granted
  #     </Directory>
  # </VirtualHost>
  # EOF
  # )
  # echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf



FULL SCRIPT
#!/usr/bin/env bash
# VOID installer for Debian, Ubuntu and CentOS

# Use single quotes instead of double quotes to make it work with special-character passwords
# PASSWORD=''

# Detect Debian users running the script with "sh" instead of bash
if readlink /proc/$$/exe | grep -qs "dash"; then
  echo "This script needs to be run with bash, not sh"
  exit 1
fi

if [[ "$EUID" -ne 0 ]]; then
  echo "Sorry, you need to run this as root"
  exit 2
fi

if grep -qs "CentOS release 5" "/etc/redhat-release"; then
  echo "CentOS 5 is too old and not supported"
  exit 3
fi
if [[ -e /etc/debian_version ]]; then
  OS=debian
  GROUPNAME=nogroup
  RCLOCAL='/etc/rc.local'
elif [[ -e /etc/centos-release || -e /etc/redhat-release ]]; then
  OS=centos
  GROUPNAME=nobody
  RCLOCAL='/etc/rc.d/rc.local'
else
  echo "Looks like you aren't running this installer on Debian, Ubuntu or CentOS"
  exit 4
fi

echo ""
echo "Please, use one word only, no special characters"
read -p "Project folder name: " -e -i projectfolder PROJECTFOLDER
echo ""

if [[ -e /var/www/html/${PROJECTFOLDER} ]]; then
  while :
  do
    clear
    echo "Looks like ${PROJECTFOLDER} is already installed"
    echo ""
    echo "What do you want to do?"
    echo "   1) Exit"
    read -p "Select an option [1]: " option
    case $option in
      1) exit;;
    esac
  done
else
  clear
  echo "Welcome to this quick ${PROJECTFOLDER} installer"
  echo ""
  # VOID setup
  echo "I need to ask you a few questions before starting the setup"
  echo "You can leave the default options and just press enter if you are ok with them"
  echo ""
  echo "Please, use one word only, no special characters"
  read -p "Project folder name: " -e -i projectfolder PROJECTFOLDER
  echo ""
  echo "Please, use one word only, no special characters"
  read -p "GitHub username: " -e -i username USERNAME
  echo ""
  echo "Please, use one word only, no special characters"
  read -p "Repository name: " -e -i repository REPOSITORY
  echo ""
  echo "Okay, that was all I needed. We are ready to setup your ${PROJECTFOLDER} server now"
  read -n1 -r -p "Press any key to continue..."
  if [[ "$OS" = 'debian' ]]; then
    sudo apt-get update
    sudo apt-get -y upgrade
    sudo apt-get install -y apache2
    sudo apt-get install -y php5
    # install mysql
    sudo apt-get -y install mysql-server
    sudo apt-get install php5-mysql
    # install phpmyadmin
    sudo apt-get -y install phpmyadmin
    # install curl (needed to use git afaik)
    sudo apt-get -y install curl
    sudo apt-get -y install php5-curl
    # install openssl (needed to clone from GitHub, as github is https only)
    sudo apt-get -y install openssl
    # install PHP GD, the graphic lib (we create captchas and avatars)
    sudo apt-get -y install php5-gd
    # install git
    sudo apt-get -y install git
  else
    # Else, the distro is CentOS
    sudo yum -y update
    # Install apache webserver
    sudo yum -y install httpd
    # Install php
    sudo yum -y install php
    # Install mysql/mariadb
    # sudo yum -y install mariadb
    sudo yum -y install php-mysql
    # Install phpmyadmin
    sudo yum -y install phpmyadmin
    # Install curl
    sudo yum -y install curl
    sudo yum -y install php-curl
    # Install openssl
    sudo yum -y install openssl
    # Install graphical library for PHP
    sudo yum -y install php-gd
    # Install git
    sudo yum -y install git
  fi
  # Create project folder, written in 3 single mkdir-statements to make sure this runs everywhere without problems
  sudo mkdir "/var/www"
  sudo mkdir "/var/www/html"
  sudo mkdir "/var/www/html/${PROJECTFOLDER}"
  sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $PASSWORD"
  sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $PASSWORD"
  sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
  sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password $PASSWORD"
  sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password $PASSWORD"
  sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password $PASSWORD"
  sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2"
  # setup hosts file
  # VHOST=$(cat <<EOF
  # <VirtualHost *:80>
  #     DocumentRoot "/var/www/html/${PROJECTFOLDER}/public"
  #     <Directory "/var/www/html/${PROJECTFOLDER}/public">
  #         AllowOverride All
  #         Require all granted
  #     </Directory>
  # </VirtualHost>
  # EOF
  # )
  # echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf
  # enable mod_rewrite
  sudo a2enmod rewrite
  # restart apache
	if [[ "$OS" = 'debian' ]]; then
		# Little hack to check for systemd
		if pgrep systemd-journal; then
			systemctl restart apache2@server.service
		else
			/etc/init.d/apache2 restart
		fi
	else
		if pgrep systemd-journal; then
			systemctl restart apache2@server.service
			systemctl enable apache2@server.service
		else
			service apache2 restart
			chkconfig apache2 on
		fi
	fi
  # git clone VOID
  sudo git clone https://github.com/${USERNAME}/${REPOSITORY} "/var/www/html/${PROJECTFOLDER}"
  # install Composer
  curl -s https://getcomposer.org/installer | php
  mv composer.phar /usr/local/bin/composer
  # go to project folder, load Composer packages
  cd "/var/www/html/${PROJECTFOLDER}"
  composer install
  # run SQL statements from install folder
  sudo mysql -h "localhost" -u "root" "-p${PASSWORD}" < "/var/www/html/${PROJECTFOLDER}/application/_installation/01-create-database.sql"
  sudo mysql -h "localhost" -u "root" "-p${PASSWORD}" < "/var/www/html/${PROJECTFOLDER}/application/_installation/02-create-table-users.sql"
  # writing rights to avatar folder
  sudo chown -R www-data "/var/www/html/${PROJECTFOLDER}/public/avatars"
  # if this didn't work for you, you can also try the hard way:
  #sudo chmod 0777 -R "/var/www/html/${PROJECTFOLDER}/public/avatars"
  # remove Apache's default demo file
  sudo rm "/var/www/html/index.html"
  echo ""
  echo "Installation complete!"
  echo ""
  echo "Your client configuration is available at" ~/"var/www/html/${PROJECTFOLDER}/application/config/config.production.php"
fi


What I have tried:

# setup hosts file
VHOST=$(cat <<EOF
<VirtualHost *:80>
    DocumentRoot "/var/www/html/${PROJECTFOLDER}/public"
    <Directory "/var/www/html/${PROJECTFOLDER}/public">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf
Posted
Updated 9-Nov-17 3:44am
v5

1 solution

You are trying to do it too complicated. Just assign the string as it is (but escape double quotes):
VHOST="<VirtualHost *:80>
    DocumentRoot \"/var/www/html/${PROJECTFOLDER}/public\"
    <Directory \"/var/www/html/${PROJECTFOLDER}/public\">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
"
 
Share this answer
 
Comments
k5054 9-Nov-17 11:19am    
If you use single quotes around the entire block, you can omit the escapes for the double quotes
eg
VHOST='<VirtualHost *:80>
    DocumentRoot "/var/www/html/${PROJECTFOLDER}/public"
    <Directory "/var/www/html/${PROJECTFOLDER}/public">
        AllowOverride All
        Require all granted
    
<virtualhost>
'
Jochen Arndt 9-Nov-17 11:25am    
That won't work as required here because it would not expand the ${PROJECTFOLDER} variables.
p1Ggy 10-Nov-17 1:24am    
Thank you @Jochen_Arndt, I like your solution. I will accept it in a day or two, I would like to see if anyone else has other ways to do it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900