How to deploy NodeJS Rest API in Ubuntu LTS 18 in apache2 - node.js

I am new to NodeJS and I have a simple website that sends the contact-us information from ReactJS to NodeJS via Axios.
It is working on my local machine and I am trying to deploy it in my AWS EC2 Ubuntu LTS 18.
I have installed the PM2 already and I am stuck on what should I do next, how can I deploy my NodeJS in Ubuntu with Apache2 installed and make it run on my server even if I close my terminal. Also, what would be the API URL endpoint?
I hope someone could help me with this basic nodejs deployment in Ubuntu.

You have 2 problems:
to move the code to the server - you can use shipit.js (https://github.com/shipitjs/shipit) in order to do this. Take a look at this screencast about shipit.js and forever https://youtu.be/8PpBySjkWEM, forever is something like pm2.
to redirect traffic from Apache to your app. It is called reverse proxy. Conf file for that would be:
<VirtualHost *:80>
DocumentRoot **where-your-app-public-files-are**
ServerName **domain_name**
ProxyRequests off
ProxyPreserveHost on
ProxyPass / http://127.0.0.1:**your-node-port**/
ProxyPassReverse / http://127.0.0.1:**your-node-port**/
</VirtualHost>
Change **variables** to your data.
So it could be:
<VirtualHost *:80>
DocumentRoot /var/www/your-app
ServerName your-domain-name.com
ProxyRequests off
ProxyPreserveHost on
ProxyPass / http://127.0.0.1:4040/
ProxyPassReverse / http://127.0.0.1:4040/
</VirtualHost>

Related

how to configure apache2 to host nestjs service

Im trying to publish nestjs service with angular front. Angular works correctly but nest dont.
<VirtualHost *:2000>
ServerName myservername
ServerAlias myserveralias
DocumentRoot /var/www/html/path/to/dist
<Directory /var/www/html/path/to/dist >
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
So far what I got is this directives. if i launch nest it says that port 2000 is already used but it doesnt respond to any request.

Can not access api when set up Node.js app with Apache on ubuntu 20.04

I'm deploying my node.js app with apache on ubuntu following https://imstudio.medium.com/set-up-a-node-js-app-for-a-website-with-apache-on-ubuntu-18-04-e0323c333c20.
This node.js app is listening on port 4000. I put it in directory/var/www/html/api and start it by pm2.
After deployment, I tested my api and found that only my home url www.myweb.tk/api works, and other sub urls, for examplewww.myweb.tk/api/location, does not work.
Here is my conf file:
<VirtualHost *:80>
ServerName myweb.tk
ServerAlias www.myweb.tk
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /api>
ProxyPass http://127.0.0.1:4000/
ProxyPassReverse http://127.0.0.1:4000/
</Location>
</VirtualHost>
Can anyone tell me how to make other urls works? Thank you in advance.
I has same issue, but with nginx, i solved it by adding /api to proxy
Try to pass http://127.0.0.1:4000/api
UPD:
Or search for something like request url varible in Apache

How can I run NodeJS and Apache together with an Apache virtual host file on Centos8?

I have my NodeJS application running on example.com:3000. Obviously, this would be unpresentable to have to tell a user to type in :3000. I want it running on example.com, and I cannot set Node to :80 since my Apache PHP front end is on :80.
I am running CentOS 8. I created a file called proxy.conf in /etc/httpd/conf.d and dropped the following lines in it.
<VirtualHost *:80>
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
ProxyRequests On
ProxyPass / http://localhost:3000
ProxyPassReverse / http://localhost:3000
</VirtualHost>
But, it's just going to my regular non node page.
The question is, how does apache know you when you want the PHP server, or when you want the Node server. You need to split them by ServerName.
You need a ServerName attribute so that apache knows to send that VirtualHost to the other process.
<VirtualHost *:80>
// PHP Server
ServerName example.com www.example.com
</VirtualHost>
<VirtualHost *:80>
// Node Server
ServerName node.example.com
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
ProxyRequests On
ProxyPass / http://localhost:3000
ProxyPassReverse / http://localhost:3000
</VirtualHost>
Additionally, you will have to put node.example.com within DNS.

Proxy a node/react application in an apache server

Using forever to forever run the node server on the virtual machine, I am unable to get the app to run without explicitly adding the port in the url like so: URL.com:8080
If I don't use the port in the URL, I do load up the file structure of the application.
Steps to reproduce: I have a create-react-app application.On the virtual server I run 'npm run build' to make sure I have a build to serve. I then run forever start on the root of the application.
The code below should give all the necessary details. I can provide more if you need.
I have spent so much time tweaking the .conf file to try different configurations but I can't seem to get it. I am using it and successfully hosting two static html sites but not this node application.
Package.json:
...
"main":"server/index.js",
"proxy":"http://localhost:8080"
...
Apache url.conf:
<VirtualHost *:80>
ServerName URL.com
ServerAlias URL.com:8080/
DocumentRoot /var/www/nameOfApp/
<Directory />
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/nameOfApp/public>
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
</Directory>
</VirtualHost>
Node server file using express:
app.use(express.static(`${__dirname}/../build`));
I've also made sure I have the modules enabled to allow for proxying. So I think essentially, what I need is to request this site and not need the :8080 at the end.
Apache and NodeJS are 2 different and separate application.
You interact with them by sending request to the port that they are listening to. In your case here,
Apache is listening at port 80
Your NodeJS application is listening at port 8080
So all request to port 80 will be handled by Apache, and since you do not has an index.html, Apache will default to just list out the files and directory (Options Indexes). Up until this point, your node application do not know anything about your request.
So what you need to do is define some endpoint, say url.com/node, and tell Apache to forward all request of this endpoint to port 8080 and let your node application to do the job.
How to do this?
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Best practices when running Node.js with port 80 (Ubuntu / Linode)
Node.js + Nginx - What now?
Hope this points you to the right direction.
The configuration that eventually worked was as simple as this:
<VirtualHost *:80>
ServerName yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
Looks like I was over complicating it trying to create a complicated proxy but the solution was very simply adding this to the config for the node application and then running sudo systemctl restart apache2 and everything worked beautifully.

Node application redirect to subdomain using virtual hosts

I'm trying to create a redirect for my nodejs app running a port 5001 to a subdomain and while I browse my nodejs app, I'm trying to keep the subdomain url.
I'm using Debian 8 and the latest Apache version.
I have the following setup:
A node app running on server at http://example.com:5001
A redirect in apache config
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Redirect / http://exemple.com:5001/
</VirtualHost>
This works fine but the url switches
On my other server using Debian 7, I have the following setup :
<VirtualHost *:80>
ServerName subdomain.example.com
ProxyPreserveHost On
ProxyPass / http://example.com:5001/
ProxyPassReverse / http://example.com:5001/
</VirtualHost>
When I try using the same method on my new server, apache won't restart after changing the virtual host.
What am I doing wrong ?
Thanks

Resources