使用容器部署nextcloud ## 使用fpm的方式部署 ### 申请域名并配置好A记录 先申请一个域名,并且将A记录解析到需要部属的机器IP。 这里以`pan.yinzihao.com`为例 ### 使用docker-compose部署服务 首先在目录下创建这些文件,创建nextcloud文件夹 ![](/static/upload/202111/file_1637160161000_32.png) run.sh ``` nohup bash /home/loop.sh & /entrypoint.sh php-fpm ``` loop.sh ``` apt update apt install smbclient -y apt install sudo procps -y while : do echo will run cron sudo -u www-data php -d memory_limit=512M -f /var/www/html/cron.php sleep 600 done ``` 注意:随着nextcloud的镜像不断更新,sources.list要同步更新。 如果不在意速度,注释掉docker-compose.yml中的:`- ./sources.list:/etc/apt/sources.list` sources.list ``` deb http://mirrors.ustc.edu.cn/debian bullseye main deb-src http://mirrors.ustc.edu.cn/debian bullseye main deb http://mirrors.ustc.edu.cn/debian-security/ bullseye-security main deb-src http://mirrors.ustc.edu.cn/debian-security/ bullseye-security main deb http://mirrors.ustc.edu.cn/debian bullseye-updates main deb-src http://mirrors.ustc.edu.cn/debian bullseye-updates main ``` docker-compose.yaml ``` version: '2' services: app: container_name: nextcloud image: nextcloud:25.0.4-fpm command: bash -c "bash /home/run.sh" restart: always volumes: - ./nextcloud/custom_apps:/var/www/html/custom_apps - ./nextcloud/config:/var/www/html/config - ./nextcloud/data:/var/www/html/data - ./nextcloud/apps:/var/www/html/apps - ./loop.sh:/home/loop.sh - ./run.sh:/home/run.sh - ./sources.list:/etc/apt/sources.list environment: - MYSQL_PASSWORD= - MYSQL_DATABASE=nextcloud - MYSQL_USER=nextcloud - MYSQL_HOST=db web: image: nginx restart: always ports: - 8199:80 - 8200:443 links: - app volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro volumes_from: - app redis: image: redis restart: always links: - app ``` nginx.conf 注意:在nginx_https.conf中,将`pan.yinzihao.com` 替换成自己的域名 ``` worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; set_real_ip_from 10.0.0.0/8; set_real_ip_from 172.16.0.0/12; set_real_ip_from 192.168.0.0/16; real_ip_header X-Real-IP; #gzip on; upstream php-handler { server app:9000; } server { listen 80; server_name pan.yinzihao.com; # Add headers to serve security related headers # Before enabling Strict-Transport-Security headers please read into this # topic first. #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always; # # WARNING: Only add the preload option once you read about # the consequences in https://hstspreload.org/. This option # will add the domain to a hardcoded list that is shipped # in all major browsers and getting removed from this list # could take several months. add_header Referrer-Policy "no-referrer" always; add_header X-Content-Type-Options "nosniff" always; add_header X-Download-Options "noopen" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Permitted-Cross-Domain-Policies "none" always; add_header X-Robots-Tag "none" always; add_header X-XSS-Protection "1; mode=block" always; # Remove X-Powered-By, which is an information leak fastcgi_hide_header X-Powered-By; # Path to the root of your installation root /var/www/html; location = /robots.txt { allow all; log_not_found off; access_log off; } # The following 2 rules are only needed for the user_webfinger app. # Uncomment it if you're planning to use this app. #rewrite ^/.well-known/host-meta /public.php?service=host-meta last; #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last; # The following rule is only needed for the Social app. # Uncomment it if you're planning to use this app. #rewrite ^/.well-known/webfinger /public.php?service=webfinger last; location = /.well-known/carddav { return 301 $scheme://$http_host/remote.php/dav; } location = /.well-known/caldav { return 301 $scheme://$http_host/remote.php/dav; } # set max upload size client_max_body_size 10G; fastcgi_buffers 64 4K; # Enable gzip but do not remove ETag headers gzip on; gzip_vary on; gzip_comp_level 4; gzip_min_length 256; gzip_proxied expired no-cache no-store private no_last_modified no_etag auth; gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy; # Uncomment if your server is build with the ngx_pagespeed module # This module is currently not supported. #pagespeed off; location / { rewrite ^ /index.php; } location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ { deny all; } location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) { deny all; } location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) { fastcgi_split_path_info ^(.+?\.php)(\/.*|)$; set $path_info $fastcgi_path_info; try_files $fastcgi_script_name =404; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $path_info; #fastcgi_param HTTPS on; # Avoid sending the security headers twice fastcgi_param modHeadersAvailable true; # Enable pretty urls fastcgi_param front_controller_active true; fastcgi_pass php-handler; fastcgi_intercept_errors on; fastcgi_request_buffering off; } location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) { try_files $uri/ =404; index index.php; } # Adding the cache control header for js, css and map files # Make sure it is BELOW the PHP block location ~ \.(?:css|js|woff2?|svg|gif|map)$ { try_files $uri /index.php$request_uri; add_header Cache-Control "public, max-age=15778463"; # Add headers to serve security related headers (It is intended to # have those duplicated to the ones above) # Before enabling Strict-Transport-Security headers please read into # this topic first. #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always; # # WARNING: Only add the preload option once you read about # the consequences in https://hstspreload.org/. This option # will add the domain to a hardcoded list that is shipped # in all major browsers and getting removed from this list # could take several months. add_header Referrer-Policy "no-referrer" always; add_header X-Content-Type-Options "nosniff" always; add_header X-Download-Options "noopen" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Permitted-Cross-Domain-Policies "none" always; add_header X-Robots-Tag "none" always; add_header X-XSS-Protection "1; mode=block" always; # Optional: Don't log access to assets access_log off; } location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap|mp4|webm)$ { try_files $uri /index.php$request_uri; # Optional: Don't log access to other assets access_log off; } } ``` 然后,运行 ``` docker-compose up -d ``` 此时,`http://ip:8199`端口已经运行了服务。进入服务,一路按提示配置。 数据库使用sqlite即可,个人用户推荐使用,运维方便。完美。 ## 添加域名 目前nextcloud在内网已经可以使用了。为了能在外网使用,我使用了两层nginx,里层的用做nextcloud的运行时,外层的用做所有服务的反向代理(网关)。 ``` nginx-> nginx(nextcloud) -> 其它服务 ``` 外层的nginx是使用nginx manager部署的。打开nginx manager: 添加转发解析,域名用自己买的域名,注意要和nginx.conf中的匹配: ![](/static/upload/202303/file_1679563394000_38.png) 并在ssl tab中申请证书。 ## 修改配置 为了更好的使用,需要在安装成功后,在config/config.php中添加下面几行: ``` 'overwriteprotocol' => 'https', # 避免在https下出现重定向问题。 'appstoreenabled' => true, # 这是为了免科学上网装插件 'appstoreurl' => 'https://www.orcy.net/ncapps/v1/', ``` ## 升级 备份原来的文件夹: ``` tar czf nextcloud.tar.gz /home/nextcloud ``` 停止容器 ``` docker-compose stop ``` 修改docker-compose.yml中的app配置中的镜像为新版 ``` docker-compose up -d ``` 此时,登录首页,按页面提示操作 ## 常用操作 ### 替换源使容器可以`apt update` 容器内如果不能执行`apt update`,替换163的源 ``` sed -i 's#http://deb.debian.org#https://mirrors.163.com#g' /etc/apt/sources.list sed -i 's#http://security.debian.org#https://mirrors.163.com#g' /etc/apt/sources.list ``` ### 安装ocdownloader插件 这个插件非常特殊,安装完后,并不能使用,需要在容器内安装需要的组件。 具体见:https://github.com/e-alfred/ocdownloader 命令: ```bash apt update apt-get install aria2 curl sudo -y mkdir /var/log/aria2c /var/local/aria2c touch /var/log/aria2c/aria2c.log touch /var/local/aria2c/aria2c.sess chown www-data.www-data -R /var/log/aria2c /var/local/aria2c chmod 770 -R /var/log/aria2c /var/local/aria2c sudo -u www-data aria2c --enable-rpc --rpc-allow-origin-all -c -D --log=/var/log/aria2c/aria2c.log --check-certificate=false --save-session=/var/local/aria2c/aria2c.sess --save-session-interval=2 --continue=true --input-file=/var/local/aria2c/aria2c.sess --rpc-save-upload-metadata=true --force-save=true --log-level=warn --rpc-listen-all=false ``` ### 清理回收站 ``` sudo -u www-data php occ trashbin:cleanup --all-users ``` 来自 大脸猪 写于 2020-02-20 17:44 -- 更新于2023-11-11 13:34 -- 0 条评论