Node.js / VPS - page doesn't show update - node.js

I've signed up with Digital Ocean to start learning Node.js; I've got it all working but when I change this:
res.end('Hello World\n');
to this:
res.end('Something else\n');
and click refresh in FF and Chrome, they both still show 'Hello World', even if I clear the history.
However, when I click [Power Cycle] on my droplet in the Digital Ocean dashboard, the changes are shown when I refresh FF & Chrome.
Any ideas why?
Do I need to configure something on the VPS?
Thanks in advance...

Think I found the problem...
I was using a batch script called deploy.bat that:
copied any js file updates using WinSCP.exe
called a restart file with plink.exe to update the server
The restart file had these two lines:
sudo /sbin/stop www
sudo /sbin/start www
So I was trying to write the updates to my Node server whilst Node was still running (using ps aux | grep node to show running processes) and when I tried to refresh my browsers, the updates weren't showing.
If I opened Putty.exe and ran sudo /sbin/stop www then made changes to my server.js file and then ran sudo /sbin/start www, the updates showed up.
tl;dr - stop server -> update file(s) -> start server

Related

After stopping Apache service, why can I still get Apache default page on my IP address?

I want to install Nginx, but the port 80 has been taken up by Apache2. I stop it by:
$ sudo kill -9 my-apache-pid
$ sudo service apache2 stop
$ sudo /etc/init.d/apache2 stop
[ ok ] Stopping apache2 (via systemctl): apache2.service.
and I can install Nginx. I usesudo systemctl status nginx.
It shows working well and Apache2 seems inactive. But when I enter my IP address in the browser, it still shows Apache2 hello-page. Why?
I had the same problem. To my wonder clearing history and cookies of the browser worked.
Apache and Nginx home pages located in this directory:
/var/www/html
But there is a small point, and that is that each of these two apps that were installed earlier takes up the index.html file, and when you enter the address of localhost in the browser, that file actually opens.
As a result, all you have to do is go into this directory and see what the name of Nginx home file.you must do this in your terminal:
ls -l /var/www/html
that show index.nginx-debian.html name for Nginx html file, So if you search for this address in your browser:
localhost/index.nginx-debian.html
you can see the home page of Nginx.
all you need for show Nginx home page when search localhost is change then name of those two files.
Even after you remove apache2 completely, you will still have its "default site" files sitting in /var/www/
Run the following command and refresh the page.
mv /var/www/html/index.html index.html_bkp

I have Godaddy Shared Web Hosting I need to host node.js website can host site? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Anyone have an idea to host a site or reference for how to install a node server on Godaddy. We have Godaddy shared hosting which provides full Cpanel and looking to customize this shared hosting. What is the step to follow and Is we can able to customize this kind of hosting with editing in the Cpanel setting?
I had tried to host the Node.js website bu not able to get up and running it's shows the pure HTML coded website, not User Interface.
Yes this is possible. Somehow I have never seen anyone actually answer this question correctly. This works with the most basic shared hosting plans. I have successfully been able to set it up a couple different ways. I think the second is probably what you want :
1. cgi-node http://www.cgi-node.org/home
Basically this replaces PHP on the lamp stack. You can run javascript through node like you would run PHP. This has all the same functionality of node js but is only really geared towards template rendering.
<html>
<body>
<?
var helloWorld = 'Hello World!';
write(helloWorld + '<br/>');
?>
<?= helloWorld ?>
<br/>
<b>I can count to 10: </b>
<?
for (var index= 0; index <= 10; index++) write(index + ' ');
?>
<br/>
<b>Or even this: </b>
<?
for (var index= 0; index <= 10; index++) {
?>
<?= index ?>
<? } ?>
</body>
</html>
OR
2. Standalone Server (this works with NameCheap hosting and GoDaddy shared hosting)
In your shared hosting account you will need SSH in order to do this. So you may need to upgrade or request SSH access from their customer support. Download the latest NodeJS https://nodejs.org/en/download/. The shared hosting is probably in linux 64 bit. You can check this on linux or unix by running :
uname -a
Download the Linux binaries and put the bin/node (and the bin/npm file if you want to use npm on the server) file from the download in /home/username/bin/ (create the bin folder if it doesn't exist) on the server. Put permissions 755 on the node binary. So you should have a new file here :
/home/username/bin/node
Open up the .htaccess file in /home/username/public_html and add the following lines :
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
Create a file in /home/username/public_html and just call it app.js. Add the following lines in that file :
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
SSH into the server run these commands :
cd /home/username/public_html
which node # this should return ~/bin/node
node app.js & # This will create a background process with the server running
If you can get this set up right this will save you a ton of money in the long run as opposed to using something like AWS or Heroku etc.
Yes, this is possible on even the cheapest shared hosting tier. #nebulr instructions are correct. Here is a slightly updated and expanded version for noobs like me.
(1) Enable SSH on your shared hosting account:
• Log into your GoDaddy hosting and turn on SSH Access (on the Dashboard, it's in "Settings" on the bottom right). Take note of the cPanel login username and change the password if you don't remember it. Note that you also may need to create keys in CPanel, under "Security" and "SSH Access".
(2) Install the nodejs program itself:
• Download the Node.js binaries from https://nodejs.org/en/download/ Specifically you'll want the Linux x64 version (direct link https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xz)
• Unpack this .tar file on your computer and look inside for the bin folder (on a Mac you may need a program such as The Unarchiver to unpack it). The bin folder will have a file called "node" that's about 40Mb. This "node" file is the only thing we're going to use in this package.
• Using the CPanel File Manager or FTP program, create a folder on the server called "bin" in /home/yourUserName/ and give it permissions of 755. Note this is NOT inside public_html.
• Upload the "node" file to /home/yourusername/bin/
(3) Create a simple nodejs script:
• Open a text editor (like Sublime) and create a new file called "app.js" (or whatever):
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('NodeJS server running on Shared Hosting\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://${hostname}:${port}/');
});
Note that this is just the basic server app from https://nodejs.org/en/docs/guides/getting-started-guide/
• Open your CPanel File Manager or FTP program, and upload the app.js file to /home/yourusername/public_html/
(4) Modify the .htaccess file:
• Use your FTP program to add these lines to the .htaccess file:
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
Note that the .htaccess file is probably blank by default. Also note that you can use nano, vim, or emacs in SSH to edit the .htaccess file if you're brave or 1337.
(5) Start the node server:
• SSH into the godaddy server by opening Putty (Windows) or Terminal (Mac) and at the command line type: ssh username#website.com (where username is your hosting account's cPanel login)
The server should respond with username#website.com's password: which is where you enter the cPanel login password.
Note: If it's your first time SSH'ing to the server, you'll get a message: The authenticity of host 'X.X.X.X' can't be established. RSA key fingerprint is XXXX. Are you sure you want to continue connecting (yes/no)? Type yes and continue on.
• Navigate to /home/yourUserName/public_html/ by typing cd public_html. Start the node server script by typing: node app.js &
In a couple seconds you should see the message: Server running at http://127.0.0.1:3000/
(6) Check it out:
• Open a web browser and type your website's URL. You should get a white page with the text NodeJS server running on Shared Hosting or whatever message you put in line 9 of app.js above. Note that you can't use the IP address on a shared hosting account, you need to use the domain name.
I installed node on godaddy shared hosting with multiple domains by:
SSH to the godaddy server
Install nvm in the home folder
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
nvm install 10.14.0
(or the preferred node version)
Note: if you get nvm command not found when doing $nvm --version, just close and restart the SSH terminal
In the folder of the site you want to run the node app, add the .httaccess file
~/public_html/site folder/.htaccess
RewriteEngine on
RewriteRule (.*) http://localhost:3000/$1 [P,L]
note: .htaccess can be used to target individual folders and changes take effect immediately without having to restart the server.
Run your app.js or node server in the site folder as #nebulr indicated
$node app.js &
One other way to do this is via NVM. First get connected to your server using SSH. Then there's a curl command allowing you to install NVM through a bash script :
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
For me, two additional steps were necessary :
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Sources:
https://yoember.com/nodejs/the-best-way-to-install-node-js/
https://github.com/creationix/nvmub.com/creationix/nvm
As #nebuir states it is possible to run your node.js developed app using GoDaddy's shared host option because I just did it. The .htaccess portion is extremely key to making this work. The only other line I needed to add was
RewriteRule ^index.html.var$ http://www.yoursite.com:3000/$1 [L,P,QSA]
before
RewriteRule (.*) http://www.yoursite.com:3000/$1 [P,L]
You have two choices to host node app.
- Either buy your own VPS server from godaddy or digitalocean, etc..
- Godaddy itself provide the option to run their node instances. please refer https://in.godaddy.com/pro/one-click-installation/node-js

noVNC Multiple Localhost Servers

Ive got 4 dev VMs for four projects (all VMware Player VMs w/ubuntu 15.04 host) where each is running VNC (ports 5900, 5901, 5902, 5903) respectively.
I downloaded noVNC and saved to /var/www/html (my apache2 server on same host). Based on the ReadMe I then ran on my terminal
./utils/launch.sh --vnc localhost:5900
I received a missing websockify error, so downloaded it and placed it into the util folder. I then ran the same command and it worked! The terminal told me to Navigate to a url and sure enough I could control my VM.
However -- I'm wondering how can I use noVnc to access all 4 VM's? Is there some simple way to extend the port to a range like in iptables or firewalld?
./utils/launch.sh --vnc localhost:5900-5903
Okay, Ill answer for myself here in case it helps someone in the future...
First, create a token file where each line has a nickname, ip address, and port.
I created a file named token.list where each line looks like:
localhostnickname1: localhost:5900
localhostnickname2: localhost:5901
...
Then I use my terminal to go into the websockify folder so I can see the run file. I issue it the command:
./run --web /path/to/noVNC --target-config /path/to/token.list localhost:6080
Finally, I open my web browser and go to :
http://localhost:6080/vnc_auto.html?path=?token=localhostnickname1
Where localhost1 is the nickname of my first server on the first line of token.list
This link was my reference. If you want to serve this outside of localhost -- change the parameter localhost:8060 from localhost to an IP

Ubuntu-Apache: How to start automatically apache at boot?

I have zend server. So I tried to make an option in System->Preferences->Sessions
Added /usr/local/zend/bin/zendctl.sh.
Also tried /usr/local/zend/bin/zendctl.sh start-apache and /usr/local/zend/bin/sh zendctl.sh start-apache.
Nothing started.
The same is for MySQL - doesn't started also.
Any ideas how to make autorun for these?
remove your start up application entry which you stated above.
Below solution would be fine if you can start your zend server with:
sudo /usr/local/zend/bin/zendctl.sh start-apache
if your server can be started by the command, then process
open /etc/init.d/rc.local using your favorite text editor (with super-user privilege)
e.g.
sudo nano /etc/init.d/rc.local
append your start up command at the end of the file,then save & exit & reboot
/usr/local/zend/bin/zendctl.sh start-apache

Node.js, WebSocket Location Issue?

I created a server using Node listening on port 8000, localhost. Verified it's running properly, but I cannot access the WebSocket on the client (Chrome 5). Tried several implementations from various Git repos, node + websocket, socketIO, articles, etc. Nothing.
No port conflicts (sudo lsof -i tcp);
Tried server.listen(8000, "*");
Pointed to ws = new WebSocket("ws://:8000/test");
Debian Lenny, Apache22
Node v0.1.98-31-g1c6671a
I'm thinking there may be a conflict with url rewrite. Or possible permissions. Any ideas?
I had a similar issue on Ubuntu 10.04 LTS 32-bit and Chrome 5.0.375.125 and found out it is a bug in Chrome.
Here's how you can test and work around the problem. I used WebSocket with PHP and later Node.JS:
PHP: Download a tutorial file from http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ and extract it in a folder called 'socket' in your webroot. This folder now contains a readme.txt and the folders 'server' and 'client'. Now start the script in the console according to the readme (for me the command was: sudo php -q /var/www/socket/server/startDaemon.php ). It should print 'Start listening on Socket.' . Leave the console window open. Now go to localhost/socket/client/client.php in Chrome. It should say 'Socket Status 0' and if you look in the console you see no new messages (no connection was made).
Now here comes the trick: open a second Chrome tab. Point this tab to the same url: localhost/socket/client/client.php (It also says 'Socket Status 0'). And then close it again. Your original tab should now say 'Socket Status: 1 (open)' and in the console you see a handshake was made. WebSocket now works.
I repeated the same trick as above but this time using Node.JS with the Socket.IO script. The chat example included in Socket.IO-node ( github.com/LearnBoost/Socket.IO-node ) had the same issue, hanging at the 'Connecting...' stage. Opening a second tab to the chat box and then closing it again solved the problem and the chat box proceeded to load properly. The Node.JS server confirmed the connection in the console. From then on WebSocket worked fine.

Resources