我是靠谱客的博主 冷艳眼睛,最近开发中收集的这篇文章主要介绍OpenWrt安装配置NextCloud,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Openwrt很小巧,底层是基于类Debian的linux系统,既可以做嵌入式系统,也可以编译成x86的系统在PC上跑。NextCloud是一套不错的开源云盘程序,基于PHP开发。

本文记录了如何在Openwrt上安装配置NextCloud的例子。

此例所有操作在ubuntu下进行,用virtualbox的虚机模拟。

openwrt主机IP设置为192.168.100.1。

没有用https,考虑到这个一般是个人做云盘,野证书实在意义不大,还老被浏览器拦截,烦。

以前用过一个一键安装的脚本,体验不是很好。目前大火的DOCKER也没觉得方便到哪去,至少在openwrt上,冗余太多。

调优玩法很多,本文只是最低限度的修改一些配置让整个系统能跑起来。

尽量做到数据和系统分离,备份出来的配置回刷也不要紧,方便openwrt系统升级。

 

第一步 编译openwrt

 

选择release的版本,比如21.02。不推荐用最新的源码编译,那个还不稳定,有很多问题的。

 

包选择:

Nginx所有

Mariadb所有

PHP所有

Openssl-util 必需,就算不用https也需要的。

Luci任意,不想多个uhttpd就选择nginx版本的luci,反正nginx是逃不掉的。

Base system里blockd(block-mount会自动被选中),用于自动挂载附加的磁盘。

 

可选的:shadow-util,Aria2,bind-tool,wget

其它根据自己喜好选,如果要用DDNS就需要带上bind-tool,wget

Aria2是不错的,带上,后期在NC里面可以挂远程下载的ocDownloader插件。其它的缺啥不要紧,后期可以再装。

 

设置:

Target image里可以设置生成镜像的选项,如果要刻到U盘,就不要选GZIP images。根目录大小500M就够了,太大生成镜像时很慢还占硬盘。不够了后期刻好U盘后可以再扩容。

Image configuration > Preinit configuration options里可以设置初始的网卡IP地址,默认第一块网卡(eth0)是LAN,第二块(eth1)是WAN,如果你是双网卡的话。

刻U盘推荐rufus,真心好用。

 

第二步 设置nginx

 

把蛋疼的uci给毙了,vi etcconfignginx

config main global
	option uci_enable 'false'

 

进到etcnginx执行下面操作

cp uci.conf conf.dluci.conf

cp uci.conf nginx.conf

把新复制出来的nginx.conf裁剪成这样:

worker_processes auto;

user root;

events {

	use epoll;

}

http {
	access_log off;
	log_format openwrt
		'$request_method $scheme://$host$request_uri => $status'
		' (${body_bytes_sent}B in ${request_time}s) <- $http_referer';

	include mime.types;
	default_type application/octet-stream;
	sendfile on;

	client_max_body_size 128M;
	large_client_header_buffers 2 1k;

	gzip on;
	gzip_vary on;
	gzip_proxied any;

	include conf.d/*.conf;
}

进到conf.d里面,把luci.conf裁剪成这样:

server { 
	listen 80;
	listen [::]:80;
	server_name luci;
	root /www;	
	include conf.d/luci.locations;
}

登录下http://192.168.100.1,如果luci好使,说明nginx就正常在运行了。

 

第三步 挂载主硬盘

主硬盘要先格式化好,分区最好是EXT4的,其它的没试过。

进luci,system菜单里有mount points,如果没有,执行opkg install blockd,应该就有了。

选中你的主硬盘,勾enabled,save&apply就行了。如果没有找到要挂载的硬盘,点一下页面上方的Generate config。

默认是挂到/mnt/sda1里。

第四步 设置Mariadb(mysql)

vi /etc/config/mysqld,修改成这样,让mysql可以自动启动:

config mysqld 'general'
	option enabled '1'			# 0 - disabled, 1 - enabled
	#option options '--syslog'		# Options passed to mysqld_safe

创建数据库目录,mkdir /mnt/sda1/mysql

编辑mysql配置文件,vi /etc/mysql/conf.d/50-server.cnf,修改datadir参数对应上面创建的目录:

# Don't put this on flash memory
# Figure out where you are going to put the databases and run
# mysql_install_db --force
datadir		= /mnt/sda1/mysql

执行mysql_install_db --force初始化数据库,如果/mnt/sda1/mysql下有一堆文件生成,那就成功了。

Reboot一下,htop看看有没有mysqld的进程,正常会有一大串的。

设置mysql的root密码,mysqladmin -uroot password xxxxxx,xxxxxx就是要设置的新密码。

第五步 安装phpmyadmin

如果不需要,这步可略过。

创建目录,mkdir /mnt/sda1/www

下载最新的phpmyadmin,放到上面创建的www目录里。
wget https://files.phpmyadmin.net/phpMyAdmin/5.0/phpMyAdmin-5.0-all-languages.zip
或者下载好了用scp传进去。

在/mnt/sda1/www下,unzip phpMyAdmin-5.1.0-all-languages.zip,把解压出来的目录改短,比如pma。

创建pma的nginx配置文件, touch /etc/nginx/conf.d/pma.conf,编辑内容如下:

server {
	listen 3030;
	root /mnt/sda1/www/pma;
	index index.html index.php;

	location ~ .php$ {
		include fastcgi_params;

		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		fastcgi_index index.php;

		fastcgi_pass unix:/var/run/php7-fpm.sock;
	}
}

修改php.ini,vi /etc/php.ini,把doc_root=”/www”注释掉:

;doc_root = "/www"

重启下nginx后,访问http://192.168.100.1:3030/setup,设置并生成config.inc.php,点击页面中的“新建服务器”:

只需要填主机名和端口,“应用”后点“下载”,保存生成的config.inc.php:

把config.inc.php传进openwrt,

scp config.inc.php root@192.168.100.1:/mnt/sda1/www/pma

创建mariadb的临时目录

mkdir /mnt/sda1/www/pma/tmp

访问http://192.168.100.1:3030,登录后看页面下有警告信息,根据提示生成一下phpmyadmin自己的库,结束。新界面还挺好看的。

第六步 安装配置Nextcloud

写这篇文章时最新是20版的,下载链接,https://download.nextcloud.com/server/releases/nextcloud-20.1.zip

传到/mnt/sda1/www里,解压:

unzip nextcloud-21.0.1.zip

改下目录名,短些方便:

mv nextcloud nc

退到上级目录,修改文件夹属性:

chmod 777 nc -R
chown nobody:nogroup nc -R

编辑生成/etc/nginx/conf.d/nc.conf,内容参考https://docs.nextcloud.com/server/21/admin_manual/installation/nginx.html,里面有两种配置,这里参考第一种,裁剪下去掉https支持:
 

upstream php-handler {
    #server 127.0.0.1:9000;
    server unix:/var/run/php7-fpm.sock;
}

server {
    listen 3018;
    listen [::]:3018;
    #server_name cloud.example.com;

    # Enforce HTTPS
    #return 301 https://$server_name$request_uri;

    # HSTS settings
    # 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 Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;

    # 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;

    # Pagespeed is not supported by Nextcloud, so if your server is built
    # with the `ngx_pagespeed` module, uncomment this line to disable it.
    #pagespeed off;

    # HTTP response headers borrowed from Nextcloud `.htaccess`
    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 /mnt/sda1/www/nc;

    # Specify how to handle directories -- specifying `/index.php$request_uri`
    # here as the fallback means that Nginx always exhibits the desired behaviour
    # when a client requests a path that corresponds to a directory that exists
    # on the server. In particular, if that directory contains an index.php file,
    # that file is correctly served; if it doesn't, then the request is passed to
    # the front-end controller. This consistent behaviour means that we don't need
    # to specify custom rules for certain paths (e.g. images and other assets,
    # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus
    # `try_files $uri $uri/ /index.php$request_uri`
    # always provides the desired behaviour.
    index index.php index.html /index.php$request_uri;

    # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
    location = / {
        if ( $http_user_agent ~ ^DavClnt ) {
            return 302 /remote.php/webdav/$is_args$args;
        }
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Make a regex exception for `/.well-known` so that clients can still
    # access it despite the existence of the regex rule
    # `location ~ /(.|autotest|...)` which would otherwise handle requests
    # for `/.well-known`.
    location ^~ /.well-known {
        # The following 6 rules are borrowed from `.htaccess`

        location = /.well-known/carddav     { return 301 /remote.php/dav/; }
        location = /.well-known/caldav      { return 301 /remote.php/dav/; }
        # Anything else is dynamically handled by Nextcloud
        location ^~ /.well-known            { return 301 /index.php$uri; }

        try_files $uri $uri/ =404;
    }

    # Rules borrowed from `.htaccess` to hide certain paths from clients
    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
    location ~ ^/(?:.|autotest|occ|issue|indie|db_|console)              { return 404; }

    # Ensure this block, which passes PHP files to the PHP process, is above the blocks
    # which handle static assets (as seen below). If this block is not declared first,
    # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
    # to the URI, resulting in a HTTP 500 error response.
    location ~ .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 off;

        fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice
        fastcgi_param front_controller_active true;     # Enable pretty urls
        fastcgi_pass php-handler;

        fastcgi_intercept_errors on;
        fastcgi_request_buffering off;
    }

    location ~ .(?:css|js|svg|gif)$ {
        try_files $uri /index.php$request_uri;
        expires 6M;         # Cache-Control policy borrowed from `.htaccess`
        access_log off;     # Optional: Don't log access to assets
    }

    location ~ .woff2?$ {
        try_files $uri /index.php$request_uri;
        expires 7d;         # Cache-Control policy borrowed from `.htaccess`
        access_log off;     # Optional: Don't log access to assets
    }

    location / {
        try_files $uri $uri/ /index.php$request_uri;
    }
}

修改/etc/php.ini,把php可用内存,超时上限都加大:

; Resource Limits

max_execution_time = 300	; Maximum execution time of each script, in seconds.
max_input_time = 600		; Maximum amount of time each script may spend parsing request data.
;max_input_nesting_level = 64
memory_limit = 1024M		; Maximum amount of memory a script may consume.

nextcloud要求PHP可用内存最小512M,土豪请任性。

reboot下,正常情况可以访问http://192.168.100.3018安装了:

安装完成可能会有那么一下下路径出错,稍等,重开浏览器尝试直接访问http://192.168.100.1:3018,祝好运!

 

 

最后

以上就是冷艳眼睛为你收集整理的OpenWrt安装配置NextCloud的全部内容,希望文章能够帮你解决OpenWrt安装配置NextCloud所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部