启用mod_rewrite All的Bash脚本

时间:2021-08-06 11:18:56

I am moving away from AMPPS / MAMP and looking to build a dev environment as close to the production environment as possible.

我正在远离AMPPS / MAMP,并希望构建一个尽可能接近生产环境的开发环境。

As such, I am using Vagrant / VirtualBox on my Mac with CentOS 6.4 64bit os box installed.

因此,我在我的Mac上使用的是一个带有CentOS 6.4 64bit操作系统的“垃圾”/ VirtualBox。

In my vagrant file, I have a provisioning script:

在我的流浪文件中,我有一个准备脚本:

config.vm.provision :shell, :path => "bootstrap.sh"

And at the moment, my bootstrap.sh looks as follows:

现在,我的鞋带。sh看起来如下:

#!/usr/bin/env bash

# Install the remi repos
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

# Update the repositories
sudo yum -y update

sudo yum -y --enablerepo=remi,remi-php55 install php-pecl-apc php-cli php-pear php-pdo php-mysqlnd php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

Now I have my apache server installed, I want my bash script to edit the /etc/httpd/conf/httpd.conf file and change AllowOverride None to AllowOverride All in my html directory.

现在我已经安装了apache服务器,我希望我的bash脚本编辑/etc/httpd/conf/httpdconf文件和更改AllowOverride None以允许在我的html目录中覆盖所有文件。

<Directory "/var/www/html">

...

    Options Indexes FollowSymLinks

...

    # Need to change this to AllowOverride All via bash script
    AllowOverride None

...

    Order allow,deny
    Allow from all

</Directory>

2 个解决方案

#1


3  

Here is a nice oneliner for you:

这里有一个不错的oneliner给你:

sed "$(grep -n "AllowOverride None" input.file |cut -f1 -d:)s/.*/AllowOverride All/" input.file > output.file

For the breakdown:

分解:

  • grep -n "AllowOverride None" input.file returns the lines that match the pattern, preceeded by the line number
  • grep -n“AllowOverride None”输入。file返回与模式匹配的行,在行号之前
  • cut -f1 -d: cuts the string and returns the first number it encounters
  • 剪切-f1 -d:剪切字符串并返回它遇到的第一个数字
  • sed "$LINE_NUMBERs/.*/AllowOverride All/" input.file puts "AllowOverride All" at the line n° $LINE_NUMBER
  • sed“LINE_NUMBERs美元/。* / AllowOverride /”输入。文件把“AllowOverride所有”行n°LINE_NUMBER美元

You then just have to redirect the output to the file you want.

然后只需将输出重定向到所需的文件。

Et voila !

果不其然!

#2


0  

Use Linux SED command to search & replace a line between two patterns

使用Linux SED命令搜索和替换两个模式之间的一行

Another nice solution is to use the SED command to search & replace a line between two patterns. In your case the patterns would be:

另一个不错的解决方案是使用SED命令来搜索和替换两个模式之间的一条线。在你的情况下,模式是:

'<Directory "/var/www/html">' and </directory>'

This way you replace only the line 'AllowOverride None' inside your html directory (<Directory "/var/www/html">) and not every line that contains 'AllowOverride None' in your httpd.conf file.

这样,您只需替换html目录中的“AllowOverride None”行(< directory”/var/www/html“>”),而不是在httpd中包含“AllowOverride None”行。conf文件。

The command SED supports patterns ranges in this form:

命令SED支持以下形式的模式范围:

sed '/startpattern/,/endpattern/ <sed-commands>' file

In your case, this becomes:

在你的例子中,这变成:

sed -i '/<Directory "\/var\/www\/html">/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' /etc/httpd/conf/httpd.conf

The -i option is used to edit files in place. If you don’t use the option -i, then the modified output will be printed on the screen and the file wouldn’t be modified!

i选项用于就地编辑文件。如果您不使用选项-i,那么修改后的输出将被打印到屏幕上,文件将不会被修改!

#1


3  

Here is a nice oneliner for you:

这里有一个不错的oneliner给你:

sed "$(grep -n "AllowOverride None" input.file |cut -f1 -d:)s/.*/AllowOverride All/" input.file > output.file

For the breakdown:

分解:

  • grep -n "AllowOverride None" input.file returns the lines that match the pattern, preceeded by the line number
  • grep -n“AllowOverride None”输入。file返回与模式匹配的行,在行号之前
  • cut -f1 -d: cuts the string and returns the first number it encounters
  • 剪切-f1 -d:剪切字符串并返回它遇到的第一个数字
  • sed "$LINE_NUMBERs/.*/AllowOverride All/" input.file puts "AllowOverride All" at the line n° $LINE_NUMBER
  • sed“LINE_NUMBERs美元/。* / AllowOverride /”输入。文件把“AllowOverride所有”行n°LINE_NUMBER美元

You then just have to redirect the output to the file you want.

然后只需将输出重定向到所需的文件。

Et voila !

果不其然!

#2


0  

Use Linux SED command to search & replace a line between two patterns

使用Linux SED命令搜索和替换两个模式之间的一行

Another nice solution is to use the SED command to search & replace a line between two patterns. In your case the patterns would be:

另一个不错的解决方案是使用SED命令来搜索和替换两个模式之间的一条线。在你的情况下,模式是:

'<Directory "/var/www/html">' and </directory>'

This way you replace only the line 'AllowOverride None' inside your html directory (<Directory "/var/www/html">) and not every line that contains 'AllowOverride None' in your httpd.conf file.

这样,您只需替换html目录中的“AllowOverride None”行(< directory”/var/www/html“>”),而不是在httpd中包含“AllowOverride None”行。conf文件。

The command SED supports patterns ranges in this form:

命令SED支持以下形式的模式范围:

sed '/startpattern/,/endpattern/ <sed-commands>' file

In your case, this becomes:

在你的例子中,这变成:

sed -i '/<Directory "\/var\/www\/html">/,/<\/Directory>/ s/AllowOverride None/AllowOverride all/' /etc/httpd/conf/httpd.conf

The -i option is used to edit files in place. If you don’t use the option -i, then the modified output will be printed on the screen and the file wouldn’t be modified!

i选项用于就地编辑文件。如果您不使用选项-i,那么修改后的输出将被打印到屏幕上,文件将不会被修改!