Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run nodejs application in apache server

I would like to run my nodejs application via subdomain on apache server. I created subdomain of primary domain in cpanel. My project having more than 3 subdomains and all subdomains points different nodejs application. Subdomain redirects me in proper folder but when I run an application via subdomain in browser, it does not run node application on apache server. It displays folder lists of project only.

I refer following link for reference.

http://www.codingtricks.biz/run-nodejs-application-apache/

Running Node.js in apache?

Project Directory :

/home/abc/public_html/node

My primary domain is :

https://www.example.com

I created sub-domain in cpanel :

https://www.node.example.com

I access sub-domain in browser :

node.example.com

I deployed my application on softlayer server.

Following is apache configuration:

File :

/usr/local/apache/conf/includes/abc_node.conf

Configuration:

<VirtualHost 132.159.25.21:80>
   ServerAdmin [email protected]
   ServerName node.example.com
   ServerAlias www.node.example.com

   DocumentRoot /home/abc/public_html
   <Directory /home/abc/public_html/node>
      Options -Indexes +FollowSymLinks
      AllowOverride None
      Require all granted
   </Directory>

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location /node>
      ProxyPass http://132.159.25.21:8080
      ProxyPassReverse http://132.159.25.21:8080
   </Location>

   ErrorLog /home/abc/public_html/node/error.log

   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.
   LogLevel warn

   CustomLog /home/abc/public_html/node/access.log combined

</VirtualHost>

Command Summary:

Command:

/usr/local/apache/bin/httpd -tD DUMP_VHOSTS

Output:

AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445
VirtualHost configuration:
127.0.0.1:80           wb01-development.example.com (/usr/local/apache/conf/httpd.conf:504)
127.0.0.1:443          wb01-development.example.com (/usr/local/apache/conf/httpd.conf:554)
132.159.25.21:443      is a NameVirtualHost
         default server example.com (/usr/local/apache/conf/httpd.conf:368)
         port 443 namevhost example.com (/usr/local/apache/conf/httpd.conf:368)
                 alias www.example.com
         port 443 namevhost wb01-development.example.com (/usr/local/apache/conf/httpd.conf:554)
                 wild alias cpanel.*
                 wild alias whm.*
                 wild alias webmail.*
                 wild alias webdisk.*
                 wild alias cpcalendars.*
                 wild alias cpcontacts.*
132.159.25.21:80       is a NameVirtualHost
         default server 132.159.25.21 (/usr/local/apache/conf/httpd.conf:274)
         port 80 namevhost 132.159.25.21 (/usr/local/apache/conf/httpd.conf:274)
         port 80 namevhost example.com (/usr/local/apache/conf/httpd.conf:297)
                 alias www.example.com
         port 80 namevhost node.example.com (/usr/local/apache/conf/httpd.conf:446)
                 alias www.node.example.com
         port 80 namevhost wb01-development.example.com (/usr/local/apache/conf/httpd.conf:504)
                 wild alias cpanel.*
                 wild alias whm.*
                 wild alias webmail.*
                 wild alias webdisk.*
                 wild alias cpcalendars.*
                 wild alias cpcontacts.*
         port 80 namevhost node.example.com (/usr/local/apache/conf/includes/abc_node.conf:1)
                 alias www.node.example.com
*:*                    wb01-development.example.com (/usr/local/apache/conf/httpd.conf:286)

Command

/usr/local/apache/bin/httpd -tD DUMP_MODULES

Output:

AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445
Loaded Modules:
    core_module (static)
    authn_file_module (static)
    authn_core_module (static)
    authz_host_module (static)
    authz_groupfile_module (static)
    authz_user_module (static)
    authz_core_module (static)
    access_compat_module (static)
    auth_basic_module (static)
    socache_shmcb_module (static)
    socache_dbm_module (static)
    so_module (static)
    include_module (static)
    filter_module (static)
    deflate_module (static)
    http_module (static)
    mime_module (static)
    log_config_module (static)
    logio_module (static)
    env_module (static)
    expires_module (static)
    headers_module (static)
    unique_id_module (static)
    setenvif_module (static)
    version_module (static)
    proxy_module (static)
    proxy_connect_module (static)
    proxy_http_module (static)
    slotmem_shm_module (static)
    ssl_module (static)
    mpm_prefork_module (static)
    unixd_module (static)
    status_module (static)
    autoindex_module (static)
    asis_module (static)
    suexec_module (static)
    cgi_module (static)
    negotiation_module (static)
    dir_module (static)
    actions_module (static)
    userdir_module (static)
    alias_module (static)
    rewrite_module (static)
    bwlimited_module (shared)
    suphp_module (shared)
    security2_module (shared)

I run my node js application using following ip and port

132.159.25.21:3030

I defined my port in nodejs application in following way

var port = parseInt(process.env.PORT, 3030) || 8080;

With the reference of this error : AH00548: NameVirtualHost has no effect and will be removed in the next release /usr/local/apache/conf/httpd.conf:445 following code has been written. NameVirtualHost 132.159.25.21:80

I put may vhosts file at the end of /usr/local/apache/conf/httpd.conf file, becuase in file they wrote a warning of "# DO NOT EDIT. AUTOMATICALLY GENERATED. IF YOU NEED TO MAKE A CHANGE PLEASE USE THE INCLUDE FILES."

I required help to resolve this issue. Please let me know if you required further more to know about application.

like image 328
Dipak Avatar asked Aug 23 '16 05:08

Dipak


People also ask

Can I configure an Apache server for a NodeJS application?

Join Deborah Emeni on September 28th at 11 a.m. EDT and learn how to create a server solution that can respond to requests quickly and handle a high traffic volume of users by configuring an Apache server for Node.js. We will cover how Apache can be configured and the benefits of configuring Apache for a Node.js application.

How to run node app on localhost/node?

For example, if you already have Apache server running on localhost and want to run Node.js app on localhost/node, the flow should looks like this First let’s start the node application to listen on port 3000. app.listen(3000, () => console.log('Example app listening on port 3000!'))

How to listen to node and Apache on the same port?

The Concept Since we cannot run the both node.js and apache to listen the same port. we’ll to config apache to act like a reverse proxy and pass the request to node.js application for a specific url. For example, if you already have Apache server running on localhost and want to run Node.js app on localhost/node, the flow should looks like this

How do I access the NodeJS script from the web?

To access the Node.js script from the web, install the Apache modules proxy and proxy_http with the commands: Once the installation is complete, restart Apache for the changes to take effect: Next, you will need to adjust the Apache proxy configurations.


1 Answers

The configuration looks correct except for one bit, the /node location path should have the target matching slashes for reverse proxying correctly without unexpected consequences:

I'd rather define it like this for clarity's shake:

ProxyPass /node/ http://132.159.25.21:8080/
ProxyPassReverse /node/ http://132.159.25.21:8080/

Take into account while Proxying using the target http://132.159.25.21:8080 is incorrect, and it should always defined as http://132.159.25.21:8080/ and therefore the source should match slashes in the same fashion, therefore /node/ is the correct way.

As for "NameVirtualHost" warning. It's just telling you, that it is not needed any longer in 2.4.X version of Apache HTTPD since that directive is only needed for 2.2, 2.4 got "smarter" detecting named virtualhosts.

SideNote: <Proxy *> has no effect, so defining it is futile. Proxy * is used for forward proxying and clearly you do not want it and have disabled such functionality with ProxyRequests off. So remove that one also.

--- Answer extension due to your comments:

To reach your node you would have to access:

http://node.example.com/node/

or add a "default" redirection that takes you to it.Such as:

RedirectMatch ^/$ /node/

You are getting indexes (file list) by accessing node.example.com because -Indexes is defined for a subdirectory, meaning previous one has Indexes enabled, So define:

<Directory /home/abc/public_html>
    Options -Indexes +FollowSymLinks
    ....

Instead of defining /home/abc/public_html/node, which does not apply to this case, since node/ is not a directory, but a virtual path in a backend, so for correction shake it should never be included in a Directory path.

If you just want to access the backend while accessing http://node.example.com/ Just use this instead of ProxyPass /node/:

ProxyPass / http://132.159.25.21:8080/
ProxyPassReverse / http://132.159.25.21:8080/
like image 199
ezra-s Avatar answered Oct 10 '22 14:10

ezra-s