How i can run adonis in https?

Nowadays my api is running only in http.

I have a installed ssl certified in my server, so my frontend (in angular) is running in https, i need to make the backend running in https too.

First i’m trying to make this in my local development,

So, following this tutorial: https://adonisjs.com/recipes/4.1/using-https

I put this in my server.js:

const { Ignitor } = require('@adonisjs/ignitor')

const path = require('path')

const https = require('https')

const fs = require('fs')

const options = {

 key: fs.readFileSync(path.join(__dirname, './server.key')),

 cert: fs.readFileSync(path.join(__dirname, './server.crt'))

}

new Ignitor(require('@adonisjs/fold'))

  .appRoot(__dirname)

  .fireHttpServer(handler => {

   return https.createServer(options, handler)

  })

  .catch(console.error)

But when i run adonis serve --dev

But my http and https is not working. There’s no way to test this in local?

I put the same server.key and server.crt that is working in my server from my frontend in the adonis folder.

How i can do this?

I receive:

Could not get any response

There was an error connecting to https://localhost:3333/

As i know, you can’t make localhost or ip address over https, u need to use domain.

1 Like

oh, ok.

I need to do something more, like in apache?

Nowadays my front is running in https, i don’t know if when i put this config in production will work https

You were using Apache as proxy to Node?
Set certs in Apache and it will handle SSL for everything where it proxies too (Node current case)

Yes, i’m using apache.

This config is in httpd.conf?

Nowadays i’m using a conf that is generated by my cpanel. I added an node in section “Application Manager”, and my frontend is in public_html.

My frontend is running normally in https, but my adonis backend no.

This is my virtual host configuration from port 80 and 443:

<VirtualHost ip:80 ip:80>
  ServerName domainname.com
    ServerAlias mail.domainname.com www.domainname.com
  DocumentRoot /home/domainname/public_html
  ServerAdmin webmaster@domainname.com
  UseCanonicalName Off

  ## User domainname# Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir disabled
          UserDir enabled domainname
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule include_module>
    <Directory "/home/domainname/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>

 <IfModule suphp_module>
    suPHP_UserGroup domainname domainname
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup domainname domainname
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid domainname domainname
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID domainname domainname
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser domainname 
    PassengerGroup domainname
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/domainname/public_html/cgi-bin/
  </IfModule>


    # Global DCV Rewrite Exclude
    <IfModule rewrite_module>
        RewriteOptions Inherit
    </IfModule>

Include "/etc/apache2/conf.d/userdata/std/2_4/domainname/domainname.com/*.conf"


  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/std/2_4/domainname/domainname.com/*.conf"
</VirtualHost>
<VirtualHost ip:443 ip:443>
  ServerName domainname.com
  ServerAlias mail.domainname.com www.domainname.com cpanel.domainname.com webmail.domainname.com webdisk.domainname.com
  DocumentRoot /home/domainname/public_html
  ServerAdmin webmaster@domainname.com
  UseCanonicalName Off

  ## User domainname# Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir disabled
          UserDir enabled domainname
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule mod_include.c>
    <Directory "/home/domainname/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>

<Proxymatch ^https?://ip\.0\.0\.1:(2082|2083|2077|2078|2079|2080|2086|2087|2095|2096)/>
       <IfModule security2_module>
          SecRuleEngine Off
       </IfModule>
  </Proxymatch>

  <IfModule mod_suphp.c>
    suPHP_UserGroup domainname
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup domainname domainname
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid domainname domainname
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID domainname domainname
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser domainname
    PassengerGroup domainname
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/domainname/public_html/cgi-bin/
  </IfModule>

 <IfModule ssl_module>
    SSLEngine on

    SSLCertificateFile /var/cpanel/ssl/apache_tls/domainname.com/combined

    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
    <Directory "/home/domainname/public_html/cgi-bin">
      SSLOptions +StdEnvVars
    </Directory>
  </IfModule>





    Include "/etc/apache2/conf.d/userdata/ssl/2_4/domainname/domainname.com/*.conf"




  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/ssl/2_4/domainname/domainname.com/*.conf"

    <IfModule headers_module>
    RequestHeader set X-HTTPS 1
    </IfModule>

    RewriteEngine On
            RewriteCond %{HTTP_HOST} =cpanel.domainname.com [OR]
            RewriteCond %{HTTP_HOST} =cpanel.domainname.com:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_cpanel/$1 [PT]
                ProxyPass "/___proxy_subdomain_cpanel" "http://ip:2082" max=1 retry=0
            RewriteCond %{HTTP_HOST} =webdisk.domainname.com [OR]
            RewriteCond %{HTTP_HOST} =webdisk.domainname.com:443

        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_webdisk/$1 [PT]
                ProxyPass "/___proxy_subdomain_webdisk" "http://127.0.0.1:2077" max=1 retry=0
            RewriteCond %{HTTP_HOST} =webmail.domainname.com [OR]
            RewriteCond %{HTTP_HOST} =webmail.domainname.com:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_webmail/$1 [PT]
                ProxyPass "/___proxy_subdomain_webmail" "http://127.0.0.1:2095" max=1 retry=0

            RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                RewriteCond %{HTTP_HOST} =cpanel.domainname.com [OR]
                RewriteCond %{HTTP_HOST} =cpanel.domainname.com:443

            RewriteRule ^/(.*) /___proxy_subdomain_ws_cpanel/$1 [PT]
            RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                RewriteCond %{HTTP_HOST} =webmail.domainname.com [OR]
                RewriteCond %{HTTP_HOST} =webmail.domainname.com:443

            RewriteRule ^/(.*) /___proxy_subdomain_ws_webmail/$1 [PT]
</VirtualHost>

Seems that in port 443 is already applying a cert, i also have to make this config too? https://adonisjs.com/recipes/4.1/using-https

Hi,
To do this, use PORT “443” instead of “80” in the configuration.

Also, in your server.js use the following code:

const { Ignitor } = require('@adonisjs/ignitor')
const https = require('https')
const pem = require('pem')

pem.createCertificate({ days: 1, selfSigned: true }, (error, keys) => {
  if (error) {
    return console.log(error)
  }

  const options = {
    key: keys.serviceKey,
    cert: keys.certificate
  }

  new Ignitor(require('@adonisjs/fold'))
    .appRoot(__dirname)
    .fireHttpServer((handler) => {
      return https.createServer(options, handler)
    })
    .catch(console.error)
})

Remember to install HTTPS and PEM (via npm i https & npm i pem)

1 Like