顯示具有 WebDAV 標籤的文章。 顯示所有文章
顯示具有 WebDAV 標籤的文章。 顯示所有文章

2021年6月10日 星期四

How to Set Up WebDAV With Lighttpd



WebDAV stands for Web-based Distributed Authoring and Versioning and is a set of extensions to the HTTP protocol that allow users to directly edit files on the lighttpd server so that they do not need to be downloaded/uploaded via FTP. Of course, WebDAV can also be used to upload and download files.

Installing WebDAV

You can install lighttpd (if it’s not already installed), the lighttpd WebDAV module and the apache2-utils package (which contains the tool htpasswd which we will need later on to generate a password file for the WebDAV share) as follows:

1
sudo apt-get install lighttpd lighttpd-mod-webdav apache2-utils

Afterwards, we must make sure that the directory /var/run/lighttpd is owned by the www-data user and group. This directory will contain an SQLite database needed by WebDAV:

1
sudo chown www-data:www-data /var/run/lighttpd/

Next, we enable the modules mod_auth and mod_webdav:

1
2
sudo lighty-enable-mod auth 
sudo lighty-enable-mod webdav

Reload lighttpd afterwards:

1
sudo systemctl restart lighttpd

Creating A Virtual Host

I will now create a lighttpd vhost (www.example.com) in the directory /var/www/web1/web. If you already have a vhost for which you’d like to enable WebDAV, you must adjust this tutorial to your situation.

First, we create the directory /var/www/web1/web and make the lighttpd user (www-data) the owner of that directory:

1
2
sudo mkdir -p /var/www/web1/web 
sudo chown www-data:www-data /var/www/web1/web

Then we open /etc/lighttpd/lighttpd.conf and add the following vhost to the end of the file:

1
2
3
4
5
:~$ vim /etc/lighttpd/lighttpd.conf
[...]
$HTTP["host"] == "www.shixuen.com" {
server.document-root = "/var/www/web1/web"
}

Afterwards we restart lighttpd:

1
:~$ sudo systemctl restart lighttpd

Configure The Virtual Host For WebDAV

Now we create the WebDAV password file /var/www/web1/passwd.dav with the user test (the -c switch creates the new password file):

1
:~$ htpasswd -c /var/www/web1/passwd.dav test

You will be asked to type in a password for the user test.

(Please don’t use the -c switch if /var/www/web1/passwd.dav is already existing because this will recreate the file from scratch, meaning you lose all users in that file!)

Now we change the permissions of the /var/www/web1/passwd.dav file so that only root and the members of the www-data group can access it:

1
2
:~$ chown root:www-data /var/www/web1/passwd.dav
:~$ chmod 640 /var/www/web1/passwd.dav

Now we modify our vhost in /etc/lighttpd/lighttpd.conf so that it looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
:~$ vim /etc/lighttpd/lighttpd.conf
$HTTP["host"] == "www.shixuen.com" {
server.document-root = "/var/www/web1/web"
alias.url = ( "/webdav" => "/var/www/web1/web" )
$HTTP["url"] =~ "^/webdav($|/)" {
dir-listing.activate = "enable"
dir-listing.encoding = "utf-8"
webdav.activate = "enable"
webdav.is-readonly = "disable"
webdav.sqlite-db-name = "/var/run/lighttpd/lighttpd.webdav_lock.db"
auth.backend = "htpasswd"
auth.backend.htpasswd.userfile = "/var/www/web1/passwd.dav"
auth.require = ( "" =>
(
"method" => "basic",
"realm" => "webdav",
"require" => "valid-user"
)
)
}
}

The alias.url directive makes ( together with $HTTP[“url”] =~ “^/webdav($|/)” ) that when you call /webdavWebDAV is invoked, but you can still access the whole document root of the vhost. All other URLs of that vhost are still “normal” HTTP.

Restart lighttpd afterwards:

1
:~$ sudo systemctl restart lighttpd

Test WebDAV

Browser

1
:~$ firefox http://www.shixuen.com/webdav

Cadaver - WebDAV client

We will now install cadaver, a command-line WebDAV client:

1
:~$ apt-get install cadaver

To test if WebDAV works, type:

1
:~$ cadaver http://www.shixuen.com/webdav/

You should be prompted for a user name. Type in test and then the password for the user test. If all goes well, you should be granted access which means WebDAV is working ok. Type quit to leave the WebDAV shell:

1
2
3
4
5
6
7
root@server1:~# cadaver http://www.shixuen.com/webdav/
Authentication required for webdav on server 'www.shixuen.com':
Username: test
Password:
dav:/webdav/> quit
Connection to 'www.shixuen.com' closed.
root@server1:~#

Modules

lighttpd docs

mod_auth

lighttpd_auth module

Supported Methods

lighttpd supports both authentication methods described by RFC 2617:

basic

The Basic method transfers the username and the password in cleartext over the network (base64 encoded) and might result in security problems if not used in conjunction with a crypted channel between client and server.

digest

The Digest method only transfers a hashed value over the network which performs a lot of work to harden the authentication process in insecure networks.

Backends

Depending on the method lighttpd provides various way to store the credentials used for the authentication.

  • For basic auth:
    • plain
    • htpasswd
    • htdigest
    • ldap
    • gssapi
    • mysql
    • pam
    • sasl
  • For digest auth:
    • plain
    • htdigest

References:

---- The End  Thanks ----

 


lighttpd.conf


2020年12月3日 星期四

Ubuntu 搭建 WebDAV NAS

爲了做個NAS,Samba 看似簡單,其實Samba的使用者設定實在太繁瑣,坑太深。使用者許可權和目錄許可權、甚至磁碟格式稍有不同,都會導致無法登入。實在不靠譜,實際體驗也不是很穩定。 不像Samba是一個微軟開發的軟體體系,WebDav只是一種協議,確切說是世界上最普遍的HTTP協議的一個小擴充套件。 它不是一個軟體 。 所以就好理解,為什麼搜尋不到WebDav的官網和官方安裝指南了——因為沒有「官方」。誰都可以開發軟體支援這個協議,就像水都可以開發瀏覽器支援HTTP協議瀏覽網站一樣。 步驟 1. 搭建 參考:How To Set Up WebDAV With Apache2 On Debian Etch  


# 安裝Apache2伺服器

sudo apt-get install -y apache2


# 開啟Apache2中對WebDav協議的支援 (記住最好在使用者目錄下執行否則報錯)

cd ~

sudo a2enmod dav

sudo a2enmod dav_fs


# 建立共享目錄並修改許可權

sudo mkdir -p /var/www/webdav

sudo chown -R www-data:www-data /var/www/webdav


# 建立WebDav的訪問用戶數據庫,順便建立使用者`pi`

sudo htpasswd -c /etc/apache2/webdav.password pi


# 建立guest使用者

#sudo htpasswd /etc/apache2/webdav.password guest


# 修改用戶數據庫訪問許可權

sudo chown root:www-data /etc/apache2/webdav.password

sudo chmod 640 /etc/apache2/webdav.password


# 開啟預設配置檔案

sudo vim /etc/apache2/sites-available/000-default.conf


# 全部替換為以下內容(記得先備份): 


Alias /webdav /var/www/webdav


<Location /webdav>

Options Indexes

DAV On

AuthType Basic

AuthName "webdav"

AuthUserFile /etc/apache2/webdav.password

Require valid-user

</Location>


# 重啟Apache2伺服器

sudo systemctl restart apache2

# 或 sudo /etc/init.d/apache2 reload

然後就可以用任意瀏覽器輸入: http://IP地址/webdav 來訪問了。 注意, webdav 後面沒有 / 斜槓。 網頁中如果正常顯示目錄中的檔案結構,則可以正常訪問: image 這一步完成,我們就可以開始把這個共享資料夾對映到Mac、Windows上的本地資料夾了。 2. 磁碟對映 網頁裡只能像FTP一樣顯示檔案目錄和下載檔案。 如果要正常使用,我們需要把它對映為本地目錄才行: Mac上:在Finder中用 CMD+K 開啟連線伺服器選項,輸入 http://樹莓派IP地址/webdav ,輸入Webdav建立過的使用者名稱密碼來完成對映。 iPhone上:安裝網盤訪問最強的 Readdle Documents ,新增WebDav服務,輸入資訊後就可以訪問。直接看文件、看視訊、聽歌都行。 Windows上:比較麻煩的是,Win7以上預設只支援HTTPS的網路驅動器,做為HTTP的WebDav是不能連的。所以要修改Windows登錄檔,讓它支援HTTP。方法入下: 開始選單 -> 執行 -> 輸入regedit 並按回車,就開啟了登錄檔 登錄檔中找到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WebClient\Parameters\BasicAuthLevel 這個專案,把值改為 2 。 開始選單 -> 執行 -> 輸入cmd 並按回車,開啟命令列 輸入 net stop webclient 並按回車,停止網路客戶端 輸入 net start webclient 並按回車,開啟網路客戶端 然後在資料夾選單中找到 對映網路驅動器 ,輸入網址 http://樹莓派IP地址/webdav 或 \\樹莓派IP地址\webdav ,然後輸入使用者名稱密碼,就能對映成功了。 瀏覽器上:隨便什麼裝置,只要是個瀏覽器就能支援。可以線上播放常用視訊,直接開啟圖片瀏覽。但是不能上傳。

原文網址:https://itw01.com/8S6M4EC.html