How to run my express app on digital ocean droplet - node.js

I have a droplet on Digital Ocean and I spent quite some time setting it up by following their tutorials and all that. Then I SFTP'd my app to my server and it got transferred and everything then I ran sudo npm start and the console says that it's running but when I visit http://share3na.com as you can see nothing shows up. I installed all of the modules by running sudo npm install --save [module] then after manually doing all of them just to be sure, I ran sudo npm install.
I also changed the name servers in my domain.

In Digital Ocean,
Initially we buy a droplet. Later we have to choose the operating system to be installed, in your case you have chosen Linux maybe Ubuntu.
First, set your ssh access and configure ssh if needed
Later we have to update the package manager by using sudo apt-get update
Copy the NodeJs app into your droplet.
Set up the NodeJs by installing its dependencies and configuring the database etc.
Configure Nginx for the your NodeJs app and also add SSL certificate if any to the nginx.
Restart Nginx after configuring it.
Run your NodeJs app.
You should be able to access your NodeJs app if the Nginx is configured correctly.
Hope this helps.

Related

nodejs - install production build of reactjs app on offline server, then set up to run as a service

I installed the following package globally using
npm install -g serve
on my development PC which has internet. It is currently the only package installed globally on my PC.
https://github.com/vercel/serve#readme
After I compile and create a build folder, i run the following command to have the production version of code run using HTTPS.
serve -s build --listen 3000 --ssl-cert "/my/cert/server.crt" --ssl-key "/my/key/server.key"
Ok, so here comes the problem. I have to run my code on a server that is not connected to the internet. I've copied and moved the global node_modules folder over to the server and confirmed the permissions are the same between the server and dev pc. I also confirmed that the package is seen as installed on the server by:
npm list -g --depth=0
When I run the serve command above on the server, it has no clue what serve is. CentOS complains about
bash: serve not found...
So, I added an alias to the ./~bashrc file on the server (which I also had to do on my dev pc and reloaded:
vim ~/.bashrc
export PATH="$(npm bin -g):$PATH"
source ~/.bashrc
I found the export command in another SO post, and it worked on my dev pc, however linux still does not recognize the serve command. I also need to know how to determine how to run this command from within a service, which means I also need the absolute path of the "serve" command.
I'm kinda stuck on this, since all the articles online only talk about how to run "npm start" as a service, which I can do for a development build of software with no issues. I cannot find anything on how to set up a service for a production build.
I dont 100% need to use serve, but my other coworker is using it for his project, which has internet access.
I've even gone as far as trying to piece together the location of the main js file in the module:
/path/to/node_modules/serve/build/main.js -s build --listen 3000 --ssl-cert "/my/cert/server.crt" --ssl-key "/my/key/server.key"
This will allow me to start the service, but then I get all kinds of cross site scripting errors from my apache backend server.
Any help would, as always, be appreciated! Thanks!
Disregard,
When I ran npm install -g serve on the pc with internet, it created a soft link to the serve module. I discovered this by running the following on my dev pc:
which serve
I created that soft link on the offline server, and it appears to be working.

Install React JS on Cent OS

I am very very new to React JS and have tried to install it on my VPS server that is running Cent OS.
Node.js seems to be working,
I have build a React project using the following code as a root level user on SSH:
npx create-react-app my-react-project
cd my-react-project
npm start
but when trying to view in browser I get a blank page (instead of react js default template)?
I see many people install this locally but I haven't found any examples on a hosted VPS, is this something I am doing wrong?
Any help would be much appreciated, thanks!
if you have ssh access to your VPS, the rest is pretty the same as your local environment.
You can copy/paste your project to your CentOS host and use the following commands in order to run it:
cd your_project_folder_which_includes_package_json_file
npm install
npm start
Also, if you are using this server as a production host, You should consider getting a production build of your React app on your local env by running npm run build and then publishing the build folder on your server and serving it using a static file server or using a reverse-proxy such as Nginx as a static server.
Actually this page in React documentation does a good job in explaining the details of deploying a React app, I encourage you to take a look at it.

how to host angular and node js project on the shared hosting (cpanel or plesk)?

I have two different shared hosting on GoDaddy server (cPanel and Plesk odin), I want to host my NodeJS project there.
You CAN do this.
Most tutorials have you downloading node + npm through the command line... and since you cannot use sudo in terminal on a shared GoDaddy server you have to install nvm to install npm and node.
But really, you only need a single file from the Node.js package to get a server running. It is much easier to download the package, and manually add the one file to cPanel.
Follow this tutorial for the most simple method of running a Node.js server on GoDaddy hosting.
You can't host Node project on cpanel. You will need a node server for that. Cpanel does not come with node installed, neither it could be Installed. You can check with aws or blueocean for that.

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.

Resources