跳到主要內容

Set up Virtual Hosts in Apache on Mac OSX El Capitan

Apache-IP-BasedVirtualHosting.png 

Allow the vhosts configuration from the Apache configuration file httpd.conf

Open the httpd.conf



sudo nano /etc/apache2/httpd.conf
Search for ‘vhosts‘ and uncomment the include line
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
Also allow another module to run by uncommenting:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

Edit the vhosts.conf file

Open this file to add in the virtual host.
sudo nano /etc/apache2/extra/httpd-vhosts.conf
An example domain in the file is given of the format required to add in additional domains, just follow this to create your new virtual host:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>
We can take this example and extend on it, if you wanted a domain named apple.com for example, you can copy the existing text block and edit to suit:
<VirtualHost *:80>
    ServerName apple.com
    ServerAlias www.apple.com
    DocumentRoot "/Users/USERNAME/Sites/apple"
    ErrorLog "/private/var/log/apache2/apple.com-error_log"
    CustomLog "/private/var/log/apache2/apple.com-access_log" common
    ServerAdmin web@coolestguidesontheplanet.com
</VirtualHost>
So in the example above a vhost for apple.com is created and the document root is in the Sites folder, in the text block above I have also added in some log files, what you need to change is the document root location username and domain name to suit your needs. Finish and save the file.
Now also you need to map the IP address to be the localhost.

Map Your IP address to localhost

sudo nano /etc/hosts
Add the Domain and ‘www‘ alias to resolve to the localhost address
127.0.0.1 apple.com www.apple.com

Restart Apache

sudo apachectl restart
Check out your local vhost domain in the browser

Losing Localhost

One caveat to note about virtual hosts is that once set up you lose your older document root previously at /LIbrary/WebServer/Documents or accessed in the browser athttp://localhost what happens is that you get a 403 Forbidden Error. But the ~/username document root is still compatible.
To get around this, you need to add in a vhost for localhost and declare this vhost before any of the others, in the same file:
sudo nano /etc/apache2/extra/httpd-vhosts.conf
Add in:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>

Restart Apache

sudo apachectl restart

Changing the WebServer Default User

One of the frustrations of using the Users/username/Sites folder for vhosts is the permissions issues with things like updates and authentication.
wordpress-webserver-permissions.png 

This is because the default webserver user which runs httpd is known as _www, which will not be the user in your local account. If your machine is only in use by you and the webserver will run only under your account then you can change the user.

Find Your User and Group

In the Terminal use the id command to see your username and group
id
You will get a bunch of user groups, you need your primary user uid and group gid names
uid=502(<mark>admin</mark>) gid=20(<mark>staff</mark>)
Change this back in /etc/apache2/httpd.conf
osx-El Capitan-webserveruser

Restart Apache

sudo apachectl restart
Restart Apache and now you are running httpd as your local account.

這個網誌中的熱門文章

正確設置404頁面及其他.505,500....

正確設置404頁面 404頁面的設置是否正確直接關係到網站粘性,而現在很多網站的錯誤頁面返回碼都是200和302,只要蜘蛛爬行錯誤頁面不是404,那麼你的404頁 面設置就是錯誤的了,這裡跟大家介紹如何正確設置404頁面。 怎麼正確設置404頁面?很多人看到這個話題可能覺得下文不屑一顧,其實你是否知道自己的404頁面有沒有設置正確呢?很多開源的cms系統和博客 系統都會帶有404頁面,你是不是覺得這樣已經ok了?不用設置了?這些想法是錯誤的,我們做優化的時候,應該測試404頁面時候生效,設置是否正 確等。下面我們詳細說明怎麼設置404頁面。 這先說一下怎麼樣的404頁面才是有效的404頁面。大家應該都知道搜索引擎是通過http狀態碼來識別網頁狀態的,那麼當蜘蛛檢索到一個錯誤鏈 接時,就需要返回404狀態碼來告訴搜索引擎,這個頁面是錯誤頁面,以後不用索取了。而如果返回200,則告訴搜索引擎這個頁面是正常頁面。 所以我們要查看網站錯誤頁面的返回碼是200還是404,而現在很多網站的404頁面返回碼是200而不是404.    那怎麼正確設置404頁面?

Mac OS X Server 架設兩個網站、兩個郵件伺服器

from:http://www.bnw.com.tw/conference/viewtopic.php?t=208 我的需求要更簡單。  朋友的工作室就兩三人而已,另外一個工作室也是兩三人。  希望有屬於自己的網站及網域名稱郵件  www.123.com   abc@123.com  

Mac mini server(2012 年末和 2011 年中):如何將 OS X Server 安裝到軟體 RAID

重要事項 這些步驟會清除 Mac mini Server 中兩個磁碟機上的所有資料。雖然您可以重新安裝 OS X Server,但是請務必確認這些磁碟機上的任何其他資料都已完成備份,然後再繼續。 您無法在 RAID 卷宗上建立恢復分割區。如果沒有恢復分割區, 將無法支援 OS X 的某些功能 ,因此您應該考慮使用 恢復磁碟輔助程式 建立外接恢復磁碟,然後才建立 RAID 卷宗。 本文所提供的步驟需要使用透過 WiFi 或乙太網路的寬頻 Internet 連線。您的網路連線必須符合 這篇連結文章 的「重新安裝 OS X 的需求」一節中,針對 Internet 回復功能所述的需求。 安裝步驟 按住 Command、Option 和 R 鍵的同時啟動 Mac mini Server,以便啟動進入 Internet 回復模式。這可能要花數分鐘的時間。看到旋轉地球和“正在從網路啟動回復程序”訊息時,放開這些按鍵。