Run a NodeJS app with Apache - node.js

I have a ubuntu server running a few apache websites. I want to run a nodejs app on the same server. I have the app running on the server now out of port 3000 (www.example.com:3000) no problems there.
I now want to direct a new domain to the nodejs app with my existing apache setup. Below is an example of the config i'm running however it's pointing to a folder. What i require is the config that points to the app's port. And any extras that I maybe missing.
<VirtualHost *:80>
ServerAdmin spam#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
</VirtualHost>

First of all, you should to install mod_proxy and mod_proxy_http.
Then you can use something like the following configuration:
<VirtualHost *:80>
ServerAdmin spam#example.com
ServerName example.com
ServerAlias www.example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>

Related

Apache make subdomain and change main domain port

I have vps server in Ubuntu 18.04. my backend project is Laravel so frontend is Nuxt. I want deploy my laravel project as subdomain in 80 port. example : api.domain.com. Frontend project deploy in 3000 port as main domain. example domain.com. How I configure my apache for subdomain and how redirect main domain 3000 port?
You can configure your Apache to proxy port 80 -> 3000.
<VirtualHost *:80>
ServerName api.domain.com
DocumentRoot "/your/laravel/application/path/public"
</VirtualHost>
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName domain.com
ServerAlias domain.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
Once you're done with configuring Apache, you'll have to enable the required Apache modules and restart.
sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart
[Derived from this answer.]
As for your Laravel application, I'm going to assume you've installed PHP and loaded the Apache PHP module.

port redirection of my nodejs application with apache2

My system is hosted by Amazon LighSail.
My application is running with Nodejs on the port 3000.
I installed my HTTPS certificate for my domain name with Let's Encrypt.
Now I want to reach my nodejs application directly to my sub-domain.domain.com.
When I go to my domain name (sub-domain.domain.com), I'm directed to the bitnami home page.
So I have:
https://sub-domain.domain.com --> bitnami home page
IP-ADDRESS:3000 --> my nodejs application
I add a proxy to redirect all HTTP request to my port 3000 but with no sucess.
My proxy added in /etc/apache2/sites-enabled/000-default.conf :
< VirtualHost *:80 >
ServerAdmin webmaster#localhost
ServerName www.sub-domain.domain.com
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
< / VirtualHost >
Someone has an idea to solve my problem or any clue?
With SSL Cert the config should look more like this:
<VirtualHost *:80>
ServerName yoursite.com
ServerAlias *yoursite.com
ServerAdmin example#yoursite.com
DocumentRoot /var/www/yourSite
LogLevel debug
SSLEngine on
SSLCertificateFile /path/to/lets/encrypt/cert
SSLCertificateKeyFile /path/to/lets/encrypt/key
SSLProxyEngine on
ProxyPass "/" "http://127.0.0.1:3000/"
ProxyPassReverse "/" "http://127.0.0.1:3000/"
ErrorLog ${APACHE_LOG_DIR}/yourSite_error_https.log
CustomLog ${APACHE_LOG_DIR}/yourSite_access_https.log combined
</VirtualHost>
Solved ! I had to add my proxy in this file: /opt/bitnami/apache2/conf/bitnami/bitnami.conf

Apache ReverseProxy not working with Node and SSL

I'm trying to setup my app on HTTPS on a web server. I have a valid certificate that is installed on my InMotion host using AutoSSL. I have the node app running on my Centos server on port 3000 and my apache SSL server running on port 443. To route requests made to my website, I am attempting to create a reverse-proxy. I made a file called default-site.conf which is stored in /etc/http.d/conf.d and has the following contents:
<VirtualHost _default_:443>
SSLProxyEngine on
ServerName example.com
ServerAlias www.example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ServerAdmin info#example.com
DocumentRoot /home/USER/my-app/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /home/USER/ssl/certs/certificate.crt
SSLCertificateKeyFile /home/USER/ssl/keys/certificate.key
ProxyPass / https://example.com:3000/
ProxyPassReverse / https://example.com:3000 /
<Directory "/home/USER/my-app/public">
AllowOverride All
</Directory>
</VirtualHost>
I will emphasize that the app is accessible at http://example.com:3000 (insecure) but when I attempt to go to https://example.com (secure) I get my blank index.html with a 404 error:
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (index.js, line 0)
I managed to fix this issue by completely simplifying my config to the following two lines:
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
This may not be the most flexible approach but it reroutes the traffic the way I need it to be routed, so for my purposes, it works.

how to access virtual host outside

I have this on my Apache 2 file "example.com.conf". I am only able to access it only by IP, but when I try to access it for the server name always I got the following error:
Network Error (dns_server_failure)
<VirtualHost *:80>
ServerAdmin myemail
DocumentRoot /var/www/html/example.com
ServerName example.com
ServerAlias www.example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I'm running Ubuntu 14.04.03 (X64)
I try to access this on a machine with Windows 8.1.

Is it possible to use node.js server without having the Port number appear in the URL.

I have a VPS using apache on port 80, but want to use node.js for a subdomain, without the port node will be using appear in the address bar.
Yes it is possible
In your apache virtual host file configure with the following:
<VirtualHost *:80>
ServerName subdomain.myapp.com
ProxyRequests off
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyPreserveHost on
</VirtualHost>
You should have
NameVirtualHost *:80

Resources