Class: Rack::Handler::Apache

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/handler/apache.rb

Class Method Summary collapse

Class Method Details

.run(app, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rack/handler/apache.rb', line 23

def self.run(app, options={})

  unless ::File.exists? ::PhusionPassenger::APACHE2_MODULE
    puts "Fatal: Passenger apache module missing, did you run passenger-install-apache2-module?"
    exit
  end

  @root      = ::Dir.pwd
  @port      = options[:Port] || 8080
  @host      = options[:Host] || '127.0.0.1'
  @pid_file  = "#{@root}/tmp/rack-helper-apache.pid"
  @conf_file = "#{@root}/tmp/httpd.conf"

  # Find the right passenger-install-apache2-module to get the config snipper
  piam = ::File.expand_path("../../bin/passenger-install-apache2-module", $".find {|f|f=~/phusion_passenger.rb$/})
  @passenger = `#{piam} --snippet`

  puts "Warning: Please use SSLCertificateFile, not SSLCertificate" if
    options[:SSLCertificate] && !options[:SSLCertificateFile]

  puts "Warning: Please use SSLPrivateKeyFile, not SSLPrivateKey" if
    options[:SSLPrivateKey] && !options[:SSLPrivateKeyFile]

  config = <<-EOD.gsub(/^ {10}/, '')
    #{@passenger}
    User #{Etc.getlogin}
    Listen #{@host}:#{@port}
    PidFile #{@pid_file}
    ErrorLog #{$stdout.ttyname}
    LockFile #{@root}/tmp/rack-helper-apache.lock
    ServerName localhost
    <VirtualHost *:#{@port}>
      ServerName localhost
      #{options[:SSLEnable] ? "SSLEngine on" : ""}
      DocumentRoot #{@root}/public
      <Directory #{@root}/public>
        AllowOverride all
        Options -MultiViews
      </Directory>
      #{options[:HostConfig]}
    </VirtualHost>
    #{options[:ServerConfig]}
  EOD

  if options[:SSLEnable]
    config = <<-EOD.gsub(/^ {12}/, '')+config
      LoadModule ssl_module libexec/apache2/mod_ssl.so
      SSLCertificateFile #{options[:SSLCertificateFile]}
      SSLCertificateKeyFile #{options[:SSLPrivateKeyFile]}
      SSLSessionCache none
    EOD
  end

  ::Dir.mkdir("tmp") unless ::Dir.exists? "tmp"

  kill_httpd()
  ::File.open(@conf_file, 'w') { |f| f.write(config) }
  system(apache_bin,'-f',@conf_file)

  print "Waiting for apache to start..."
  10.times { break if is_running?;  sleep 0.5; print "."; STDOUT.flush }

  if is_running?
    puts "...started [pid:#{get_pid}]"
    puts "Available at #{options[:SSLEnable]?'https':'http'}://#{@host}:#{@port}"
    sleep 0.5 while is_running?
    puts "Apache terminated."
  else
    puts "...never started!"
    exit
  end

end

.shutdownObject



98
99
100
# File 'lib/rack/handler/apache.rb', line 98

def self.shutdown()
  kill_httpd()
end

.valid_optionsObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/handler/apache.rb', line 11

def self.valid_options
  {
    "Port=PORT"                   => "Port to listen on (default: 8080)",
    "Host=HOST"                   => "Hostname to listen on (default: 127.0.0.1)",
    "SSLEnable=BOOL"              => "Enable https",
    "SSLCertificateFile=FILENAME" => "SSL Certificate file name",
    "SSLPrivateKeyFile=FILENAME"  => "SSL Private Key file name",
    "ServerConfig=TEXT"           => "Additional httpd.conf lines for server",
    "HostConfig=TEXT"             => "Additional httpd.conf lines for virtual host"
  }
end