E
E
Evgeny Petrov2017-07-11 21:26:10
PHP
Evgeny Petrov, 2017-07-11 21:26:10

How to implement a scheme in PHP: several subdomains - one engine - each subdomain has its own database?

Dear! Please help.
I have a certain engine (CRM) and several subdomains use it. Each subdomain has its own database. When updating the engine, it is necessary to update all subdomains in the catalog as well - it is not rational from all points of view.
Question: How can I set up a server or engine so that all subdomains work on the same engine and with a different database? Can you at least tell me what it's called?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
E
Evgeny Petrov, 2017-07-12
@kazsat

It turns out they are called aliases. When accessing through subdomains, we parse through $_SERVER['SERVER_NAME'] and then connect the databases accordingly :) Thanks for the help

N
Nikolai Kargin, 2017-07-11
@kgnk

You set up virtual hosts, in php you add a couple of lines to connect to the database, depending on the host.

T
ThunderCat, 2017-07-11
@ThunderCat

If there is a limited and not increasing number of subdomains - add subdomains, assign them the engine folder as a root document, in the engine determine which subdomain is loaded (by URL), load the appropriate database settings.
For "batch" adding - cname to the main domain (mask *.domen.tld), in Apache you need to register subdomain aliases, I don't remember exactly the line, it's easy to search by Google. The database is also loaded depending on the subdomain, well, here you can come up with the logic yourself, make a prefix for the subdomain there, for example ..

M
Maxim Grishin, 2017-07-12
@vesper-bot

Push the entire engine into a folder, say core, and alias it in vhosts to the same physical path. As a result, each subdomain has its own configs, and the engine is common.

A
Andrey K, 2017-07-12
@kuftachev

If the question is only about the database, then you can parse the url and, based on it, connect the database with the usual switch. If there is some kind of framework, then there may be more elegant solutions.

A
Arris, 2017-07-12
@Arris

Your subdomains are separate folders (virtual hosts), right?
Let's say you have the engine files in /var/www/host1/core/, and the site configs (database connection, etc.) in /var/www/host1/config
We shift the engine code somewhere in /var/www/my_engine_core/and create symbolic links
If my memory serves me , there should be something like

ln -s -T /var/www/my_engine_core/ /var/www/host1/core/
ln -s -T /var/www/my_engine_core/ /var/www/host2/core/

etc
Well, of course, you still need to correctly configure the access rights, folder owners, and so on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question