利用软件Nextcloud在服务器上搭建个人云盘

旧时模样 提交于 2019-12-10 06:24:41

利用软件Nextcloud在服务器上搭建个人云盘

因为学校给每个学生都分配了云服务器,容量上限是100G,但服务器只给内网ip,校外网是连接不了的,因此不能对外开放,这种服务器做个人网盘比较好,于是利用免费的Nextcloud自己搭建了云盘,方便存储部分大的文件。

对于我说,我有两台电脑,一台台式和一台笔记本,笔记本用于日常带去上课,两台电脑的烦恼在于信息共享,有时候,在一台电脑编辑一个文件但没共享到另一台就很烦,对于小文件采用微软的Onedrive进行共享,基本一台电脑编辑文件的时候,自动上传,另一台电脑就获得了最新的文件,但是由于国内网速问题,下载的网速很慢,不适合大文件共享,这个学校的服务器就可以进行大文件的存储,上传和下载的速度都在10MB/S。

在这里插入图片描述

一、环境说明

  • 环境:Centos7
  • Mysql: 5.7.24
  • nginx:1.16
  • nextcloud:13.0.2

二、步骤

1.安装nginx和php

nextcloud是PHP项目,对于网页服务器来说,可以是Apache或者Nginx,搭建的过程大同小异,我这里采用的是Nginx。

# 先查看系统可能自带的PHP和nginx,用下面命令查找是否有安装
rpm -qa |grep php
rpm -qa |grep nginx
#如果有就不必再安装了,当然你也可以卸载,再重新安装新版

#安装nginx
yum install -y nginx
#安装PHP
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#安装php的扩展插件
yum install -y php70w-devel php70w-pear php70w-pecl php70w-gd php70w-opcache php70w-cli php70w-pdo php70w-process php70w-pecl-apcu php70w-mcrypt php70w-mysql php70w-fpm php70w-pecl-memcached php70w-common php70w-xml php70w-mbstring php70w-pecl-igbinary

#此时应该已经完成安装,可以查看版本
nginx -v; php -v

在此过程中,可能提示需要给服务器添加其他依赖,如果提示,利用yum安装即可;

2.配置数据库

nextcloud需要数据库支持,我这里使用mysql数据库。可以参考另一篇博文:数据库的安装和配置
在mysql中创建一个新的database用于nextcloud使用

# 在装有mysql的服务器上执行
mysql -u root -p
> create database nextcloud_db;
# 授权给自定义用户,这里以用户名:nextclouduser,密码:nextcloudpasswd,代替。
> grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextcloudpasswd';
> flush privileges;

3.为nextcloud生成自签名ssl证书

$ cd /etc/nginx/cert/    # 没有则创建此文件夹
$ openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
# 会出现下面的选项需要填写,可以随便填。
Country Name (2 letter code) [XX]:cn                                 # 国家
State or Province Name (full name) []:jiangsu                     # 省份
Locality Name (eg, city) [Default City]:nanjing                   # 地区名字
Organization Name (eg, company) [Default Company Ltd]:nju           # 公司名
Organizational Unit Name (eg, section) []:SE                 # 部门
Common Name (eg, your name or your server's hostname) []:xx        # CA主机名
Email Address []:xxx                                     # Email地址
# 修改证书和文件夹权限
$ chmod 600 /etc/nginx/cert/*
$ chmod 700 /etc/nginx/cert

4.下载nextcloud,并配置php和nginx

1.安装nextcloud

# 下载nextcloud,官网地址为: https://nextcloud.com/install/#instructions-server
$ cd /usr/local/src
#安装能解压zip的软件
$ yum install -y wget unzip
$ wget https://download.nextcloud.com/server/releases/nextcloud-13.0.2.zip    # 下载
$ unzip nextcloud-13.0.2.zip    # 解压
$ mv nextcloud /usr/share/nginx/html/    # 移动到指定文件夹内
$ cd /usr/share/nginx/html/nextcloud     # 进行nextcloud 目录中
$ mkdir data    # 创建数据文件夹
$ chown nginx:nginx -R nextcloud/    # 将nextcloud文件授权给nginx

2.配置PHP

# 配置php-fpm
$ vim /etc/php-fpm.d/www.conf
--------------------------------------------------------------------------------
user = nginx                                   //将用户和组都改为nginx
group = nginx
listen = 127.0.0.1:9000
env[HOSTNAME] = $HOSTNAME                     
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
--------------------------------------------------------------------------------
 
 
# 为php创建session文件夹
$ mkdir -p /var/lib/php/session
$ chown nginx:nginx -R /var/lib/php/session/
 
 
# 配置nginx
$ cd /etc/nginx/conf.d/
$ vim nextcloud.conf
--------------------------------------------------------------------------------
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php5-fpm.sock;
}
 
 
server {
    listen 80;
    server_name localhost;
    # enforce https
    rewrite ^(.*)$ https://$host$1 permanent;
}
 
 
server {
    listen 443 ssl;
    server_name localhost;
 
    ssl_certificate /etc/nginx/cert/nextcloud.crt;
    ssl_certificate_key /etc/nginx/cert/nextcloud.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;";
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    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;
 
    # Path to the root of your installation
    root /usr/share/nginx/html/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;
 
 
    location = /.well-known/carddav {
      return 301 $scheme://$host/remote.php/dav;
    }
    location = /.well-known/caldav {
      return 301 $scheme://$host/remote.php/dav;
    }
 
 
    # set max upload size
    client_max_body_size 10240M;    # 上传文件最大限制,php.ini中也要修改,最后优化时会提及。
    fastcgi_buffers 64 4K;
 
    # Disable gzip to avoid the removal of the ETag header
    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;
 
 
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;
 
 
    location / {
        rewrite ^ /index.php$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/.+|ocs-provider/.+|core/templates/40[34])\.php(?:$|/) {
        include fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        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;
        fastcgi_param front_controller_active true;
        fastcgi_pass php-handler;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }
 
 
    location ~ ^/(?:updater|ocs-provider)(?:$|/) {
        try_files $uri/ =404;
        index index.php;
    }
 
 
    # Adding the cache control header for js and css files
    # Make sure it is BELOW the PHP block
    location ~* \.(?:css|js)$ {
        try_files $uri /index.php$uri$is_args$args;
        add_header Cache-Control "public, max-age=7200";
        # 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;";
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options "SAMEORIGIN";
        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;
        # Optional: Don't log access to assets
        access_log off;
    }
 
    location ~* \.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
        try_files $uri /index.php$uri$is_args$args;
        # Optional: Don't log access to other assets
        access_log off;
    }
}
----------------------------------------------------------------------------------
 
# 将nginx原配置中80端口的配置删除vim /etc/nginx/nginx.conf
--------------------------------------------------------
server {          # 将80端口的server整个删除,应该我们在上面nextcloud.conf中已经配置了,这里不删除的话会导致冲突不生效。
    listen 80;
...
  }

可以通过修改上述文件,修改访问的端口。

5.启动nginx和php-fpm

 nginx -t    # 检查nginx配置是否正确,出现下面输入则正确。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
 
# 启动并设为开机启动 nginx,php-fpm
systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm
 
 
# 关闭防火墙
systemctl stop firewall.service

6、访问网页界面,完成安装,注册云盘账号密码

访问云盘的地址为ip地址加端口号,登陆界面为
在这里插入图片描述

登陆后就可以自由玩耍了,其中的细节可以慢慢研究。

另外也有nextcloud的手机版,下载后可以手机上传和下载文件。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!