Koozali.org: home of the SME Server

NodeJS - running w/o SCL

Offline Jáder

  • *
  • 1,099
  • +0/-0
    • LinuxFacil
Re: NodeJS - running w/o SCL
« Reply #15 on: January 28, 2019, 11:08:44 AM »
I did show you how to do this above.

You have to understand that you are trying to do something quite complicated. That means you really need to go and learn how this works by reading some of the code.

TemplatePath will point to a path for a custom virtualhost with a name of your choice.

If you bother to look in the templates directory you will see a default directory called ProxyPathVirtialHosts.

But you can create your own directories with each virtualhost having its owm settings.

So if you read what I wrote I created a directory for rocketchat and added my own proxy templates in there and set TemplatePath to point to it.

You can have 3 directories and 3 TemplatePaths, one for each domain.

Get yourself a test machine, read carefully, and test.

Until you start reading code and fumdamentally understand how this works (exactly what I had to do) you are going to struggle.

Hey... please hold on... I'm here to learn, and later I'll document my discoveries!

I can do tests on one of those domains, even without using templates (or TemplatePath) by edit httpd.conf directly.
What I did not understand is why is so complicate to do a http redirect.
I've been reading (a lot) about this feature on Apache and it appears to be so simple:
HEre () I can see:
Code: [Select]
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName myhost.com
ServerAlias ww.myhost.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

As I can see just two extra lines for proxypass and proxypassreverse... and those are there on my config file.
I'm not sure it these two are required:
Code: [Select]
ProxyPreserveHost On
ProxyRequests Off

If I can make it work, I can create a Template  and put his path on TemplatePath... I think it would be named like "Proxy2NodeJS". But I cannot make it work even changing config file!
As I can see the redirect do not follow directories... just works for "index.html" file .
The https://w8k.jadermarasca.com.br has a temperature app (sample from some site about NodeJS) and even that do not load the CSS file.
As you can see at https://handyman.dulare.com/proxying-node-application-through-apache/ this is possible.
I just don't know about modules.

Remember: togheter we're better than alone!

EDIT: I moved one step forward!
Changed my template file /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/VirtualHosts/27ProxyNodeJS to
Code: [Select]
{
    use esmith::DomainsDB;
    my $domains = esmith::DomainsDB->open_ro;

    my $nodeport = $domains->get_prop($virtualHost, "NodePort") || "disabled";
    return "    # skipping NodeJS redirect - no NodePort parameter\n" unless not $nodeport eq "disabled";

    $OUT .= "    # NodeJS port redirect\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPreserveHost On\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyRequests Off\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPass / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPassReverse / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';

}

Now sample app of temperature works, load CSS (it's a lot better in visual!).
I'll try to learn about the NodeJs requirements and update/create wiki page about it.
« Last Edit: January 28, 2019, 11:20:25 AM by Jáder »
...

Online ReetP

  • *
  • 3,722
  • +5/-0
Re: NodeJS - running w/o SCL
« Reply #16 on: January 28, 2019, 02:29:36 PM »
Hey... please hold on... I'm here to learn, and later I'll document my discoveries!

You are not really learning. You are skipping over things without understanding them.

This is turning in to a classic X Y problem.

http://xyproblem.info/

Quote
User wants to do X.
User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
User doesn't know how to do Y either.
User asks for help with Y.
Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.


Quote
I can do tests on one of those domains, even without using templates (or TemplatePath) by edit httpd.conf directly.

Of course you can for testing, but you want a permanent solution don't you? And that is where you are struggling.

Sure, you can do it for one domain, but that isn't what you asked to do. You want to do it for three different domains with slightly differing configs. That is not so simple. Well, it is if you understand what is happening.

Quote
What I did not understand is why is so complicate to do a http redirect.

Because you don't understand httpd configurations, and you don't understand how SME works and are not taking the time to try and learn. Believe me, I am no genius at it, but I sat down, read code and worked it out.

You want something complicated to be easy. Unfortunately that isn't going to happen.

Quote
I've been reading (a lot) about this feature on Apache and it appears to be so simple:
HEre () I can see:
Code: [Select]
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName myhost.com
ServerAlias ww.myhost.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

Just because it 'appears' to be simple does not mean that it is.

Yup - easy for one domain isn't it ? But that still isn't what you want to do.

Quote
If I can make it work, I can create a Template  and put his path on TemplatePath... I think it would be named like "Proxy2NodeJS". But I cannot make it work even changing config file!

Because you aren't reading what I wrote or trying to work out how it works.

Quote
As I can see the redirect do not follow directories... just works for "index.html" file .
The https://w8k.jadermarasca.com.br has a temperature app (sample from some site about NodeJS) and even that do not load the CSS file.
As you can see at https://handyman.dulare.com/proxying-node-application-through-apache/ this is possible.
I just don't know about modules.

EDIT: I moved one step forward!
Changed my template file /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/VirtualHosts/27ProxyNodeJS to
Code: [Select]
{
    use esmith::DomainsDB;
    my $domains = esmith::DomainsDB->open_ro;

    my $nodeport = $domains->get_prop($virtualHost, "NodePort") || "disabled";
    return "    # skipping NodeJS redirect - no NodePort parameter\n" unless not $nodeport eq "disabled";

    $OUT .= "    # NodeJS port redirect\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPreserveHost On\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyRequests Off\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPass / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPassReverse / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';

}


Now, why do you think that only works for one domain?

If you read what I wrote above about Rocketchat you might understand.

I'll try it once more. Please go and look in the directories and find this stuff.

You have a STOCK directory.

/etc/e-smith/templates/etc/httpd/conf/httpd.conf/ProxyPassVirtualHosts

You can then add a TemplatePath "ProxyPassVirtualHosts" which forces SME to look in the above directory for templates.

This will enable simple proxying for a domain.

To do something more complicated you can create your own directories and put your own custom templates in there to create you own virtual host

So as per Rocketchat I have:

/etc/e-smith/templates/etc/httpd/conf/httpd.conf/ProxyPassVirtualRocketchat

Then I set the TemplatePath for a domain to "ProxyPassVirtualRocketchat"

SME will now look in that directory for templates.

I could call it say:

/etc/e-smith/templates/etc/httpd/conf/httpd.conf/VirtualHost-1

Set the TemplatePath for a domain to "VirtualHost-1"

Now copy the first three templates over from ProxyPassVrtualHosts to VirtualHosts-1. These will set up the new virtualhost. You can then add your other options with additional templates

00Setup
02ServerName
03ServerAlias

Then I have (not all of this may be necessary)

04ProxyPassContent


Code: [Select]
{
    use esmith::DomainsDB;
    use esmith::ConfigDB;
    my $domainsDB = esmith::DomainsDB->open_ro;
    my $configDB = esmith::ConfigDB->open_ro;
    my $target  = $domainDB->get_prop($virtualHost, 'ProxyPassTarget') || 'http://127.0.0.1:3000';
    $target  =~ s~http[s]*://~~g;
    my $SSLProxy = $config->get_prop('rocketchat', 'SSLProxy') || '';

    if ( $port eq "80" ) {
    $OUT .= "# Host $virtualHost $target\n";
        $OUT .= "    # Redirect Letsencrypt queries\n";
        $OUT .= "    RewriteRule ^/.well-known/acme-challenge(/.*|\$) https://%{HTTP_HOST}/.well-known/acme-challenge\$1 [L,R]\n";
        $OUT .= "    # Everything else goes to https\n";
        $OUT .= "    RewriteRule ^/(.*|\$) https://%{HTTP_HOST}/ [R,L]\n";

    }

    if ( $port eq "443" ) {

        $OUT .= "    # SSL Directives\n";
        $OUT .= "    SSLEngine on\n";
        $OUT .= "    ProxyPreserveHost On\n";
        $OUT .= "    SetEnv proxy-initial-not-pooled 1\n";
        $OUT .= "    # Letsencrypt - proxy bypass\n";
        $OUT .= "    ProxyPass /.well-known/acme-challenge/ !\n";
        $OUT .= "    ProxyPassMatch ^.*/sockjs/(.*)/websocket ws://$target/sockjs/\$1/websocket\n";
        $OUT .= "    ProxyPass /websocket ws://$target/websocket\n";
        $OUT .= "    ProxyPass / http://$target/ retry=1 acquire=3000 timeout=600 Keepalive=on\n";
        $OUT .= "    ProxyPassReverse / http://$target/\n";
        $OUT .= "    \n";
    }
}

When you expand the httpd.conf file you will find a new virtualhost for your domain. My Rocket config creates these:

Code: [Select]
<VirtualHost 0.0.0.0:80>

    ServerName chat.mydomain.com
    ServerAlias
    RewriteEngine on
    RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
    RewriteRule .* - [F]

    # Redirect Letsencrypt queries
    RewriteRule ^/.well-known/acme-challenge(/.*|$) https://%{HTTP_HOST}/.well-known/acme-challenge$1 [L,R]
    # Everything else goes to https
    RewriteRule ^/(.*|$) https://%{HTTP_HOST}/ [R,L]

</VirtualHost>


<VirtualHost 0.0.0.0:443>

    ServerName chat.mydomain.com
    ServerAlias

    # SSL Directives
    SSLEngine On
    ProxyPreserveHost On
    SetEnv           proxy-initial-not-pooled 1
    # Letsencrypt - proxy bypass
    ProxyPass /.well-known/acme-challenge/ !
    # Websockets
    ProxyPassMatch ^/sockjs/(.*)/websocket ws://localhost:3000/sockjs/$1/websocket
    ProxyPass /websocket ws://127.0.0.1:3000/websocket
    ProxyPass / http://127.0.0.1:3000/ retry=1 acquire=3000 timeout=600 Keepalive=On
    ProxyPassReverse / http://127.0.0.1:3000/

</VirtualHost>

Note that may not be perfect or exact but should give you an idea.

I could add additional template directories VirtualHost-2 VirtualHost-3 etc and paths as I want them, each one creating a new virtual host.

Quote
I'll try to learn about the NodeJs requirements and update/create wiki page about it.
https://wiki.contribs.org/NodeJS#For_SME_9


...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Offline Jáder

  • *
  • 1,099
  • +0/-0
    • LinuxFacil
Re: NodeJS - running w/o SCL
« Reply #17 on: January 29, 2019, 10:23:26 AM »
(...)
I'll try it once more. Please go and look in the directories and find this stuff.
You have a STOCK directory.
/etc/e-smith/templates/etc/httpd/conf/httpd.conf/ProxyPassVirtualHosts
You can then add a TemplatePath "ProxyPassVirtualHosts" which forces SME to look in the above directory for templates.
This will enable simple proxying for a domain.

Thanks by your new explanation.
I finally understand why a TemplatePath and find out I really do not need it.
As I was thinking from beginning, NodeJS just need to port redirect. One redirect by app.
So I just need to create a new variable (NodePort) to specify that port and use a custom fragment template file /etc/e-smith/templates-custom/etc/httpd/conf/httpd.conf/VirtualHosts/27ProxyNodeJS with:
Code: [Select]
{
    use esmith::DomainsDB;
    my $domains = esmith::DomainsDB->open_ro;

    my $nodeport = $domains->get_prop($virtualHost, "NodePort") || "disabled";
    return "    # skipping NodeJS redirect - no NodePort parameter\n" unless not $nodeport eq "disabled";

    $OUT .= "    # NodeJS port redirect\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPreserveHost On\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyRequests Off\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPass / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';
    $OUT .= "    ProxyPassReverse / http://localhost:$nodeport/\n" unless $nodeport eq 'disabled';

}
the variable can be set by using this db command:
Code: [Select]
db domains setprop w8k.portalgap.com.br NodePort 3003
and expand new template and restart
Code: [Select]
expand-template /etc/httpd/conf/httpd.conf
/etc/init.d/httpd-e-smith restart
That's all. It's working.
The configuration works for any (as far I have tested) NodeJS app even code is not clean because it's generated all redirects to /server-manager :980/server-manager and so on... but that it's a NFR for SME generate those only for main domain, so it's another discussion.
If you need SSL using LetsEncrypt you can follow appropriate wiki page.

I'll document all these NodeJS thing in wiki page ASAP.
« Last Edit: January 29, 2019, 10:26:51 AM by Jáder »
...

Online ReetP

  • *
  • 3,722
  • +5/-0
Re: NodeJS - running w/o SCL
« Reply #18 on: January 29, 2019, 03:30:48 PM »
Can you go back and check your code?

I believe 'unless' should be used like

unless ('something' eq 'somethingelse')

And this?

Code: [Select]
return "    # skipping NodeJS redirect - no NodePort parameter\n" unless not $nodeport eq "disabled";
How does the 'not' work in this scenario?

Something like this might be better?

Code: [Select]
return "Blah" unless ($nodeport ne "disabled");
And then something like this:

Code: [Select]
$OUT .= "    ProxyPreserveHost On\n" unless ($nodeport eq "disabled");
However, if it passes the first 'return' one presumes that you have a port set so really the following 'unless' parts are irrelevant - you may as well just output.

Also, be consistent with your use of " or '

Please and make sure you test code thoroughly before putting it on the wiki to try and ensure it is bug free.

...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Online ReetP

  • *
  • 3,722
  • +5/-0
Re: NodeJS - running w/o SCL
« Reply #19 on: January 29, 2019, 03:32:22 PM »
There's lots of stuff about but this seemed quite good

https://www.perltutorial.org/perl-unless/
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation

Online ReetP

  • *
  • 3,722
  • +5/-0
Re: NodeJS - running w/o SCL
« Reply #20 on: January 29, 2019, 03:39:17 PM »
Can you also explain the last line in the wiki please - it doesn't really make much sense.

"It's possible to access using SME with one LetsEncrypt certificate by directory/port"

It might be also good to show a snippet from httpd.conf to see what the new code looks like.
...
1. Read the Manual
2. Read the Wiki
3. Don't ask for support on Unsupported versions of software
4. I have a job, wife, and kids and do this in my spare time. If you want something fixed, please help.

Bugs are easier than you think: http://wiki.contribs.org/Bugzilla_Help

If you love SME and don't want to lose it, join in: http://wiki.contribs.org/Koozali_Foundation