2010-04-27 20:11:47 +00:00
|
|
|
module Apache
|
2010-05-10 19:57:34 +00:00
|
|
|
# Options that aren't specific to a particular purpose go here. Once enough like methods for a
|
|
|
|
# particular purpose exist, break them out into a separate module.
|
2010-04-27 20:11:47 +00:00
|
|
|
module Master
|
2011-09-27 19:38:12 +00:00
|
|
|
class << self
|
|
|
|
def listening_on
|
|
|
|
@listening_on ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Build a module list.
|
|
|
|
# Wraps around Modules.build
|
2010-04-27 20:27:31 +00:00
|
|
|
def modules(*modules, &block)
|
2010-05-04 21:04:44 +00:00
|
|
|
@config += Modules.build(*modules, &block)
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
|
|
|
|
2010-05-18 16:43:57 +00:00
|
|
|
# Listen on network interfaces and ports
|
|
|
|
#
|
|
|
|
# Each provided parameter generates a new Listen line:
|
|
|
|
# listen "1.2.3.4:80", "2.3.4.5:80" #=>
|
|
|
|
# Listen "1.2.3.4:80"
|
|
|
|
# Listen "2.3.4.5:80"
|
2010-05-11 19:19:16 +00:00
|
|
|
def listen(*opt)
|
2011-09-27 19:38:12 +00:00
|
|
|
opt.collect(&:quoteize).each do |adapter|
|
|
|
|
if !Apache::Master.listening_on.include?(adapter)
|
|
|
|
self << "Listen #{adapter}"
|
|
|
|
Apache::Master.listening_on << adapter
|
|
|
|
else
|
|
|
|
$stderr.puts "Multiple Listens for #{adapter}".foreground(:red)
|
|
|
|
end
|
|
|
|
end
|
2010-05-11 19:19:16 +00:00
|
|
|
end
|
|
|
|
alias :listen! :listen
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Add a User/Group block
|
|
|
|
# runner('www', 'www-data') #=>
|
|
|
|
# User www
|
|
|
|
# Group www-data
|
2010-05-18 21:43:23 +00:00
|
|
|
def runner(user, group)
|
2010-04-27 20:27:31 +00:00
|
|
|
user! user
|
2010-05-18 21:43:23 +00:00
|
|
|
group! group
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Enable Passenger on this server
|
|
|
|
#
|
|
|
|
# This assumes that Passenger was installed via the gem. This may or may not work for you, but it works for me.
|
2010-04-27 20:27:31 +00:00
|
|
|
def passenger(ruby_root, ruby_version, passenger_version)
|
2010-05-04 15:48:59 +00:00
|
|
|
load_module 'passenger_module', "#{ruby_root}/lib/ruby/gems/#{ruby_version}/gems/passenger-#{passenger_version}/ext/apache2/mod_passenger.so"
|
|
|
|
passenger_root "#{ruby_root}/lib/ruby/gems/#{ruby_version}/gems/passenger-#{passenger_version}"
|
|
|
|
passenger_ruby "#{ruby_root}/bin/ruby"
|
2010-04-28 20:19:03 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Enable gzip compression server-wide on pretty much everything that can be gzip compressed
|
2010-05-05 14:44:20 +00:00
|
|
|
def enable_gzip!
|
|
|
|
directory '/' do
|
|
|
|
add_output_filter_by_type! :DEFLATE, 'text/html', 'text/plain', 'text/css', 'text/javascript', 'application/javascript'
|
|
|
|
browser_match! '^Mozilla/4', "gzip-only-text/html"
|
|
|
|
browser_match! '^Mozilla/4\.0[678]', "no-gzip"
|
|
|
|
browser_match! '\bMSIE', '!no-gzip', '!gzip-only-text/html'
|
|
|
|
end
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Set the TCP timeout. Defined here to get around various other timeout methods.
|
2010-05-18 17:08:17 +00:00
|
|
|
def timeout(time)
|
|
|
|
self << "Timeout #{time}"
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Add a comment to the Apache config. Can pass in either a String or Array of comment lines.
|
2010-05-18 17:08:17 +00:00
|
|
|
def comment(what)
|
|
|
|
self + [ '', what.commentize, '' ].flatten.collect { |line| "# #{line.strip}".strip }
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Create a ScriptAlias, checking to make sure the filesystem path exists.
|
2010-05-05 16:25:07 +00:00
|
|
|
def script_alias(uri, path)
|
|
|
|
directory? path
|
2010-05-18 17:08:17 +00:00
|
|
|
self << %{ScriptAlias #{[ uri, path ].quoteize * ' '}}
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
alias :script_alias! :script_alias
|
2010-05-07 02:26:12 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Add a MIME type, potentially also adding handlers and encodings
|
|
|
|
# add_type! 'text/html', '.shtml', :handler => 'server-parsed'
|
|
|
|
# add_type! 'text/html', '.gz', :encoding => 'gzip'
|
2010-05-07 02:26:12 +00:00
|
|
|
def add_type!(mime, extension, options = {})
|
|
|
|
self << "AddType #{mime} #{extension}"
|
|
|
|
options.each do |type, value|
|
|
|
|
self << "Add#{type.to_s.capitalize} #{value} #{extension}"
|
|
|
|
end
|
|
|
|
end
|
2010-05-07 15:26:40 +00:00
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Include other config files or directories.
|
|
|
|
# Used to get around reserved Ruby keyword.
|
2010-05-07 15:26:40 +00:00
|
|
|
def apache_include(*opts)
|
|
|
|
self << "Include #{opts * " "}"
|
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Alias a URL to a directory in the filesystem.
|
|
|
|
# Used to get around reserved Ruby keyword.
|
2010-05-07 15:26:40 +00:00
|
|
|
def apache_alias(*opts)
|
2010-05-18 17:08:17 +00:00
|
|
|
self << "Alias #{opts.quoteize * " "}"
|
2010-05-07 15:26:40 +00:00
|
|
|
end
|
|
|
|
|
2010-05-10 19:57:34 +00:00
|
|
|
# Set multiple headers to be delivered for a particular section
|
|
|
|
# set_header 'Content-type' => 'application/octet-stream',
|
|
|
|
# 'Content-disposition' => [ 'attachment', 'env=only-for-downloads' ] #=>
|
|
|
|
# Header set "Content-type" "application/octet-stream"
|
|
|
|
# Header set "Content-dispoaition" "attachment" env=only-for-downloads
|
2010-05-07 15:26:40 +00:00
|
|
|
def set_header(hash)
|
|
|
|
hash.each do |key, value|
|
2010-05-18 21:43:23 +00:00
|
|
|
self << "Header set #{key.quoteize} #{value.headerize}"
|
2010-05-07 15:26:40 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-11 13:25:29 +00:00
|
|
|
|
|
|
|
def server_name(*opts)
|
|
|
|
if first = opts.shift
|
2010-05-18 17:08:17 +00:00
|
|
|
self << "ServerName #{first.quoteize}"
|
|
|
|
opts.each { |name| server_alias name } if !opts.empty?
|
2010-05-11 13:25:29 +00:00
|
|
|
end
|
|
|
|
end
|
2010-05-12 22:10:19 +00:00
|
|
|
|
|
|
|
def document_root(*opts)
|
2010-05-18 17:08:17 +00:00
|
|
|
dir = opts.first
|
|
|
|
directory? dir
|
|
|
|
self << "DocumentRoot #{dir.quoteize}"
|
2010-05-12 22:10:19 +00:00
|
|
|
end
|
|
|
|
alias :document_root! :document_root
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
|
|
|
end
|