Phusion Passenger: 'Initialize language runtime' and 'Load or execute application' error with nodejs app - node.js

My nodejs app works fine on my local machine in the dev environment, however getting it to work on the production server is a nightmare.
In a nutshell the app is starting but the content is not loading on the site. It is failing to fully initialize as the screenshot shows.
Screenshot
Anyone have any ideas about this? Even the hosting company isn't sure what is causing it and I can't find anything relevant on google so far. All I know is it is coming from Phusion Passenger...

I will post my solution in case anyone else runs into this issue.
If you have created a nodejs app using express and have used the express generator to set the app up, you will notice the start up file is called www inside the bin folder in your projects directory. Usually it is named app.js.
Phusion Passenger looks for app.js to start the app. You need to tell Phusion Passenger to look for ./bin/www inside your config file. It will likely be different for different systems. In apache it is located here:
/etc/apache2/conf.d/userdata/std/2_4/YOUR_USERNAME/YOUR_DOMAIN/YOUR_APPNAME.conf
You need to add this startup file line:
PassengerStartupFile ./bin/www
Your config file should look something like this:
<Location "/">
<IfModule mod_passenger.c>
PassengerAppEnv "development"
PassengerEnabled on
PassengerBaseURI "/"
PassengerAppRoot "/home/YOUR_USERNAME/YOUR_DOMAIN"
PassengerAppGroupName "YOUR_APP_NAME"
PassengerRuby /opt/cpanel/ea-ruby27/root/usr/libexec/passenger-ruby27
PassengerPython /usr/bin/python
PassengerNodejs /opt/cpanel/ea-nodejs10/bin/node
PassengerAppType node
PassengerStartupFile ./bin/www
</IfModule>
</Location>
Then you just need to save the file, rebuild the apache http config file and restart apache.
I used the terminal inside WHM to run these two lines:
1# /usr/local/cpanel/scripts/rebuildhttpdconf
2# /usr/local/cpanel/scripts/restartsrv_httpd
The instructions in this post are found here:
https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/

Related

Only seeing Index of/ at domain even though index.js is present in the pubic_html directory

I've been driving myself a little mad trying to deploy this node.js and express app
I'm hosting on bluehost, so therefor, deploying via cpanel -
Currently:
Git repo cloned and deployed head commit success
Node app registered in app manager and Npm dependencies have been successfully ensured.
I after cd'ing into my repository directory in cpanel terminal I ran
/opt/cpanel/ea-nodejs10/bin/node app.js
and it was confirmed that
Server running at http://127.0.0.1:3000
Then after logging into whm root I ran
curl http://127.0.0.1:3000
And am returned with my index.ejs file , which seems like a good sign.
However, when I go to my domain -- deltadesigns.co
All you can see is :
Index of /
Name Last modified Size Description
DeltaDesigns22/ 2021-02-27 06:23 -
cgi-bin/ 2021-02-19 03:00 -
DeltaDesigns22 is the repo with all of required files and folders, public, views, app.js cpanel.yml etc.
I can't figure out why it's not working, feel like I'm so close but am just missing something! All help is appreciated!

How to host a Gatsby+Node.js project on a shared hosting?

I have a project in gatsby which uses Node.js/express for backend with MySQL.
Now, I know that all I have to do is gatsby build and that will create the static html/css/js files for me in the project/public folder and I can paste all of them in public_html folder and that will work(it is working), but Im confused about the database thing:
My issue is that in the gatsby-config.js when I change the mySql connection from localhost to the hosted db settings such as:
(The commented one is the hosted db configurations)
If I run gatsby develop while uncommenting the code. It says No such DB Error(obviously). So How can I configure the db settings here and also in the gatsby-node.js file to connect the db with the project?
I know this might sound like a dumb question but please help as I'm confused about what to do next.
Thanks.
Okay! Spent a lot of time on this. Hope it will help others.
Static Gatsby site
If you're trying to host a static gatsby site on any shared hosting. By static, I mean just plain gatsby styled pages,
You can do as the gatsby doc says:
Run :       gatsby build        or        npm run build.
According to gatsby:
Gatsby will perform an optimized production build for your site, generating static HTML and per-route JavaScript code bundles.
After this : try npm run serve.
According to gatsby :
Gatsby starts a local HTML server for testing your built site. Remember to build your site using gatsby build before using this command.
serve will test your build files(newly created files in yourprojectroot/public dir)
This will run your project(using the build files) on a test server(localhost:9000) to basically test your build files.
Test this localhost:9000, If everything is working good. You can go to your remote cPanel and paste all your build files into the public_html folder.
Head over to your domain and you're good to go.
Gatsby with MySQL and Node/express
If you are trying to host your gatsby site which works a little with node and mysql as well and you are a newbie in hosting like me, Here's what you'll want to do:
Try both the points mentioned above. (Build your static files and try serve)
Setup your db on the remote as well with the same name dbname, username and password as your local one.
Two extra things:
Now, what you are going to do is to run both the node and gatsby(webpack) servers on the same port (say 8001). So we are going to use only the node server and serve all our gatsby files(build files) as static content to node server.
In your node file, add:
app.use(express.static(path.join(__dirname, 'public')));
app.get('/*', function(req,res) {
res.sendFile(path.join(__dirname,'public/index.html'));
});
As you are going to run all your gatsby pages through index.html the last get('/*'... (above) will take care of all the pages request. Change the path public according to your remote folder structure
Add the build files along with the node(server connection) file in the public_html folder on remote.
Next add or change your .htaccess file (in the remote) to :
RewriteEngine On
RewriteRule ^$ http://127.0.0.1:8001/ [P,L]
RewriteRule ^(.*)$ http://127.0.0.1:8001/$1 [P,L]
So when you run your node file through the server's terminal, instead of yourdomainname.com:8001 the above mentioned .htaccess will redirect it to yourdomainame.com only
All done.
Your public_html now should contain the build files,a node/express conn file and .htaccess file.
Now, just go to your terminal. cd into public_html and run node yournodefilename.
You can head over to your domain now.
Note : You can use pm2 package to keep your node server always running.
Hope it helps somebody.
You should use environment variables to switch between configurations (locally and production). Environment files are files that store sensitive data such as API keys, tokens, etc, so they must be ignored and untracked to avoid pushing critical data to a public repository.
By default, Gatsby uses .env.development and env.production respectively for gasby develop and gatsby build commands, of course, you can override this behaviour but, assuming the default configuration, you should add the following snippet to your gatsby-config.js:
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
Then, you need to create a .env.development and .env.production in the root of your project with the following content:
DB_HOST:yourHost
DB_USER:yourUserName
DB_PASSWORD:yourPassword
DB_NAME:youDatabaseName
Of course, each file should have different variables if you want to switch between databases or configurations.
Add them to your gatsby-config.js:
connectionDetails:{
host: process.env.DB_HOST
user: process.env.DB_USER
password: process.env.DB_PASSWORD
database: process.env.DB_NAME
}
The final step is to add, in your host, the environment file in order to make them accessible by Gatsby. S3 by Amazon allows to configure them but I guess that it's a common configuration for the hostings.

When runing a Node.JS app from WHM/cPanel Application Manager, the Passenger file is missing "PassengerStartupFile" and won't actually run

I've installed Node.JS via cPanel and all the Phusion Passenger files and dependencies. I can run the application manually via SSH. I have created the application in cPanel's Application Manager. However, after the application is "enabled" it's not actually "running" and only results in the Passenger error page. It says it was not able to execute the application.
If I manually inspect the application configuration file in /etc/apache2/conf.d/userdata/ssl/2_4/username/subdomain/application_name.conf I can see it doesn't have an entry for PassengerStartupFile. If I add the line
PassengerStartupFile ./bin/www
and restart the Passenger service, is works and I can access it via the URL. However, if I disable and re-enable the application via cPanel, the conf file is regenerated and that entry is lost.
How can I get cPanel to add the PassengerStartupFile entry, or get Passenger to auto-detect the startup file from the package.json file or environment variables or something?
As a work-around, I've created a second application_name_patch.conf file with the missing configuration line so that it all eventually gets compiled together, but that's kludgy...
Your solution to add a second application_name_patch.conf file next to the appliation_name.conf file is exactly the solution you need as is explained here: How to Install a Node.js Application.
The linked article suggests you use the path /etc/apache2/conf.d/userdata/ssl/2_4/user/domain.nodejs.conf for the custom startup file, but I would recommend you use the path /etc/apache2/conf.d/userdata/ssl/2_4/user/domain/nodejs.conf as this will place the custom configuration in the same directory as the original application conf file.

Beginner - setting up local host for Angular app using XAMPP on Windows 7

I have taken a Angular App which works on Local host when I use visual studio IIE server.
I don't want to use Visual Studio and I am trying to test the app locally using XAMPP.
I am a complete beginner, and I cannot get the local host to pick-up the html initialization file. I have checked the following:
Skype is off and the port is set to 80 in XAMMP
the root server in HTTPd.conf is setup correctly (no slash at end)
I am getting a little confused as to 'how' angular will work on the XAMMP local host.
Do I need to get Yeoman or grunt (seen these apps in some posts, but they seem to be for command line environment).
Any help, or even a pointer to a step by step set-up would be great!
Angular is no different from a normal html web page, when it comes to working in a localhost.
Does your XAMPP work with standard HTML files?
Do you have any error generated?
OK. Got this sorted. I was over complicating things entirely, angular runs sweet on regular XAMPP, no Grunt/ Yeoman etc. required.
All you need to do is: 1. install XAMPP 2. Close Skype 3. Place you app, libraries and everything else in the folder C:\xampp\htdocs\
localhost/Angularappstartpage.html
BOOM!
it works.
You may need to adjust the port number on the localhost (default is 80 e.g. localhost:80/...)
You can also change the root folder in your XAMPP config setup to point to your local GIT.
Do this by changing the line in the HTTPD.conf file (click 'config' on the apache module)
DocumentRoot "C:/xampp/htdocs" to DocumentRoot "C:/myLocalGIT"
Hopefully this will help all the new guys out there. Good luck!
Skype blocks the port 80, which is used for browsing. XAMPP uses the same port so exactly.
https://support.skype.com/en/faq/FA148/which-ports-need-to-be-open-to-use-skype-for-windows-desktop
If you use mysql, also open first XAMPP and start your services and then keep working on your SO as always.

How to install nodejs on Xampp localhost

Been seeing a lot of how to's on how to install nodejs but nothing is at all clear.
So I ask...
Can someone provide a step by step installation guide for installing and using nodejs on a xampp server?
After searching (source), I have found, that it's easier to install Node.js directly (so, no need of XAMP/WAMP):
Install http://nodejs.org/download/
Create a test file (example) C:\myFolder\test.js and put this code in that file:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
Open CMD (COMMAND PROMPT) and execute:
node C:\myFolder\test.js
Open this address in your browser: http://127.0.0.1:1337/
Now It's really easy to install and use Node.js even with Apache if you are using Xampp/Wamp etc. Because unlike old days, now Node.js org has created MSI installer for windows.
Below are the steps to install Node.js with Apache. It is assumed that you have already installed xampp
Download windows installer of Node.js from it's site http://nodejs.org/ click on download. Hit the Node.js website and click the big green Install button. It'll detect your OS and give you the appropriate installer. If for some reason it doesn't, click the downloads button and grab the one you need. Run the installer. That's it, you have installed Node.js and, equally, NPM – Node Package Manager – which lets you add all kinds of great stuff to Node quickly and easily.
Note
Keep your Apache and Node ports different. Declare Node port other than 80 or 8080 while creating server in Node because these are the default ports of Apache.
May be these Notes may help someone in future.
1) When Node.js is installed Node and NPM become available globally. Means that you can create your site anywhere on your hard drive and with command prompt go to your directory like in Windows Command prompt
d:/NodeSite/node server.js
and now you can access it via
http://localhost:3000
because your server.js is running with node.
2) Similarly, you can install any Node Package like installing Memcached package or Library
d:/NodeSite/npm install memcached
"NodeSite" is a folder contain your project.
You can see that node and npm have become globals.
It is possible to run NodeJS trough Apache/XAMPP. Great tutorial how to setup httpd.conf / vhosts.conf http://thatextramile.be/blog/2012/01/hosting-a-node-js-site-through-apache
<VirtualHost 109.74.199.47:80>
ServerName thatextramile.be
ServerAlias www.thatextramile.be
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>
In the end it would be accessible trough port 80 thatextramile.be
I never gave a lot of answers on this site. Because most of the time I'm not an expert however. I had the same issue a while back.
1) You don't really need this XAMPP. Node will create its own http_server so I suggest you just forward calls from XAMPP to the Node app.
2) a good start would be: nodeguide.com/beginner.html
3) I work with PHPstorm which is very nice for Node.js development.
3a) Node.js plugin -> https://www.jetbrains.com/phpstorm/help/installing-updating-and-uninstalling-repository-plugins.html
3b) read this: http://blog.jetbrains.com/webstorm/2014/01/getting-started-with-node-js-in-webstorm/
3c) running: http://blog.jetbrains.com/webstorm/2014/02/running-and-debugging-node-js-application/
3d) Test your app.
You mighht also need this:
4) (MysQl db) https://codeforgeek.com/2015/01/nodejs-mysql-tutorial/
XAMPP and a node.js is two different things, which do not need to work together, nor do they need each other.
XAMPP consists of Apache, MySQL, PHP and Perl.
Where node.js is just like PHP or Apache, so an application.
Node.js can be installed from the website, http://nodejs.org or via the terminal following these instructions:
https://github.com/joyent/node/wiki/Installation
If you want to run javascript from apache you can do it as CGI module. It wont be exacly node.js server and performance because Apache is your server, but you can execute node.js like scripts http://www.cgi-node.org/
You must add a handler to your apache configuration to handle whatever extension files for example .jss via CGI modlue that essentially calls node(.exe) depndeing if linux or windows. I made it work under Bitnami WAMP
It is not possible to install NodeJs on Xammp.
Because Xammp is is simply a tool where Apache,MySql,FileZilla,Tomcat and Mercury server are available. Where you will be able to only configure and use these server.
If you want to install Nodjs on Windows Machine, You will have to install it manually.

Resources