多个网站在同一代码库上运行?
我们正在开发一个应用程序,该应用程序将作为托管解决方案提供。我对如何在不重复基本代码的情况下使用具有相同代码的多个站点感到震惊。
例如: 网站 1:www.example.com 和网站 2:www.sample.com 将在相同的代码上运行,但会有不同的配置设置和不同的主题...就像我们在 WordPress 中运行自己的域名一样。
我想知道我该怎么做。
同样在数据库的情况下..我为每个网站创建单独的数据库或使用相同的数据库并将网站 ID 作为每个表中的列会更好吗?
请帮助我。
[澄清] 它不是域别名。就像...这将是一项服务。不同的客户将在自己的域名上以不同的主题获得相同的应用程序。就像博主所做的那样......拥有自己的域名但相同的博客应用程序
[技术] 具体来说,我正在研究如何使用主机名来确定要使用的配置 我们正在使用 PHP 和 MySQL
We are developing an application that would be offered as a hosted solution. I am struck with understanding how can i use multiple sites with same code without duplicating base code.
eg:
website 1: www.example.com and website 2: www.sample.com will run on same code, but would have different configuration settings, and different theme... like we run our own domain names in wordpress.
i wish to know how can i do this.
also in case of database.. would it be better i create individual databases for each website or use the same database with website ID as a column in each table.
pls help me.
[clarification]
its not domain alias. like... it would be a service. where different clients will be offered the same application on their own domain name with different theme. something like what blogger does.. with own domain names but same blog application
[technology]
specifically am looking at how to use the host name to determine which configuration to use
we are using PHP and MySQL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
重用代码绝对是可取的。从您的帖子中很难想象这些网站之间会有多么不同。我们是在谈论徽标、配色方案还是不同的功能?
我将从不同的外观开始感觉。我还将假设这是服务器上唯一的应用程序。
我建议仔细规划您的目录结构。类似于:
这是一种简单的方法,但值得关注。
Reusing code is definitely desirable. It is hard to imagine from your posts how different the sites will be from each other. Are we talking about logos, and color schemes or different functionality?
I'll start with the different look & feel. I'll also assume that this is the only application on the server.
I recommend planning your directory structure carefully. Something like:
This is a simplistic approach but something to thing about.
其实很简单。
您的代码库可以而且应该是单一的。您将其上传到hosting1 和hosting2。维护时,一旦更新了代码,就上传到两个托管空间。
我想这也是这个网站的运作方式。查看页面的页脚。你有 stackoverflow、meta、superuser 和 serverfault。如果你看一下右下角的评论,它会是“svn revision: 5404”。同样的事情。一个代码库发布到四个站点。当然,数据库不同并且包含完全不同的内容。
因此,可变部分是:
配置
设置
数据
您需要复制的部分。
您需要以这种方式进行编码,配置和设置不是硬编码的,而是存储在某些数据库表中或至少存储在某些外部配置文件中。这些不会成为代码的一部分(如果您愿意,也不会成为实现的一部分),因此它们不需要包含在代码中。如果你有它们,就把它们拿出来。
It's very simple actually.
Your code base can and should be single. You upload it to hosting1 and hosting2. When maintaining, as soon as you updated the code, upload it to both hosting spaces.
I suppose it's how this site works as well. Look at the footer of the page. You have there stackoverflow, meta, superuser and serverfault. If you look what the comment on bottom-right says right now, it would be "svn revision: 5404". The same thing. One code base published to four sites. The databases of course are different and contain completely different content.
So the variable parts are:
Configuration
Settings
Data
Those ones you need to copy.
You need to code it this way that configuration and settings are not hardcoded but either stores in some database table or at least in some external configuration files. These do not make part of code (or implementation if you wish) so they do not need to be in code. If you have them in, take them out.
我想您可以有两个不同的域指向同一服务器,并根据主机名设置不同的配置。但我不会推荐这个。您可能会遇到稳定性和性能问题,特别是当您拥有大量用户群或大量指向代码库的域时。
一般来说,如果您运行多个站点,则应该为每个站点配备不同的服务器。
编辑:
阅读其他答案的评论后,我更好地理解了您的用例。
如果您有大量想要使用此设置的站点,如果您正确设计了体系结构,并实施某种负载平衡方案来处理大量用户负载,则可以实现此目的。
回顾一下:
I suppose you could have two different domains pointing at the same server, and set different configurations based on the host name. I wouldn't recommend this though. You may encounter issues with stability and performance, especially if you have a large user base or a large number of domains pointing to the code base.
Generally, if you are running multiple sites, you should have a different server for each.
Edit:
After reading comments on the other answer, I understand your use-case a little better.
If you have a large number of sites that you want to use this set up for, if you design the architecture correctly, and implement a load-balancing scheme of some sort to handle large user loads, you could make this work.
To recap:
目前,我们对大约 15,000 个独特的域名进行了此操作。这并不困难,但确实需要一些设计考虑才能正确实施。
您可以通过单个代码库来完成此操作服务器,本质上只是构建代码以完全依赖于传入请求的 HTTP_HOST 值。然后,数据库中的所有内容都以该值为基础,作为正确隔离数据并使用适当的数据/html 应答请求的方法。
We do this with about 15,000 unique domains right now. It's not difficult, but it does take some design considerations to implement properly.
You can do it off a single codebase & server, and essentially just build your code to depend entirely upon the incoming request's HTTP_HOST value. Then everything in your DB keys off of that value as a means of segregating data properly and answering the request with the appropriate data/html.
出于安全考虑,您可以考虑:
A) 为每个主机设置单独的 open_basedir 限制,使用户上传的文件无法被其他网站访问,例如:
/共享库
/host1/用户图像
/host2/user_images
您可以在允许的路径中包含共享目录和域特定目录。
数据,并允许更轻松地扩展数据库服务器(例如,将繁忙的域移至不同的数据库服务器)
B)为每个主机连接到不同的数据库,以防止数据库查询中的任何错误意外抓取另一个网站的 简单的应用程序,您可能只需一个数据库就可以了。
无论如何,您应该制定一个推出策略,以便在您进行更改时不会出现任何站点中断的情况。
For security reasons, you might consider:
A) set a separate open_basedir restriction for each host so that user uploaded files cannot be accessed by other websites, for example:
/shared_library
/host1/user_images
/host2/user_images
You can include shared and domain specific directories in the allowed paths.
B) connect to a different database for each host, to prevent any errors in your database queries from accidentally grabbing another website's data, and to allow easier scaling of database servers (e.g. Move busy domains onto a different database server)
If you have an extremely simple application, You might get away with one database.
In any case, you should have a roll out strategy so that none of the sites break when you make changes.