markdown Ubuntu WebDav文件共享
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Ubuntu WebDav文件共享相关的知识,希望对你有一定的参考价值。
# HTTP
> 参考 https://www.jianshu.com/p/17da6608dc74
```
# 安装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
```
## 磁盘映射
网页里只能像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`这个项目,把值从1改为2。
在计算机-管理-服务中重启WebClient服务,或者
```
开始菜单 -> 运行 -> 输入cmd 并按回车,打开命令行
输入net stop webclient并按回车,停止网络客户端
输入net start webclient并按回车,开启网络客户端
```
然后 __在文件夹菜单栏中找到映射网络驱动器(不能空白处右键映射)__,输入网址http://树莓派IP地址/webdav或\\树莓派IP地址\webdav,然后输入用户名密码,就能映射成功了。
浏览器上:随便什么设备,只要是个浏览器就能支持。可以在线播放常用视频,直接打开图片浏览。但是不能上传。
## 挂载外部磁盘(移动硬盘、U盘)
和Samba一样,只要在/var/www/webdav/这个共享出来的文件夹中,创建个空目录,然后把移动硬盘用mount命令挂载到这个目录上。外部就可以访问了。
# HTTPS
> 参考 https://jovicailic.org/2013/10/how-to-install-apache-with-ssl-webdav-on-ubuntu/
Installing Apache with SSL
`$ sudo apt-get install apache2 libaprutil1-dbd-mysql`
Then, setup and generate a cerfitecate for the web server:
`$ sudo openssl genrsa -des3 -out server.key 1024`
You’ll be asked to enter a pass phrase.
`$ sudo openssl rsa -in server.key -out server.key.insecure`
You’ll be asked for the pass parse you used in the previous step.
This command generates the certificate you will be asked to fill in some details:
`$ sudo openssl req -new -key server.key -out server.csr`
`$ sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt`
The certificate now need coping in to the SSL directory:
```
$ sudo cp server.crt /etc/ssl/certs
$ sudo cp server.key /etc/ssl/private
```
Now wee need to enable to SSL site:
```
$ sudo a2enmod ssl
$ sudo a2ensite default-ssl
```
The web server needs to be restarted for the change to take place:
`$ sudo /etc/init.d/apache2 restart`
Now, you should now be able to access the server by typing https://ip_address in your browser.
You’ll get a certificate warning about it not being from a trust source you need to click on more details to check that it is the right computer you are connecting to.
Installing WebDAV
First you need to enable WebDav modules:
```
$ sudo a2enmod dav_fs
$ sudo a2enmod dav
```
You need to create a directory to share. For example, it can be in /home/user:
`$ mkdir webdav`
We need to give the web server and the user access to the WebDav directory:
`$sudo chown www-data:user /home/user/webdav`
Next we have to setup a password:
`$ sudo a2enmod auth_digest`
Create a directory where you will store your password files:
`$ sudo mkdir /etc/password`
Create the password file like this (WebDavCloud is the AuthName, and user1 is actual username for accessing the WebDav):
`$ sudo htdigest -c /etc/password/digest-password WebDavCloud user1`
You will be asked to type in a password. Select a strong password, you will use it for accessing to your WebDav directory.
Now we need to edit the default-ssl config files:
`$ sudo vim /etc/apache2/sites-enabled/default-ssl`
You need to find the part of the file that says:
`CustomLog /var/log/apache2/ssl_access.log combined`
and under that you need to place the following in to the file
```
Alias /webdav /home/user/webdav
#
<Directory /home/user/webdav/>
Options Indexes MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
#
<Location /webdav>
DAV On
AuthType Digest
AuthName "WebDavCloud"
AuthUserFile /etc/password/digest-password
Require valid-user
</Location>
```
其实就是:
```
Alias /webdav /var/www/webdav
#
<Directory /var/www/webdav/>
Options Indexes MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
#
<Location /webdav>
DAV On
AuthType Digest
AuthName "WebDavCloud"
AuthUserFile /etc/apache2/webdav.password
Require valid-user
</Location>
SSLEngine on
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
</VirtualHost>
</IfModule>
```
Now you need to restart the Apache:
`$ sudo /etc/init.d/apache2 restart`
You should now be able to access the WebDAV folder by going to https://ip_address/webdav
__这种方式自己产生整数不能映射到Windows上__
Note: If you would like to map your WebDav directory as a network drive from Windows, it’s most likely that you will have big problems. If you want to use WebDav from Windows, you will need to buy a commercial certificate and use it instead of self-generated certificate.
以上是关于markdown Ubuntu WebDav文件共享的主要内容,如果未能解决你的问题,请参考以下文章
markdown Linux的用davfs2挂载WebDAV的网盘