2010-11-03 202 views
2

當我試圖配置我的虛擬主機在Apache。我把這樣的事情,Apache:虛擬主機配置

NameVirtualHost *:80 

<VirtualHost *:80> 
    DocumentRoot /xampp/htdocs/gift 
    ServerName gift.loc 
</VirtualHost> 

在我的hosts文件,我把這樣的事情,

127.0.0.1  localhost 
127.0.0.1  gift.loc 

而且我在瀏覽器上運行它,

http://gift.loc - is fine 

但是當我試圖使用這個,

http://localhost/othersite - can't found 

我是否sed有人配置?任何想法...

由於提前,

+0

感謝您的建議,我找到了解決方案。我在定義虛擬主機塊上面放置了一些默認目錄: DocumentRoot/xampp/htdocs這將呈現與定義的虛擬主機不匹配的所有請求。 – Trez 2010-11-03 04:59:15

回答

0

docs,看起來我們需要爲每個不同的主機,你會願意爲塊。

此外,在同一文檔中,如果要將虛擬主機添加到現有Web服務器,還必須爲現有主機創建一個塊。

+0

這將是很好的知道,但我想gift.loc由虛擬主機服務,而'othersite'是通過'localhost/othersite'語法運行。 – Trez 2010-11-03 02:27:07

3

你需要一個虛擬主機條目,每個你想要apache處理的主機。如果沒有其他VirtualHosts匹配請求,配置文件中的第一個將被用作默認值。

例如,如果我們有:

<VirtualHost *:80> 
    DocumentRoot /xampp/htdocs/gift 
    ServerName gift.loc 
</VirtualHost> 

<VirtualHost *:80> 
    DocumentRoot /example/htdocs/gift 
    ServerName example.com 
</VirtualHost> 

爲foobar.org的請求將被gift.loc虛擬主機得到處理。

2

你需要把本地主機的vhosts.conf

NameVirtualHost *:80 

    <VirtualHost *:80> 
     DocumentRoot /xampp/htdocs/ 
     ServerName localhost 
    </VirtualHost> 

    <VirtualHost *:80> 
     DocumentRoot /xampp/htdocs/gift 
     ServerName gift.loc 
    </VirtualHost> 

這工作正常(請確保您重新啓動Apache)。如果你需要檢查你的配置,你可以(至少在linux上)運行httpd -S。

0

有您需要按照設置的虛擬主機上的Ubuntu幾個步驟: 讓我們說你的項目文件夾的名字是myProject的

步驟1:將的/ var裏面的文件夾/ www/html等

sudo mv ~/myProject /var/www/html/ 

步驟2:給項目文件夾的所有權到WWW的數據

sudo chown -R www-data:www-data /var/www/html/myProject 

步驟3:C reate新網站內提供的網站:

cd /etc/apache2/sites-available/ 
ls 

在這裏你會看到現有的000-default.conf和默認的ssl.conf .Copy兩個文件的內容到一個文件中並替換您的文件夾名稱或拷貝一個到名爲myProject的新文件中。CONF

<VirtualHost *:80> 
    # The ServerName directive sets the request scheme, hostname and port that 
    # the server uses to identify itself. This is used when creating 
    # redirection URLs. In the context of virtual hosts, the ServerName 
    # specifies what hostname must appear in the request's Host: header to 
    # match this virtual host. For the default virtual host (this file) this 
    # value is not decisive as it is used as a last resort host regardless. 
    # However, you must set it for any further virtual host explicitly. 
    #ServerName www.example.com 

    ServerAdmin [email protected] 
    DocumentRoot /var/www/html/myProject/ 
     ServerName project.com 
     ServerAlias www.project.com 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 

</VirtualHost> 

<VirtualHost *:443> 
     ServerAdmin [email protected] 

     DocumentRoot /var/www/html/myProject/ 
     ServerName project.com 
     ServerAlias www.project.com   

     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 
     SSLEngine on 

     SSLCertificateFile /etc/ssl/certs/mobidev_cert.pem 
     SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem 


     <FilesMatch "\.(cgi|shtml|phtml|php)$"> 
       SSLOptions +StdEnvVars 
     </FilesMatch> 
     <Directory /usr/lib/cgi-bin> 
       SSLOptions +StdEnvVars 
     </Directory> 

</VirtualHost> 

包括在此自簽名證書也路徑如圖所示的SSL密鑰,並且可以很容易地下載的SSL證書。

第4步:將您的項目添加到Apache配置文件。

sudo vi /etc/apache2/apache2.conf 

把這個文件中的行:

DocumentRoot "/var/www/html/myProject" 
<Directory /var/www/html/myProject/> 
    Options Indexes FollowSymLinks 
    AllowOverride All 
    Require all granted 
</Directory> 

第5步:添加您的虛擬服務器名稱(在myProject.conf指定)到主機file.add該行:

sudo gedit /etc/hosts 
127.0.1.1 project.com 

第6步:現在所有設置,啓用網站,重新啓動apache

sudo a2ensite /etc/apache2/sites-availabl/myProject.conf 
sudo systemctl reload apache2 
sudo update-rc.d apache2 defaults 
sudo update-rc.d mysql defaults 
sudo a2enmod ssl 
sudo a2ensite default-ssl 

只需在瀏覽器中點擊project.com即可。