logico / vestacp-nodejs

Run automatically Node.JS apps in VestaCP.
114 stars 68 forks source link

app.sock not found #10

Open andersondn opened 4 years ago

andersondn commented 4 years ago

Hola amigo!

The file app.sock is not being created, how can I fix this?

timeignacios commented 4 years ago

Buenas tardes @andersondn El archivo app.sock es creado por node a momento de intentar conectarse a él. Cuando lo creas debes darle los permisos necesarios para que nginx pueda interactuar con él. Sólo pone la ruta al unix socket en lugar del puerto de conexión, y Node lo creará.

beird commented 4 years ago

I think this is related to user/ folder permissions. i have made some tests I have made below post (under hestiacp) https://forum.hestiacp.com/t/node-js-support/653/9

iamkbkarthikeyan commented 3 years ago

Hi there, I was completed all steps. and create a nodejs with express ejs basic app. and I just directly uploaded the file into my vesta's website directory. If I hit the domain, I got 500 error. also, I don't have the .sock file. can you please explain the .sock part only. how to create the .sock file or what we need to do in our node app.

Thanks, Karthikeyan

nchsala commented 3 years ago

@iamkbkarthikeyan If you put a listen like this: http.listen('app.sock', callback); Node will create the app.sock file for you.

Maybe you need some extra code for service restart cases, because the socket file needs to be removed and re-created for every NodeJS restart.

The following code detects errors when creating the server. Delete and recreate the socket file

  http.on('error', function (e) {
    if (e.code == 'EADDRINUSE') {
        var clientSocket = new net.Socket();
        clientSocket.on('error', function(e) { // handle error trying to talk to server
            if (e.code == 'ECONNREFUSED') {  // No other server listening
                fs.unlinkSync('app.sock');
                http.listen('app.sock', callback);
            }
        });
        clientSocket.connect({path: 'app.sock'}, function() { 
            console.log('Server running, giving up...');
            process.exit();
        });
    }
  });

Good luck !!

iamkbkarthikeyan commented 3 years ago

So, we can't use express.js instead of express we're using http server right? or is there any possibility to run express app.

nchsala commented 3 years ago

@iamkbkarthikeyan With something like

const express = require('express');
const app = express();
const http = require('http').Server(app);

You can use the express app object and all the http events without problems

iamkbkarthikeyan commented 3 years ago

Hi, can you please create a simple node app for demonstration and push into git repo. Also attach the link into this repo's readme file. It might helpful for everyone. Still I can't deploy it. Getting same errors.

Thanks, Karthikeyan

aligoren commented 3 years ago

In our example you can use express like that;

const express = require('express')
const app = express()
const port = '/home/admin/web/YOUR_DOMAIN.COM/nodeapp/app.sock';
const hostname = 'YOUR_HOST_NAME';

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/about', (req, res) => {
  res.send({ info: 'APP test server'  });
})

app.listen(port, hostname, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Then run this command;

pm2 start app.js

Don't forget to chmod 777 "$(pwd)/app.sock" command after started server.

I have a bash script like that;

/home/admin/web/YOUR_DOMAIN.COM/nodeapp/server.sh

rm -f "$(pwd)/app.sock"
pm2 start app.js
echo "Starting server..."
sleep 2
chmod 777 "$(pwd)/app.sock"
echo "Server started..."
ryanspandu commented 3 years ago

In our example you can use express like that;

const express = require('express')
const app = express()
const port = '/home/admin/web/YOUR_DOMAIN.COM/nodeapp/app.sock';
const hostname = 'YOUR_HOST_NAME';

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/about', (req, res) => {
  res.send({ info: 'APP test server'  });
})

app.listen(port, hostname, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Then run this command;

pm2 start app.js

Don't forget to chmod 777 "$(pwd)/app.sock" command after started server.

I have a bash script like that;

/home/admin/web/YOUR_DOMAIN.COM/nodeapp/server.sh

rm -f "$(pwd)/app.sock"
pm2 start app.js
echo "Starting server..."
sleep 2
chmod 777 "$(pwd)/app.sock"
echo "Server started..."

hello, i want to ask, for hostname, we put public ip address or 127.0.0.1 localhost ? thanks

aligoren commented 3 years ago

In our example you can use express like that;

const express = require('express')
const app = express()
const port = '/home/admin/web/YOUR_DOMAIN.COM/nodeapp/app.sock';
const hostname = 'YOUR_HOST_NAME';

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/about', (req, res) => {
  res.send({ info: 'APP test server'  });
})

app.listen(port, hostname, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Then run this command;

pm2 start app.js

Don't forget to chmod 777 "$(pwd)/app.sock" command after started server. I have a bash script like that; /home/admin/web/YOUR_DOMAIN.COM/nodeapp/server.sh

rm -f "$(pwd)/app.sock"
pm2 start app.js
echo "Starting server..."
sleep 2
chmod 777 "$(pwd)/app.sock"
echo "Server started..."

hello, i want to ask, for hostname, we put public ip address or 127.0.0.1 localhost ? thanks

I put a public IP address. I didn't try it with a local address. But I think it should work both ways.

hisamaszaini commented 3 years ago

I have some problem, if I use bash server.sh the error is connection refused