大网数据广告活动
服务器运维问答

script to migrate apache 2.2 to 2.4 configuration files

· 52CE 我爱测

The syntax for configuration files has changed from Apache 2.2 to 2.4 e.g.: 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 configurat…

问题原文

原帖链接 · Wolfgang Fahl · CC BY-SA 3.0

The syntax for configuration files has changed from Apache 2.2 to 2.4 e.g.:

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

Now I'd like to check all my .htaccess files for necessary changes. The script below is a first attempt to find the .htaccess files that need changes.

  • What other keywords or cases would the function docheck() have to check?
  • Is there some other script available that already does the job?

已采纳解答

原帖链接 · Wolfgang Fahl · CC BY-SA 3.0

This script is the best I could do so far:

#!/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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-11-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。