树莓派上搭建Nextcloud

0x00 环境说明

树莓派型号:Raspberry Pi 4B 4G
SD卡:Sandisk 128G
系统:Ubuntu Server 20.04 LTS(64bit)
软件源:mirrors.tuna.tsinghua.edu.cn
所用软件:mariadb+nginx+php7.4
Nextcloud数据盘位置: /data/data

0x01 软件安装

本文使用nginx进行配置,您可能需要卸载掉系统默认安装的apache
卸载命令 sudo apt remove apache2

安装软件:sudo apt install vim curl wget zip mariadb-server nginx php7.4 php7.4-fpm php7.4-mysql php7.4-xml php7.4-curl php7.4-zip php7.4-gd php7.4-mbstring php7.4-intl php7.4-bcmath php7.4-gmp php7.4-imagick

至此,大致所需软件已经安装完成,现在我们前往官网下载nextcloud安装包
笔者安装时最新版本为20.05,大家可自行前往官网下载并放入/var/www/nextcloud目录内
cd /var/www && sudo wget https://download.nextcloud.com/server/releases/nextcloud-20.0.5.zip && sudo unzip nextcloud-20.0.5.zip

0x02 数据库配置

# 以下密码YOUR_PASSWORD请自行设置

sudo mysql

# 修改root用户密码

set password for 'root'@'localhost'=password('YOUR_PASSWORD');

# 创建nextcloud数据库

create database nextcloud;

# 创建nextcloud数据库用户(当然如果觉得省事你大可直接使用root用户)

create user 'nextcloud'@'localhost' identified by 'YOUR_PASSWORD';

# 授权

grant all privileges on nextcloud.* to 'nextcloud'@'localhost' identified by 'YOUR_PASSWORD';

exit;

0x03 Nginx配置

sduo vim /etc/nginx/sites-enabled/default
然后使用vim快捷键删除所有内容并添加如下我给的配置文件
gg:回首行,dG当前行之后的全部内容
(当然,这是为了方便我自己后面部署其他服务,您也可以参照nextcloud文档自行配置)

upstream php-handler {
    server unix:/var/run/php/php7.4-fpm.sock;
}
server {
    listen 80 default_server;
    root /var/www;
    index index.php index.html index.htm;
    #include /etc/nginx/conf.d/*.conf;
    server_name _;
    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Scheme $scheme;
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
            fastcgi_split_path_info    ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            include fastcgi_params;
    }
    location /.well-known {
        rewrite ^/\.well-known/host-meta\.json  /nextcloud/public.php?service=host-meta-json    last;
        rewrite ^/\.well-known/host-meta        /nextcloud/public.php?service=host-meta         last;
        rewrite ^/\.well-known/webfinger        /nextcloud/public.php?service=webfinger         last;
        rewrite ^/\.well-known/nodeinfo         /nextcloud/public.php?service=nodeinfo          last;
        location = /.well-known/carddav   { return 301 /nextcloud/remote.php/dav/; }
        location = /.well-known/caldav    { return 301 /nextcloud/remote.php/dav/; }
        try_files $uri $uri/ =404;
    }   
    location ^~ /nextcloud {
        client_max_body_size 40960M;
        fastcgi_buffers 64 4K;
        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;
        location /nextcloud {
            rewrite ^ /nextcloud/index.php$request_uri;
        }
        location ~ ^\/nextcloud\/(?: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 modHeadersAvailable true;
            fastcgi_param front_controller_active true;
            fastcgi_pass php-handler;
            fastcgi_intercept_errors on;
            fastcgi_request_buffering off;
            add_header X-Frame-Options SAMEORIGIN;
            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 "max-age=15768000;includeSubDomains;";
        }
        location ~ ^\/nextcloud\/(?:updater|oc[ms]-provider)(?:$|\/) {
            try_files $uri/ =404; index index.php;
        }
        location ~ ^\/nextcloud\/.+[^\/]\.(?:css|js|woff2?|svg|gif)$ {
            try_files $uri /nextcloud/index.php$request_uri;
            add_header Cache-Control "public, max-age=15778463";
            add_header Strict-Transport-Security "max-age=15768000;includeSubDomains;";
            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";
            access_log off;
        }
        location ~ ^\/nextcloud\/.+[^\/]\.(?:png|html|ttf|ico|jpg|jpeg)$ {
            try_files $uri /nextcloud/index.php$request_uri;
            access_log off;
        }
    }
}

配置完之后使用sudo systemctl restart nginx重启nginx

0x04 安装Nextcloud

首先更改一下文件目录权限:sudo chown -R www-data:www-data config apps

请确保数据目录所有权为www-data

接着在浏览器输入树莓派的IP/nextcloud即可看见nextcloud安装界面如 http://192.168.0.107/nextcloud

紧接着我们输入自己创建的配置点击安装即可,配置如下图所示

0x05 优化

如在nextcloud设置-概览里遇到一些警告,您可以尝试阅读以下文章

Nextcloud搭建完成后的一些安全问题的解决方案

0x06 后记

原创文章,如需转载,请注明出处

LICENSED UNDER CC BY-NC-SA 4.0
Comment