[英]Bash script to enable mod_rewrite AllowOverride All
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. 因此,我在安装了CentOS 6.4 64位操作系统盒的Mac上使用Vagrant / VirtualBox。
In my vagrant file, I have a provisioning script: 在我的vagrant文件中,我有一个配置脚本:
config.vm.provision :shell, :path => "bootstrap.sh"
And at the moment, my bootstrap.sh looks as follows: 目前,我的bootstrap.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/httpd.conf
文件,并在我的html目录中将AllowOverride None
更改为AllowOverride All
。
<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>
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" input.file
返回与模式匹配的行, grep -n "AllowOverride None" input.file
是行号 cut -f1 -d:
cuts the string and returns the first number it encounters cut -f1 -d:
剪切字符串并返回它遇到的第一个数字 sed "$LINE_NUMBERs/.*/AllowOverride All/" input.file
puts "AllowOverride All" at the line n° $LINE_NUMBER
sed "$LINE_NUMBERs/.*/AllowOverride All/" input.file
将“AllowOverride All”放在第n $LINE_NUMBER
You then just have to redirect the output to the file you want. 然后,您只需将输出重定向到所需的文件。
Et voila ! 瞧瞧!
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. 另一个不错的解决方案是使用SED命令搜索和替换两个模式之间的一条线。 In your case the patterns would be:
在您的情况下,模式将是:
'<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目录
(<Directory "/var/www/html">)
中的'AllowOverride None'行,而不是httpd.conf文件中包含'AllowOverride None'的每一行。
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. -i选项用于编辑文件。 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选项,则修改后的输出将打印在屏幕上,文件不会被修改!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.