Serverfull hosting

First, what a great framework. I see there is a definite focus on serverless, which is great for startups and middle to large businesses and a great benefit to scalability.

However, I’m a freelancer and have a lot of smaller clients and host my own Linux server with node.js instances in PM2 with a reverse proxy in Nginx. I was looking into deploying an example Redwood app to my own Linux server. When I build the project, the web/dist output is simple enough - just files to host in Nginx (yay JAMstack!).

However, how can I host the function artefacts in api/dist? Do these conform to a spec like the serverless framework and how can I deploy them to PM2? Or do I need to use something like OpenFAAS or the Fn Project?

2 Likes

Hey there @nickg,

We’re right on top of this and we’ve actually split the dev-server already. I would love some help with pm2 to make this a regular deploy target. The ability to self-host is really important to me.

This is how you “run” the Serverless Functions:

  1. cd api; yarn add @redwoodjs/api-server
  2. NODE_ENV=production yarn api-server --functions ./dist/functions/ --port 8911

You would have to run the command in step 2 via pm2; and then you would proxy your requests in nginx to the process running on 8911.

They’re unfortunately AWS Lambda functions now, but we’re going to convert them into express and then make it so that they can be deployed to different targets.

Let me know if you need anymore help with this.

Thanks! It’s working locally here. I’m very happy the ability to self-host is important to you. I was afraid Redwood was being too dependant on cloud providers.

However, I think you made a mistake, you meant @redwoodjs/api-server instead of @redwoodjs/api, right? Also, I think you want to add it to non-the dev dependencies for production: yarn add @redwoodjs/api-server

I’m going to play with this with PM2 when I have some time later this week. I’ll upload an example repo if I have something working if you want to take a look.

Whoops! Yup! I’ll fix it above.

Thanks so much!

I have a working example! :sunglasses:

Live site:

http://redwood-pm2.nickgeerts.com

Repo:

Some comments:

  • It would be nice to have .env loaded in api-server. Now I have to explicitly load dotenv in the node_args in the PM2 configuration.
  • I map the API at /api (how creative eh), but the links in the root refer to absolute paths instead of relative paths. I could create a pull request for that, but I think one would actually prefer to not expose all the functions at that root API page (in production mode)?

Shall I create a cookbook recipe based on this for the Redwood site? I think it will help a lot of people. :blush:

4 Likes

:partying_face: Please do! I did a quick try after the last meeting and noticed that same thing about env’s.

I had a look to see how serverless-aws was doing it and there is sets them up by declaring them in the serverless.yml.

Good point; that page should probably be removed in api-server.

I guess it’s totally fair to expect .env to be loaded by the api-server. Let’s do it!

1 Like

@nickg pm2 is far more impressive than I originally realised. Do you think it makes sense to wrap “api-server” in pm2 and just make that the default experience for users?

1 Like

Hmm, I think there should be a boundary somewhere… Perhaps the responsibility of the hosting solution should be up to the user? For example, one could prefer to use Forever or Phusion Passenger (which I did for a while because of hosting Rails apps).

Also, because PM2 is a hosting and deploy tool, there is always some specific config necessary for PM2. How do you expose this functionality to the user? A simplified config file, or does the user provide a PM2 config file which overrides Redwood defaults? Something extra to think about.

Still, I think it could be nice to have an integrated PM2 option as long as there is the option of using something else.

@nickg - this looks really great.

one thing I can offer here is making this into a “push button” deploy based around Pulumi - if you think that’s something that would be useful. my experience interviewing redwood devs is that even if they can set up some of the infrastructure you describe here, they sure would prefer not to!

well within realm of possibility to have this setup the all the requirements you list, and return to the user keys so they can ssh onto the machine + IP/host info + a deployment script.

I don’t see you on discord but would love to chat further…

1 Like

That’s a good point - I’m trying to optomise for users who don’t know there’s a difference or even an option, but maybe this is something that we can do via documentation instead.

Just jumping on this thread, as I need to also consider some self-hosting options here.

@nickg have you made any further improvements to your PM2 setup?

@peterp is there plans to all go fully SSR with redwood in future?

Another popular option to consider is also a docker setup for this.

If you were to host RW on something like Linode Shared Compute, what would be the minimum specs to go for? And what would the limiting factors be? I.e. what limit would you most likely hit first? (Let’s assume that apart from hosting your RW app serverfully, the rest of the app is “jamstacky”, so your DB is on something like Heroku, your files are on Filestack, your images on ImageKit etc.)

Any particular reason you ask @Tobbe ? for SPA + API, it really does not need a lot of resources.

Just curious what my options are when it comes time for me to deploy my project. 90% sure I’ll be running it on Netlify, but as I said, always good to know about the options available

Usually you’ll know if you can and want to self-host, since you need a little more Linux knowledge than the occasional sudo apt upgrade. Of course you can automate the setup, but the user also needs to understand there is configuration and upkeep for things like security, database, backups. :sweat_smile:

About performance, it depends on the app, but my minimum example only needs 40MB memory. I’ve got 7 (arguably small) production sites running on a VPS instance with 1GB memory. A mix of Rails and Node. This also runs Postgres, no need for an extra service. I have other, heavier sites on their own VPS. :muscle:

1 Like

@nickg are you using the netlify auth provider with your self hosted setup? I am having issues with the API server after running it via PM2. This my issue. Did you encounter that before?

I have not played with auth yet.