Search code examples
ruby-on-railssslhttpspumaforeman

Foreman, Rails, Puma SSL/HTTPS


How can I combine these two commands so Rails uses the -b options in the puma command?

puma: puma -b 'ssl://127.0.0.1:3000?key=/Users/cdrum/.ssh/server.key&cert=/Users/cdrum/.ssh/server.crt' -b 'tcp://127.0.0.1:3001'
rails: bundle exec rails s --binding=127.0.0.1 -p 3000 -e $RAILS_ENV puma

Presently with this I get this error:

puma/binder.rb:272:in `initialize': Address already in use - bind(2) for"127.0.0.1" port 3000 (Errno::EADDRINUSE)

Alternatively, can I tell Rails to look for the puma instance already running instead of trying to boot it again?


Solution

  • You can actually solve this by solely running puma and storing all the config in config/puma.rb. Different cert. management but no matter:

    threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
    threads threads_count, threads_count
    port ENV.fetch('PORT') { 3000 }
    environment ENV.fetch('RAILS_ENV') { 'development' }
    plugin :tmp_restart
    
    localhost_key = "#{Dir.pwd}/#{File.join('config', 'certs', 'localhost.key')}"
    localhost_cert = "#{Dir.pwd}/#{File.join('config', 'certs', 'localhost.crt')}"
    
    unless File.exist?(localhost_key)
      def generate_root_cert(root_key) # rubocop:disable Metrics/AbcSize
        root_ca = OpenSSL::X509::Certificate.new
        root_ca.version = 2
        root_ca.serial = 0x0
        root_ca.subject = OpenSSL::X509::Name.parse '/C=BE/O=A1/OU=A/CN=localhost'
        root_ca.issuer = root_ca.subject
        root_ca.public_key = root_key.public_key
        root_ca.not_before = Time.now
        root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60
        root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
        root_ca
      end
    
      root_key = OpenSSL::PKey::RSA.new(2048)
      file = File.new(localhost_key, 'wb')
      file.write(root_key)
      file.close
    
      root_cert = generate_root_cert(root_key)
      file = File.new(localhost_cert, 'wb')
      file.write(root_cert)
      file.close
    end
    
    ssl_bind '0.0.0.0', '8443', key: localhost_key, cert: localhost_cert
    

    Now both HTTP and HTTPS work for my Rails app.

    Procfile is now just:

    web: puma
    

    You do need a special branch of the puma gem though to avoid an SSL error.