第一章 安装niginx + php

本文的默认环境(安装路径,配置路径,证书路径等),都继承于我的另一篇博文:

【亲测】PHP Version 7.3.8 + Nginx 1.16.1 (100%无坑版)编译安装

请务必按照这篇文章的流程安装nginx和php,否则接下来的内容,有些配置(主要是默认路径,用户)会对应不上,特此说明!


第二章 安装Nextcloud

1. 下载nextcloud:

wget https://download.nextcloud.com/server/releases/nextcloud-16.0.3.zip
unzip -x nextcloud-16.0.3.zip
cp -a -R nextcloud /usr/local/nginx/html (将nextcloud拷贝进默认网站根目录)

2. 创建nextcloud配置文件:

nano /usr/local/nginx/conf/nextcloud.conf

复制粘贴以下内容,红字部分是需要修改的, 黑字不要动:

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

    server {
        listen 443 ssl http2;
        server_name www.your.website; # 你的网站域名或IP地址
        root /usr/local/nginx/html; # 网站的根目录

        ssl_certificate /usr/local/nginx/cert/your_ssl_cert.crt; # 你的SSL证书crt文件
        ssl_certificate_key /usr/local/nginx/cert/your_ssl_cert.key;# 你的SSL证书key文件

        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;

        fastcgi_hide_header X-Powered-By;

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

       location = /.well-known/carddav {
         return 301 $scheme://$host:$server_port/nextcloud/remote.php/dav;
       }
       location = /.well-known/caldav {
         return 301 $scheme://$host:$server_port/nextcloud/remote.php/dav;
       }

       location /.well-known/acme-challenge { }

       location ^~ /nextcloud {

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

        location /nextcloud {
            rewrite ^ /nextcloud/index.php$request_uri;
        }

        location ~ ^\/nextcloud\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
            deny all;
        }
        location ~ ^\/nextcloud\/(?:\.|autotest|occ|issue|indie|db_|console) {
            deny all;
        }

        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 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 ~ ^\/nextcloud\/(?:updater|oc[ms]-provider)(?:$|\/) {
            try_files $uri/ =404;
            index index.php;
        }

        location ~ ^\/nextcloud\/.+[^\/]\.(?:css|js|woff2?|svg|gif|map)$ {
            try_files $uri /nextcloud/index.php$request_uri;
            add_header Cache-Control "public, max-age=15778463";
            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;

            # Optional: Don't log access to assets
            access_log off;
        }

        location ~ ^\/nextcloud\/.+[^\/]\.(?:png|html|ttf|ico|jpg|jpeg)$ {
            try_files $uri /nextcloud/index.php$request_uri;
            # Optional: Don't log access to other assets
            access_log off;
            }
        }
    }

存盘退出。


3. 修改nginx的主配置文件:

nano /usr/local/nginx/conf/nginx.conf

找到:

http {
    include       mime.types;
    default_type  application/octet-stream;

在http下面添加一行include       nextcloud.conf; 最后变成这个样子就行了:

http {
    include       nextcloud.conf;
    include       mime.types;
    default_type  application/octet-stream;

4. 建立data目录:

mkdir /usr/local/nginx/html/nextcloud/data

5. 修改nextcloud目录的拥有者:

chown -R www:www /usr/local/nginx/html/nextcloud

6. 创建Nextcloud数据库:

mysql -uroot -p (输入root密码,进入mysql命令行)
mysql>CREATE DATABASE Nextcloud;

这个步骤也以用navicat等图形化工具来完成。


7. Nextcloud的初始设置
在浏览器输入https://网站IP/nextcloud,
如果这一步一直出现“内部错误”,多半是nextcloud和data目录的所有者不对造成的,尝试:

1. 重新将nextcloud的所有权交给www用户:
  chown -R www:www /usr/local/nginx/html/nextcloud

2. 如果还是不行,可以尝试把整个网站根目录都交给www:
  chown -R www:www /usr/local/nginx/html

3. 如果实在没辙,尝试一下将nextcloud设为777,注意此处不能加-R参数:
  chmod 777 /usr/local/nginx/html/nextcloud

如果正确无误,就正常进入管理员和数据库设置界面了,数据库名就填前文所述那个Nextcloud,如图:

如果点击 “完成安装” 按钮后,出现:

Error while trying to create admin user: Failed to connect to the database: An exception occurred in driver: SQLSTATE[HY000] [2002] No such file or directory

尝试把数据库地址填为:127.0.0.1:3306


12. Nextcloud客户端设置
PC端程序下载: https://nextcloud.com/install/
设置方面没有什么可说的,非常简单,安装后输入自己的Nextcloud地址即可:https://127.0.0.1/nextcloud (填你自己实际的IP或域名)
 

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐