本地主机上的子域与 CodeIgniter 下的生产服务器上的子域

发布于 2024-11-02 05:25:10 字数 243 浏览 1 评论 0原文

我正在尝试在开发站点上的子域上设置本质上的配置文件。 “配置文件”将由用户创建,即可以随时发生。

在本地,我知道我不能使用通配符主机条目或任何东西(在 MAMP 环境中运行,操作系统 10.6),并且我不确定如何动态创建条目。该网站在虚拟主机上运行。

下一个障碍当然是生产服务器,它是 Media Temple gs(共享)服务器。再次,不知道如何自动创建这些(在生产服务器的情况下)DNS 条目。

非常感谢任何帮助/建议!

I'm trying to set up what are essentially profiles on subdomains on a development site. The 'profiles' would be user-created, i.e. can happen at any time.

Locally, I know I can't use wildcard host entries or anything (running in a MAMP environment, OS 10.6), and I'm not sure how to go about dynamically creating entries. The site runs on a vhost.

The next hurdle of course is the production server, which is a Media Temple gs (shared) server. Once again, no idea how to go about creating these (in the case of the production server) DNS entries automatically.

Any assistance/advice is very much appreciated!

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

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

发布评论

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

评论(2

怂人 2024-11-09 05:25:10

您要查找的内容可能如下:

DNS

在您的 DNS (*.example.com) 中创建一个“包罗万象”的 A 条目。

Apache 配置:

将以下内容添加到您的 .htaccess 文件中:

RewriteEngine On

# Extract the username from the subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /profile.php?username=%1 [L]

PHP:

在您的 profile.php 中,您的 $_GET['username'] 变量中突然出现了用户名。

What you're looking for might be the following:

DNS:

Create a "catch-all" A-entry in your DNS (*.example.com).

Apache configuration:

Add the following to your .htaccess file:

RewriteEngine On

# Extract the username from the subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^$ /profile.php?username=%1 [L]

PHP:

In your profile.php you suddenly have the username in the $_GET['username'] variable.

淡淡绿茶香 2024-11-09 05:25:10

我不确定我是否理解 CI 与虚拟主机有什么关系,但最近,我创建了自己的 VHost 函数:

define('VHOST_FILE','/etc/httpd/conf.d/vhost.conf');

/**
 * Generate Apache vhost entry.
 * @param string $domain Domain or subdomain name (eg: foo.example.com or example.biz).
 * @param string $path Path to document root.
 * @return string The generated vhost entry.
 */
function vhost_gen($domain,$path){
    $eol=ISWIN ? CRLF : LF;
    return  '<VirtualHost *:80>'.$eol.
            TAB.'ServerName '.str_replace(array(' ',CR,LF,TAB,NULL),'',$domain).$eol.
            TAB.'DocumentRoot "'.str_replace(array(CR,LF,TAB,NULL),'',$path).'"'.$eol.
            '</VirtualHost>'.$eol.$eol;
}

/**
 * Writes the new vhost config file.
 * @param array $items List of objects(domain,docroot) used in config.
 */
function vhost_write($items){
    $eol=ISWIN ? CRLF : LF;
    $data='NameVirtualHost *:80'.$eol.$eol;
    foreach($items as $item)
        $data.=vhost_gen ($item->domain,$item->docroot);
    return @file_put_contents(VHOST_FILE,$data);
}

/**
 * Returns a list of vhost entries.
 * @return array List of objects(id,domain,docroot) entries.
 */
function vhost_read(){
    $entries=array();
    $lines=explode(LF,str_replace(CR,LF,@file_get_contents(VHOST_FILE)));
    $entry=null;
    foreach($lines as $line){
        // parse line
        $line=explode(' ',str_replace(TAB,' ',trim($line)),2);
        if(isset($line[0]))$line[0]=strtolower(trim($line[0]));
        // state engine
        if($line[0]=='<virtualhost'){
            $entry=new stdClass();
            continue;
        }
        if($line[0]=='</virtualhost>' && $entry!==null){
            $entry->id=count($entries)+1;
            $entries[$entry->id]=$entry;
            $entry=null;
            continue;
        }
        if($line[0]=='servername' && $entry!==null){
            $entry->domain=str_replace('"','',trim($line[1]));
            continue;
        }
        if($line[0]=='documentroot' && $entry!==null){
            $entry->docroot=str_replace('"','',trim($line[1]));
            continue;
        }
    }
    return $entries;
}

/**
 * Backup vhost config file.
 * @return boolean True on success, false otherwise.
 */
function vhost_backup(){
    if(!@file_exists(VHOST_FILE))
        return @file_put_contents(VHOST_FILE,'')
            && @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
    return @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
}

/**
 * Restore vhost backup file.
 * @return boolean True on success, false otherwise.
 */
function vhost_restore(){
    return @copy(ServerCentral::path().'vhost.conf.bak',VHOST_FILE);
}

/**
 * Restarts apache/httpd service daemon.
 * @return boolean True on success, false otherwise.
 */
function httpd_restart(){
    $r=System::execute('service httpd restart');
    return $r['return']==0;
}

因为我基本上是复制+粘贴它,所以您可能想要替换一些内容:

  • ServerCentral:: path() - 当前运行脚本的绝对路径。
  • System::execute - 运行 shell 命令,其工作方式与 这个
  • ISWIN - 定义如下:define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()) ),'达尔文')===假);

I'm not sure I understand what CI has to do with virtual hosts, but as of late, I created my own VHost functions:

define('VHOST_FILE','/etc/httpd/conf.d/vhost.conf');

/**
 * Generate Apache vhost entry.
 * @param string $domain Domain or subdomain name (eg: foo.example.com or example.biz).
 * @param string $path Path to document root.
 * @return string The generated vhost entry.
 */
function vhost_gen($domain,$path){
    $eol=ISWIN ? CRLF : LF;
    return  '<VirtualHost *:80>'.$eol.
            TAB.'ServerName '.str_replace(array(' ',CR,LF,TAB,NULL),'',$domain).$eol.
            TAB.'DocumentRoot "'.str_replace(array(CR,LF,TAB,NULL),'',$path).'"'.$eol.
            '</VirtualHost>'.$eol.$eol;
}

/**
 * Writes the new vhost config file.
 * @param array $items List of objects(domain,docroot) used in config.
 */
function vhost_write($items){
    $eol=ISWIN ? CRLF : LF;
    $data='NameVirtualHost *:80'.$eol.$eol;
    foreach($items as $item)
        $data.=vhost_gen ($item->domain,$item->docroot);
    return @file_put_contents(VHOST_FILE,$data);
}

/**
 * Returns a list of vhost entries.
 * @return array List of objects(id,domain,docroot) entries.
 */
function vhost_read(){
    $entries=array();
    $lines=explode(LF,str_replace(CR,LF,@file_get_contents(VHOST_FILE)));
    $entry=null;
    foreach($lines as $line){
        // parse line
        $line=explode(' ',str_replace(TAB,' ',trim($line)),2);
        if(isset($line[0]))$line[0]=strtolower(trim($line[0]));
        // state engine
        if($line[0]=='<virtualhost'){
            $entry=new stdClass();
            continue;
        }
        if($line[0]=='</virtualhost>' && $entry!==null){
            $entry->id=count($entries)+1;
            $entries[$entry->id]=$entry;
            $entry=null;
            continue;
        }
        if($line[0]=='servername' && $entry!==null){
            $entry->domain=str_replace('"','',trim($line[1]));
            continue;
        }
        if($line[0]=='documentroot' && $entry!==null){
            $entry->docroot=str_replace('"','',trim($line[1]));
            continue;
        }
    }
    return $entries;
}

/**
 * Backup vhost config file.
 * @return boolean True on success, false otherwise.
 */
function vhost_backup(){
    if(!@file_exists(VHOST_FILE))
        return @file_put_contents(VHOST_FILE,'')
            && @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
    return @copy(VHOST_FILE,ServerCentral::path().'vhost.conf.bak');
}

/**
 * Restore vhost backup file.
 * @return boolean True on success, false otherwise.
 */
function vhost_restore(){
    return @copy(ServerCentral::path().'vhost.conf.bak',VHOST_FILE);
}

/**
 * Restarts apache/httpd service daemon.
 * @return boolean True on success, false otherwise.
 */
function httpd_restart(){
    $r=System::execute('service httpd restart');
    return $r['return']==0;
}

Since I basically copy+pasted it, you might want to replace some stuff:

  • ServerCentral::path() - Absolute path to the current running script.
  • System::execute - Runs a shell command, works exactly like this.
  • ISWIN - Defined like this: define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文