docker安裝nextcloud私人網盤,開啟https配置證書

辛哥發表於2021-06-19

docker安裝nextcloud私人網盤

之前一直用的百度網盤最近svip超級會員到期了,續費要¥199元,對於一個打工人的我來說有點兒貴.作為技術人的一員,我就來發揮發揮自己的長處,來搭建一個私人網盤用用!

功能介紹

軟體名:nextcloud給你的資料一個家

  • 設定本地同步資料夾,資料夾中的檔案自動同步到雲
  • 客戶端支援手機端、PC端、蘋果app store
  • 自動備份手機上的照片到雲
  • 支援各種豐富的外掛各種功能應有盡有
  • 免費開源、社群活躍
  • 安裝完maps外掛可以將你手機備份上來的照片自動顯示到地圖上噢非常的好用、像下面這樣

安裝服務端

準備工作:通過ssh連線到伺服器一臺linux伺服器

  • 第一步安裝docker

    1. 解除安裝有可能存在的舊版本的docker

          sudo yum remove docker \
                    docker-client \
                    docker-client-latest \
                    docker-common \
                    docker-latest \
                    docker-latest-logrotate \
                    docker-logrotate \
                    docker-engine
      
    2. 安裝需要的工具

         sudo yum install -y yum-utils
      
    3. 配置安裝源(阿里源)# 源不可用的話可以自行百度搜尋docker 阿里源

      sudo yum-config-manager \
      --add-repo \
      http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
      
    4. 安裝最新版本的 Docker Engine-Community 和 containerd

      yum install docker-ce docker-ce-cli containerd.io
      
    5. 啟動docker

      systemctl start docker
      
    6. 執行docker的hello world驗證docker是否安裝成功

      docker run hello-world
      
  • 第二部安裝mysql

     docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=你的資料庫密碼 -d mysql
    

    這裡要保證宿主伺服器的3306埠沒有被佔用

  • 第三步安裝nextcloud
    這裡要安裝的是nextcloud:fpm版本,後面會用nginx代理到nextcloud的fpm 9000埠。使用nginx的為了方便開啟https訪問部署證照,因為nextcloud的很多外掛和功能要求使用https訪問才能夠使用

    docker run -d \
    --name nextcloud \
    -v /home/xiner/nextcloud:/var/www/html \   
    --link mysql:mysql \
    nextcloud:fpm
    
  • 第四步安裝nginx

    docker run -d   \
    --name nginxfornextcloud \
    -p 8080:80 \
    -p 4433:443 \
    -v /home/xiner/nextcloud:/var/www/html \
    -v /docker/volumns/nginx/nginx.conf:/etc/nginx/nginx.conf  \
    -v /docker/volumns/nginx/conf.d:/etc/nginx/conf.d  \
    -v /docker/volumns/nginx/ssl_certs:/etc/nginx/ssl_certs \
    --link mysql:mysql  \
    --link nextcloud:nextcloud \
    nginx
    

    這裡的/home/xiner/nextcloud要與第三步的/home/xiner/nextcloud掛在路徑保持一致

  • 第五部修改配置nginx
    將你的ssl證照放入第四步掛載的宿主伺服器目錄:/docker/volumns/nginx/ssl_certs 下

    新建一個配置檔案default.conf內容如下,放入第四步中掛載的宿主伺服器目錄:/docker/volumns/nginx/nginx.conf 下

    upstream php-handler {
        server nextcloud:9000;
    
    }
    
    server {
        listen 80;
        listen [::]:80;
        server_name  localhost;
        # enforce https
        return 301 https://$server_name:443$request_uri;
    }
    
    server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name localhost;
    
        # Use Mozilla's guidelines for SSL/TLS settings
        # https://mozilla.github.io/server-side-tls/ssl-config-generator/
        # NOTE: some settings below might be redundant
         ssl_certificate /etc/nginx/ssl_certs/你的證照.pem;
         ssl_certificate_key /etc/nginx/ssl_certs/你的證照.key;
    
        # 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;";
        #
        # 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 X-Content-Type-Options nosniff;
        add_header X-XSS-Protection "1; mode=block";
        add_header X-Robots-Tag none;
        add_header X-Download-Options noopen;
        add_header X-Permitted-Cross-Domain-Policies none;
        add_header Referrer-Policy no-referrer;
        add_header Strict-Transport-Security  15552000;
       # add_header X-Frame-Options SAMEORIGIN;
    
    
        # 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;
    
    
    
        # 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://$host:$server_port/remote.php/dav;
        }
        location = /.well-known/caldav {
          return 301 $scheme://$host:$server_port/remote.php/dav;
        }
    
        # set max upload size
        client_max_body_size 512M;
        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$request_uri;
        }
    
        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)(\/.*|)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_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;";
            #
            # 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 X-Content-Type-Options nosniff;
            add_header X-XSS-Protection "1; mode=block";
            add_header X-Robots-Tag none;
            add_header X-Download-Options noopen;
            add_header X-Permitted-Cross-Domain-Policies none;
            add_header Referrer-Policy no-referrer;
    
            # Optional: Don't log access to assets
            access_log off;
        }
    
        location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
            try_files $uri /index.php$request_uri;
            # Optional: Don't log access to other assets
            access_log off;
        }
    }
    

    放好配置檔案後重啟nginx docker restart nginxfornextcloud

  • 第六部安裝完成
    經過上面五步你的私人云盤就搭建成功了!是不是特別簡單,現在訪問的的nginx的監聽埠吧,我的第四步對映的埠為4433所以訪問
    https://[你的伺服器IP]:4433

    輸入你的網盤管理員使用者名稱和密碼。這裡資料庫的主機名要填寫你第三步安裝nextcloud時候的--link mysql:mysql對應我這裡應該資料庫主機名填寫:mysql
    然後點選安裝等待安裝完成就可以了。

  • 第七步 探索你的私人網盤吧

相關文章