从Inside Rails App创建/修改虚拟主机并重新加载Apache

时间:2010-03-21 11:18:55

标签: ruby-on-rails apache virtualhost

我的应用程序是一组两个rails应用程序。基于第一个应用程序中的一些参数。我需要设置第二个应用程序的虚拟主机。我只需要在apache VH中更改ServerName和ServerAlias并使用a2ensite然后'apache2 reload'启用该站点。

如何在rails应用程序中执行此操作?

谢谢, 姆兰

1 个答案:

答案 0 :(得分:1)

首先:请注意,启用Web应用程序来更改服务器配置存在安全风险。

#  First, open the config file
fd=File.open('/etc/apache2/sites/yoursite', 'r+')
#  read in the contents
content=fd.read
#  now replace the ServerName and ServerAlias lines with your new setting
if content.gsub!(/ServerName(.*)/,"ServerName NewServerName") and content.gsub!(/ServerAlias(.*)/,"ServerAlias NewServerAlias")
  fd.rewind
  puts "\tsaving file"
  fd.write content
end
fd.close

我没有测试代码和正则表达式,请将配置文件的相关部分加载到rubular.com并滚动自己的正则表达式。

也许你应该在使用

保存之前进行备份
File.copy(file,file+".bak",true)