在 Ubuntu 開啟及關閉虛擬主機, 會用 a2ensite 及 a2dissite 指令, 這個方法在管理多個 Virtual Host 時方便很多, 以下是在 RHEL 7 及 Centos 7 下, 設定類似 Ubuntu 管理 Virtual Host 的方法。
Ubuntu 在 /etc/httpd 目錄下建立兩個目錄, 分別是 sites-available 及 sites-enabled, 將每個 Virtual Host 的設定放在 sites-available 目錄, 當開啟 vhosts 時, 將 Virtual Host 的設定檔從 sites-available 搬到 sites-enabled 目錄, 而 Apache 會讀取 sites-enabled 的 Virtual Host 設定.
關於安裝 Apache 的步驟, 可參考以下文章:
RHEL 及 CentOS 7 安裝 Apache, MariaDB, PHP(LAMP)
首先建立儲存 Virtual Host 的目錄:
-
# cd /etc/httpd
# mkdir sites-available sites-enabled
然後開啟 httpd.conf 設定檔, 設定 Apache 自動讀取 /etc/httpd/sites-enabled/ 目錄下所有 .conf 結尾的檔案。
- # vi /etc/httpd/conf/httpd.conf
在 httpd.conf 最後加入以下一行:
現在建立 Virtual Host 的檔案, 建立了一個檔案後, 以下會以 opencli.com 為例子, 並假設 DocumentRoot 放在 /var/www/opencli.com, 建立 vhosts 的設定檔:
- # vi /etc/httpd/sites-available/opencli.com.conf
加入以下內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<virtualhost *:80=""> ServerName www.opencli.com ServerAlias opencli.com DocumentRoot "/var/www/opencli.com" <directory "="" var="" www="" opencli.com"=""> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all </directory> ErrorLog /var/log/httpd/opencli.com-error.log CustomLog /var/log/httpd/opencli.com-access.log combined </virtualhost> |
建立 DocumentRoot 目錄, 用作儲存網頁:
- # mkdir -p /var/www/opencli.com
現在建立 a2ensite 及 a2dissite 的 Shell Script, 以上名稱可以自訂, 方便自己記憶便好了:
建立 a2ensite:
- # vi /usr/local/bin/a2ensite
加入以下內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#!/bin/bash if test -d /etc/httpd/sites-available && test -d /etc/httpd/sites-enabled ; then echo "-----------------------------------------------" else mkdir /etc/httpd/sites-available mkdir /etc/httpd/sites-enabled fi avail=/etc/httpd/sites-available/$1.conf enabled=/etc/httpd/sites-enabled/ site=`ls /etc/httpd/sites-available/` if [ "$#" != "1" ]; then echo "Use script: a2ensite virtual_site" echo -e "\nAvailable virtual hosts:\n$site" exit 0 else if test -e $avail; then sudo ln -s $avail $enabled else echo -e "$avail virtual host does not exist! Please create one!\n$site" exit 0 fi if test -e $enabled/$1.conf; then echo "Success!! Now restart Apache server: sudo systemctl restart httpd" else echo -e "Virtual host $avail does not exist!\nPlease see available virtual hosts:\n$site" exit 0 fi fi |
建立 a2dissite:
- # vi /usr/local/bin/a2dissite
加入以下內容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/bin/bash avail=/etc/httpd/sites-enabled/$1.conf enabled=/etc/httpd/sites-enabled site=`ls /etc/httpd/sites-enabled/` if [ "$#" != "1" ]; then echo "Use script: a2dissite virtual_site" echo -e "\nAvailable virtual hosts: \n$site" exit 0 else if test -e $avail; then sudo rm $avail else echo -e "$avail virtual host does not exist! Exiting!" exit 0 fi if test -e $enabled/$1.conf; then echo "Error!! Could not remove $avail virtual host!" else echo -e "Success! $avail has been removed!\nPlease restart Apache: sudo systemctl restart httpd" exit 0 fi fi |
建立 a2ensite 及 a2dissite 後, 用 chmod 給以上 Shell Script 可執行權限:
- # chmod +x /usr/local/bin/a2*
現在可以用以下指令開啟及關閉 vhosts:
-
# a2ensite vhost_name
# a2disite vhost_name
以上面的 opencli.com 作為例子, 執行以下指令開啟及重新啟動 Apache:
-
# a2ensite opencli.com
# systemctl restart httpd
要關閉則使用以下指令:
-
# a2dissite opencli.com
# systemctl restart httpd
要建立其他 vhosts, 只要按上面 opencli.con.conf 的內容作為範本, 建立其他 vhosts 檔案即可。