Installing Node.js application on CentOS [closed] - javascript

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have OpenVZ VPS with pre-installed CentOS 6.2 blank (32-bit) and domain name mysite.com. I would like to install Node.js and my javascript application on CentOS that in result should be accessible at mysite.com. So, what steps should I do to implement this? Please, describe process from zero in details. I'm Java developer and have never worked with unix systems :(
Best Regards

"Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications" outside of the browser.
Installing node it self on a system is pretty straight forward. You can simple go to http://nodejs.org and download the correct binary corresponding to your system, or download the source.tar.gz and compile it yourself.
There is a set of installation instructions under the wiki at github.com that will take you through the steps of compiling node on the big three platforms.
I usually will add a prefix when I'm compile so the binaries get moved into the directory I want to store them in. This can be a local directory (ex. $HOME/bin) of the user you want node to be run under or you can make it a system directory where node will be accessible by every user on the system.
$ tar -zxf node-v0.10.3.tar.gz #Download this from nodejs.org
$ cd node-v0.10.3
$ ./configure --prefix=/usr/local/bin
$ make
$ sudo make install
You should now have node and npm installed on the system. From here you can check to make sure everything is working.
$ which node
$ /usr/local/bin/node
$ node -v
$ 0.10.3
$ which npm
$ /usr/local/bin/npm
$ npm -v
$ 1.2.11
With node you will be building your server from the ground up. There are a lot of libraries to help you do this. The main built in one would be http which is built on top of net.
To get your application accessible from your domain you simply need to start your node application and tell it to listen on a port (assuming http, or some networked app). On the linux side you also need to make sure what ever port your node process is listing on is open so connections can be made to your server. The most common firewall on linux system is Iptables.
I'm not a Java developer so I don't know the normal work flow for deploying a Java applet is or how its served so I can't help you bridge the cap there.
This should get you started.

Related

How to restart a Node.js project?

I'm not a Node.js developer. So I have no idea how it works. I've been a PHP developer for over 8 years.
Because of some reason, I need to make a small change in a Node.js project which is live. All I have to do is changing a payment gateway token. I did it like this:
After pulling it on the server, users still go to the old payment gateway. So I guess I need to do a restart. (I'm saying so because, for PHP projects, when you change a config-related thing, you need to restart PHP).
Not sure should I restart what thing? Noted that, the server is Ubuntu 20.04 and uses Nginx to talk to Node.js. In other word, how can I see Node is running as what service on Linux?
Also, there are two files that I think I need to run the project again after restarting Node through one of them: index.js, server.js. Am I right?
And
Your Node.js script likely runs under a process that restarts the script in case it dies. There are several "run forever" wrappers, the most popular one is pm2. Find out which one is used in your project. Try pm2 list as the user your project executes under. If pm2 type pm2 restart app_name to restart your project.
Please check if it is a node.js project so you can write the command node index.js or node server.js with this command you can start your node server.

What is best practice for executing Node.js app in production on a WINDOWS machine?

I have a Node.js application that I require to run on Windows Server. For the development process, we execute the app simply through command-line or PowerShell using the standard command:
node index.js
What is the best practice and most performant way of running this application on Windows permanently? Or is running it through CMD or PowerShell as we already doing the best way? If so, is either PS or CMD better?
Running as a service is a no-brainer, but the question still remains even if the application is 'servicified', as launching a Node app as a service still requires the specification of a shell through which to execute, such as PowerShell or CMD. Is there another shell we should use? Or is there a way to not use a shell at all?
Please advise as it surprisingly doesn't seem like there's any standardized advice anywhere on the internet; which begs the question also: is simply no one out there running production Node.js apps on Windows perhaps..?
I suggest using a package such as pm2.
PM2 runs your process in the background. Meaning that you can exit your terminal, and your app will continue to run. Only way to turn it off is:
1: Shutting down your server
2: pm2 stop ProcessName

Bot shuts down when putty window is closed

I created a discord bot and am now attempting to run it off an Ubuntu Machine.
I installed the folders of the bot and NodeJs, here is what I used to install NodeJS:
sudo apt-get install -y nodejs
Then I used cd to select the directory, and started my bot using node index.js
The bot started, however when I went to close the putty and keep it running on the VPS the bot shutdown. Here is what the directory looks like.
I think the problem is that when you start the app in the putty window, that process is linked to the window and gets terminated when that is closed.
To avoid that you can use a host service like screen, tmux, nohup, bg and so on...
If you want to know which is the best, try looking at this question from the askUbuntu Stack Exchange.
The key concept is that you open a new window using the tmux command (or screen, ...), then run your bot like you always do. When you want to leave but keep the process runing, you can detach the session with a key combination, that changes from service to service.
If you want to access that window again, you can run a command that will "restore" your session, like
tmux list-sessions
tmux attach-session -t 0
The NodeJS instance is terminated when putty is closed. You need something to keep the instance alive. Try:
PM2: http://pm2.keymetrics.io/
or,
Forever: https://github.com/foreverjs/forever#readme
Recommended though is to run the node instance as a service that can reboot on startup. Try looking at this:
https://stackoverflow.com/a/29042953/7739392
The shell runs in the foreground. This means any scripts you start there will end once you end your session. A simple solution would be to run your script in the background by adding the & after the call:
node index.js &
A better solution would be to create a service you can ask the service daemon to run for you. However, adding the & should get you what you want for now.
I recommend using one of these two node modules - ForeverJS or PM2. I'll show you how to quickly get started with ForeverJS but PM2 would be very similar.
You can easily install ForeverJS by typing the following in your terminal:
$ npm install forever -g
You may need to use SUDO depending on your user's privileges to get this working properly. It is NOT recommended to use it in production due to the security risks.
Once installed CD to your projects file directory and like you typed 'node index.js' you will do something similar with ForeverJS.
$ forever start index.js
Now when you exit the terminal your NodeJS application will remain as a running process.

Install node.js at Shared Dreamhost

Im trying to install node.js to shared dreamhost. I have followed the directions for Dan dean here http://dandean.com/nodejs-on-dreamhost-shared-server/
but when I go test,
$ node --version
Then it says:
-bash: node: command not found
I follow the step by step, this is the correct way to install the node in the shared server?
Dreamhost has supported Node.js on shared host recently.
I put the following source code in app.js and it works.
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("Hello World!\n");
});
server.listen(3000);
But it seems to be unstable when I try to restart it.
Check https://help.dreamhost.com/hc/en-us/articles/217185397-Node-js-overview for details.
You may want to look into nvm when installing Node somewhere else than your local environment.
Check out my blog post on nvm:
http://mycodesmells.com/post/node-version-management/
Sometimes, installing node.js on certain linux instances creates the command nodejs instead of node. Try nodejs --version. If you want to be able to use node as the command, simply create a simlink to /usr/local/bin/nodejs for /usr/local/bin/node.
Currently DreamHost says “Node.js can be installed onto Shared, VPS, and Dedicated Servers.” However, for shared servers, currently Node is only supported using Passenger, and the version of Passenger “running on DreamHost servers does not currently function with Node.js versions 14+” as of March 1, 2022. They have not shared a target date for allowing newer versions of Node.
For more instructions, and up-to-date compatibility information, see their support documentation on Node.js at Dreamhost.
It may not have been the case at the time this question was posted, but Dreamhost does not allow compiling or running nodejs on shared hosting.
Can Node.js be used on shared servers?
No. DreamHost does not support Node.js on shared web servers, as the security setup on DreamHost shared servers is incompatible with compiling or running Node.js.
Furthermore, Dreamhost claims they will ban users who attempt to do so:
If you try to compile Node.js on one of the shared web servers, your user will automatically be banned through grsec (taking down all the PHP websites that run under that user) and the server will have to be rebooted before your user can be unbanned. If you do it one more time, you will be forced to move to a VPS.
I've had Dreamhost shared hosting for nearly 10 years, but they don't allow long-running processes. This also prevents common tools like composer from running if they take more than a few seconds. I recently moved to a VPS because of this.
https://help.dreamhost.com/hc/en-us/articles/217185397-Node-js-overview

Installing/setting up Socket.IO on my server

Ok so I have read through the Socket.IO docs and I am still a little unsure of a couple of points:
The documentation says...
To run the demo, execute the following:
git clone git://github.com/LearnBoost/Socket.IO-node.git socket.io
cd socket.io/example/
sudo node server.js
Now I don't know what this means at all! I think it may be command line interface. I of course have access to this on my localhost, but my online hosting package is a shared LAMP setup. Meaning I don't have access to the root command line (i think).
How do I actually setup socket.IO, is it impossible on my shared server package?
Appreciate any help...
W.
If you aren't familiar with node.js or with basic command line usage then I would suggest that you use a hosted WebSockets solution like pusherapp. Trying to learn WebSockets, and Node.js, and the Linux command line all at once is going to lead to a lot of frustration. Take a look a pusherapp's quick start guide, it's very easy to get started. You can have 5 simultaneous connections with a single application for free (I'm not affiliated with pusherapp).
Updated (with inline answers to questions):
If you are going to go the direction of running a Socket.IO application:
You don't technically need git since you can download node.js and Socket.IO from their respective download links on github.
You don't actually need a LAMP server to use Socket.IO. By default Socket.IO functions as a simple webserver in addition to a WebSockets server. If you want server side scripting then you might want Apache with mod_php, mod_python, etc.
You don't technically need a dedicated server or even root access. You do need a system where you can have long running process. And if you want the service to start automatically when the system is rebooted, you probably want to add a startup file to /etc/init.d, /etc/rc.d which will require root access. Both node.js and Socket.IO can be installed and run from a normal home directory. If you want to run Socket.IO on a standard port like 80 or 443 then you will need to run it with root privilege.
Node.JS scales quite well so Socket.IO will probably scale pretty well too.
It's not a simple matter to get everything setup and working, but if your goal is a free solution for web serving+WebSockets then Socket.IO is probably is good route to at least explore if you are brave.
First you'll have to determine if your host supports SSH. Sometimes they don't by default on shared hosting, but if you ask they can turn it on. If it does you'll use some sort of SSH client to connect to it. Putty for windows is the most common. Then you'll use git, which is a source control program. Which you'll probably have to install on your host, which may or may not be allowed. If you can, this can be accomplished a number of ways, you'll want to read the git documentation, it will depend largely on what linux distribution you're running. CD is change directory, basic command line stuff. sudo on the last line is telling the system to run the command as root, which it will ask you the password for, which you may not have access to on your host. Sounds like you're gonna have an uphill battle on shared hosting. You may want to opt for a VPS instead.
If your shared host is a LAMP system with no command line access you're not going to get very far with Socket.IO. The instructions you posted assume you have command line access and that you've installed the node.js runtime on your system.
If you really want to try this I recommend you get a VPS of your own (I use prgmr.com) to test it out. For what it's worth I found the Socket.IO platform pretty nice to use once I got it up and running.

Categories