Deploying an app made on MEAN stack to Nginx on docker - node.js

I am currently learning about docker and have read a few conflicting articles about the process so thought I would ask the oracles here for answers.
Having setup my app I use Node as the server, which handles the routing and API connection to Mongo just fine on my localhost, a few articles have said that you do not use Node on production so one of my friends followed a tutorial that introduces nginx to the equation
While his app loads the static content, he has lost the dynamic side of things, items in the database are not loading in for example.
First question is, does nginx take over from Node or add to it in these instances?
To me it seems that node is still involved to serve the content so express and mongo should also still work and my hope is theres some config that we haven't found that's the blocker.
Second question is, if my assumption is correct, and nginx is simply being introduced as well as Node, is it the server, is node the server or am I missing the relationship between them somehow. The tutorials and articles usually refer to nginx as the server hence the confusion.
My end goal is to work out how to use my Linode lab, setup docker and then have my angular app running in a publicly accessible way. I think I have the docker side of things worked out but my head isn't figuring out how things work after that with node and nginx - especially when my previous experience is with regular old servers with php and sql.

Related

Deploying Next.js to Apache server

I've been developing a Next.js website locally and now want to set it up on my Apache server (with cPanel). However, I'm very new to Next.js and Node apps and not too sure how to go about it.
Has anyone done this successfully? Can you list the required steps and what files should be on the server?
Also, can this be done on a subdomain?
Thank you!
To start with some clear terms just so we're on the same page, there are two or three very different things people mean when they say "server":
A Server Machine is a computer that is connected to the internet that you intend to use to serve something to people on the internet.
A Server Program is some software you run on your Server Machine. The job of the Server Program is to actually calculate the responses to various requests.
A Server as a Service is a webapp provided by a company that stores your code and then puts it onto Server Machines with the right Server Program as needed.
While we're here, let's also define:
A Programming Language is the language your website is written in. Some sites have no language (and are just raw HTML/CSS files that are meant to be returned directly to the user). Many sites, though, have some code that should be run on the server and then the result of that code should be returned to the user.
In your case, you have a Machine whose condition we don't know other than that it is running the Program Apache (or probably "Apache HTTP Server"). Apache HTTP server is very old and proven and pretty good at serving raw files back to users. It can also run some Programming Languages like PHP and return the result.
However, Next.JS is built on top of the Programming Language Javascript, which Apache does not have the ability to run. Next.JS instead wants its Server Program to be Node.
So the problem here is basically that you have a hammer, but only screws. You can't use the tool you have, Apache, to solve the problem you need solved, running Node code and returning the result. To get around this you have two options:
First, you can find a way to access the Server Machine that is currently running Apache and tell it, instead, to run Node pointed at your Next.JS code whenever it starts up. This might not be possible, depending on who owns this machine and how they've set it up.
Second, and probably easier, is to abandon this Machine and instead use a Server as a Service. Heroku, AWS, and Netlify all support Next.JS and have a free tier. The easiest solution, though, is probably to just deploy it on Vercel, which is a Server as a Service run by the same team that makes Next.JS and which has a very generous free tier for you to get started with.
The good news, though, is that yes next.js does totally support being hosted from a subdomain.
Next.JS allows you to build fully functional Node Applications, as well as simple statically-generated sites like Jeckyl or Docpad. If your use case is a simple statically generated site look here: https://nextjs.org/docs/advanced-features/static-html-export
In particular the next build && next export command will create all the HTML and assets necessary to host a site directly via an HTTP server like Apache or Ngnix. Contents will be outputed to an out directory that could serve as the server root.
Pay very close attention to what features are not supported via this approach.

Vue.js on local, NGINX docker container, how to save files with Node?

I apologize if this is a general question, but apparently what I'm googling doesn't make sense, and I don't have anyone at my work I can ask.
Here's my situation:
Simple Vue.js app created with the Vue CLI 3.5, with an <input type="file">. Nothing hooked up yet when the form submits.
Docker container with Node and NGINX that pulls in and builds my Vue app - this is working. I basically copied the Dockerfile right from the vue.js site.
Now I need to store the file from the web app (and eventually FTP it to another server).
It was suggested I use Node, but I'm new to all the server-side stuff, so I don't know how to do that. My container has both Node and NGINX in it running the app, so can I stick with one container? And how do I build locally for development and then transfer that to the Docker image?
I'm just looking for pointers/articles/tutorials to help me think about this the right way.
Attention opinion:
In my experience NGINX is more of a router managing traffic.
Its in the name of Node to make a single one per task. So () i think its uesfull to split those.
Node is similar to "frotnend" except you have access to common system apis - wrapped by node.
But basically like you consume rest or graphql apis you can do with node.
Best is to simply peek into the basics you most likely gonna need:
file system, stream,...
https://nodejs.org/api/

MEAN Stack using Docker containers

New to this...
I'm trying to understand if a modern MEAN app should be deployed with 3 or 2 Docker containers:
Option 1: Express Server as container + Mongo DB as container
Option 2: All three as separate Docker containers
The second option sounds like the appropriate path so you can update any part of the stack without taking down other components if you don't want to. But then the question is does the ng app container need it's own server to serve the ng app files. I'm seeing some examples on Github where they are running the ng app with ng serve -H 0.0.0.0 from the Docker container which from my understanding is a no-no because that's not a prod ready server, just webpacks dev server.
To me, if you run all three separately then you actually need two servers, one to server the ng app (index.html, js, css, etc.) and the other to sever the backend app, the API.
The advantage I see if you run the Express Sever + ng app in one container then you can serve the initial index.html with ng app dependencies AND the API but then they both go down when they get updated.
What's the best practice here?
IMHO 2 Containers seems like the better solution with one for Mongo and one for Express. Anytime you're pushing new code, it doesn't make sense to have a front end still up if back end is down or vice versa. Also serving front end files from the same server reduces headaches of dealing with CSRF.
Regarding your other question, I think you can deploy your front end to something like AWS S3 and still only manage one server for your backend.
on a side note, you could also do it all in one container. It really depends on your other requirements to figure out the best architecture.

Where to run node.js

So I thought about giving node.js a try seeing the possibilities it has for a little test chat project (with mysql) I'm doing.
But what I couldn't find out is where to run the file from and whats most common.
What I currently have:
A FreeBSD server with latest Node and PHP 5.3.x
A vhost
some tutorials on how to start with node (which I looked through and got exited about)
knowledge on how to run it from terminal without having to keep my terminal open (screen)
So far so good.
What I need:
Some basic information of where to put the (lets say:) chat.js file.
Most logical port to run it on
So the web root (www) runs on a user (not root obviously). And the webroot has an underlying folder where I could put the script (away from visitors grabby little hands). This seems to me to be the most safe place to put it seeing nobody can get to it, which is probably what I want seeing I'm going to connect to a db and don't want my DB login data out there (I don't know how this works yet but I'll figure out db connect with node later, no answer required).
But if a file is not in the webroot, it seems to me a connection cannot be made from outside. Cause my webroot is configured to only allow 80 (or ssl on 443) incomming traffic, which is logical. Outgoing obviously has no problems.
All the examples that I found don't really help me. They just do everything on a local machine, which sucks for me cause I don't want to do that.
So what I would like to is the best practice for:
Where to put the file
port to run it on.
What is Node.js?
A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help.
An important thing to realize is that Node is not a webserver. By itself it doesn't do anything. It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is simply a JavaScript runtime.
A nice tutorial How to Deploy Node JS Applications, With Examples
You'll need to have your non-node application on port 9000 (for
Apache, this will be in /etc/apache2/ports.conf and in your
sites-available file for your site), and you'll need your node
application to listen on 8080. You'll also need to set up DNS 'A'
records for the different hostnames you'll be using for your servers.
Companies like Heroku allow for automated deployment of apps from the desktop to the cloud.
Nodejitsu provides a tool called jitsu that makes deploying an Node.js application super simple. You can install jitsu with npm.
npm install jitsu -g
Heroku How To
Getting started with jitsu
Use monit, forever, upstart or systemd to start your node server. Use Varnish or HAProxy or Nginx (Nginx not work with websockets).
Ultimately you can stick it anywhere you want. I recommend running your application using Forever or similar instead of directly with Node. I usually keep my apps in /var/ and let each one run under a unique user. I do not recommend keeping them in your http root, as your .js files are should NOT be interpreted by Apache, php, etc.
To be clear - you do NOT need a traditional webserver, nor do you need php,mySQL or anything else. Node is all you need. It'll serve content directly - it IS the webserver.
Often times you'll have each app use a high port number (3000+) and use NGINX to proxy them all to different hostnames off of port 80 (allowing you to easily have multiple apps on a single machine). If you aren't using some sort of proxy, then 3000 is very default, but there is no correct or incorrect port, so long as you don't use a reserved port.

Node equivalent to index.html

I'm coming from a LAMP background and want to test out node for production.
What seems a little confusing to me is that in Apache url's map to folders, and the server will automatically look for an index.html or index.php if you aren't re-writing urls.
What would the equivalent be in node?
I'm thinking it'd be something like checking the request url and matching it, then loading a specific node module which runs the app.
This might seems simple for a single app, but we run tons of client apps on our server so i'm used to having different frameworks in different folders and index.php just runs it.
to be more specific. i'm currently running a few codeigniter and wordpress installations on our server. so i'd want to run a few node apps/frameworkds in different 'subfolders'
With Node you don't really use Apache. It's similar to Ruby in that is runs its own web server.
However, you can probably get Apache to run Node files using mod_node. As far as I know this is non-standard though and you definitely lose the "non-blocking" advantages of Node. But for experimenting (and not load testing) it's fine.
Check out Express if you're looking for an MVC architecture written in Node.
If you're just looking to run the most basic sample web server, just run the example hosted on the main page: http://nodejs.org/
Lastly, I had that same issue you are experiencing where I have one box hosting a lost of stuff and Apache taking up port 80. The answer here is to use a reverse proxy like Nginx to run on port 80 and redirect traffic to Apache / Node / Ruby / etc. Best of both worlds and since Nginx is written non-blocking you still gain the benefits of node.
I actually wrote an in-depth blog article about this a few months ago:
http://readystate4.com/2011/07/15/nginx-apache-and-node-all-living-harmony/

Resources