When running two NodeJs apps in same vps one use others .env conf and get connected to the wrong db - node.js

I have a VPS with Instance (Debian 9.4 (stretch) 20180404)
There I have a few WordPress sites and also a nodejs app that I will call "prod" running with PM2 without any problem whit apache2.
Now I want to run a new nodejs app on a subdomain which basically will be the dev environment of the previous and working node.
My issue is that when I start dev, prod gets connected to dev's database, no matter if I run dev as "node inde.js" or by PM2.
Each node environment is in its correct web folder and has a specific .env that has the credentials to access each database (one for prod and another for dev)
Could anybody please help me to sort out this issue?
On the apache2 config, I configure a proxypass reverse so I don't have to add the:3000 and I copied the conf to dev, could be that a problem? Are both listening to the same port? if yes, how do you suggest solving it?
Thanks in advance!

I resolved it.
I leave the solution in case anybody comes to the same issue.
The problem was indeed the port, so I'm using port 3000 for production and port 3002 for dev.
Also with the .env files, I decided to have one .env file for each environment and both are in the same location outside the git repository.
Everything works as expected.
Cheers!

Related

Deploying MEAN stack application to Heroku

I am trying to deploy an application I am building with the MEAN stack to Heroku and am having some trouble. I think the issue is with the structure of my application. I have all my server code in a folder called server, which has its own package.json and src folder that contains the actual server code.
Right now I am simply trying to get Heroku to deploy the client side of the application. I am only getting an error... I know that the database and server are not running but I cannot even get past the initial displaying of the app. I have one web dyno set up to run ng serve (npm start) on the app.
If someone would be willing to look at the structure of my application and sees why I am unable to deploy to Heroku without really digging into the code, that would be much appreciated.
Here is the code
Please note that it is on the deploy branch, and this is on purpose. I do not want to push anything to the master until I am sure it is working.
The Angular web server targets localhost:4200 by default. That can be changed with a couple command-line options. --port accepts a port and --host accepts a host IP address.
So you could modify the ng serve script as follows: ng serve --host [host-ip] --port [port-number] --disableHostCheck. That last flag (--disableHostCheck) tells the dev server to bypass host checking when normally ng serve fails on anything except localhost. Terrible idea if meant for anything except private development/testing.
Another issue: Heroku runs off web dynos and from what I understand about them, they use a random port and IP each time. While the random port is stored under the variable $PORT, the IP does not seem to have a similar variable. Web dynos keep that information to themselves.
Heroku does offer this command: heroku local web. It runs your application on localhost:5000. That means ng serve --port 5000 works perfectly with this command. This should tell you how your front-end will run on Heroku. Your angular dev server will function as expected too.
For actual deployment to Heroku, use that express server of yours. Run ng build from your file system and it will spit out an index.html in the dist folder. This file holds your entire front-end application. You can then upload that file into your browser from the server.
For express that usually breaks down into:
app.get('*', function(req, res) {
res.sendFile('path/to/index.html');
});
Hopefully this helps! Let me know if I missed the mark anywhere.

Connecting app to AWS EC2 instance

I'm pretty new to DevOps and I'm trying to set up my Node.js app on a AWS server instance. Steps I've taken:
Set up Elastic IP
Launched EC2 instance with Ubuntu server
Connected IP to instance
Allowed incoming connections on port 3000
SSH'd into the server with a .pem file
Now I'm at the point where I need to get my files uploaded to the server. I've used FileZilla (and like it) in the past to upload files but the initial part was already set up. When I set up the site on FileZilla there is no /var/www folder on the remote site.
Don't know how to connect these dots.
Also not sure what I need to run once I successfully upload the files. I imagine npm install when I'm ssh'd into the server? Most of the tutorials out there only go through the basic instance setup.
Thanks!
You don't need to have /var/www. Also, it's better that you use a version control and a remote repository like Github and then SSH to your EC2 and then clone your repository there.
Then cd into your repo and run npm install and then start your app.
And check.
Once you connect to the EC2 instance then clone your code in there. It not mandatory to be in /var/www/html but, it's best practice to keep it there. Once you clone npm install into your project home directory so all the required packages get installed. Then for running your node application in production you have to run it on service as pm2, supervisor, forever, passenger, etc. You can use any of these services and configured appropriately to run your application on desired port. As with pm2, you can follow this guide, install pm2 Then you can run with the following command w.r.t. your environment, like I want to run my application on port 5555 for production
$ PORT=5555 pm2 start app.js --name API --env production -f
Check the status using pm2 list Now, your application is running on http://server-ip:5555/ But, you won't be typing port number every-time. So, you need to configure the web server in front of your application like apache or nginx which will forward all request to your application running port. You could find the best guide to their home page. Then your application is available at http://server-ip/ You can follow this for single configuration of multiple node apps
Hope this helps.

Prod and Dev environment in nodejs

*** I am a beginner of nodejs, hope can give me some advice, thank you so much :)
I have one server to run my nodejs application (REST API).
My application have two environment (development and production),
i update the code in development quite frequent to do testing,
after i complete the development, i will release the code to production.
I am using pm2 in nodejs to run the application.
Here is the folder structure of my REST API
restapi
app1
node_modules
db.js
....
index.js
Here is my question:
What is the best way to setup the dev and prod environment for my app?
I have two idea
Idea 1:
Put prod and dev seperate to two folder, both listening to same port and run as single service
prod: www.xxx.com:8080/app_dev/
dev: www.xxx.com:8080/app_prod/
restapi
app_dev
node_modules
db.js
....
app_prod
node_modules
db.js
....
index.js
Idea 2:
prod: www.xxx.com:8080/app/
dev: www.xxx.com:8081/app/
Run as two services, listening to different port.
In my server, i just need to seperate two folder:
dev
prod
I can always change the code in dev and restart the app and never impact prod
If you want to deploy your application to a public web host, there might be a bit more steps to take. You can have a look at this guide, which proposes a deployment solution using Heroku.

Running AdonisJS on C9.io

I'm trying to create a new Node.JS app using the Adonis framework. I'm using c9.io as my IDE.
I've succesfully installed everything and created a new project. But when I run it (using npm run dev) c9.io keeps showing me the "No application seems to be running here!" message.
The .env file is set for port 3333 for default. But I also tried changing it to 8080, and nothing changed.
Anyone managed to run AdonisJS on c9.io before? Thank you!
Make sure you are running the app on 0.0.0.0, not localhost. Localhost won't work on Cloud9 so you'll need to use 0.0.0.0 or process.env.IP (which will usually be the same thing).
I created a github for installing AdonisJS on c9 for anyone using this Framework on Cloud 9, as I imagine more people will run into issues installing Adonis on c9.
https://github.com/Devitgg/Cloud9-Adonis-JS-Framework-Installer

How to run the seed.js, given in the generator-angular-fullstack, on OpenShift?

I'm developing an app, using MEAN.js and its generator (https://github.com/DaftMonk/generator-angular-fullstack), and Openshift as a hosting.
The project template of the generator includes a script (server/config/seed.js) to populate the database with two users.
In localhost, it is called automatically, but I also can call it using node server/config/seed.js (suppose you're on the root app directory).
The problem is, when I deploy it to Openshift, I run it and no error is reported, but the mongodb database is not updated. The exactly steps I do to run it on Openshift are:
Connect to ssh: ssh ....
cd app-root/runtime/repo/
`node server/config/seed.js``
What am I missing?
Thanks in advance.
You have few options:
in server/config/production.js add
seedDB: true
or change NODE_ENV to development coz during 1st deployment it is set to production
then
grunt
grunt:buildcontrol:openshift
should be working now

Resources