憨豆说安全 · 2020年08月12日

云芯一号教程 - NextCloud安装及配置教程

  NextCloud是一款开源网络硬盘系统,可以用于在家庭或公司构建私有且免费的网络硬盘,是一个安全且功能完整的文件同步与共享解决方案。Nextcloud提供 Android、iOS和PC桌面客户端,可以通过加密的链接同步和共享数据。

1.安装软件
  NextCloud是PHP项目,可以基于LAMP或LNMP搭建。本教程采用LNMP搭建。
1.1 安装PHP
  PHP的版本及所需要的PHP模块,请参考

  https://docs.nextcloud.com/server/19/admin_manual/installation/source_installation.html。    

  NextCloud安装完成后,启动时,如果提示还有PHP模块缺失,可以根据提示补充安装。

  jishu@Jishu:~$ sudo apt-get install php php-mysql php-fpm php-zip php-dom php-xml php-mbstring php-curl php-gd
  jishu@Jishu:~$ php -v
  PHP 7.2.24-0ubuntu0.18.04.6 (cli) (built: May 26 2020 13:09:11) ( NTS )

1.2 安装MySQL

  jishu@Jishu:~$sudo apt-get install mysql-server-5.7 
  jishu@Jishu:~$ sudo mysql
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 2
  Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)      
  ......     
  mysql> 

1.3 安装Nginx

  jishu@Jishu:~$ sudo apt-get install nginx 
  jishu@Jishu:~$ nginx -v
  nginx version: nginx/1.14.1

1.4 下载NextCloud

  jishu@Jishu:~$ wget  https://download.nextcloud.com/server/releases/nextcloud-19.0.0.zip

2.系统配置
2.1 配置数据库
  NextCloud使用MySQL数据库,需要进行数据库的配置。

  jishu@Jishu:~$ sudo mysql -u root -p
  Enter password: 
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  ......     
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  
  #创建nextcloud数据库
  mysql> create database nextcloud;
  Query OK, 1 row affected (0.00 sec)
  
  #创建访问nextcloud数据库的账号cloudadmin
  mysql> create user 'cloudadmin'@'localhost' identified by '123456';
  Query OK, 0 rows affected (0.01 sec)
  
  #赋予cloudadmin账号操作nextcloud数据库的权限 
  mysql> grant all privileges on nextcloud.* to 'cloudadmin'@'localhost' identified by '123456';
  Query OK, 0 rows affected, 1 warning (0.00 sec)
  
  #数据库变更生效
  mysql> flush privileges;
  Query OK, 0 rows affected (0.00 sec)

2.2 配置Nginx WEB服务
  NextCloud的Nginx配置请参考https://docs.nextcloud.com/se...。可根据实际情况进行修改。

  jishu@Jishu:~$ cd /etc/nginx/sites-enabled
  jishu@Jishu:/etc/nginx/sites-enabled$ sudo vim nextcloud
  upstream php-handler {
      #使用php7.2-fpm.sock加速访问
      #server 127.0.0.1:9000;
      server unix:/var/run/php/php7.2-fpm.sock;
  }
  
  #不使用https,注释该server
  #server {
  #    listen 80;
  #    listen [::]:80;
  #    server_name cloud.example.com;
  #    # enforce https
  #    return 301 https://$server_name:443$request_uri;
  #}
  
  server {
      #不使用SSL,注释掉SSL端口
      #    listen 443 ssl http2;
      #    listen [::]:443 ssl http2;
      #修改侦听8081端口
      listen 8081;
      #可根据实际情况,修改server_name
      server_name _;
  
      #不使用SSL,注释掉SSL证书        
      # 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/ssl/nginx/cloud.example.com.crt;
      #    ssl_certificate_key /etc/ssl/nginx/cloud.example.com.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;" 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
      # nextcloud代码文件目录,可修改
      root /var/www/nextcloud;
      
      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://$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;
      }
  
      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\/.+|.+\/richdocumentscode\/proxy)\.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;
          #不支持HTTPS,注释掉
          #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;
      }
  }

2.3 解压NextCloud
  解压缩NextCloud的压缩文件,并把目录复制到Nginx配置文件中指定的root目录/var/www下。

  jishu@Jishu:~$ unzip nextcloud-19.0.0.zip
  jishu@Jishu:~$ sudo mv ./nextcloud  /var/www/
  jishu@Jishu:~$ cd /var/www
  jishu@Jishu:/var/www$ sudo chown www-data:www-data -R nextcloud/

3.启动NextCloud服务
  NextCloud服务需要重新启动Nginx和php7.2-fpm。

  jishu@Jishu:~$ sudo service nginx restart
  jishu@Jishu:~$ sudo service php7.2-fpm restart

  NextCloud服务启动后,即可用浏览器访问http://ip:8081进行系统配置。配置完成后,即可在电脑、手机端通过浏览器或APP访问私有云盘了。

推荐阅读
关注数
4271
内容数
71
低成本Arm微服务器开发平台“云芯1号”教程及应用,欢迎关注
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息