【问题标题】:Cakephp3 app subdomain sharing same application code but different databseCakephp 3 应用程序子域共享相同的应用程序代码但不同的数据库
【发布时间】:2016-02-19 19:49:53
【问题描述】:
我想用 CakePHP 3 构建多租户 Web 应用程序,就像 http://www.orangescrum.com/,它也是用 CakePHP 编写的。
每个租户都有独立的子域和独立的数据库,所有子域只有应用程序源代码相同。域将有自己的文件夹,如 x.domain.com、y.domain.com 映射到文件夹 x, y, z。
我不想在所有子域中都有重复的应用程序源代码。我想为所有人重用相同的应用程序代码。
当请求的每个子域时,我如何使用相同的应用程序代码但不同的数据库?欢迎对任何类型的实施提出任何建议。
【问题讨论】:
标签:
php
cakephp
cakephp-3.0
multi-tenant
【解决方案1】:
您必须为此创建一个 conf 文件。
您可以使用 sh 文件自动创建,如下所示
#!/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 adminemail@example.com
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /home/user/www.${2}/htdocs
ServerName ${1}.${2}
ServerAlias ${3}
ServerAdmin adminemail@example.com
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
指定的路径和邮箱可以根据你的配置修改,用vhost_gen.sh保存在'/usr/local/bin/'
或任何你喜欢的地方。
在 cakephp 中调用这个 sh 文件
$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 是 ServerAlias。