User Tools

apache

add site

#! /bin/bash
#check
#add
#

echo "username domain"

[ $# -eq 0 ] && { echo "not ok"; exit 999; }

#-------- function to add a vhost --------
add_vhost(){

  echo "  Creating folder : /home/$username/www/$domain/config/apache"
  mkdir -p /home/$username/www/$domain/config/apache

  echo "  Creating folder : /home/$username/www/$domain/htdocs"
  mkdir -p /home/$username/www/$domain/htdocs

  echo "  Creating folder : /home/$username/www/$domain/log/apache"
  mkdir -p /home/$username/www/$domain/log/apache

  echo "  adding : $domain to vhosts"
  echo "
<VirtualHost *:80>
  ServerName $domain
  ServerAdmin webmaster@localhost

  DocumentRoot /home/$username/www/$domain/htdocs

  <Directory /home/$username/www/$domain/htdocs/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  ErrorLog \"|/usr/sbin/rotatelogs /home/$username/www/$domain/log/apache/error_log.%Y-%m-%d-%H 86400\"

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

  CustomLog \"|/usr/sbin/rotatelogs /home/$username/www/$domain/log/apache/access_log.%Y-%m-%d-%H 86400\" combined

  php_admin_value sendmail_path \"/usr/bin/msmtp -t -C /home/$username/www/$domain/config/msmtp/.msmtprc\"

</VirtualHost>
" >> /home/$username/www/$domain/config/apache/$domain

  echo "  Setting rights to /home/$username/www/$domain"
  chown -R $username:www-data /home/$username/www/$domain
  cd /home/$username/www/$domain
  find ./* | while read file; do [[ -f "$file" ]] && chmod 644 "$file"; [[ -d "$file" ]] && chmod 2755 "$file"; done

  ln -s /home/$username/www/$domain/config/apache/$domain /etc/apache2/sites-enabled/$domain
  ls -al /etc/apache2/sites-enabled/$domain
}
vhost_exists=0
check_vhost(){
    # check for vhost file
    echo "check if /home/$username/www/$domain/config/apache/$domain exists"
  if [ -f /home/$username/www/$domain/config/apache/$domain ];
    then
      echo -e "\033[31m Apache vhost file exists."
        vhost_exists=1
    else
        echo -e "\033[32m Apache vhost does NOT exists."
    fi
    echo -n -e "\033[0m"

    # check for www folder
    echo "check if /home/$username/www/$domain/htdocs exists"
    if [ -d /home/$username/www/$domain/htdocs ];
    then
      echo -e "\033[31m Htdocs folder exists."
        vhost_exists=1
    else
        echo -e "\033[32m Htdocs folder does NOT exists."
    fi
    echo -n -e "\033[0m"

    # check for logs folder
    echo "check if /home/$username/www/$domain/log/apache exists"
    if [ -d /home/$username/www/$domain/log/apache ];
    then
      echo -e "\033[31m Logs folder exists."
        vhost_exists=1
    else
        echo -e "\033[32m Logs folder does NOT exists."
    fi
    echo -n -e "\033[0m"
}

if [ $# ]; then
        username=$1
        domain=$2
  check_vhost
    if [ $# -eq 2 ]; then
  if [ $vhost_exists -eq 0 ]; then
            read -p "Do you really want to run these parameters ? (y/n)" -n 1
            if [[ $REPLY =~ ^[Yy]$ ]]
            then
          add_vhost
                echo "ok"
            else
                echo "Tant pis"
                exit 0
            fi
    else
      echo "$username exists in vhost"
            echo $vhost_exists
    fi
  else
    echo "$0 [erreur]: manque username et domain"
  fi
else
  echo "$0 [erreur]: usage: passer un argument \ncomme chais pas moi...euh...toto"
    exit 0
fi

my lamp

#! /bin/bash

# gadget for coloring text
ColEscape="\033";
ColBlackF="${ColEscape}[30m";
ColRedF="${ColEscape}[31m";
ColGreenF="${ColEscape}[32m";
ColYellowF="${ColEscape}[33m";
ColBlueF="${ColEscape}[34m";
ColPurplef="${ColEscape}[35m";
ColCyanF="${ColEscape}[36m";
ColWhiteF="${ColEscape}[37m";
ColBlackB="${ColEscape}[40m";
ColRedB="${ColEscape}[41m";
ColGreenB="${ColEscape}[42m";
ColYellowB="${ColEscape}[43m";
ColBlueB="${ColEscape}[44m";
ColPurpleB="${ColEscape}[45m";
ColCyanB="${ColEscape}[46m";
ColWhiteB="${ColEscape}[47m";
ColBoldOn="${ColEscape}[1m";
ColBoldOff="${ColEscape}[22m";
ColItalicsOn="${ColEscape}[3m";
ColItalicsOff="${ColEscape}[23m";
ColUnderlineOn="${ColEscape}[4m";
ColUnderlineOff="${ColEscape}[24m";
ColBlinkOn="${ColEscape}[5m";
ColBlinkOff="${ColEscape}[25m";
ColInvertOn="${ColEscape}[7m";
ColInvertOff="${ColEscape}[27m";
ColReset="${ColEscape}[0m";

# symbols
SymbolOk="${ColGreenF}${ColReset} "
SymbolNotOk="${ColRedF}${ColReset} "

# display message
# $1 message
# $2 level
print_message() {
  case "$2" in
    ok)
      echo -e "${SymbolOk}$1"
    ;;

    notok)
      echo -e "${SymbolNotOk}$1"
    ;;

    error)
      echo -e "${ColRedF}$1${ColReset}"
    ;;

    question)
      echo -e "${ColYellowF}$1${ColReset}"
    ;;

    *)
      echo "$1"
    ;;
  esac
}

</pre>
<pre class="brush:bash">
#! /bin/bash

[ $# -eq 0 ] && { echo "example : chwww toto"; exit 999; }

# check if the theme file exists
if [ -f "/opt/scripts/theme" ];then
  source "/opt/scripts/theme"
else
  print_message() {
    echo "$1"
  }
fi

DOMAIN=$1

if [ -d /var/www/${DOMAIN}/htdocs ];then
  print_message "Domain exists" "ok"
else
  print_message "Domain does not exist" "error"
  exit 1;
fi

chown www-data:www-data /var/www/${DOMAIN}/htdocs
chmod 2775 /var/www/${DOMAIN}/htdocs
find /var/www/${DOMAIN}/htdocs/* | while read file; do [[ -f "$file" ]] && chmod 664 "$file"; [[ -d "$file" ]] && chmod 2775 "$file"; done

</pre>
<pre class="brush:bash">
#! /bin/bash
#check
#add
#

[ $# -eq 0 ] && { echo "example : addsite toto"; exit 999; }

# check if the theme file exists
if [ -f "/opt/scripts/theme" ];then
  source "/opt/scripts/theme"
else
  print_message() {
    echo "$1"
  }
fi

DOMAIN=$1

# add a vhost
add_vhost(){

  print_message "Creating folder : /var/www/${DOMAIN}/htdocs"
  mkdir -p /var/www/${DOMAIN}/htdocs

  print_message "Creating folder : /var/www/${DOMAIN}/etc/apache2"
  mkdir -p /var/www/${DOMAIN}/etc/apache2

  print_message "Creating folder : /var/www/${DOMAIN}/var/log/apache2"
  mkdir -p /var/www/${DOMAIN}/var/log/apache2

  print_message "Creating folder : /var/www/${DOMAIN}/var/backup"
  mkdir -p /var/www/${DOMAIN}/var/backup

  print_message "adding : ${DOMAIN} to vhosts"
  echo "
<VirtualHost *:80>
  ServerName ${DOMAIN}
  ServerAlias www.${DOMAIN}
  ServerAdmin webmaster@${DOMAIN}

  DocumentRoot /var/www/${DOMAIN}/htdocs

  <Directory /var/www/${DOMAIN}/htdocs/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  ErrorLog /var/www/${DOMAIN}/var/log/apache2/error.log

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

  CustomLog /var/www/${DOMAIN}/var/log/apache2/access.log combined
</VirtualHost>

" >> /var/www/${DOMAIN}/etc/apache2/${DOMAIN}

  print_message "Setting rights to /var/www/${DOMAIN}"
  chown -R www-data:www-data /var/www/${DOMAIN}
  chmod 2770 /var/www/${DOMAIN}
  find /var/www/${DOMAIN}/* | while read file; do [[ -f "$file" ]] && chmod 664 "$file"; [[ -d "$file" ]] && chmod 2775 "$file"; done
  ln -s /var/www/${DOMAIN}/etc/apache2/${DOMAIN} /etc/apache2/sites-enabled/${DOMAIN}
  print_message "Job finished." "ok"
}

# check if vhost exists
vhost_exists=0
check_vhost(){

    # check for vhost file
    #print_message "-- check if /var/www/${DOMAIN}/etc/apache2/${DOMAIN} exists"
    if [ -f /var/www/${DOMAIN}/etc/apache2/${DOMAIN} ];
    then
      print_message "Apache vhost file exists." "ok"
        vhost_exists=1
    else
        print_message "Apache vhost does NOT exists." "notok"
    fi

    # check for www folder
    #print_message "-- check if /var/www/${DOMAIN}/htdocs exists"
    if [ -d /var/www/${DOMAIN}/htdocs ];
    then
      print_message "Htdocs folder exists." "ok"
        vhost_exists=1
    else
        print_message "Htdocs folder does NOT exists." "notok"
    fi

    # check for logs folder
    #print_message "-- check if /var/www/${DOMAIN}/var/log/apache2 exists"
    if [ -d /var/www/${DOMAIN}/var/log/apache2 ];
    then
      print_message "Logs folder exists." "ok"
        vhost_exists=1
    else
        print_message "Logs folder does NOT exists." "notok"
    fi
}

check_vhost
if [ ${vhost_exists} -eq 0 ]; then
    print_message "Do you really want to run these parameters ? (y/n)" "question"
    read answer
    if [[ ${answer} =~ ^[Yy]$ ]]; then
      add_vhost
      echo "ok"
    else
      echo "Tant pis, bye"
      exit 0
    fi
  else
    print_message "already exists in vhost" "error"
fi

simple apache vhost

<VirtualHost *:80>
  ServerName mysite.com
  ServerAdmin webmaster@localhost

  DocumentRoot /var/www/mysite/htdocs

  <Directory /var/www/mysite/htdocs/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

  LogLevel warn
  ErrorLog "|/usr/sbin/rotatelogs /var/www/mysite/log/apache/error_log.%Y-%m-%d-%H 86400"
  CustomLog "|/usr/sbin/rotatelogs /var/www/mysite/log/apache/access_log.%Y-%m-%d-%H 86400" combined

</VirtualHost>

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information