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

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.

Related

What is the easiest way to configure Nginx alone to serve multi-app on different ports

I have centOS 7 server that has a Wordpress site and nodeJs application, I intend to serve these both on port 80 such as using only Nginx, Where nodejs running on port 3000, and how to configure Nginx to accomplish this task

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

Redirect all request internally to 3000 port apache2

I have a web application hosted on apache server. When I hit https://example.com then it points to the server but my application is running on 3000 port. So I want all the users to hit example.com (443 port) and internally it redirects all the request to 3000 port and make my application running.
You want to create a reverse proxy with Apache. These tutorial may help you:
https://linuxtogether.org/configuring-reverse-proxy-for-node-using-apache-mod-proxy/
http://garrows.com/blog/running-node-js-and-apache-together-using-mod_proxy/

Access Node.js server by URL without port at the end

My server is running on a Node.js environment with Express. My server works fine, but I can't remove the port at the end of the domain name from the URL.
What is the right way to access my app with an URL without port at the end ?
Client side
By default, the port is 80 when a browser make an HTTP request.
If you type localhost, the real request is localhost:80 because no port is specified. It will be the same with any domain name. If you type example.com, the real request is example.com:80.
It is the client (here the browser) which choose on which port it will make his request to the server.
You can force your browser to emit a request on any port by adding :port_number after the domain name, as localhost:3000 or example.com:3000. Here we change the port from 80 to 3000.
Server side
The web server chooses on which port it listens for requests. It can be 80, 3000 or any other port.
If a client makes an HTTP request, your web server needs to listen to the right port. If the client emits example.com:4000, your web server must listen on port 4000 to get and process the request.
To make a web server, you can use Node.js, Apache (used in LAMP), Nginx etc. You can have multiple web servers running on your system and each of them can use multiple ports, but you can't make them listen on the same port. One of your web server may not start or could take the lead on others or crash...
Solutions are to use only one web server or to use multiple web server on different ports. In your situation, you are using LAMP so Apache web server. Its probably running on port 80 in his configuration. In this case you can't run a Node web server on port 80 because it's already in use. You should choose another port like 3000 for example. Both Node and Apache will then run on your system but on different ports respectively 3000 and 80.
In this last situation, you can access directly to Apache, but not to Node without precise the port 3000. To be able to access Node web server by port 80 without stopping Apache, you need to go through Apache and to make it redirect requests to your Node server in some cases. To do that, you need to configurate a proxy in your Apache. Note that it would be the same if you was using Nginx or other web servers.
Example
Let's take a simple express server on port 3000 :
// server.js
var express = require('express'),
app = express(),
http = require('http').createServer(app),
port = 3000;
app.get('*', function (req, res, next) { res.sendFile(__dirname + '/views/index.html'); });
http.listen(port, function () { console.log('App running & listening on port ' + port); });
If you type in the terminal node server.js, you can access from browser by localhost:3000, but you can't access by localhost because no web server is running on port 80.
If you change port variable to 80, you can access from browser by localhost or localhost:80, but not by localhost:3000 anymore.
If you edit /etc/hosts (sudo nano /etc/hosts) with a new line 127.0.0.1 example.com, you can access from browser by example.com if port is 80, else example.com:port_number like example.com:3000. This third solution maps domain name to ip address in your local client only.
If the chosen port, 80 for example, is already in use by another process (as LAMP), your node server may not works. In this case you should close this other process first or choose another port for your node process. In the third example, if you close the LAMP first, you can access from browser by example.com, if you choose another port for Node, you can access from browser by example.com:port_number like example.com:3000 for Node and still access your LAMP server on port 80.
Don't forget that 80 is the default port used by the browser if no port is specified. If you use another port, you should precise it from the browser by adding :port_number after your domain.
Now if you own a real domain name you will need to make a real DNS mapping not juts edit /etc/hosts. Configure your DNS on your registar account (where you bought your domain name) to make it point to your server's IP. Like that, when a client make an HTTP request to the domain name, it will be redirected to your server.
To have both Apache and Node.js running and available on port 80, you should make a proxy as explain above. Indeed, for you the problem is probably that you have a web server already running on port 80 (Apache with LAMP) and you want also your Node.js app to run on port 80 to don't force clients to precise the port at the end of the url. To fix that, you need to make a proxy in Apache conf to redirect requests which come from the specific domain name to your localhost node server process on the right port.
Something like that in your apache conf :
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
Here when a request arrive on your server on port 80, Apache will check if it comes from example.com and if it is, it will redirect to 127.0.0.1:3000 where your node server will take the lead. The two different process (Apache & Node) should run in the same time on your server on different port.
If you want to run your node js server without any port and simply by http://localhost then listen your express js server on port 80 .
You could either do as stated by the previous answers and run on port 80 OR
you could keep the server running on whatever port you want and setup a proxy server such as nginx and forward the HTTP requests to said server.
This could be helpful in case you want to spin up multiple instances or even different processes.
When you see a URL, without a port, it means one of two ports are being served:
https:// - port 443
http:// - port 80
Even assuming the port is not in use, you can't service directly to port 80 without superuser privileges because port 80 and port 443 are privileged ports.
If you want to test the server running on port 80 directly:
sudo node index.js
Where index.js is the name of your Express application.
Keeping it running
Because you tagged apache, I'm assuming you want to know how to set up a node server using Apache. If you don't need a production quality server and just want to keep it running all the time, you can do that too.
Dev/Just keep it running
You can daemonize your server. A quick look for a "node" solution exposes forever as a way to do that. Simply install and run like this:
yarn global add forever
# or
# npm i -g forever
# remember, sudo for port 80
sudo forever start index.js
Production/Apache
Use a non-privileged port for Node, and set up a proxy in Apache. Something like:
ProxyPass / http://localhost:8000
If you set the port to 8000. Put that in a <VirtualHost>. Examples here. Likely you would still want to daemonize your nodejs Application using forever or some similar daemon tool (systemd is great for Linux services)

node js installation on Apache HTTP server (centOS)

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)

Resources