1. 程式人生 > >Ubuntu Apache2 安裝及配置檔案學習(及二級域名配置)

Ubuntu Apache2 安裝及配置檔案學習(及二級域名配置)

Apache2 安裝:

  1. Install Apache.
    • apt install
      sudo apt-get install apache2
    • download and install
      $ sudo wget http://mirror.cc.columbia.edu/pub/software/apache//httpd/httpd-2.4.12.tar.gz
      $tar zxvf httpd-2.4.12.tar.gz
      $ ./configure [OPTION]... [VAR=VALUE]
      #such as ./configure --with-prefix=/usr/local/apache2 --with-apr= ......
      $ sudo make
      $ sudo make install.
  2. Enable apache.
    sudo /etc/init.d/apache2 start
  3. Check result:
    launch browser –> localhost –> It works!

配置檔案解析:

就像main()函式一樣,apache2 配置檔案的載入也是有這樣一個“main()”。 就是apache2_path/apache2.conf (主配置檔案)
其他配置檔案都是通過 include方式載入進來。 不建議修改此檔案。

$ cat /etc/apache2/apache2.conf | grep Include
# Include module configuration:
Include mods-enabled/*.load Include mods-enabled/*.conf # Include all the user configurations: Include httpd.conf # Include ports listing Include ports.conf # Include of directories ignores editors' and dpkg's backup files, # Include generic snippets of statements Include conf.d/ # Include the virtual host configurations:
Include sites-enabled/

配置檔案關係
1. *-enabled 裡面都是 連線檔案,指向 available. 如:
$ll mods-enabled/alias.conf
lrwxrwxrwx 1 root root 28 Dec 24 2013 mods-enabled/alias.conf -> ../mods-available/alias.conf

sites-enabled sites-available 是存放apache功能模組的配置檔案和連結的,當我用apt-get install php5安裝了PHP模組後,在這兩個目錄裡就有了php5.load、php5.conf和指向這兩個檔案的連結。這種目錄結果對於啟用、停用某個 Apache模組是非常方便的。
所以想enable 某個mod 或者 site, 直接在xx-enabled 建立連線指向xx-available
2. httpd.conf
主要使用者配置檔案, 一般虛擬主機、訪問許可權等都是在這裡修改
3. ports.conf
這裡面設定了Apache使用的埠。如果需要調整預設的埠設定,建議編輯這個檔案。
4. conf.d/*
其他配置檔案
5. sites-enabled/*
這個是很多人忽略的配置檔案,/var/www的預設位置就是從這裡定義。 定義其他可參照sites-available/default 中定義。

舉例: apache2 二級域名設定:

1. 確認apache2 安裝成功
2. 建立目標檔案:

sudo mkdir /etc/apache2/BobTest
sudo vim /etc/apache2/index.html
<h1>
For Bob test! Thanks!
</h1>

儲存退出: :wq
3. 修改httpd.conf:

#This is a sample for Bob to setup secpmdaru domain name of apache2
Alias /bobtest /etc/apache2/BobTest/

<Directory /etc/apache2/BobTest>
        Order deny,allow
        Allow from all
</Directory>

4. 重啟apache2

sudo /etc/apache2 restart

5. open browser –> localhost/bobtest