node js installation on Apache HTTP server (centOS) - node.js

I'm working at a project in school that includes Apache server.
All i need to do right now to start working with the server is create An index file (html, ph) at my folder on the server (inside the public_html) and the server will return that page.
but the thing is that I want to write the server with nodejs.
I have already manage to install node on the server but I know how to ignore the Apache server and start working with node.
I read about that and I saw that i need to start node on a different port? or use proxy?
but I really don't know that much about servers.

You can use apache as proxy for nodejs https://httpd.apache.org/docs/2.2/mod/mod_proxy.html.
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
Or, if you want to run the nodejs not from root directory of server
ProxyPass /mynodejsproject http://localhost:3000/
ProxyPassReverse /mynodejsproject http://localhost:3000/
For example, nodejs application listens on 3000 port, apache on 80 port, and it proxies requests to nodejs application.
But i recommend you to use nginx as proxy for nodejs application, this is the config i used in my projects https://github.com/vodolaz095/hunt/blob/master/examples/serverConfigsExamples/nginx.conf

service apache2 stop Stops your apache server (It works on 80 port default)
Also there is a good tool for nodejs ,you will able to manage your nodeJS server(you can give 80 port now) like services with forever on nodeJS.(I assumed you know how to creating your nodejs http server)

Related

Spring boot .jar to digital ocean droplet (Linux Ubuntu) :Web server failed to start. Port 80 was already in use

I have an Digitalocean Droplet (virtual private server) that has Ubuntu 18.04 running on it. I installed Apache Web Server and have my website running on it . It's open to traffic on HTTP port 80. My virtual host is setup at /var/www/MyDomainNameHere/public_html/ and I do have a custom domain name pointing to the IP.
I am trying to deploy/run a executable .jar that contains a Spring boot API. It has some basic GET/POST/DELETE HTTP requests. When I run the jar by
java -jar rest-service.jar
I get this error message
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 80 was already in use.
Does this mean the Apache Web Server that is open to Traffic on port 80 on this Digital ocean droplet won't let me run the API jar on the same server (The JAR loads up an Apache Tomcat embedded server, I set it to port 80 based on research)? Do I need to buy another droplet? Or can I maybe change the port number for the spring boot jar to something other than port 80? It's an API, so I need to be able to hit the end points
Yes, Apache Web Server using the port and you change your spring application 80 to any other port and use the reverse proxy in Apache Web server. here is the link to configure the reverse proxy
Apache as a Reverse Proxy with mod_proxy
So I will answer my own question. I googled and searched stackoverflow but most people were using the reverse proxy to direct traffic to their standalone embedded Tomcat server (most of time a Spring Boot app) which wasn't applicable to my situation.
However, I already had a static html website at alpizano.me hosted on Digital Ocean that I wanted to use as my Front-end, and only forward certain HTTP requests to my Spring Boot app, which was a REST API essentially.
So after installing Java and PostgreSQL on my DigitalOcean droplet, I SCP'd my JAR file (running ./mvnw clean package -Dmaven.test.skip=true to create it) to my server to a directory like, /var/myapp.
Then after researching for a few days and trying multiple things, I was able to figure out the combination that would allow me to view my website when navigating to alpizano.me, but still allow me to hit my API endpoints that were running on my server as-well, after I ran my jar via java -jar myapp.jar
So I basically had to set up my .conf file in the /etc/apache2/sites-available dir (I only used 1 virtual host for this project) as:
<VirtualHost *:80>
ServerName yourservername.com
DocumentRoot /var/www/yourservername.com/public_html
ProxyPreserveHost On
ProxyPass /api http://127.0.0.1:8080/
ProxyPassReverse /api http://127.0.0.1:8080/
</VirtualHost>
Notice the /api route for the routing that goes to the embedded Tomcat at port 8080 (you can't use port 80 or it will conflict with Apache Server already listening on port 80), else if it's just / , then it will not allow traffic to base website anymore (alpizano.me), which isn't what I wanted.
This wouldn't be needed if you just had a standalone app that you wanted to route traffic, then you could just use / as your route obviously. I saw other posts talking about forwarding the headers but that didn't seem to make a difference for me and I believe ProxyPreserveHost On takes care of that anyway
Good luck.
References:
https://www.digitalocean.com/community/tutorials/how-to-use-apache-as-a-reverse-proxy-with-mod_proxy-on-ubuntu-16-04

How to bind port 4200 (Angular web server) to port 80 (Apache web server)?

I have Apache Web Server running on EC2 (RHEL 7) on port 80.
Also have Angular App running on port 4200 on the same server.
How to bind port 80 so that when someone from outside goes to IP (e.g. 1.2.3.4) goes directly to Apache port 80 (that part works now) which then redirect (or bind) to port 4200 so that the end user can see Angular app output (instead of Apache output)?
Thanks.
Use a reverse proxy Nginx for example.
The step will be the following: Nginx listens 80 port from outside and passes this request to 4200 to your Angular app.
Why do you use apache on this port? And what does apache do?
You should instead like Red Cricket says follow the docs.
First run ng build prod or whatever build you need.
Then add the .htaccess file with redirect rules into your dist folder and place it wherever your apache will serve the angular app.
For extra details see the link provided by Red Cricket.

Running both Node.js and Apache on the same domain and "URL"

Is it possible to run Both node and Apache on the same domain without adding the port in the URL ?
and serve both on the same page, i already have node running on port 8443 and Apache on port 433 and they both work fine but i need to specify in the link the port "8443" to access node which is not what i want,
i want to serve both on the same URL if possible without adding the port to the URL.
You can use Apache reverse proxy
Add this configuration to your apache conf.
ProxyPass "/nodeapp" "http:/localhost:8443"
You can access node application by http://www.example.com/nodeapp
A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from server. These resources are then returned to the client as if they originated from the web server itself.
You can set an nginx proxy before them and separate routes to apache or node.
https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

Where do I put my Node JS app so it is accessible via the main website?

I've recently installed a nodejs app (keystone) app in my home/myusername/myappname directory.
When I visit www.mydomain.com, nothing displays - even after turning on my nodejs app.
Where should these files be?
I am running ubuntu 16.04.
In the past I have worked with a var/www folder, but I am not using apache - do I need to manually create this folder?
Thanks!
For your app to be visible it has to be running (obviously) and accessible on port 80 (if you want it to be available without adding a port number to the URL).
It doesn't matter where it is on the disk as long as it's running.
You don't need Apache or nginx or any other server. Your Node app may listen on port 80. But alternatively it can listen on some other port and your other server (Apache, nginx, etc.) can proxy the requests to that port.
But if your app is listening on, e.g. port 3000 then you should be able to access it as http://www.example.com:3000/.
Also, make sure that your domain is configured correctly. It's A record for IPv4 (or AAAA for IPv6) of the www subdomain should be equal to the publicly accessible IP address of your server.
And make sure that the port you use is not blocked by the firewall.
Update
To see how you can set the port with Keystone, see:
http://keystonejs.com/docs/configuration/#options-server
It can be either changed in the config or you can run your app with:
PORT=80 node yourApp.js
instead of:
node yourApp.js
but keep in mind that to use the port number below 1024 you will usually need the program to run as root (or add a special privilege which is more complicated).
It will also mean that this will be the only application that you can run on this server, even if you have more domain names.
If you don't want to run as root or you want to host more application, it is easiest to install nginx and proxy the requests. Such a configuration is called a "reverse proxy" - it's good to search for info and tutorials using that phrase.
The simplest nginx config would be something like this:
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://localhost:3000;
}
}
You can set it in:
/etc/nginx/sites-available/default
or in a different file as e.g.:
/etc/nginx/sites-available/example
and then symlinked as /etc/nginx/sites-enabled/example
You need to restart nginx after changing the config.
You can find more options on configuring reverse proxies here:
https://www.nginx.com/resources/admin-guide/reverse-proxy/
You need to make a proxy between Apache and your Node.js application because Node.js has a built-in server. Supose your Node.js app is served on 9000 port. Then you need to make a proxy to redirect all trafic in 80 port to 9000 port where the Node.js app is running.
1. Enable mod_proxy
You can do this through a2enmond.
sudo a2enmod proxy
sudo a2enmod proxy_http
2. Set the proxy
Edit the /etc/apache2/sites-available/example.com.conf file and add the following lines:
ProxyRequests Off
Order deny, allow from All
ProxyPass / http://0.0.0.0:9000 ProxyPassReverse / http://0.0.0.0:9000
This basically say: "Redirect all traffic from root / to http://0.0.0.0:9000. The host 0.0.0.0:9000 is where your app is running.
Finally restart apache to enable changes.

How to run sails.js application on port 80 with example?

I have to put sails.js in port 80, but apache is already using it. How can I put both (sails.js and apache) on the same port 80? I need it because in my company all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application). Thanks a lot
You have to run Sails on any free port (1337, for example) and use Apache with mod_proxy. Make sure it's loaded in Apache config, then your virtual host will be something like this:
<VirtualHost *:80>
ServerName www.youserver.com
ProxyPass / http://localhost:1337/
ProxyPassReverse / http://localhost:1337/
</VirtualHost>
See mod_proxy documentation for more details.
Put nginx in front of sailsjs, and dump apache, or make apache run on a different port.
DigitalOcean has a great tutorial on doing this with two nodejs apps.. You can find it here.
I also wrote a blog post about it here
I do this on my server so i can run ghost blog, and sailsjs on the same server. This is how i have it setup.
NGINX Proxies blog.gorelative.com -> localhost:2368
NGINX Proxies gorelative.com -> localhost:1337
Just adapt the tutorial to proxy one domain to localhost:1337 for sailsjs, and add in the other hosts to host them normally.. or proxy the others to apache2, which probably is a waste of resources and not ideal.
AFAIK: With a bit more work your hosts don't even need to be on the same server. nginx can proxy to another server, and also do load balancing tasks.

Resources