0

Not sure if the title is well suited, but I have a laravel application with a database and I want to make copies of them programmatically and serve them from diffent subdomains (each app with it's database).

What's better: - Make copies of the laravel folder and database, adding the server routing somehow - Create a docker or LXC container and deploy them as needed

How can this be achieved or what can I do to make this possible?

beerLantern
  • 482
  • 7
  • 23

1 Answers1

1

Neither is a good approach - having multiple copies of the application code base will be a nightmare to maintain, while running multiple containers for different subdomains is excessive.

You should instead look at subdomain routing instead - that way a single application can work off multiple subdomains. It's often used for cases such as multi-tenant applications where each client has their own subdomain.

Databases could be a thorny issue. Obviously you can have your database entities relate to a subdomain, possibly using an Eloquent scope to limit them to a specific subdomain. Or, if you're using PostgreSQL, it might be possible to use a different schema within the database for each subdomain. A Google search may turn up other approaches.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
  • Thanks, I didn't know about subdomain routing and I think it could be very interesting. I could set up before hitting any controller the database and a custom configuration for each subdomain without too much overhead. I'll leave the question open a little in case someone comes with more ideas. – beerLantern May 12 '20 at 20:52
  • @beerLantern I'd suggest also searching Google for something like "multi tenant laravel" as there's multiple approaches people have used in the past and documented, so you can see which approach fits your use case. There's also a few off-the-shelf packages, and if one does what you need it'll obviously save you a lot of work – Matthew Daly May 12 '20 at 20:59
  • the problem with multitenant is that it makes code coupled with the database, and I would like to offer my clients the option to self hosting without giving away my business logic. – beerLantern May 12 '20 at 21:16
  • @beerLantern TBH I'm not sure how practical that would be for a PHP application. If it was in a compiled language you can distribute the binary, but you can't really obfuscate PHP code in that way. [This suggests](https://devops.stackexchange.com/questions/1116/how-to-prohibit-access-to-internals-of-docker-container) you can't obfuscate the contents of a container in any way either. Realistically that means the only way you can protect the business logic if it's built in PHP is through legal/licensing means, which works fine for things like Laravel Nova and the Wordpress paid plugin ecosystem – Matthew Daly May 12 '20 at 21:27
  • I understand it sounds strange, but idea is to build an open source proyect and make the business with hosting for non techies. – beerLantern May 12 '20 at 21:47
  • 1
    @beerLantern No, that sounds pretty standard to offer a paid hosted version as well as an open source package. But if it's open source it's even less likely you can hide anything about the business logic. I think you'd probably want a way for the code base to be able to tell if it was in a multi tenant environment similar to Wordpress multisite, OR have multi tenancy implemented in a closed source package you retain solely for your hosted service. – Matthew Daly May 12 '20 at 22:12