1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
|
#!/usr/bin/env ruby
# Phusion Passenger - https://www.phusionpassenger.com/
# Copyright (c) 2010-2015 Phusion Holding B.V.
#
# "Passenger", "Phusion Passenger" and "Union Station" are registered
# trademarks of Phusion Holding B.V.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
## Magic comment: begin bootstrap ##
require 'phusion_passenger'
## Magic comment: end bootstrap ##
PhusionPassenger.locate_directories
require 'digest/sha2'
require 'optparse'
require 'fileutils'
PhusionPassenger.require_passenger_lib 'platform_info/ruby'
PhusionPassenger.require_passenger_lib 'platform_info/openssl'
PhusionPassenger.require_passenger_lib 'abstract_installer'
PhusionPassenger.require_passenger_lib 'utils/terminal_choice_menu'
PhusionPassenger.require_passenger_lib 'utils/tmpio'
PhusionPassenger.require_passenger_lib 'utils/shellwords'
DOWNLOAD_OPTION = {
:connect_timeout => 30,
:idle_timeout => 30
}
class Installer < PhusionPassenger::AbstractInstaller
include PhusionPassenger
TerminalChoiceMenu = PhusionPassenger::Utils::TerminalChoiceMenu
def dependencies
specs = [
'depcheck_specs/compiler_toolchain',
'depcheck_specs/ruby',
'depcheck_specs/gems',
'depcheck_specs/libs',
'depcheck_specs/utilities'
]
ids = [
'cc',
'c++',
'download-tool',
'libcurl-dev',
'openssl-dev',
'zlib-dev',
'rake',
'ruby-openssl',
'rubygems'
]
if @languages.include?("ruby")
if PlatformInfo.passenger_needs_ruby_dev_header?
ids << 'ruby-dev'
end
ids << 'rack'
end
return [specs, ids]
end
def install_doc_url
"https://www.phusionpassenger.com/library/install/nginx/"
end
def troubleshooting_doc_url
"https://www.phusionpassenger.com/library/admin/nginx/troubleshooting/"
end
def run_steps
# Make sure the configure script finds the correct
# passenger-config command.
ENV['PATH'] = PhusionPassenger.bin_dir + ":" + ENV['PATH']
show_welcome_screen
query_interested_languages
check_gem_install_permission_problems || exit(1)
check_directory_accessible_by_web_server
check_nginx_module_sources_available || exit(1)
check_dependencies || exit(1)
if needs_compiling_support_files?
check_whether_we_can_write_to(PhusionPassenger.build_system_dir) || exit(1)
end
check_whether_os_is_broken
check_whether_system_has_enough_ram
download_and_install = should_we_download_and_install_nginx_automatically?
if pcre_is_installed?
@pcre_source_dir = nil
else
@pcre_source_dir = download_and_extract_pcre
end
if download_and_install
nginx_source_dir = download_and_extract_nginx
if nginx_source_dir.nil?
show_possible_solutions_for_download_and_extraction_problems
exit(1)
end
nginx_prefix = ask_for_nginx_install_prefix
check_whether_other_nginx_installations_exist(nginx_prefix)
if @extra_configure_flags == "none"
extra_nginx_configure_flags = nil
else
extra_nginx_configure_flags = @extra_configure_flags
end
else
nginx_source_dir = ask_for_nginx_source_dir
nginx_prefix = ask_for_nginx_install_prefix
check_whether_other_nginx_installations_exist(nginx_prefix)
extra_nginx_configure_flags = ask_for_extra_nginx_configure_flags(nginx_prefix)
end
check_whether_we_can_write_to(nginx_prefix) || exit(1)
nginx_config_already_exists_before_installing = nginx_config_exists?(nginx_prefix)
if needs_compiling_support_files?
if !compile_passenger_support_files
show_possible_solutions_for_compilation_and_installation_problems
exit(1)
end
end
if install_nginx(nginx_source_dir, nginx_prefix, extra_nginx_configure_flags)
if nginx_config_already_exists_before_installing || !locate_nginx_config_file(nginx_prefix)
show_passenger_config_snippets(nginx_prefix)
else
insert_passenger_config_snippets(nginx_prefix)
end
show_deployment_example
else
show_possible_solutions_for_compilation_and_installation_problems
exit(1)
end
end
def before_install
super
myself = `whoami`.strip
@working_dir = PhusionPassenger::Utils.mktmpdir("passenger.", PlatformInfo.tmpexedir)
end
def after_install
super
FileUtils.remove_entry_secure(@working_dir) if @working_dir
end
private
def show_welcome_screen
render_template 'nginx/welcome', :version => VERSION_STRING
wait
end
def query_interested_languages
menu = TerminalChoiceMenu.new(["Ruby", "Python", "Node.js", "Meteor"])
menu["Ruby"].checked = interesting_language?('ruby')
menu["Python"].checked = interesting_language?('python')
menu["Node.js"].checked = interesting_language?('nodejs', 'node')
menu["Meteor"].checked = interesting_language?('meteor')
new_screen
puts "<banner>Which languages are you interested in?</banner>"
puts
if interactive?
puts "Use <space> to select."
puts "<dgray>If the menu doesn't display correctly, press '!'</dgray>"
else
puts "Override selection with --languages."
end
puts
if interactive?
begin
menu.query
rescue Interrupt
raise Abort
end
else
menu.display_choices
puts
end
@languages = menu.selected_choices.map{ |x| x.downcase.gsub(/\./, '') }
end
def interesting_language?(name, command = nil)
if @languages
return @languages.include?(name)
else
return !!PlatformInfo.find_command(command || name)
end
end
def check_nginx_module_sources_available
if PhusionPassenger.custom_packaged? &&
(!PhusionPassenger.nginx_module_source_dir || !File.exist?(PhusionPassenger.nginx_module_source_dir))
new_screen
render_template 'nginx/nginx_module_sources_not_available'
return false
else
return true
end
end
def needs_compiling_support_files?
return !PhusionPassenger.build_system_dir.nil?
end
def compile_passenger_support_files
new_screen
puts "<banner>Compiling Passenger support files...</banner>"
Dir.chdir(PhusionPassenger.build_system_dir) do
return sh("env NOEXEC_DISABLE=1 #{PlatformInfo.rake_command} nginx:clean nginx RELEASE=yes")
end
end
def should_we_download_and_install_nginx_automatically?
new_screen
render_template 'nginx/query_download_and_install',
:nginx_version => PREFERRED_NGINX_VERSION
puts
if @auto_download
puts "<b>=> Proceeding with choice 1.</b>"
return true
elsif @nginx_source_dir
puts "<b>=> Proceeding with choice 2.</b>"
return false
elsif @auto
puts "<b>=> Proceeding with choice 1.</b>"
return true
else
choice = prompt("Enter your choice (1 or 2) or press Ctrl-C to abort") do |input|
if input == "1" || input == "2"
true
elsif input.empty?
puts "<red>No choice has been given.</red>"
false
else
puts "<red>'#{input}' is not a valid choice.</red>"
false
end
end
return choice == "1"
end
end
def download_and_extract_pcre
new_screen
puts "<banner>PCRE (required by Nginx) not installed, downloading it...</banner>"
url = "http://downloads.sourceforge.net/project/pcre/pcre/#{PREFERRED_PCRE_VERSION}/pcre-#{PREFERRED_PCRE_VERSION}.tar.gz"
dirname = "pcre-#{PREFERRED_PCRE_VERSION}"
tarball = "#{@working_dir}/pcre.tar.gz"
if download(url, tarball, DOWNLOAD_OPTION)
Dir.chdir(@working_dir) do
puts "<banner>Verifying PCRE checksum...</banner>"
if sha256_file(tarball) != PCRE_SHA256_CHECKSUM
new_screen
render_template "nginx/pcre_checksum_could_not_be_verified"
wait
return nil
end
puts "<banner>Extracting PCRE source tarball...</banner>"
if sh("tar", "xzvf", tarball)
return "#{@working_dir}/#{dirname}"
else
new_screen
render_template "nginx/pcre_could_not_be_extracted"
wait
return nil
end
end
else
new_screen
render_template "nginx/pcre_could_not_be_downloaded"
wait
return nil
end
rescue Interrupt
exit 2
end
def download_and_extract_nginx
new_screen
puts "<banner>Downloading Nginx...</banner>"
url = "http://www.nginx.org/download/nginx-#{PREFERRED_NGINX_VERSION}.tar.gz"
dirname = "nginx-#{PREFERRED_NGINX_VERSION}"
tarball = "#{@working_dir}/nginx.tar.gz"
if download(url, tarball, DOWNLOAD_OPTION)
Dir.chdir(@working_dir) do
puts "<banner>Verifying Nginx checksum...</banner>"
if sha256_file(tarball) != NGINX_SHA256_CHECKSUM
return nil
end
puts "<banner>Extracting Nginx source tarball...</banner>"
if sh("tar", "xzvf", tarball)
return "#{@working_dir}/#{dirname}"
else
return nil
end
end
else
return nil
end
rescue Interrupt
exit 2
end
def show_possible_solutions_for_download_and_extraction_problems
new_screen
render_template "nginx/possible_solutions_for_download_and_extraction_problems"
puts
end
def ask_for_nginx_install_prefix
new_screen
puts "<banner>Where do you want to install Nginx to?</banner>"
puts
if @prefix
puts "<b>=> #{@prefix}</b>"
return @prefix
elsif @auto
puts "<b>=> /opt/nginx</b>"
return "/opt/nginx"
else
prefix = prompt("Please specify a prefix directory [/opt/nginx]") do |input|
if input.empty? || input =~ %r(/)
true
else
puts "<red>Please specify an absolute path.</red>"
false
end
end
if prefix.empty?
prefix = "/opt/nginx"
end
return prefix
end
end
def check_whether_other_nginx_installations_exist(nginx_prefix)
check_for = [
"/usr/bin/nginx",
"/usr/sbin/nginx"
]
existing_binary = nil
check_for.each do |filename|
if File.exist?(filename)
existing_binary = filename
break
end
end
if existing_binary
new_screen
render_template 'nginx/other_nginx_installations_exist',
:existing_binary => existing_binary,
:prefix => nginx_prefix
wait
end
end
def ask_for_nginx_source_dir
new_screen
puts "<banner>Where is your Nginx source code located?</banner>"
puts
if @nginx_source_dir
puts "<b>=> #{@nginx_source_dir}</b>"
return @nginx_source_dir
else
return prompt("Please specify the directory") do |input|
if input =~ %r(/)
if File.exist?("#{input}/src/core/nginx.c")
true
else
puts "<red>'#{input}' does not look like an Nginx source directory.</red>"
false
end
else
puts "<red>Please specify an absolute path.</red>"
false
end
end
end
end
def ask_for_extra_nginx_configure_flags(prefix)
done = false
while !done
new_screen
render_template 'nginx/ask_for_extra_configure_flags',
:command => build_nginx_configure_command(prefix)
puts
if @extra_configure_flags
if @extra_configure_flags == "none"
extra_args = ""
puts "<b>=> No extra configure flags.</b>"
else
extra_args = @extra_configure_flags
puts "<b>=> #{extra_args}</b>"
end
return extra_args
elsif @auto
puts "<b>=> No extra configure flags.</b>"
return ""
else
extra_args = prompt "Extra arguments to pass to configure script"
new_screen
render_template 'nginx/confirm_extra_configure_flags',
:command => build_nginx_configure_command(prefix, extra_args)
puts
answer = prompt("Is this what you want? (yes/no) [default=yes]") do |input|
if input.empty? || input == "yes" || input == "no"
true
else
puts "<red>Please enter 'yes' or 'no'.</red>"
false
end
end
done = answer.empty? || answer == "yes"
end
end
return extra_args
end
def check_whether_we_can_write_to(dir)
FileUtils.mkdir_p(dir)
File.new("#{dir}/__test__.txt", "w").close
return true
rescue
new_screen
if Process.uid == 0
render_template 'nginx/cannot_write_to_dir', :dir => dir
else
render_template 'installer_common/run_installer_as_root',
:dir => dir,
:sudo => PhusionPassenger::PlatformInfo.ruby_sudo_command,
:sudo_s_e => PhusionPassenger::PlatformInfo.ruby_sudo_shell_command("-E"),
:ruby => PhusionPassenger::PlatformInfo.ruby_command,
:installer => "#{PhusionPassenger.bin_dir}/passenger-install-nginx-module #{ORIG_ARGV.join(' ')}"
end
return false
ensure
File.unlink("#{dir}/__test__.txt") rescue nil
end
def nginx_config_exists?(prefix)
return !!locate_nginx_config_file(prefix)
end
def install_nginx(source_dir, prefix, extra_configure_flags)
Dir.chdir(source_dir) do
new_screen
puts "<banner>Compiling and installing Nginx...</banner>"
if !sh(build_nginx_configure_command(prefix, extra_configure_flags)) ||
!sh("make") ||
!sh("make install")
return false
end
end
return true
rescue Interrupt
raise Aborted
end
def show_passenger_config_snippets(prefix)
new_screen
render_template 'nginx/config_snippets',
:config_file => locate_nginx_config_file(prefix),
:passenger_root => PhusionPassenger.install_spec,
:ruby => PlatformInfo.ruby_command
wait
end
def show_deployment_example
line
puts
render_template 'nginx/deployment_example',
:deployment_guide_url => "https://www.phusionpassenger.com/library/deploy/nginx/deploy/",
:phusion_website => PHUSION_WEBSITE,
:passenger_website => PASSENGER_WEBSITE
end
def show_possible_solutions_for_compilation_and_installation_problems
line
puts
render_template 'nginx/possible_solutions_for_compilation_and_installation_problems',
:support_url => SUPPORT_URL
end
def locate_nginx_config_file(prefix)
["#{prefix}/conf/nginx.conf", "#{prefix}/etc/nginx.conf"].each do |filename|
if File.exist?(filename)
return filename
end
end
return nil
end
def insert_passenger_config_snippets(prefix)
config_file = locate_nginx_config_file(prefix)
contents = File.read(config_file)
contents.sub!(/^http \{/,
"http {\n" <<
" passenger_root #{PhusionPassenger.install_spec};\n" <<
" passenger_ruby #{PlatformInfo.ruby_command};\n")
File.open(config_file, 'w') do |f|
f.write(contents)
end
new_screen
render_template 'nginx/config_snippets_inserted',
:config_file => config_file,
:passenger_root => PhusionPassenger.install_spec,
:ruby => PlatformInfo.ruby_command
wait
end
def build_nginx_configure_command(prefix, extra_configure_flags = nil)
extra_cflags = "-Wno-error #{PlatformInfo.openssl_extra_cflags}".strip
extra_ldflags = PlatformInfo.openssl_extra_ldflags
command = "sh ./configure --prefix='#{prefix}' "
command << "--with-http_ssl_module "
command << "--with-http_gzip_static_module "
command << "--with-http_stub_status_module "
command << "--with-cc-opt=#{Shellwords.escape extra_cflags} "
command << "--with-ld-opt=#{Shellwords.escape extra_ldflags} "
if @pcre_source_dir
command << "--with-pcre='#{@pcre_source_dir}' "
elsif !pcre_is_installed?
command << "--without-http_rewrite_module "
end
command << "--add-module='#{PhusionPassenger.nginx_module_source_dir}' #{extra_configure_flags}"
command.strip!
return command
end
def pcre_is_installed?
if @pcre_is_installed.nil?
@pcre_is_installed = begin
File.open('/tmp/passenger-check.c', 'w') do |f|
f.puts("#include <pcre.h>")
end
Dir.chdir('/tmp') do
# Nginx checks for PCRE in multiple places...
system("(gcc -I/usr/local/include -I/usr/include/pcre " <<
"-I/usr/pkg/include -I/opt/local/include " <<
"-c passenger-check.c) >/dev/null 2>/dev/null")
end
ensure
File.unlink('/tmp/passenger-check.c') rescue nil
File.unlink('/tmp/passenger-check.o') rescue nil
end
end
return @pcre_is_installed
end
def sha256_file(path)
# We do this instead of using #file, for Ruby 1.8.5 support.
digest = Digest::SHA256.new
File.open(path, "rb") do |f|
buf = ''
buf.force_encoding('binary') if buf.respond_to?(:force_encoding)
while !f.eof?
f.read(1024 * 16, buf)
digest.update(buf)
end
end
return digest.hexdigest
end
end
ORIG_ARGV = ARGV.dup
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: passenger-install-nginx-module [options]"
opts.separator ""
indent = ' ' * 37
opts.separator "Options:"
opts.on("--auto", "Automatically confirm 'Press ENTER to\n" <<
"#{indent}continue' prompts.") do
options[:auto] = true
end
opts.on("--prefix=DIR", String, "Use the given Nginx install prefix instead\n" <<
"#{indent}of asking for it interactively.") do |dir|
options[:prefix] = dir
end
opts.on("--auto-download", "Download and install Nginx automatically,\n" <<
"#{indent}instead of asking interactively whether to\n" <<
"#{indent}download+install or to use an existing\n" <<
"#{indent}Nginx source directory.") do
options[:auto_download] = true
end
opts.on("--nginx-source-dir=DIR", String, "Compile and install Nginx using the given\n" <<
"#{indent}Nginx source directory, instead of\n" <<
"#{indent}interactively asking to download+install\n" <<
"#{indent}or to use an existing Nginx source\n" <<
"#{indent}directory. Conflicts with --auto-download.") do |dir|
options[:nginx_source_dir] = dir
end
opts.on("--extra-configure-flags=STRING", String, "Pass these extra flags to Nginx's\n" <<
"#{indent}'configure' script, instead of asking for\n" <<
"#{indent}it interactively. Specify 'none' if you\n" <<
"#{indent}do not want to pass additional flags but do\n" <<
"#{indent}not want this installer to ask\n" <<
"#{indent}interactively either.") do |flags|
options[:extra_configure_flags] = flags
end
opts.on("--languages NAMES", "Comma-separated list of interested\n" <<
"#{indent}languages (e.g.\n" <<
"#{indent}'ruby,python,nodejs,meteor')") do |value|
options[:languages] = value.split(",")
end
opts.on("--force-colors", "Display colors even if stdout is not a TTY") do
options[:colorize] = true
end
end
begin
parser.parse!
rescue OptionParser::ParseError => e
puts e
puts
puts "Please see '--help' for valid options."
exit 1
end
if options[:auto_download] && options[:nginx_source_dir]
STDERR.puts "You cannot specify both --auto-download and --nginx-source-dir."
exit 1
end
Installer.new(options).run
|