--- tags: Network Management 2023 Spring --- # Apache Web Server <!-- {%hackmd hackmd-dark-theme %} --> > 國立成功大學 網路管理 [IM7021E] 2023 Spring 筆記整理 課程使用之Linux Distribution:Fedora 10 Fedora 10安裝:https://hackmd.io/@cpt/Bk6qbqiNn Linux相關基本指令:https://hackmd.io/@cpt/ryg8Ncx4h :::danger 老師說我們在做DNS server和Web server的quiz時,要做以下設定: `System` -> `Administration` -> `SELinux Management` ![](https://hackmd.io/_uploads/B1peTKH83.png) 將`System Default Enforcing Mode`選成`Disable` ![](https://hackmd.io/_uploads/H1MRhFSUn.png) 以下是新版作業系統或Console mode的設定方式: 開啟`selinux`的設定檔: ```bash! vi /etc/selinux/config ``` 找到`SELINUX=`,改成`SELINUX=disabled`,修改完後儲存離開,並`reboot` ![](https://hackmd.io/_uploads/S1jLwaBL2.png) ::: --- > https://linux.vbird.org/linux_server/centos4/0360apache-centos4.php WWW伺服器常見的兩個:Apache和IIS 也因此有兩大常見的系統: - Linux + Apache + MySql + PHP,這組合又稱作LAMP - Windows + IIS + MSSQL + ASP(.NET) ## LAMP所需套件 - httpd (也就是Apache) - mysql - mysql-server - php - php-devel (與PHP加速軟體有關) - php-mysql Installation: ```bash! $ yum install httpd mysql-server php php-devel php-mysql ``` ## Apache設定檔 ### 主要設定檔 `/etc/httpd/conf/httpd.conf` ### 預設的「首頁」所在目錄 `/var/www/html` <!-- 設定主機名稱: ```bash! $ vi /etc/hosts ``` --> ## Apache啟動、終止、重啟 啟動: ```bash! $ /etc/rc.d/init.d/httpd start ``` 重啟: ```bash! $ /etc/rc.d/init.d/httpd restart ``` 終止: ```bash! $ /etc/rc.d/init.d/httpd stop ``` 狀態查看: ```bash! $ /etc/rc.d/init.d/httpd status ``` > 這些檔案是shell script ### 檢查是否有在執行 ```bash! $ ps aux | grep httpd # 檢查是否在執行httpd $ /etc/rc.d/init.d/httpd start # 確認有安裝後,用這個指令就可以啟動他 $ ps aux | grep httpd # 這時可以看到許多httpd程式在啟動中(9個) # 若同時有9個人連進來,就可以照顧到這9個人 # 要啟動多少個這是可以設定的 # 這個case,若超過9個人連進來,可能會delay,server會啟動新的demand去服務他,他並不用等那9個人結束後才能用 # 所以老師建議default就不用去改他 ``` ![](https://hackmd.io/_uploads/ByCVQFrL2.png) >可以看到`start`後再用`ps aux | grep httpd`就有9個httpd正在執行 ## `httpd.conf`設定檔 開啟Apache server的主要profile設定檔: ```bash! $ vi /etc/httpd/conf/httpd.conf ``` 此設定檔主要分成三個部分: - `Global environment` - `'Main' server configuration` - `Virtual host` ### Setting Global Environment - `ServerRoot "/etc/httpd"` - The top of the directory tree under which the server's configuration, error, and log files are kept - `PidFile run/httpd.pid` - The file in which the server should record its process identification number when it starts - `Include conf.d/*.conf` - Load config files from the config directory "`/etc/httpd/conf.d`" ### Setting of Main Server - `ServerAdmin root@localhost` - Your address, where problems with the server sohuld be e-mailed. - `DocumentRoot "/var/www/html"` - 設定說網頁的資料是存在server的哪個directory,不建議去更動這個選項 - By default, all requests are taken from this directory, but symbolic links and aliases may be used to point to other locations ![](https://hackmd.io/_uploads/SyMzCiS82.png) - `DirectoryIndex` - 用`vi`開啟設定檔之後,輸入`:/DirectoryIndex`可快速跳到此地方 - 這是設定目錄內的預設存取檔案優先順序 - 若是`DirectoryIndex hello.html test.html index.html` - 那就會先檢查有沒有`hello.html`這個檔案,若沒有就再檢查`test.html`,依此類推 - 注意不可以有`,`,用空格即可 - 整個server只會有一個設定 - 在存取每個資料夾時都會依照這個設定尋找優先顯示的網頁 ![](https://hackmd.io/_uploads/S1wKRiHI2.png) :::info 假設將DirectoryIndex改成`DirectoryIndex hello.html hello2.html index.html`,則每次在存取「每個」資料夾時都會依照這個順序,先找看看這個資料夾有沒有`hello.html`,沒有就找`hello2.html`,再沒有就找`index.html` (設定完之後記得要restart) ![](https://hackmd.io/_uploads/Hy-A4nrLh.png) <!-- ![](https://hackmd.io/_uploads/SJMRSnSU3.png) --> 假設以下是`html`資料夾裡面的檔案&資料夾: ```bash! # /var/www/html . ├── hello2.html ├── hello.html └── level1 ├── hello2.html └── level2 └── index.html ``` 輸入`http://localhost`,開啟的是位在`html`資料夾內的`hello.html` ![](https://hackmd.io/_uploads/HJnKI2H83.png) 輸入`http://localhost/level1`,開啟的是位在`level1`的`hello2.html` ![](https://hackmd.io/_uploads/rk0bO2rIn.png) 輸入`http://localhost/level1/level2`,開啟的是位在`level2`的`index.html` ![](https://hackmd.io/_uploads/H1mSdnBI3.png) ::: ## 查看網頁 假設以下是`/etc/www/html`資料夾裡面的檔案&資料夾: ```bash! # /var/www/html . ├── index.html └── level1 ├── index.html └── level2 └── index.html ``` 先啟動Apache: ```bash! $ /etc/rc.d/init.d/httpd start ``` 查看預設的首頁,也就是`/var/www/html/index.html`,在瀏覽器輸入以下網址二擇一皆可: ``` http://127.0.0.1 or http://localhost ``` 查看`level1`預設的首頁: ``` http://127.0.0.1/level1 or http://localhost/level1 ``` 若要查看`level2`預設的首頁,則依此類推。 此外,倘若某資料夾內沒有預設的首頁的檔案,像是假如`level1`資料夾裡面是空的,則輸入`http://localhost/level1`會看到以下畫面: <!-- ![](https://hackmd.io/_uploads/S1p0TpKIh.png) --> ![](https://hackmd.io/_uploads/BkI6Cpt82.png) 可以設定說若找不到目錄內的檔案,到底要顯示目錄內的清單,還是禁止看到,一般都會設定禁止看到檔案清單,以下是設定方法 <!-- 輸入網址:`localhost/test/hello.html` 這樣就能看到 這樣會看到資料夾的內容`http://localhost/test/` 若想要連到某個目錄後自動連到某個黨名,可以再directoryIndex設定 --> <!-- 進入設定檔,搜尋,輸入`:/DirectoryIndex`做搜尋的動作 DirectoryIndex hello.html index.html index.html.var 注意不能有逗號,用空格就好 到任何一個目錄內,若有hello.html,就優先連到這個檔案 後面還有index.html index.html.var,就是會依照優先度連到這些檔案 設定完之後記得要restart 這時再輸入`http://localhost/test/`,就會開啟hello.html --> <!-- ### AccessFileName .htacess 在某些晴望下 可以設定該目錄能不能被存取 assign the activated file name --> ## 禁止查看檔案清單 開啟設定檔: ```bash! $ vi /etc/httpd/conf/httpd.conf ``` 會可以查看檔案清單主要是因為預設的這段設定: ```bash! <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> ``` 是因為`Options`這個項目有`Indexes`,有`Indexes`就代表可以查看檔案目錄。此外,因為這裡是設定`/var/www/html`這個目錄可以查看檔案目錄,所以這個設定也會套用到`html`這個資料夾底下的全部資料夾。若要取消,可以: 1. 將`/var/www/html`的`Indexes`拿掉 2. 或者要設定個別目錄的權限的話,就在底下設定`level1`資料夾的權限,並且`Options`不要有`Indexes`: ```bash! <Directory "/var/www/html/level1"> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> ``` 再存取`http://localhost/level1`就會變成這樣: ![](https://hackmd.io/_uploads/HyUKR2r82.png) :::info 此外,這樣設定的話,由於`level2`資料夾在`level1`資料夾內,所以`level2`也會被套用到這個設定,若要針對`level2`個別設定,就在設定檔內依此類推寫一段針對`level2`的設定。 ++所以,我們設定的順序要先設定上層的資料夾,再設定下層的資料夾。因為如果上層的資料夾後面才設定,那上層資料夾的設定就會覆蓋下層資料夾的設定。++ ::: ## <Directory "路徑"> 說明 ```bash! <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> ``` - `Options` - `Indexes`:顯示檔案目錄 - `FollowSymLinks`:允許soft link - `AllowOverride` - 只有`None`或`All` - `None`:會啟動保護,不能override - `All`:會去讀`.htaccess`檔案,看設定什麼 - 接下來的`Order`、`Allow`、或`Deny`是一組的,決定誰可以存取目錄 - `Order`決定解讀的順序,看是先看`Allow`才看`Deny`,還是先看`Deny`才看`Allow` ```bash! Order allow,deny Allow from all Deny from ifpi.org.tw ``` - 這樣就是先解讀誰可以存取機器,所以是全部都可以去取,除了ifpi.org.tw不能存取 ```bash! Order deny,allow Deny from all Allow from ncku.edu.tw ``` - 拒絕全部的人,除了ncku.edu.tw連進來的 ## 設定存取特定資料夾需帳密才可存取 假如要將`level2`的存取設定成要有帳密才可存取,首先先到設定檔設定以下內容,記得`AllowOverRide`要設定成`All`,這樣存取`level2`資料夾時才會去讀取`.htaccess`檔案 ![](https://hackmd.io/_uploads/B1ocb6r83.png) 接下來在你要設定存取限制的資料夾內建立`.htaccess`檔案,以這個例子為例的話就是在`level2`資料夾內建立`.htaccess`檔案 ```bash! $ cd /var/www/html/level1/level2 $ vi .htaccess ``` `.htaccess`檔案裡面的內容: ```bash! # 第一個選項是密碼檔放在哪邊,檔名可以自己設定 # 這樣Apache就會去你指定的資料夾讀取帳密檔 AuthUserFile /tmp/.passwordfile # 第二個是提示字: AuthName "We need your password to confirm your authorization" AuthType Basic require valid-user ``` :::info - 建議`.passwordfile`檔案不要放在網頁存取的資料夾內,建議放在其他資料夾,這裡的例子是放在`/tmp/`底下 - 檔名可以自己設定,不一定要`.passwordfile` ::: ![](https://hackmd.io/_uploads/rJ4BXarI3.png) 接下來要在前面指定的資料夾內建立帳密檔-`.passwordfile`,以這個例子就是要在`/tmp`資料夾內建立`.passwordfile` ```bash! $ cd /tmp $ htpasswd -c .passwordfile user1 > 輸入密碼 ``` ![](https://hackmd.io/_uploads/S1xjNpHIn.png) :::info 這裡的`user1`跟作業系統內的使用者沒有關係,可以創自己想要的名稱 ::: 重新啟動Apache後,存取`http://localhost/level1/level2`就會要求輸入帳號密碼: ![](https://hackmd.io/_uploads/ryRkHpHLn.png) <!-- cd /var/www/html/test mv index.htm index.html http://localhost/test 這樣就能看到內容 cd /tmp ls -al 就能看到.passwordfile檔案 vi /etc/httpd/conf/httpd.conf :/Directory AllowOverride All 這樣就可以選擇性的要不要保護這個folder,用.htaccess檔案來保護 /etc/rc.d/init.d/httpd restart 這樣再輸入`http://localhost/test`就會要求輸入密碼 ![](https://hackmd.io/_uploads/BJntgcBUh.png) --> ## 設定用戶個人網站 :::info 老師說小考比較重要的部分就是個人使用者home directory這部分 ::: ### 啟動用戶個人網站功能 假如有多個用戶,user1 user2 user3...,每個用戶都希望有自己的個人網站,這是可以的,但要先到控制檔做以下設定: ```bash! # 用vi開啟Apache的設定檔 $ vi /etc/httpd/conf/conf.d # 輸入 :/UserDir,跳到要做設定的地方,找到下面的設定: <IfModule mod_userdir.c> UserDir disable #UserDir public_html </IfModule> # 將前述的內容修改成下面的內容: <IfModule mod_userdir.c> #UserDir disable UserDir public_html </IfModule> # 設定對UserDir directory的權取權 # 控制檔內有已經寫好的示範例子,但被 # 註解了,直接把#刪除即是以下的內容: <Directory /home/*/public_html> AllowOverride FileInfo AuthConfig Limit Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec <Limit GET POST OPTIONS> Order allow,deny Allow from all </Limit> <LimitExcept GET POST OPTIONS> Order deny,allow Deny from all </LimitExcept> </Directory> # 這是預設對全部使用者directory的存取控制 # 若個別的使用者想要有個別的設定,只要後面再用<Directory /home/user_name/directory>做個別設定即可 $ /etc/rc.d/init.d/httpd restart # 存檔離開控制檔之後重啟Apache ``` 接下來每個使用者只要在自己的home directory內建立`public_html`資料夾,把自己網頁的東西放在這資料夾內 再來要變更使用者home directory的權限,讓Apache可以存取: ```bash! $ chmod -R 755 /home/user_name ``` 接下來即可藉由`http://localhost/~user_name`拜訪使用者的個人網頁 也就是假如我現在有一個使用者`user1`,我就在`/home/user1`底下建立`public_html`資料夾,把網頁的東西放在這資料夾內,並執行`chmod -R 755 /home/user1`來更改權限,就可以透過`http://localhost/~user1`來存取 ### 更改用戶個人網站之預設資料夾`public_html` Apache預設的個人首頁是放置在home directory下的`public_html/`目錄,假如想改成`www/`這個目錄,只要將前述設定中的`public_html`都改成`www`即可: ```bash! # 用vi開啟Apache的設定檔 $ vi /etc/httpd/conf/conf.d <IfModule mod_userdir.c> #UserDir disable UserDir www # <- 這裡改成www </IfModule> <Directory /home/*/www> # <- 這裡改成www AllowOverride FileInfo AuthConfig Limit Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec <Limit GET POST OPTIONS> Order allow,deny Allow from all </Limit> <LimitExcept GET POST OPTIONS> Order deny,allow Deny from all </LimitExcept> </Directory> # 存檔離開控制檔之後重啟Apache $ /etc/rc.d/init.d/httpd restart ``` ### 更改存取網址`~user_name` by `Alias` Apache預設存取每個用戶的網址是`http://localhost/~user_name`,但我們可以做修改,用自己想要的名字,ex. `http://localhost/myalias`,只要進入設定檔做以下設定: ```bash! $ vi /etc/httpd/conf/conf.d # 進入Apache的設定檔 Alias /myalias "/home/user1/www/" # 設定alias,讓 /myalias 可以連到/home/user1/www/ # 設定/home/user1/www的存取控制 # 在不會影響到其他設定、或不會被其他設定影響到的地方輸入以下內容: <Directory "/home/user1/www"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> # 重新啟動Apache即可 $ /etc/rc.d/init.d/httpd restart ``` :::danger 若寫成`Alias /myalias "/home/user1/www/"`,`/myalias`後面沒有`/`,這樣用`http://localhost/myalias`和`http://localhost/myalias/`都可以去拜訪網頁 但若寫成`Alias /myalias/ "/home/user1/www/"`,也就是`/myalias`後面有`/`,這樣只有`http://localhost/myalias/`才能拜訪到網頁,用`http://localhost/myalias`會無法拜訪網頁 所以建議`/myalias`後面不要加`/`,比較不會出問題 $Reference$ http://phorum.study-area.org/index.php?topic=21235.0 ::: <!-- ![](https://hackmd.io/_uploads/SygFwVLL3.png) --> ### 更改存取網址`~user_name` by Soft Link 假如在`/home/user2/`下建立`homepage`資料夾,在裡面建立`index.html`,希望可以不用透過前面那些修改控制檔就能藉由`http://localhost/user2`存取`/home/user2/homepage/index.html`,以下是作法: ```bash! $ cd /var/www/html $ ln -s /home/user2/homepage user2 # 在 /var/www/html 裡面建立一個soft link,叫做user2,這個soft link會連到 /home/user2/homepage $ chmod -R 755 /home/user2 # 更改user2這個資料夾裡面全部東西的權限成755 ``` 這樣就能藉由`http://localhost/user2`來存取`/home/user2/homepage/index.html`裡面的網頁啦~ :::info 這是由於我們對「首頁」的設定的`Options`內有`FollowSymLinks`這個參數,所以才可以使用Soft link ::: ## 較新的作業系統 ### 啟動、重啟、終止Apache 較新的作業系統可能沒辦法用`/etc/rc.d/init.d/httpd`來啟動、關閉、或重啟,必須要改成以下指令 ```bash! $ systemctl [start][restart][stop][status] httpd.service ``` ![](https://hackmd.io/_uploads/HJFW-3BL2.png) ### 使用者個人網頁 :::info 新版的作業系統要去`/etc/httpd/conf.d`修改`userdir.conf` ![](https://hackmd.io/_uploads/ByNb17UUh.png) 原本的設定是這樣,修改成下圖: ![](https://hackmd.io/_uploads/S1li1Q8L2.png) ::: ### Alias 先開啟以下設定檔: ```bash! $ vim /etc/httpd/conf.d/userdir.conf ``` 加入以下內容: ```bash! Alias /user1 "/home/user1/www" <Directory "/home/user1/www"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted # 一定要有這個 </Directory> ``` ![](https://hackmd.io/_uploads/H1m-bSU8h.png) :::danger PS. 一定要有`Require all granted`,否則會沒有權限拜訪網頁 ::: ![](https://hackmd.io/_uploads/S1EmiALLh.png) 我們也可以創多個Alias連到同一個網址,這樣`http://localhost/myalias`和`http://localhost/user1`都會連到`http://localhost/~user1` :::info 假如有另一個user,cpt1020,他的網頁的檔案放在`/home/cpt1020/homepage`裡面,他希望`http://localhost/cpt1020`可以存取到`homepage`裡面的資料,但在設定檔裡面已經設定`UserDir www`,可以在設定檔裡面添加以下資料: ```bash! Alias /cpt1020 "/home/cpt1020/homepage" <Directory "/home/cpt1020/homepage"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> ``` 並且記得執行`chmod -R 755 /home/cpt1020`,這樣就可以拜訪`http://localhost/cpt1020` 但若是`http://localhost/~cpt1020`則會因無權限無法拜訪,因為前面設定過`UserDir www`,`~cpt1020`預設是會存取`cpt1020/www/`,可以在設定檔裡面多加以下內容,並且在`www`裡面放`index.html`並記得`chmod`即可: ```bash! <Directory "/home/cpt1020/www"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all Require all granted </Directory> ``` 但這方法不好,只是記錄一下 :::