如果使用Mongrel来部署Rails,就不可避免的使用
- mongrel_rails start -C mongrel.config
这里mongrel.config是mongrel的配置文件,可以为:
- —
- :config_script:
- :environment: development
- :pid_file: log/mongrel.pid
- :num_processors: 1024
- :docroot: public
- :timeout: 0
- :host: 0.0 . 0.0
- :mime_map:
- :port: 3000
- :daemon: false
- :cwd: /home/linux/projects/mongrel/testapp
- :includes:
- - mongrel
- :debug: false
- :log_file: log/mongrel.log
然后到app的根目录下运行上述的命令,恭喜您,你启动成功了.
但是有个问题,如果我不在app的根目录下运行呢?系统会给出我们信息:
- !!! Path to log file not valid: log/mongrel.log
- mongrel::start reported an error. Use mongrel_rails mongrel::start -h to get help.
第一眼看见这个错误,觉得非常简单,Mongrels的创始人给出的答案是将相对路径改为绝对路径.
也许上述的方法能解决问题,但是我相信仍然有一种情况:即使改为绝对路径,还是出现同样的错误.
如果发生上述的错误应该怎么办?
不幸的是,上述的问题我已经碰见过,很长时间不得要领.文档看破皮了,还是不能运行.不得已,打开mongrels的源代码看看.最后发现需要指定工作目录,因为工作目录是在log之前验证的.
摘录一段代码:
ruby 代码
- module Mongrel
- class Start < GemPlugin::Plugin "/commands"
- include Mongrel::Command::Base
-
- def configure
- options [
- ["-e" , "--environment ENV" , "Rails environment to run as" , : @environment , ENV['RAILS_ENV'] || "development" ],
- ["-d" , "--daemonize" , "Run daemonized in the background" , : @daemon , false ],
- ['-p', '--port PORT', "Which port to bind to" , : @port , 3000],
- ['-a', '--address ADDR', "Address to bind to" , : @address , "0.0.0.0" ],
- ['-l', '--log FILE', "Where to write log messages" , : @log_file , "log/mongrel.log" ],
- ['-P', '--pid FILE', "Where to write the PID" , : @pid_file , "log/mongrel.pid" ],
- ['-n', '--num-procs INT', "Number of processors active before clients denied" , : @num_procs , 1024],
- ['-t', '--timeout TIME', "Timeout all requests after 100th seconds time" , : @timeout , 0],
- ['-m', '--mime PATH', "A YAML file that lists additional MIME types" , : @mime_map , nil ],
- ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)" , : @cwd , Dir .pwd],
- ['-r', '--root PATH', "Set the document root (default 'public')" , : @docroot , "public" ],
- ['-B', '--debug', "Enable debugging mode" , : @debug , false ],
- ['-C', '--config PATH', "Use a config file" , : @config_file , nil ],
- ['-S', '--script PATH', "Load the given file as an extra config script" , : @config_script , nil ],
- ['-G', '--generate PATH', "Generate a config file for use with -C" , : @generate , nil ],
- ['' , '--user USER', "User to run as" , : @user , nil ],
- ['' , '--group GROUP', "Group to run as" , : @group , nil ],
- ['' , '--prefix PATH', "URL prefix for Rails app" , : @prefix , nil ]
- ]
- end
-
- def validate
- @cwd = File .expand_path( @cwd )
- valid_dir? @cwd , "Invalid path to change to during daemon mode: #@cwd"
-
-
- Dir .chdir( @cwd )
-
- valid?(@prefix [0].chr == "/" && @prefix [-1].chr != "/" , "Prefix must begin with / and not end in /" ) if @prefix
- valid_dir? File .dirname( @log_file ), "Path to log file not valid: #@log_file"
- valid_dir? File .dirname( @pid_file ), "Path to pid file not valid: #@pid_file"
- valid_dir? @docroot , "Path to docroot not valid: #@docroot"
- valid_exists? @mime_map , "MIME mapping file does not exist: #@mime_map" if @mime_map
- valid_exists? @config_file , "Config file not there: #@config_file" if @config_file
- valid_dir? File .dirname( @generate ), "Problem accessing directory to #@generate" if @generate
- valid_user? @user if @user
- valid_group? @group if @group
-
- return @valid
- end
-
- def run
-
- settings = { :host => @address , :port => @port , :cwd => @cwd ,
- :log_file => @log_file , :pid_file => @pid_file , :environment => @environment ,
- :docroot => @docroot , :mime_map => @mime_map , :daemon => @daemon ,
- :debug => @debug , :includes => [ "mongrel" ], :config_script => @config_script ,
- :num_processors => @num_procs , :timeout => @timeout ,
- :user => @user , :group => @group , :prefix => @prefix , :config_file => @config_file
- }
-
-
-
- if @config_file
- settings.merge! YAML.load_file(@config_file )
- STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless settings[ :daemon ]
- end
-
- config = Mongrel::Rails::RailsConfigurator.new (settings) do
- if defaults[ :daemon ]
- if File .exist? defaults[ :pid_file ]
- log "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors."
- log "!!! Exiting with error. You must stop mongrel and clear the .pid before I’ll attempt a start."
- exit 1
- end
-
- daemonize
- log "Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info."
- log "Settings loaded from #{@config_file} (they override command line)." if @config_file
- end
看看上面的代码就非常清楚了.
那么如何解决,可以这样:
- mongrel_rails start -c youapp/path/to -C mongrel.config
这样就OK了.
扩展应用:可以配置为服务自动启动.