I have my Node.js app working fine on Laravel Homestead, but now need to move it to the server. I'm using Laravel Forge for the server provisioning. The server already has Node and the required packages installed.
When running locally, I can just use Homestead's http://192.168.10.10:3000 to connect. What will the address be on my server, and how can I keep the node app running?
I can run it when I am connected via SSH, and I can see that it is outputting events received. But how do I connect the client to it? I have tried my server's IP and domain but no luck.
Once connected, how can I keep the node app running so I don't need to have an open SSH session?
Many thanks,
Sam
Install NodeJS if you don't have it already on the server:
sudo install nodejs
Push your app to whatever folder you want it.
Create an app (a site) for your NodeJS app on Laravel Forge.
At the bottom part, there will be a button to edit Nginx's configuration. If there exists a location / { part, replace it with a reverse proxy (if there isn't, just add it):
location / {
proxy_pass http://SERVER_IP:APP_PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
Restart nginx, run the app.
It should all be running as you want it to on the address you've provided Laravel Forge.
To set up restarting, you'll need to look into PM2. It's fairly straightforward.
Related
I have a boring problem that I can't solve..
I tried in all ways but I don't understand why if I call the application through the IP:3000 it loads updated files, but if I try to call it trough https://domainname.ext it doesn't want to serve new files.
I tried to delete node_modules and rebuild it (npm install); to run "npm run eject" and rebuild the app; pm2 kill and restart; reboot the server but nothing...
The App is configured to run with Nginx as reverse proxy over https.
Classically one of the following solutions:
Browser cache ("external" Domains are cached, IPs not). Try a different Browser
Actually 2 apps running. Try to kill the process and see if it really goes down
Nginx caching?
Finally 😌
in my server block of nginx configuration i just changed from localhost to IP addr
location / {
proxy_pass http://IP.ADDR:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
The question above is for pure education, not for use in real world.
When reading on how to deploy a nodejs app on a VPS, I have found out, that nginx is forwarding all requests to a localhost port, which is served by node a nodejs app.
here is a nodejs config in digitalocean tutorial:
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
I was wondering is the same possible with nginx and Laravel's php artisan serve? (for sure this will also need a process manager that will restart the app in case it fails.
It is possible, and somebody asked about it: How to use "php artisan serve" in a remote server - DigitalOcean. You can do a similar thing.
artisan serve is a wrapper for PHP’s built-in server, and while this is fine for pure educational reasons, “It is not intended to be a full-featured web server. It should not be used on a public network.” See Built-in web server for more.
I wrote some nodejs services in my ubuntu local.Now I want to deploy my nodejs services into nginx server in my seperate VM.I set up the nginx in my virtual machine.How can I pull my nodejs services to nginx server and how to connect these api's through postman. I getting confusion at nginx config file.
You should setup a reverse proxy with nginx to redirect the traffic to you node application. Install node on your VM, copy your application and install all the dependencies using npm install. Afterwards, you should start the node application using node index.js where index.js is the entry point of your application. You could also use a process manager such as pm2 to start the application. Then, you have to setup the reverse proxy with nginx which is redirecting the traffic to the port of your application. (In your sample code 3000). The application should now be available on the IP of your VM. Below you find a minimal example configuration for nginx.
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I have uploaded a React client to DigitalOcean with an SSL certificate to enable HTTPS. I also have uploaded my Express server to Amazon's AWS. The reason for the different host providers is that I wasn't able to upload my client to AWS so I made the switch to DigitalOcean.
The server works great and I get normal responses from it when I use the client from my machine. However, the exact same code doesn't work in DigitalOcean's Nginx server. I get:
TypeError:Networkerror when attempting to fetch resource
But no response error code. The GraphQL/fetch requests aren't visible on the server so they either aren't being sent correctly or they cannot be accepted correctly by the server.
I played around with "proxy" in client's package.json and HOST/PORT/HTTPS attributes as seen here but I realized these have no effect in production.
I have no idea how to fix this. My only guess is that client uses HTTPS while server doesn't, but I haven't found info on if that's a problem.
This is my client's Nginx server configuration:
server {
listen 80 default_server;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
If your client lives on a domain different from your API server then you need to make sure you have CORS headers enabled on the API server, otherwise the browser will refuse to load the contents.
See here for more information regarding CORS headers.
You turn off the credentials when running the client on DigitalOcean. Maybe storing a cookie on your client is/ was not possible.
I have developed Rest API using Node JS, Express and MongoDb.
I have istalled MongoDB into one machine with Ubuntu Server OS and Node JS App on machine with Ubuntu Server OS.
Now i need to deploy to Production enviroments with a reverse proxy.
I have seen this post as example:
Deploy Node JS
Now my question is: the reverse proxy server using Nginx must be deploy on a separete machine? Instead Nginx is possible to use Apache?
Thanks for your help
You can deploy nginx on same machine.So your setup will be nginx listening to 80 port of the machine for incoming requests and redirecting all requests to you application as per you have specified in nginx configuration.
if you are deploying nginx on same machine and having application 8080. you can do some thing like this.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
and if you can configure apache to redirect request to your application you can replace nginx with apache.(but i have not worked with apache so as per me above configurations works.but if you want some help you can read this one link)