2011年12月26日 星期一

安裝 openwebmail

安裝前先更新清單
$sudo apt-get update


安裝收信套件 Dovecot
$sudo apt-get install dovecot-pop3d

$sudo apt-get install dovecot-imapd

openwebmail 應該只支援 POP3,但是我這裡還是將 IMAP 也裝起來


安裝寄信套件 Postfix
$sudo apt-get install postfix

安裝中會進行設定
 這裡要選擇監視的介面,選擇第 2 項,從網路來的郵件我都接收

 這裡要輸入你的主機的網域名稱

剩下的暫時都使用預設



libmd5-perlowm 這 2 個套件檔案放進系統內,因為這 2 個套件已經不在 Ubuntu 的更新清單中了,所以要自行另外去尋找

安裝 libmd5-perl ( 此為 openwebmail 必要套件 )
$sudo  dpkg libmd5-perl_2.03-1_all.deb

安裝 openwebmail
$sudo  dpkg owm2.53-2.deb

openwebmail 安裝過程會告訴你還少了幾個檔案,這時再用下列指令
$sudo apt-get -f install

系統會自動從網路清單下載缺少的套件

重新開機後輸入 : http://192.1681.10/openwebmail
便可以看見安裝成功的畫面

郵件登入網址 : http://192.168.1.10/cgi-bin/openwebmail/openwebmail.pl

但網址實在是太長了,所以我們要進行更名

修改網址
$sudo nano /etc/apache2/httpd.conf

ScriptAlias /webmail    /usr/lib/cgi-bin/openwebmail/openwebmail.pl

重新啟動 apache
$sudo /etc/init.d/apache2 restart

之後只要輸入 : http://192.1681.10/webmail  就可以進入登入畫面了

Apache 拒絕連線、認證

拒絕連線
$sudo nano /etc/apache2/httpd.conf
                     :
                     :
<Limit GET POST OPTIONS>
       Order allow,deny
       Allow from all
       Deny from 192.168.10.10           --阻擋此 IP 的連線
</Limit>


Apache 認證 ( 需輸入帳號密碼 )
在家目錄的 www 之下建立 .htaccess
$nano .htaccess

AuthName         "Protect by .htaccess"
AuthType           Basic
AuthUserFile  /home/student/apache.passwd        --紀錄帳號密碼的地方
Require  valid-user

$chmod +x .htaccess

執行
$htpasswd -c ~/apache.passwd student

然後輸入 2 次密碼

~/apache.passwd : 紀錄帳號密碼的地方
student : 網頁登入的帳號

到網頁連線 http://192.168.1.10/~student/ 時,就會要求輸入剛剛設定的帳號密碼

Apache 相關設定

指令 ( 檢視相關設定 )
$/usr/sbin/apache2 -V

/etc/apache2/apache2.conf          --紀錄 apache 設定檔的位置

檢視 apache 相關設定檔的設定
$nano /etc/apache2/apache2.conf

Include /etc/apache2/httpd.conf          --主要設定檔

Include /etc/apache2/sites-enabled/      --只給目錄的話它會將目錄下的設定檔全部都讀取


更改預設首頁位置
$sudo nano /etc/apache2/sites-enabled/000-default

/var/www                --設定預設首頁的目錄位置


Apache 的模組功能
$ls /etc/apache2/mods-available/          --這裡放置的是 apache 裡能使用的全部模組

$ls /etc/apache2/mods-available/          --這裡放置的是啟動中的模組

.conf  --設定檔
.load  --模組檔

要啟動模組功能,只要從 mods-available 下,將要啟動的功能的 2 個檔案複製到 mods-available之下就可以啟動了

或是使用指令
$sudo a2enmod userdir       --增加 userdir 這模組

$sudo a2dismod userdir       --刪除 userdir 這模組

製作程式 (網頁看IP、user)

建立檔案
$nano mkhome.sh


echo "<html><head></head><body>" > /var/www/index.html

echo "<pre>" >> /var/www/index.html

/home/student/ipconfig.sh >> /var/www/index.html

/home/student/ug.sh >> /var/www/index.html

echo "</pre>" >> /var/www/index.html

echo "</body></html>" >> /var/www/index.html


修改權限
$chmod +x mkhome.sh




執行程式
$./mkhome.sh

開啟瀏覽器,並輸入首頁網址

http://192.168.1.10

就可以看見執行程式的結果輸出到網頁



備註:在student家目錄下必須要有 ipconfig.sh 、 ug.sh 此執行檔才會生效

檢視使用者名單

建立檔案
$nano ug.sh
   
            u=$( cat /etc/passwd | cut -d ':' -f1 )
            g=$( cat /etc/group | cut -d ':' -f1 )

            echo '[ user ]'
            echo $u

            echo '[ group ]'
            echo $g

增加執行權限
$chmod +x ug.sh

執行檔案
$./ug.sh

撰寫程式 產生10名使用者

建立使用者名單 users.txt

 $touch users.txt

 $nano users.txt

把10名使用者及密碼寫上(可自行設定)

a001:student
a002:student
a003:student
a004:student
a005:student
a006:student
a007:student
a008:student
a009:student
a010:student


編寫自動產生使用者的程式,名字為 useradd.sh

$touch useradd.sh

$chmod +x useradd.sh

$nano useradd.sh

編寫程式如下:

[ ! -f users.txt ] && echo "file not found" && exit 1
 
 
s=$(cat users.txt)
 
 
for  us  in $s
do
    uname=${us%%:*}
 
 
    upasswd=${us##*:}
 
 
    useradd -m -s /bin/bash $uname
    echo "$uname:$upasswd" | chpasswd

    mkdir  /home/$uname/www
    chown "$uname:$uname" /home/$uname/www

    echo "$uname home" >  /home/$uname/www/index.html
    chown "$uname:$uname"  /home/$uname/www/index.html
 
done


執行程式
$sudo ./useradd.sh

Apache 建置各使用者網頁

啟動 Apache 模組功能
$sudo a2enmod userdir

a2 : apache2
en : enable
mod : module


修改 xml 設定檔
$sudo nano /etc/apache2/httpd.conf

由於檔案時空的,所以貼上以下內容

UserDir www   

<Directory /home/*/www>
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec


    # If GET is used it will also restrict HEAD requests. The TRACE method cannot be limited.
    <Limit GET POST OPTIONS>
        Order allow,deny
        Allow from all
    </Limit>


    <LimitExcept GET POST OPTIONS>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>

UserDir www                         --表示網頁目錄是在各使用者家目錄的 www 之下

<Directory /home/*/www>     --將設定使用在 home 下的"所有目錄"下的 www


重新啟動 apache2 才會套用設定
$sudo /etc/init.d/apache2 restart

在使用者家目錄下建立網頁,以 student 這使用者為例 :

$cd /home/student                   --切換目錄
    
$mkdir www                             --建立 www 目錄

$nano /www/index.html           --新增首頁並加入內容

內容輸入 : This is a test for student

連線測試,例如主機 IP 為 192.168.1.10,開啟瀏覽器在網址輸入 :
http://192.168.1.10/~student/

有看見剛剛輸入的文字就成功了