【问题标题】:Create/Modify a Virtual Host and Reload Apache from Inside Rails App创建/修改虚拟主机并从 Inside Rails 应用程序重新加载 Apache
【发布时间】:2010-03-21 11:18:54
【问题描述】:
我的应用程序是一组两个 rails 应用程序。基于第一个应用程序中的一些参数。我需要设置第二个应用程序的虚拟主机。我只需要更改 apache VH 中的 ServerName 和 ServerAlias 并使用 a2ensite 启用该站点,然后使用“apache2 reload”。
如何在 Rails 应用程序中执行此操作?
谢谢,
伊姆兰
【问题讨论】:
标签:
ruby-on-rails
apache
virtualhost
【解决方案1】:
首先:请注意,启用网络应用程序来更改服务器配置是一种安全风险。
# 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)