Create/Modify a Virtual Host and Reload Apache from Inside Rails App
-
21-09-2019 - |
Question
My application is a set of two rails applications. Based on some parameters in first app. I need to setup the virtual host of the second app. I just need to change the ServerName and ServerAlias in apache VH and enable the site using a2ensite and then 'apache2 reload '.
How can I do this from within a rails application?
Thanks, Imran
Solution
First of all: be aware that enabling web-apps to change the server configuration is a security risk.
# 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
I did not test neither code nor the regular expressions, please load the relevant parts of your config file into rubular.com and roll your own regex.
Maybe you should also make a backup before saving using
File.copy(file,file+".bak",true)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow