Ubuntu22.04通过Nginx配置webdav
#切换到root用户
sudo su -
#安装依赖
apt install libxslt-dev libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libgd-dev google-perftools libgoogle-perftools-dev libperl-dev libgeoip-dev
#如果直接apt install Nginx 只会安装nginx核心,但不会安装nginx-dav-ext-module模块,也就无法使用webdav的全部功能了,配置项是dav_ext_methods PROPFIND OPTIONS LOCK UNLOCK;这一条。
#下载nginx-1.18.0.tar.gz和nginx-dav-ext-module-master.zip并解压到桌面
root@hep:/home/jason/Desktop# ls
instation.txt nginx-1.18.0 nginx-1.18.0.tar.gz nginx-dav-ext-module nginx-dav-ext-module-master.zip
root@hep:/home/jason/Desktop#
#cd到Nginx根目录
cd nginx-1.18.0
#配置
./configure --with-http_dav_module --add-module=/home/jason/Desktop/nginx-dav-ext-module/ --prefix=/usr/local/nginx
#最后会出现下面内容,包括配置文件位置,nginx位置
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library0
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
#编译并安装
make && make install
#测试一下Nginx是否正确,会显示successful
sudo /usr/local/nginx/sbin/nginx -t
#运行Nginx命令
sudo /usr/local/nginx/sbin/nginx
#结束后台的Nginx
killall nginx
#创建一个目录给webdav
mkdir -p /var/www/webdav
#把权限给到jason用户
chown jason:jason /var/www/webdav
#编辑Nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#增加下面内容
location /webdav {
alias /var/www/webdav;
autoindex on;
dav_methods PUT DELETE MKCOL COPY MOVE;
dav_ext_methods PROPFIND OPTIONS LOCK UNLOCK;
create_full_put_path on;
dav_access user:rw group:rw all:r;
auth_basic "webdav";
auth_basic_user_file /etc/nginx/.htpasswd;
}
#我还把端口改成了88
listen 88;
#安装apache2-utils,就可以使用htpasswd命令了
apt install apache2-utils -y
#创建.htpasswd账号密码文件
mkdir -p /etc/nginx
htpasswd -c /etc/nginx/.htpasswd myUserName
#运行Nginx命令
sudo /usr/local/nginx/sbin/nginx
#开放88端口
ufw allow 88
ufw enable
#然后就通过服务IP和端口号进行访问了
http://192.168.31.194:88/webdav/
#创建service文件
vim /lib/systemd/system/nginx.service
#内容如下:
[Unit]
Description=nginx server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
#给nginx.service加可执行权限
chmod +x /lib/systemd/system/nginx.service
#重新加载systemctl
systemctl daemon-reload
#启动
systemctl start nginx
#停止
systemctl stop nginx
#重新加载
systemctl reload nginx
#显示nginx服务的状态
systemctl status nginx
#在开机时启用nginx服务
systemctl enable nginx
#在开机时禁用nginx服务
systemctl disable nginx