# Phusion Passenger - https://www.phusionpassenger.com/# Copyright (c) 2010-2015 Phusion## "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.## 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.require'erb'require'etc'PhusionPassenger.require_passenger_lib'constants'PhusionPassenger.require_passenger_lib'platform_info/ruby'PhusionPassenger.require_passenger_lib'standalone/control_utils'PhusionPassenger.require_passenger_lib'utils/tmpio'PhusionPassenger.require_passenger_lib'utils/shellwords'modulePhusionPassengermoduleStandaloneclassStartCommandmoduleNginxEngineprivatedefstart_engine_realStandalone::ControlUtils.require_daemon_controller@engine=DaemonController.new(build_daemon_controller_options)write_nginx_config_file(nginx_config_path)begin@engine.startrescueDaemonController::AlreadyStartedbeginpid=@engine.pidrescueSystemCallError,IOErrorpid=nilendifpidabort"#{PROGRAM_NAME} Standalone is already running on PID #{pid}."elseabort"#{PROGRAM_NAME} Standalone is already running."endrescueDaemonController::StartError=>eabort"Could not start the Nginx engine:\n#{e}"endenddefwait_until_engine_has_exited# Since the engine is not our child process (it daemonizes)# we cannot use Process.waitpid to wait for it. A busy-sleep-loop with# Process.kill(0, pid) isn't very efficient. Instead we do this:## Connect to the engine's server and wait until it disconnects the socket# because of timeout. Keep doing this until we can no longer connect.whiletrueif@options[:socket_file]socket=UNIXSocket.new(@options[:socket_file])elsesocket=TCPSocket.new(@options[:address],@options[:port])endbeginbeginsocket.readrescueSystemCallError,IOError,SocketErrorendensurebeginsocket.closerescueSystemCallError,IOError,SocketErrorendendendrescueErrno::ECONNREFUSED,Errno::ECONNRESETenddefbuild_daemon_controller_optionsif@options[:socket_file]ping_spec=[:unix,@options[:socket_file]]elseping_spec=[:tcp,@options[:address],@options[:port]]endreturn{:identifier=>'Nginx',:start_command=>"#{@nginx_binary} "+"-c #{Shellwords.escapenginx_config_path} "+"-p #{Shellwords.escape@working_dir}",:ping_command=>ping_spec,:pid_file=>@options[:pid_file],:log_file=>@options[:log_file],:timeout=>25}enddefnginx_config_pathreturn"#{@working_dir}/nginx.conf"enddefwrite_nginx_config_file(path)File.open(path,'w')do|f|f.chmod(0644)erb=ERB.new(File.read(nginx_config_template_filename),nil,"-")erb.filename=nginx_config_template_filenamecurrent_user=Etc.getpwuid(Process.uid).name# The template requires some helper methods which are defined in start_command.rb.output=erb.result(binding)f.write(output)putsoutputifdebugging?endenddefnginx_config_template_filenameif@options[:nginx_config_template]return@options[:nginx_config_template]elsereturnFile.join(PhusionPassenger.resources_dir,"templates","standalone","config.erb")endenddefdebugging?returnENV['PASSENGER_DEBUG']&&!ENV['PASSENGER_DEBUG'].empty?end#### Config file template helpers ####defnginx_listen_address(options=@options)ifoptions[:socket_file]return"unix:"+File.absolute_path_no_resolve(options[:socket_file])elsereturncompose_ip_and_port(options[:address],options[:port])endenddefnginx_listen_address_with_ssl_port(options=@options)ifoptions[:socket_file]return"unix:"+File.absolute_path_no_resolve(options[:socket_file])elsereturncompose_ip_and_port(options[:address],options[:ssl_port])endenddefdefault_group_for(username)user=Etc.getpwnam(username)group=Etc.getgrgid(user.gid)returngroup.nameenddefnginx_option(nginx_config_name,option_name)if@options[option_name]return"#{nginx_config_name}#{@options[option_name]};"endenddefdefault_group_for(username)user=Etc.getpwnam(username)group=Etc.getgrgid(user.gid)returngroup.nameenddefboolean_config_value(val)returnval?"on":"off"enddefserialize_strset(*items)if"".respond_to?(:force_encoding)items=items.map{|x|x.force_encoding('binary')}null="\0".force_encoding('binary')elsenull="\0"endreturn[items.join(null)].pack('m*').gsub("\n","").stripend#####################end# module NginxEngineend# module StartCommandend# module Standaloneend# module PhusionPassenger