Reputation: 135
I want to build multi-tenant web application with CakePHP 3 like this http://www.orangescrum.com/, which is also written in CakePHP.
Each tenant will have separate subdomain and separate database, only the application source code will be same for all sub domains. The domains will have their own folder like x.domain.com
, y.domain.com
mapping to folder x, y, z
.
I don't want to have duplicate application source code in all subdomains. I want to reuse same application code for all.
When each subdomain in requested how can I use same application code but different database? Any suggestions for any kind of implementation are welcome.
Upvotes: 1
Views: 505
Reputation: 510
You have to create a conf file for this.
Which you can automatically create using sh file as below
#!/bin/bash
if [ "x${1}" = "x" ]; then
echo "User is not specified"
exit 1
fi
if [ "x${2}" = "x" ]; then
echo "Domain is not specified"
exit 1
fi
if [ "x${3}" = "x" ]; then
echo "Domain is not specified"
exit 1
fi
/bin/echo ${1}${2} | /bin/egrep -q ';|`|\||>'
if [ $? -eq 0 ]; then
echo "Bad parameter"
exit 1
fi
/bin/cat > /etc/httpd/vhost.d/${1}.${2}.conf <<EOF
<VirtualHost *:80>
DocumentRoot /home/user/www.${2}/htdocs
ServerName ${1}.${2}
ServerAlias ${3}
ServerAdmin [email protected]
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /home/user/www.${2}/htdocs
ServerName ${1}.${2}
ServerAlias ${3}
ServerAdmin [email protected]
SSLEngine on
SSLCertificateFile /etc/httpd/conf/ssl.crt/example.com.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/example.key
SSLCACertificateFile /etc/httpd/conf/ssl.crt/example.crt
</VirtualHost>
EOF
service httpd restart
echo "Vhost has been added"
exit 0
The path and email specified can be modified according to your configuration and save it with a vhost_gen.sh in '/usr/local/bin/' or wherever you like to.
To call this sh file in cakephp
$subdomain_name = 'xyz';
$domain_name = 'domain.com';
$real_domain_name = 'xyz.domain.com';
exec ("/usr/local/bin/vhost_gen.sh ".$subdomain_name." ".
$domain_name." ".$real_domain_name);
$real_domain_name is the ServerAlias if you want to give any real domain as alias to that subdomain.
Upvotes: 2