node.jsapachehttpubuntu

How to run node.js app next to Apache on the same domain without using ports


I am trying to use a proxy with apache on my ubuntu server, in order to reach my node.js app without using a port, but none of the solutions I have tested worked.

Basically I have a website running on my domain examle.com and want to reach the node.js app (running on port 3000 & using https) either using a subdomain like myapp.example.com, or using something like example.com/myapp

Here is my current conf file (which doens't work):

#mainWebsite
<VirtualHost example.com:443>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin [email protected]
  ServerName  example.com
  ServerAlias www.example.com
  #Redirect
  #Directory
  <Directory "/var/www/html/example.com">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
  </Directory>
  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/example.com/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/example.com/log/error.log
  CustomLog /var/www/html/example.com/log/access.log combined
  # Error Docs
  ErrorDocument 404 /www/html/example.com/errorDocs/404.html
  ErrorDocument 503 /www/html/example.com/errorDocs/503.html
  # SSL
  SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
#node.js app
<VirtualHost myapp.example.com:433>
    # Admin email, Server Name (domain name), and any aliases
    ServerAdmin [email protected]
    ServerName myapp.example.com
    ServerAlias www.myapp.example.com
    # Proxy
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyVia Full
    <Proxy *>
       Require all granted
    </Proxy>

    <Location /nodejs>
       ProxyPass https://127.0.0.1:3000
       ProxyPassReverse https://127.0.0.1:3000
    </Location>
    # Log file locations
    LogLevel warn
    ErrorLog /var/www/html/example.com/log/error.log
    CustomLog /var/www/html/example.com/acces.log combined
    # Error Docs
    ErrorDocument 404 /www/html/example.com/errorDocs/404.html
    ErrorDocument 503 /www/html/example.com/errorDocs/503.html
    #SSL
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>```

By the way if someone could help me, why my error Docs don't show up, I would be very happy :)

Solution

  • Below is my config file which I used in Apache webs-server in Ubuntu for subdomain .

        <VirtualHost *:443>
          ServerName myapp.example.com
    
          ProxyRequests Off
          ProxyPreserveHost On
          ProxyVia Full
    
          <Proxy *>
              Require all granted
          </Proxy>
    
          ProxyPass / http://127.0.0.1:5000/
          ProxyPassReverse / http://127.0.0.1:5000/
    
          SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
          SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
          Include /etc/letsencrypt/options-ssl-apache.conf
        </VirtualHost>
    

    You can access the node app in subdomain like https://myapp.example.com

    For sub-directory hosting your can try below config:

        <VirtualHost *:443>
          ServerName example.com
    
          ProxyRequests Off
          ProxyPreserveHost On
          ProxyVia Full
    
          <Proxy *>
              Require all granted
          </Proxy>
    
          ProxyPass /myapp http://127.0.0.1:5000/
          ProxyPassReverse /myapp http://127.0.0.1:5000/
    
          SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
          SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
          Include /etc/letsencrypt/options-ssl-apache.conf
        </VirtualHost>
    

    You can access the node app in subdirectory like https://example.com/myapp