Nexrcloud搭建教程

NextCloud搭建教程
配置說明
本次安裝完成所有依賴安裝以及實現SSL訪問,80重定向443。(域名從阿里雲購買並申請免費證書)
環境準備
CnetOS7.4最小安裝

更新補丁

yum -y install epel-release 
yum update -y

安裝依賴
安裝Nginx

yum install nginx

安裝PHP及PHP依賴
更新php的yum源

yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安裝

yum --enablerepo=webtatic install mod_php72w php72w-opcache php72w-fpm
yum --enablerepo=webtatic install php72w-pear php72w-bcmath php72w-mysqlnd php72w-gd php72w-common php72w-fpm php72w-intl php72w-cli php72w php72w-xml php72w-opcache php72w-pecl-apcu php72w-pecl-jsond php72w-pdo php72w-gmp php72w-process php72w-pecl-imagick php72w-devel php72w-mbstring php72w-zip php72w-ldap php72w-imap php72w-pecl-mcrypt

安裝MariaDB(最新版)
製作國內源

cat <<EOF > /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://mirrors.aliyun.com/mariadb/yum/10.3/centos7-amd64/
gpgkey =  http://mirrors.aliyun.com/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgcheck = 1

更新緩存

yum clean all  
yum makecache  
yum repolist

安裝MariaDB

yum -y install MariaDB-server MariaDB-client

啓動各個服務

service nginx start
service php-fpm start
service mariadb start

開機自動啓動

systemctl enable mariadb
systemctl enable php-fpm
systemctl enable nginx

關閉防火牆

vim /etc/sysconfig/selinux
將SELINUX的狀態改爲
SELINUX=disabled

注:此配置永久關閉linux防火牆

再執行

setenforce 0

配置PHP

vim /etc/php-fpm.d/www.conf

在第 8 行和第 10行,user 和 group 賦值爲 nginx。

user = nginx
group = nginx

在第 22 行,確保 php-fpm 運行在指定端口。

listen = 127.0.0.1:9000

取消第 366-370 行的註釋,啓用 php-fpm 的系統環境變量。

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

重啓nginx服務

service nginx restart

配置nginx
配置Nginx.conf

vim /etc/nginx/nginx.conf
#For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        #listen       80 default_server;
        #listen       [::]:80 default_server;
        #server_name  localhost;
        #root         /var/www/html;
        #index index.html index.php index.htm;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        #location / {
        #}
      #location ~ \.php$ {
        #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        #fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #include fastcgi_params;
    #}
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

配置nextcloud.conf (注意將“你的域名”替換成你真實的域名)

vim /etc/nginx/conf.d/nextcloud.conf
upstream php-handler {
    server 127.0.0.1:9000;
    #server unix:/var/run/php/php7.0-fpm.sock;
}

server {
    #listen 80;
    #listen [::]:80;
    #server_name 你的域名;
    #add_header Strict-Transport-Security "max-age=63072000;";
    # enforce https
    #return 301 https://你的域名$request_uri;
}

server {
    listen 80;
    listen [::]:80;
    add_header Strict-Transport-Security "max-age=63072000;";
    add_header Referrer-Policy "no-referrer";
    add_header X-Frame-Options "SAMEORIGIN";
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name 你的域名;
    ssl_certificate /etc/ssl/nginx/你的域名.crt;
    ssl_certificate_key /etc/ssl/nginx/你的域名.key;
    ssl_prefer_server_ciphers on;
#   ssl_dhparam /etc/ssl/nginx/dhparam.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

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

    # Path to the root of your installation
    root /var/www/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 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;

    # Uncomment if your server is build with the ngx_pagespeed module
    # This module is currently not supported.
    #pagespeed off;

    location / {
        rewrite ^ /index.php$request_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/.+)\.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;
        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|woff|svg|gif)$ {
        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;";
        #
        # 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 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;
        # Optional: Don't log access to assets
        access_log off;
    }

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

配置SSL

mkdir /etc/ssl/nginx
yum install lrzsz -y

rz 上傳證書到/etc/ssl/nginx目錄下(注意將證書改名,與Nextcloud.conf中的名稱一致,pem後綴直接手動修改成crt後綴)
最後使用 chmod 命令將所有證書文件的權限設置爲 600。

chmod 700 /etc/ssl/nginx
chmod 600 /etc/ssl/nginx/*

nextcloud.zip服務包

cd /var/www

Ssh下使用rz命令上傳提前下載好的nextcloud.zip服務包(可以使用迅雷下載,速度較快),然後使用rz命令上傳(注:需提前安裝好lrzsz,配置SSL步驟有。)
安裝zip解壓命令

yum install -y unzip zip

解壓服務包
訪問目錄

cd /var/www/

解壓服務包到當前目錄
unzip nextcloud.zip (後面的名稱根據自己的需要解壓)

賦予nginx的訪問權限

chown -R nginx.nginx /var/www/nextcloud/

配置data目錄
創建data目錄
創建data用於存儲nextcloud用戶目錄(注意務必存儲於大分區,此項決定所以用戶可存儲在nextcloud上的文件上限)

mkdir /home/data

賦予權限

chown -R nginx.nginx /home/data

配置數據庫
初始化數據庫

mysql_secure_installation

按要求配置好數據庫的root用戶密碼
配置數據庫
進入數據庫

mysql -u root -p

(輸入root數據庫用戶密碼)

創建數據庫,數據庫名cloud

create database cloud;

刷新數據庫

flush privileges;
\q

退出數據庫編輯

訪問配置初始化
通過瀏覽器輸入https://配置的域名進行訪問 選擇MariaDB進行安裝(非雲部署內網部署需要單獨做DNS域名轉發內網IP,這樣可以解決帶寬問題)

  1. 配置初始的超管賬戶名
  2. 配置用戶數據存儲文件/home/data
  3. 輸入數據庫超管用戶root及密碼
  4. 配置數據庫名稱cloud
  5. 主機不修改爲默認的localhost
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章