将 apache 2.2 配置文件迁移到 2.4 的脚本

将 apache 2.2 配置文件迁移到 2.4 的脚本

配置文件的语法已从 Apache 2.2 更改为 2.4,例如:

2.2 configuration:   
Order deny,allow   
Deny from all

2.4 configuration:   
Require all denied

2.2 configuration:   
Order allow,deny   
Allow from all

2.4 configuration:   
Require all granted

现在我想检查我的所有 .htaccess 文件是否有必要的更改。下面的脚本是第一次尝试查找需要更改的 .htaccess 文件。

  • 函数 docheck() 还需要检查哪些其他关键字或情况?
  • 是否有其他可用的脚本可以完成这个工作?

答案1

这个脚本是迄今为止我能做的最好的:

#!/bin/bash
# WF 2015-11-28
# check htaccess files to be upgraded
# from Apache 2.2 to 2.4

#
# fix
#
fix() {
  local l_htaccess="$1"
  local l_newhtaccess=/tmp/htaccess$$
  local l_diff=/tmp/htaccessdiff$$
  cat $l_htaccess | awk '
/Order/ { 
  next
}
/Allow from all/ { 
   print "Require all granted"
   next
}
/Deny from all/ { 
   print "Require all denied"
   next 
}
{ print }
' > $l_newhtaccess 
  diff $l_htaccess $l_newhtaccess > $l_diff
  if [ $? -ne 0 ]
  then
    echo $l_htaccess had been modified
    tail -8 $l_htaccess
    tail -8 $l_newhtaccess
    tail -8 $l_diff 
    echo -n "shall the new configuration be used y/n/c? "
    read answer
    #dialog --backtitle "shall the new configuration be used? " \
    #       --begin 2 2 --title $l_htaccess \
    #       --textbox $l_htaccess 8 70 
    #       --textbox $l_newhtaccess 8 70 
    #       --textbox $l_diff 8 70 
    case $answer in
      y) 
        mv "$l_newhtaccess" "$l_htaccess"
      ;;
      n) 
       rm $l_newhtaccess
       rm $l_diff
       ;;
     c)  
       rm $l_newhtaccess
       rm $l_diff
       exit 1
       ;;
    esac 
  else
    echo $l_htaccess is unchanged 
    cat $l_htaccess
  fi
  rm $l_newhtaccess
  rm $l_diff
}

#
# loop over the htaccess files
#
docheck() {
  local l_htaccesslist="$1"
  local l_wwwroot="$2"
  for file in `cat $l_htaccesslist`
  do
    htaccess=$l_wwwroot/$file
    needsfix=false
    for keywords in Order Deny Allow
    do
      grep -i deny $htaccess > /dev/null
      if [ $? -eq 0 ]
      then
        needsfix=true 
      fi
    done
    if [ "$needsfix" = "true" ]
    then
      fix $htaccess
    fi
  done
}

htaccesslist=/tmp/htaccess.txt
wwwroot=/var/www
cd $wwwroot 
find . -name .htaccess > $htaccesslist
docheck $htaccesslist $wwwroot

答案2

这里用4个命令,修改路线。

cp /path/to/your/sites-available/ /path/to/your/sites-available/backup
perl -i -p -e "s\deny from all\Require all denied\g" /path/to/your/sites-available/*
perl -i -p -e "s\allow from all\Require all granted\g" /path/to/your/sites-available/*
sed -i '/Order/d' /path/to/your/sites-available/*

相关内容