##### 1.修改软件源 ```bash # 备份并编辑source.list文件 $ cp /etc/apt/sources.list /etc/apt/sources.back.list $ nano /etc/apt/sources.list # 注释所有内容,添加以下内容 deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main non-free contrib # 备份并编辑raspi.list文件 $ cp /etc/apt/sources.list.d/raspi.list /etc/apt/sources.list.d/raspi.back.list $ nano /etc/apt/sources.list.d/raspi.list # 注释所有内容,替换如下内容 deb http://mirrors.tuna.tsinghua.edu.cn/raspberrypi/ buster main ``` ##### 2.刷新软件源并更新操作系统 ```bash sudo apt-get update && sudo apt-get upgrade -y ``` ##### 3.安装nginx redis mariadb PHP及模块: ```bash sudo apt-get install -y nginx sudo apt-get install -y redis sudo apt-get install -y mariadb-server sudo apt-get install -y php php-fpm php-curl php-gd php-dom php-iconv php-openssl php-redis php-mysql php-zip php-bz2 php-intl php-imagick php-opcache ``` ##### 4.下载nextcloud软件包并解压 ```bash https://nextcloud.com/install/#instructions-server 解压部署程序: unzip nextcloud-19.0.1.zip 将nextcloud文件移动到网站根目录 sudo mv nextcloud /var/www/html/ ``` ##### 5. 创建数据目录,更改目录权限 ```bash # 创建数据目录 mkdir /mnt/data mount -o umask=0007,uid=www-data,gid=www-data /dev/sda1 /mnt/data/ # 配置文件夹权限 cd /var/www/html/ sudo chown -R root:root nextcloud 更改子文件夹权限 cd /var/www/html/nextcloud sudo chown -R www-data:www-data config apps ``` ##### 6.配置mysql ```mysql 优化mysqld sudo vim /etc/mysql/my.cnf [client-server] !includedir /etc/mysql/conf.d/ !includedir /etc/mysql/mariadb.conf.d/ [client] default-character-set = utf8mb4 [mysqld] bind-address = 192.168.31.180 character-set-server = utf8mb4 collation-server = utf8mb4_general_ci transaction_isolation = READ-COMMITTED binlog_format = ROW innodb_large_prefix=on innodb_file_format=barracuda innodb_file_per_table=1 skip-name-resolve innodb_buffer_pool_size = 256M innodb_buffer_pool_instances = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 32M innodb_max_dirty_pages_pct = 90 innodb_io_capacity=4000 query_cache_type = 1 query_cache_limit = 2M query_cache_min_res_unit = 2k query_cache_size = 64M tmp_table_size= 64M max_heap_table_size= 64M slow-query-log = 1 slow-query-log-file = /var/log/mysql/slow.log long_query_time = 1 sudo systemctl restart mariadb.service 设置mariadb管理员密码并创建nextlcoud数据库 mysqladmin -uroot password 'P@ssw0rd' mysql -uroot -pP@ssw0rd -e 'CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' mysql -uroot -pP@ssw0rd -e 'GRANT ALL PRIVILEGES on nextcloud.* to "nextcloud"@"localhost" IDENTIFIED BY "nextcloud";' mysql -uroot -pP@ssw0rd -e 'FLUSH privileges;' ``` ##### 7.配置php ```bash sudo vim /etc/php/7.3/fpm/php.ini expose_php = off date.timezone = Asia/Shanghai #启用opcache opcache.enable=1 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=10000 opcache.memory_consumption=128 opcache.save_comments=1 opcache.validate_timestamps=1 opcache.revalidate_freq=30 #启用大文件上传,更改文件限制: upload_max_filesize=16G post_max_size=16G max_input_time = 3600 max_execute_time = 3600 memory_limit = 512M ``` ##### 8.配置php-fpm ```bash sudo vim /etc/php/7.3/fpm/pool.d/www.conf clear_env=no env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp # 设置动态进程数量: pm = dynamic pm.max_children = 50 pm.start_servers = 3 pm.min_spare_servers = 3 pm.max_spare_servers = 10 sudo systemctl restart php7.3-fpm.service ``` ##### 9.配置nginx: 在/etc/nginx/sites-enabled目录下,创建一个nextcloud文件,其内容如下: 生成SSL证书 ```bash sudo mkdir /etc/nginx/nextcloud_SSL/ cd /etc/nginx/nextcloud_SSL/ sudo openssl genrsa -out privkey.pem 2048 sudo openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095 ``` sudo vim /etc/nginx/sites-enabled/nginx ```nginx #nextcloud upstream php-handler { server unix:/var/run/php/php7.3-fpm.sock; } #server { # listen 6080; # listen [::]:6080; # server_name szy.nextcloud.local; # enforce https # return 301 https://$server_name:6088$request_uri; #} server { listen 6088 ssl http2; #listen [::]:6088 ssl http2; server_name szy.nextcloud.local; # 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/nextcloud_SSL/cacert.pem; ssl_certificate_key /etc/nginx/nextcloud_SSL/privkey.pem; # 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 root /var/www/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; # 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 16G; fastcgi_buffers 64 128K; # 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; 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; } } ``` sudo systemctl restart nginx php7.3-fpm ##### 10.nextcloud性能优化 ```bash 1.用cron替代后台ajax刷新执行定时任务。执行命令: crontab -u www-data -e */5 * * * * php -f /var/www/nextcloud/cron.php crontab -u www-data -l sudo echo '*/5 * * * * www-data php /var/www/html/nextcloud/cron.php' >> /etc/crontab 2.使用缓存。需要安装redis以及编辑nextcloud的配置文件并使用redis锁代替文件锁 sudo apt-get install php-apcu php-redis sudo vim /etc/redis/redis.conf daemonize = yes unixsocket /var/run/redis/redis-server.sock unixsocketperm 777 # 授权redis usermod -g www-data redis chown -R redis:www-data /var/run/redis systemctl restart redis-server.service sudo vim /var/www/html/nextcloud/config/config.php 'memcache.local' => '\OC\Memcache\APCu', 'filelocking.enabled' => true, 'memcache.locking' => '\OC\Memcache\Redis', 'memcache.distributed' => '\OC\Memcache\Redis', 'redis' => [ 'host' => '/var/run/redis/redis.sock', 'port' => 0, 'dbindex' => 0, 'password' => 'secret', 'timeout' => 1.5, ], # 其他优化: sudo nano /var/www/html/nextcloud/config/config.php 'check_data_directory_permissions' => false, 接着编辑/etc/nginx/nginx.conf,在http块中修改上传限制: client_max_body_size 16G; fastcgi_read_timeout 360S; ```