This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Maciej
Maciej

Hi, I've created 2 installations on same Debian server inside of: /var/www/html/*project_name* Also set up a vhosts for both sites, and changed / edited RewriteBase for each .htaccess file. Unfortunately when I'm trying to get into page2.com it redirects me to page1.com :/

Tried almost everything. Anyone got solution for this?

DMeganoski
DMeganoski

Your problem is a little unclear. I'm not sure exactly how you have this setup.

Are you sharing a directory with the code base? or are they in separate directories?

You have two seperate domains (page1.com and page2.com) or is it one domain and speperate directories? (I.E. localhost/page1.com, localhost/page2.com)

From your description it sounds as if they are in separate directories with separate domains. In this case, there should be no need to edit the RewriteBase at all. And there should be no reason why you get redirected if they are not sharing a codebase. They are essentially completely different sites.

So what ARE they sharing? that is a start.

Btw, I have created a multi-tenant installation of October, so whatever you're trying to accomplish SHOULD be possible.

ruude4s
ruude4s

@DMeganoski: I'm also looking for a way to setup a multi-tenant installation. Care to share how you achieved this?

DMeganoski
DMeganoski

Well... It is a company project and although the multi-tenant plugin is not portable enough to put on market, it may eventually go there (if the extended license is implemented by that time, to be fair to other developers). So I feel like exposing too much at this time would be unethical (and probably in violation of some agreements). But I will give you some tips.

First, let me ask you some questions about your requirements. These will make a big difference in the approach. Do you want / need separate databases? Do you want SSO across all the sites?

ruude4s
ruude4s

Thanks a lot for your offer. To answer your questions, I don't need separate databases. SSO accross all sites for the main admin would be nice, but it's not a necessity. The most important things are separate websites using one installation of OCMS and separate users for each website, who can only access that single website. They don't need full admin access, only the ability to edit content of static pages. The sites would be hosted on a directadmin system, using domain pointers to add additional domains for additional sites.

DMeganoski
DMeganoski

Well, there are a few ways to go about this.

First things first, you're going to want to create a plugin that allows you to create 'sites' and define domains for them. You can have this relate to companies or however you choose. I kept it simple. Name and domain for each site. (Eventually I'll add domain aliases)

you are probably going to want to create your own extension of Kernal.php where you include an additional custom middleware. You can add middleware to your plugin without modifying any of the base files, but this does not seem to get called for every single instance (did not work for me on login page, and that is crucial)

In this custom middleware, you will perform your check of the current domain against your site records, and assign a global 'tenant id' or 'site id' to the app (session is a good place to store it, to be accessed anywhere)

In a Laravel project I created, to create a single-database multi-tenant app, I told each model that was to have separate data for each domain to use a Trait that automatically adds the tenant_id to every database query from that model.

Here is an example of a package that uses this logic: https://github.com/HipsterJazzbo/laravel-multi-tenant

I had a hard time figuring out how to attach a trait to an existing model, though. So this would be your major roadblock with this approach.

You could also simply filter out the ones which don't belong by using an event, and listening for the backend list and filtering items from the model in your middleware. Not sure what kind of complications this would bring.

THEN there's the issue of creating migrations to add the tenant_id to each table.

THEN there's the issue of assigning a tenant_id every time a model is saved.

So essentially, it is easier to use multiple databases. Then you do not have to worry about modifying existing tables or models. You just define the connection dynamically at boot.

BUT... how do you do that? Isn't that loaded by config before most of the other app boots? Well, yes. Initially, my approach was to load a default connection and then switch it dynamically in my middleware. This seemed overly redundant, though. Why load a connection just to re-load it a second later (every time the app is booted, I.E. every new page)? That has to have a negative impact on performance.

So... how could you dynamically load the database connection information before the app boots?

Well, when the configuration is loaded, most of the classes have not been instanced yet, so you can't use normal features of the CMS. Can't pull it from a database not loaded yet. You could hard-code different connections for each site, but that is tedious to do when you are constantly creating new sites.

This next section would be where I explain how I achieved the dynamic load, but I feel like this would be the point where I have an ethical obligation to hold my tongue. ^^

I will give you the hint that you can still use raw composer packages before the app boots, though. ;)

Also, to save you a headache, Laravel and October use a file for the cache by default. Since October implements caching in the site settings and the different sites are sharing a file base, this is an issue. With separate databases, you can simply switch the cache to database mode. If you decide to go with a single database, you may need to get a bit more creative.

And if you want single sign on, with the single database method you would have to do nothing except NOT implement the trait on the user model. Though this would mean that users could log into other websites as well. With separate databases, you will have to either extend the default user model and switch the connection on it, or create your own version that uses a different connection by default.

Hope this helps! Other people too.

I'll try to answer any other questions you may have. Let me know how it goes.

Last updated

ruude4s
ruude4s

Thanks a lot. I'm currently entangled in another project, but when that is finished I'll start working on this. With the information you've provided I should be able to come a long way.

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.