apache-config-generator/README.rdoc

103 lines
3.6 KiB
Plaintext
Raw Normal View History

2010-05-11 19:19:16 +00:00
= Apache Config Generator
Programmatically construct your Apache configuration using a powerful DSL built in Ruby.
2011-01-06 16:20:32 +00:00
As of version 0.2.7, your destination config directory is destroyed and re-created each time the apache:create Rake task is run.
2010-05-11 19:19:16 +00:00
== Installation
<tt>gem install apache-config-generator</tt>
== Usage
Run <tt>apache-configurator <directory></tt> to create a new directory to hold your config files.
2010-10-26 14:31:49 +00:00
A Rakefile, Gemfile, and config.yml file will also be generated.
=== Rakefile tasks
Apache Config Generator defines several tasks for managing Apache config files:
* apache:create[environment] creates your config files for the specified environment
* apache:environments lists all possible environments that can be generated
* apache:default[environment] sets a default environment to use with a parameter-less apache:create (or the default rake task)
2010-05-11 19:19:16 +00:00
== Building a config file
Configs center around the Apache::Config.build method:
Apache::Config.build('sites-available/my-site.conf') do
server_name 'my-cool-website.cool.wow'
document_root '/var/www/my-cool-website'
2010-11-29 23:22:03 +00:00
server_admin! "john@coswellproductions.com"
2010-05-14 18:56:01 +00:00
2010-05-11 19:19:16 +00:00
directory '/' do
options :follow_sym_links, :indexes
allow_from_all
end
location_match %r{^/secret} do
deny_from_all
basic_authentication "My secret", '/etc/apache2/users/global.users', :user => :john
satisfy :any
end
2010-05-14 18:56:01 +00:00
rewrites "My old content" do
2010-11-29 23:22:03 +00:00
cond "%{HTTP_REFERER}", '!^my-cool-website\.cool\.wow$'
rule %r{\.(gif|jpg|png|pdf)$}, '/lol-image-stealer.html', :last => true, :redirect => true
2010-05-14 18:56:01 +00:00
2010-11-29 23:22:03 +00:00
rewrite_test '/index.html', '/index.html', :http_referer => 'other.site'
rewrite_test '/index.gif', '/lol-image-stealer.html', :http_referer => 'other.site'
rewrite_test '/index.gif', '/index.gif', :http_referer => 'my-cool-website.cool.wow'
2010-05-14 18:56:01 +00:00
end
2010-05-11 19:19:16 +00:00
end
2010-05-14 18:56:01 +00:00
Notes on how the conversion works:
* Methods within the build block are translated into NerdCapsed Apache directives.
* Directives that house children take blocks that contain the child methods.
* Directives that expect regular expressions take a Regexp object.
* Passing a String as a parameter, by default, double-quotes it.
* Passing in a Symbol does not quote the parameter.
2010-11-29 23:22:03 +00:00
* Some directives NerdCap Symbols, such as Options
2010-05-14 18:56:01 +00:00
* Appending an exclamation point to the method turns off quoting.
* Shortcut methods are defined as modules under the Apache module.
There are also sanity checks that occur when configuration is being generated:
* Directives that rely on a path will check to see if the path exists.
* Since you need to use Regexp objects for directives that require a regular expression,
bad expressions will be flagged by the Ruby interpreter.
* Rewrite rules can be tested with the rewrite_test method.
The above config is transformed into the following:
2010-11-29 23:22:03 +00:00
ServerName "my-cool-website.cool.wow"
DocumentRoot "/var/www/my-cool-website"
ServerAdmin john@coswellproductions.com
2010-05-14 18:56:01 +00:00
2010-11-29 23:22:03 +00:00
<Directory "/">
Options FollowSymLinks, Indexes
Allow from all
</Directory>
2010-05-14 18:56:01 +00:00
2010-11-29 23:22:03 +00:00
<LocationMatch "^/secret">
Deny from all
2010-05-14 18:56:01 +00:00
2010-11-29 23:22:03 +00:00
AuthType Basic
AuthName "My secret"
AuthUserFile "/etc/apache2/users/global.users"
Require user john
</LocationMatch>
2010-05-14 18:56:01 +00:00
2010-11-29 23:22:03 +00:00
RewriteCond "%{HTTP_REFERER}" "^!my-cool-website\.cool\.wow"
RewriteRule "\.(gif|jpg|png|pdf)$" "/lol-image-stealer.html" [L,R]
2011-01-06 16:20:32 +00:00
The destination configs directory is destroyed and re-created each time the apache:create Rake task is run.
2010-11-29 23:22:03 +00:00
== Using Apache::Config separately
Include the gem and access the methods on Apache::Config directly. See test/example_standalone.rb
for an example.