2011-04-17 46 views
3

我試圖設置什麼本質上在開發網站上的子域上的配置文件。 '個人資料'將由用戶創建,即可隨時發生。本地主機上的子域與生產服務器下的CodeIgniter

本地,我知道我不能使用通配符主機條目或任何東西(在MAMP環境中運行,OS 10.6),我不知道如何去動態創建條目。該網站在虛擬主機上運行。

當然的下一個障礙是生產服務器,這是一個媒體寺GS(共享)服務器。再次,不知道如何去自動創建這些(在生產服務器的情況下)DNS條目。

任何協助/建議非常感謝!

回答

2

什麼你正在尋找可能如下:

DNS:

創建一個 「包羅萬象」 的A-進入你的DNS(* .example.com)。

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']變量。

+0

就這麼簡單嗎?據我瞭解,當我向hosts文件添加條目時,A記錄基本上是我在本地環境中創建的內容? – Gavin 2011-04-18 15:28:32

+0

BlueEel你還在使用PHP 4還是什麼?這應該是'$ _GET ['用戶名']'不'$用戶名... ... – Christian 2011-04-18 22:59:21

+0

不幸的是,不能在您的主機文件中創建通配符條目,所以這隻能在生產環境中使用。 – BlueEel 2011-04-19 10:02:02

0

我不知道我明白了什麼CI有虛擬主機做的,但爲晚,我創建了自己的虛擬主機功能:

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([email protected]_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命令,其工作方式與this完全相同。
  • ISWIN - 這樣定義的:define('ISWIN', strpos(strtolower(php_uname()),'win')!==false && strpos(strtolower(php_uname()),'darwin')===false);
+0

這看起來很有趣,特別是對於本地主機 - 不會在共享服務器上工作,是嗎?那麼BlueEel在下面提出的A名建議會出現在哪裏? (另外,CI可能與此無關,但我只是提到了所有涉及的技術) – Gavin 2011-04-18 15:25:49

相關問題