Struggling with pm2 and Nginx [SOLVED]

Hi guys,

I decided to open a post asking for help after more than one month fighting with this. Maybe is just me because I’m too noob with server stuff.

I’m deploying to my server (DigitalOcean) using mp2-meteor. Everything works as expected and when I run mp2 list on my server terminal I get this:

┌─────────┬─────────┬────────┬────────┬─────┬────────────┐
│ Name    │ mode    │ status │ ↺      │ cpu │ memory     │
├─────────┼─────────┼────────┼────────┼─────┼────────────┤
│ recipic │ cluster │ online │ 214905 │ 1%  │ 121.9 MB   │
└─────────┴─────────┴────────┴────────┴─────┴────────────┘

But when I go to my website I can’t see anything, just a 404 Not Found.

I’m using Ngnix without SSL and domain, just IP address. I created a conf file on /etc/nginx/sites-enabled (and a copy on sites-available) and I tried too many different kinds of configurations for these files.

What do I need to do to see my app running?

Thanks mates!

Maybe try pasting your nginx conf file and we can help you from there?

what does your config file look like?

Sure, this is my nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##
# server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        ##
access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Phusion Passenger config
        ##
        # Uncomment it if you installed passenger or passenger-enterprise
        ##

        # include /etc/nginx/passenger.conf;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

And this the file in sites-enabled:

server {
    listen 80;
    server_name _;
    location / {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:3000;

        #configure websockets handling
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

But I also tried this other one:

# this section is needed to proxy web-socket connections
map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}
# HTTP
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        location = /favicon.ico {
          root /home/recipic/apps/recipic/bundle;
          access_log off;
        }

        location ~* "^/[a-z0-9]{40}\.(css|js)$" {
          gzip_static on;
          root /home/recipic/apps/recipic/bundle;
          access_log off;
        }

        location ~ "^/packages" {
          root /home/recipic/apps/recipic/bundle;
          access_log off;
        }

        # pass requests to Meteor
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade; #for websockets
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
        }
}

When you have your meteor process running, what do you see if you navigate to http://yourdomain.com:3000 or http://yourip:3000?

http://138.68.171.51/ is a 404 and with port doesn’t work at all

If you are trying to reverse proxy port 80, then I would expect http://138.68.171.51:3000 to be serving your app. Are you sure that your app is accessible?

As long as I know it should be.

I’m using Gitlab CI to push my app to my sever and I run mp2-meteor deploy. This launches the app on my server and running pm2 list I can see that my app is running (as I showed on first comment).

I understand it’s working, but it’s the first time I’m using pm2 so can be sure.

If you’re using this, it looks like their conf file has meteor running at port 4004? Are you providing a different value?

I understand that that is just an example. My pm2-meteor.json looks like this:

{
  "appName": "recipic",
  "appLocation": {
    "local": "../"
  },
  "meteorSettingsLocation": "../imports/settings.json",
  "meteorBuildFlags": "--architecture os.linux.x86_64",
  "env": {
    "ROOT_URL": "http://138.68.171.51",
    "PORT": 3000,
    "MONGO_URL": "mongodb://localhost:27017/foodapp"
  },
  "server": {
    "host": "138.68.171.51",
    "username": "recipic",
    "pem": "../private/recipic_mup_rsa",
    "deploymentDir": "/home/recipic/apps",
    "loadProfile": "",
    "exec_mode": "cluster_mode",
    "instances": 1
  }
}

I also created this issue as well on their repo 14 days ago but with no answers yet: https://github.com/andruschka/pm2-meteor/issues/60

If you aren’t blocking port 3000 from public access, I’d expect your app to be visible at http://138.68.171.51:3000/, so to me your issue is probably more with pm2 than with nginx? Maybe try re-titling the post or start a new one with that as the issue?

Done.

I also tried btw to do this with 3001 port and faced the same.

I wouldn’t like to do that but what about if I remove the droplet and create a new one?

You should first check if your app is running. Check PM2 logs. You will find it in .pm2/logs from the home folder of the Id you used to install PM2 and start the app. you can also check status of app using pm2 options.

There are a lot of pieces involved and therefore a lot of places to go wrong. However, I can confirm it is possible to deploy using pm2-meteor on from GitLab CI as I just went through that recently.
http://blog.philcruz.com/2017/05/continuous-integrationdeployment-with.html
I would suggest getting pm2-meteor working from your local machine first. It will be much easier to troubleshoot than doing it from GitLab CI. If you can deploy with pm2-meteor from your local machine, then getting it to work from GitLab CI will be straight forward.

What do you see when you run pm2-meteor logs?

Hi!
likely your app running but crashed,
troubleshoot PM2 pretty simple, just look
PM2_HOME=/etc/.pm2 pm2 logs your-app --lines=1000
or run
PM2_HOME=/etc/.pm2 pm2 show your-app
and

error log path    │ /etc/.pm2/logs/dostavka-error-0.log │
out log path      │ /etc/.pm2/logs/dostavka-out-0.log   │
pid path          │ /etc/.pm2/pids/dostavka-0.pid |

PM2_HOME may vary depending on the your setup

Good point @perumalkuk. I just checked it out with pm2 monit and pm2 log and it looks like my app is restarting itself all the time.

I also linked my app to this website a few weeks ago https://app.keymetrics.io/ but didn’t check it out till now and when I saw it… well… I’ve got more than 350.00 restarts 0_0

Anyway, the logs show this:

  • In blue:
PM2        | 2017-05-07 10:34:27: Starting execution sequence in -cluster mode- for app name:recipic id:1
PM2        | 2017-05-07 10:34:27: App name:recipic id:1 online
  • In green:
1|recipic  |     at Module._compile (module.js:571:32)
1|recipic  |     at Object.Module._extensions..js (module.js:580:10)
1|recipic  |     at Module.load (module.js:488:32)
1|recipic  |     at tryModuleLoad (module.js:447:12) code: 'MODULE_NOT_FOUND' }
1|recipic  | { Error: Cannot find module '../build/Release/bson'
1|recipic  |     at Function.Module._resolveFilename (module.js:470:15)
1|recipic  |     at Function.Module._load (module.js:418:25)
1|recipic  |     at Function._load (/usr/lib/node_modules/pm2/node_modules/pmx/lib/transaction.js:91:21)
1|recipic  |     at Module.require (module.js:498:17)
1|recipic  |     at require (internal/module.js:20:19)
1|recipic  |     at Object.<anonymous> (/home/recipic/apps/recipic/bundle/programs/server/npm/node_modules/meteor/cfs_gridfs/node_modules/mongodb/node_modules/bson/ext/index.js:15:10)
1|recipic  |     at Module._compile (module.js:571:32)
1|recipic  |     at Object.Module._extensions..js (module.js:580:10)
1|recipic  |     at Module.load (module.js:488:32)
1|recipic  |     at tryModuleLoad (module.js:447:12) code: 'MODULE_NOT_FOUND' }
  • In red:
1|recipic  |   meteor npm install --save bcrypt
1|recipic  | 
1|recipic  | in the root directory of your application.
1|recipic  | js-bson: Failed to load c++ bson extension, using pure JS version
1|recipic  | Error: bind EADDRINUSE 0.0.0.0:3000
1|recipic  |     at Object.exports._errnoException (util.js:1050:11)
1|recipic  |     at exports._exceptionWithHostPort (util.js:1073:20)
1|recipic  |     at listenOnMasterHandle (net.js:1336:16)
1|recipic  |     at rr (internal/cluster/child.js:111:12)
1|recipic  |     at Worker.send (internal/cluster/child.js:78:7)
1|recipic  |     at process.onInternalMessage (internal/cluster/utils.js:42:8)
1|recipic  |     at emitTwo (events.js:111:20)
1|recipic  |     at process.emit (events.js:194:7)
1|recipic  |     at process.nextTick (internal/child_process.js:766:12)
1|recipic  |     at _combinedTickCallback (internal/process/next_tick.js:73:7)

[STREAMING] Now streaming realtime logs for [all] processes
1|recipic  | Note: you are using a pure-JavaScript implementation of bcrypt.
1|recipic  | While this implementation will work correctly, it is known to be
1|recipic  | approximately three times slower than the native implementation.
1|recipic  | In order to use the native implementation instead, run
1|recipic  |   meteor npm install --save bcrypt
1|recipic  | in the root directory of your application.

I get meteor npm install --save bcrypt also when I run my app locally so it’s not too relevant. I’ve got this on my CI process as well but it doesn’t install properly.
Maybe the issue is more related with Error: Cannot find module '../build/Release/bson' but I don’t get this issue running my app locally (makes sense because it’s not a deployed version).


Thanks for confirming @philcruz. As long as I know it shouldn’t be any different deploying locally than from a CI. In fact there could be more problem on local if you connection is not good enough as I faced before with MUP.
Anyway, I tried with a better connection and I pushed my app to the server as expected but with same result.
My YAML file is quite similar to the one you posted, the only difference is the deploy command, which on that one is meteor npm run deploy. I understand it’s running pm2-meteor deploy through the package.json with that command. Otherwise I don’t understand why he install pm2-meteor on the pipeline.

About you question is answered above.


Hi @orloff.

/.pm2 doesn’t exist on my server, at least not on my /etc folder. But I understand it should be similar to the one posted on this answer, right?


Thanks guys for your help!
I think the deploy is a pain in the $%! and I’m documenting all of this to make this process much easier for everyone including server set up. Hope with all your help this can be accomplished. :+1:

I’m reading about the issue with bcrypt. There are more people with it and it’s not easy to fix aparently:


I tried those fixed but still the same

Fixed here: https://github.com/andruschka/pm2-meteor/issues/60

1 Like