Deploy MEAN.JS on Google Cloud Platform - node.js

I tried to deploy MEAN.JS on Google Cloud Platform (also the KeystoneJS CMS), but it doesn't work. I use the command-line tool online.
In the order, i installed Node.js, MongoDB, bower and grunt, then I try to deploy MEAN.JS :
Install Node.js :
sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs nodejs-legacy
Install MongoDB :
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
sudo apt-get install -y mongodb-org
Install bower et Grunt :
sudo npm install -g bower
sudo npm install -g grunt-cli
Install MEAN.js :
sudo npm install -g generator-meanjs
mkdir mean
cd mean
yo meanjs
grunt
Here is the result on the command-line board :
Running "jshint:all" (jshint) task
53 files lint free.
Running "csslint:all" (csslint) task
2 files lint free.
Running "concurrent:default" (concurrent) task
Running "watch" task
Running "nodemon:dev" (nodemon) task
Waiting...
[nodemon] v1.2.1
[nodemon] to restart at any time, enter rs
[nodemon] watching: app/views//. gruntfile.js server.js config//.js app/*/*.j
s
[nodemon] starting node --debug server.js
debugger listening on port 5858
NODE_ENV is not defined! Using default development environment
js-bson: Failed to load c++ bson extension, using pure JS version
Failed to load c++ bson extension, using pure JS version
MEAN.JS application started on port 3000
I can define the NODE_ENV variable (test, development, all...) but it's the same problem.
The problem is :
It should work but whan i try to access to my IP:port (in this case 146.148.113.68:3000) : "This webpage is not available".
Is it a problem with the VM, the packages, MEAN.JS ? I have the same problem with the KeystoneJS CMS.
Thanks !

Are you sure ports to your machine are open for access? When you deploy an app on Compute Engine, you have to edit network settings to allow custom ports. There's easy checkmark options for allowing HTTP and HTTPs traffic, but for custom ports, you will have to add port in settings.
This documentation might be helpful but you can always find these things in the Cloud Console.
Also, now Google Cloud Launcher also supports MEAN stack deployment with both MEAN.io and MEAN.js flavors which simplifies the whole process.

Everything looks fine Only thing to change is the firewall settings which is blocking your web application ,there is no problem with VM or KeystoneJS CMS. You Need to just change the firewall settings for incoming and Outing Traffic:
You need to specify the ip and port number , i have given access to all ports temporarily for testing purpose.

Try other services to host your MEANJS Web Application.
Heroku : https://www.heroku.com/
Nodejitsu : https://www.nodejitsu.com/
If it works in these platoforms,then the problem is not with your cloud.

Related

How to apply Node.js secuirty updates?

How to apply node.js security patches?
Is there a specific process to apply security patches when using meteor js on ubuntu 16.04?
When you're running meteor in production mode, it is run as a (pure) node.js app. So the short answer to your question is to just update node (depending on how you installed it; probably sudo apt-get update -y && sudo apt-get install nodejs -y).
There are a variety of tools you can use to deploy a meteor app (e.g. meteor-up), but all of them have essentially the same two steps, which are easy enough to do yourself:
Bundle your meteor app into a node.js app
meteor build ../my-build-output-folder --server https://my.production.site.url --architecture os.linux.x86_64
This will create a meteor-server.tar.gz file in the folder you specified, containing the node.js app. The process is then (as per the README file that is included in the bundle):
Transfer the meteor-server.tar.gz file to your server
tar -zxvf meteor-server.tar.gz to extract the node application
The included README file tells you the rest :
README:
This is a Meteor application bundle. It has only one external dependency:
Node.js v8.11.4. To run the application:
$ (cd programs/server && npm install)
$ export MONGO_URL='mongodb://user:password#host:port/databasename'
$ export ROOT_URL='http://example.com'
$ export MAIL_URL='smtp://user:password#mailhost:port/'
$ node main.js
Use the PORT environment variable to set the port where the
application will listen. The default is 80, but that will require
root on most systems.
Set up a system to survive restarts e.g. upstart , pm2, supervisord , or docker

Nodejs or node returns nothing on Ubuntu

I've built a Javascript app running on Node within my MacOS environment, and everything works great. Now I've created an Azure Ubuntu server, rsync'd the source from my machine.
I've duplicated the app requirements by installing npm, node, and all the packages required. I SSH into the server and when I run the app from the Ubuntu server via
$node app.js
All that is returned is
$
Reading that Ubuntu uses nodejs-legacy, i've also tried
$nodejs app.js
Same result
$node -v
v4.7.2
I've also built a package.json file and when executing with
npm start
it immediately returns back to $.
The reason why it wasn't working is the default APT repository that is called when installing nodejs on Ubuntu is outdated. I ran the following to code to fix the problem. It automatically uninstalls all the other incorrect packages, sets the correct repository, and re-installs.
# Sets up the correct APT repository hosted by NodeSource, and adds the PGP key to the system's APT keychain
$ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
# Installs Node.js
$ sudo apt-get install -y nodejs
# Updates NPM
$ sudo npm install npm --global
All apps work as intended now!

Deploying my node.js app from Github to a VPS

I have an node.js application on Github. I have never done any VPS deployment before and I am learning on the go.
I am using the VPS by Hostinger.in, the OS being used is Ubuntu 14.04. So far this is what I have done:
Connected to their SSH successfully from my Terminal
Installed node.js on the server [https://www.hostinger.com/tutorials/vps/how-to-install-node-js-on-ubuntu]
Installed Git on the server [https://www.hostinger.com/tutorials/how-to-install-git-on-ubuntu]
I could not find any online resources for deploying my node.js to Hostinger VPS so I am following the ones written for DigitialOcean.
The one tutorial I followed is this: https://code.tutsplus.com/tutorials/setting-up-continuous-integration-continuous-deployment-with-jenkins--cms-21511
I cloned my repository doing:
git clone https://github.com/myusername/node-project.git
and it seems it was deployed (didnt give me any errors).
All the installations I did on the server I did as the root/admin user. So far I have not created any separate user to perform any of these tasks.
The server hostname given to me is dangerous-pigs.com. Now I am assuming my node.js application is deployed, but when I go to dangerous-pigs.com it shows me server not found error.
I also installed forever for my node app and when I run
forever start app.js
it says:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
error: Cannot start forever
error: script /root/app.js does not exist.
Which means the app is either not installed or installed somewhere other than the root folder.
There is a lot going on and I am confused where to start fixing issues.
How can I deploy the app to running it on the dangerous-pigs.com?
Update
So it seems I have to go inside the project folder in root and do the
npm install --production
after which I did
node app.js
The server seems to be running but, I can only access my application if I do to the actual IP provided by the service.
So if I type http://93.188.163.249:8000 --> that's my application.
How do I change it to point to a domain?
After some more research this is what I found:
Currently by default Apache2 runs on port 80. To run nodejs on port 80 first I need to install libcap2-bin in my Ubuntu server by doing:
sudo apt-get install libcap2-bin
after which I do
sudo setcap cap_net_bind_service=+ep /usr/bin/nodejs
the above command works if you have a mac, for windows the command maybe
sudo setcap cap_net_bind_service=+ep /usr/local/bin/node
but please confirm before doing it.
Also your nodejs server needs to be stopped before you make these changes else it will not work. In my case I had forgotten to stop my node server and kept running the sudo setcap command but it did not changed the port (for obvious reasons).
If you are using forever to run node then do:
forever stopall

React: How to publish page on server using React-starter-kit

So, I created a page using repo from:
https://github.com/kriasoft/react-starter-kit
I have my own global ftp server. And now I would like to publish my project on server.
What is the best way to do it. Should I copy all files to ftp server and just exec command 'npm start'? Or maybe I should deploy it?
I'm new in web deployment and not sure how it works.
Thanks for any tips.
It may be a little more complicated than just ftping your project up. Here are the instructions I use to setup a server at digitalocean.
sudo apt-get update
sudo apt-get install npm
sudo apt-get install git
sudo apt-get install ufw
sudo apt-get install build-essential libssl-dev
curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
nvm install stable //may require new ssh session before this
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80/tcp
ufw allow out to any port 53
ufw enable
sudo npm install -g forever
sudo npm install -g node-gyp
cd /var
mkdir www
cd www
git clone https://github.com/calitek/palminfo --recursive
npm install
npm ls -depth 0
export PORT=80
node js/server.js
test using ip / when good exit then
forever start js/server.js
set dns
The server will need to support you adding node.js. Then you need to preferably use github to clone the project. You will want to do the npm install on the server to be sure you are using the correct modules. Its a little complicated the first time out, just keep good notes for the next time.
This is a big topic to cover, whatever your linux distribution is, you probably will always need 2 servers - a proxy server, and an app server.
I'd recommend you wrap your compiled sites with a simple Node.js server like Express or Hapi. Then configure nginx to properly route all the requests to the application. Checkout some detailed guides from DigitalOcean
CentOS: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7
Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04
Minor steps might be slightly different but you get the idea
One approach is to build the app on the server, but you will need something like Heroku which you already decided to go with.
But an alternative to that is to do a paradigm shift. You're app is basicaly just some html, css and javascript, which is compiled and servered to the public. You could compile that yourself with npm run build and then just copy the compiled files to your hosting server which can use what ever server it wants: apache httpd, nginx, etc. This is also cheaper cause you only need basic hosting, not some complex nodejs compiling server.
I created a starter kit http://redux-minimal.js.org/ which helps you create rich real-world apps with the minimum amount of packages and very light configuration setup. If you look at the folder structure you can see that the app uses the index.html from the public folder, and then the css and js file are compiled directly in the public folder. Which makes it easier to just copy the public folder to what ever server you want.

Node.js Koa app and CouchDB in single container

I have a koa.js app which I want to run in a docker container. This loa app requires couchdb to run, which I want to ship in the same container. I know this is not best practise but it is indeed the best way for my users to get started.
Dockerfile:
# DOCKER-VERSION 1.2.0
FROM centos:centos6
# Enable EPEL for CouchDB and Node.js
RUN yum -y update; yum clean all
RUN yum -y install epel-release; yum clean all
RUN yum -y install tar
# Install Node.js and CouchDB
RUN yum -y install couchdb; yum clean all
RUN service couchdb start
RUN yum install -y nodejs
RUN yum install -y npm
# Bundle app source
ADD . .
# Install app dependencies
RUN npm install
RUN npm install -g n
RUN n 0.11.12
# Expose port and run
EXPOSE 8080
CMD ["npm", "start"]
which works fine, app gets launched but it can't connect to the couchdb. throwing in a
RUN service couchdb start
response with OK, so it seems to work, but
curl -X GET 127.0.0.1:5984
response with
curl: (7) couldn't connect to host
same for the koa.js app:
error: error stack=Error: connect ECONNREFUSED
at exports._errnoException (util.js:745:11)
at Object.afterConnect [as oncomplete] (net.js:995:19), code=ECONNREFUSED, errno=ECONNREFUSED, syscall=connect
someone knows what I am missing or what I am doing wrong?
The only command that is run when you start this image is what is in the CMD line. Every line before that creates a read only, non-running image. Thus, the line RUN service couchdb start will start the service for an instant, until it is marked as successful, then docker will stop that image, save it, and move on to the next line. The "running" state of the service doesn't persist.
It is a common misconception, and one I fell into when I started.
The three options, top being fastest yet most hacky, and last being most work but most proper:
Put service couchdb start && npm start as your CMD line.
Create a startup.sh script, do all the "running" that you need to do in there, and call that as your CMD line
Use a service designed to do this stuff for you, supervisord is often recommended.
This is a common issue, so if you have a read through google results searching for "start service in docker" you'll see more information around the subject.

Resources