Debian 配置 Apache2 多個虛擬主機

前言

本例中共配置兩個網站,兩個網站所在服務器假定爲 127.0.0.1,信息如下:

網站名稱 網站硬盤路徑 網站端口
博客一 /var/blog1 2018
博客二 /var/blog2 2019

配置 Apache2 監聽的端口:

sudo nano /etc/apache2/ports.conf
# 監聽 2018 端口,與後面的 <VirtualHost *:2018> 相對應;
NameVirtualHost *:2018
Listen 2018

# 監聽 2019 端口,與後面的 <VirtualHost *:2019> 相對應;
NameVirtualHost *:2019
Listen 2019

創建 blog1 的虛擬主機配置文件:

sudo nano /etc/apache2/sites-available/blog1
<VirtualHost *:2018>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/blog1

        <Directory /var/blog1/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

創建 blog2 的虛擬主機配置文件:

sudo nano /etc/apache2/sites-available/blog2
<VirtualHost *:2019>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/blog2

        <Directory /var/blog2/>
                Options FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

創建虛擬主機配置文件軟鏈接

/etc/apache2/sites-enabled/ 目錄下建立 blog1blog2 的軟鏈接:

cd /etc/apache2/sites-enabled/
# 創建名爲 000-blog1 的軟鏈接,鏈接指向 /etc/apache2/sites-available/blog1
sudo ln -s ../sites-available/blog1 000-blog1
# 創建名爲 000-blog2 的軟鏈接,鏈接指向 /etc/apache2/sites-available/blog2
sudo ln -s ../sites-available/blog2 000-blog2

軟鏈接的名字可以隨意取,只要不與當前目錄下已有的重名即可,因爲 /etc/apache2/apache2.conf 文件已經定義了虛擬主機配置文件的路徑,如下:

# Include the virtual host configurations:
Include sites-enabled/

建完後可以通過 ls -al 命令查看:

shawearn@localhost:/etc/apache2/sites-enabled$ ls -al
total 8
drwxr-xr-x 2 root root 4096 Nov 23 14:38 .
drwxr-xr-x 7 root root 4096 Aug 8 23:42 ..
lrwxrwxrwx 1 root root 26 Mar 21 2017 000-default -> ../sites-available/default
lrwxrwxrwx 1 root root 25 Nov 23 14:38 000-blog1 -> ../sites-available/blog1
lrwxrwxrwx 1 root root 27 Nov 23 14:34 000-blog2 -> ../sites-available/blog2

可以看到文件已經鏈接上了;

結語

至此,配置便告一段落了,啓動 Apache2 服務器,如無意外,通過以下鏈接即可訪問:

# 博客一
http://127.0.0.1:2018/

# 博客二
http://127.0.0.1:2019/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章