使用 PHP 在 LAMP 中设置子域

发布于 11-24 17:37 字数 124 浏览 1 评论 0原文

有没有办法拥有 LAMP 服务器并通过 PHP 以编程方式创建子域?

我不想安装 Plesk 或 cPanel,我知道他们有 API,可以让我创建子域。但是有没有办法绕过它们并拥有一个仅安装 LAMP 服务的裸服务器。

is there a way to have a LAMP server and create subdomain programatically via PHP?

I don't want to install Plesk or cPanel, I know they have API's which lets me create subdomains. But is there a way to go around them and have a bare server with just LAMP services installed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸2024-12-01 17:37:22

只需将所有子域指向您的文档根目录,然后找出您的 PHP 代码中访问了哪个子域。使用通配符 DNS 和通配符虚拟主机,您不需要“创建”子域,所有可能的子域都简单存在。

<VirtualHost *:80>
    ServerName *.example.com
    DocumentRoot /var/www/html
</VirtualHost>

这就是向每个用户分发子域的服务的工作方式。他们实际上并不创建 DNS 记录、编辑 Web 服务器配置文件以及每次用户注册时重新启动所有服务。

Just point all subdomains to your document root, and figure out which subdomain was accessed in your PHP code. With wildcard DNS and a wildcard virtual host, you do not need to "create" subdomains, all possible subdomains simply exist.

<VirtualHost *:80>
    ServerName *.example.com
    DocumentRoot /var/www/html
</VirtualHost>

This is how services that hand out a subdomain to each user work. They do not actually create DNS records, edit web server config files, and restart all their services every time a user signs up.

凹づ凸ル2024-12-01 17:37:22

你可以看看Apache提供的海量虚拟主机模块:

使用 PHP,当您想要创建新域时,只需创建一个新目录...并实现此配置的部分内容(请参阅上面的链接)关于如何配置 Apache。这将允许每个主机有一个单独的虚拟主机...而不是替代答案,后者仍然需要每个唯一主机名进行更多配置工作...

 # get the server name from the Host: header
 UseCanonicalName Off

 # this log format can be split per-virtual-host based on the first field
 LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
 CustomLog logs/access_log vcommon

 # include the server name in the filenames used to satisfy requests
 VirtualDocumentRoot /www/hosts/%0/docs
 VirtualScriptAlias /www/hosts/%0/cgi-bin

You can look at the mass virtual hosting module provided by Apache:

With your PHP, when you want to create a new domain, simply create a new directory ... and implement parts of this configuration (consult the link above) on how to configure your Apache. This will allow a seperate vhost per host ... as opposed to the alternate answer which still requires more configuration work per unique hostname...

 # get the server name from the Host: header
 UseCanonicalName Off

 # this log format can be split per-virtual-host based on the first field
 LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
 CustomLog logs/access_log vcommon

 # include the server name in the filenames used to satisfy requests
 VirtualDocumentRoot /www/hosts/%0/docs
 VirtualScriptAlias /www/hosts/%0/cgi-bin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文