Nextcloud部署私人雲盤流程全記錄


Nextcloud-13.0.5部署實踐記錄:

一、環境說明LNMP版本信息
系統: CentOS Linux release 7.5.1804 (Core)
軟件:nginx-1.12.2   mariadb-10.2.15    php7.0    
-----------------------------------------------

配置163YUM源
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache

二、安裝倉庫源、redis
1、添加EPEL包的倉庫源  
yum install epel-release  

2、添加 PHP7-FPM webtatic 倉庫
rpm-Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

yum install redis

三、安裝PHP7和PHP7-FPM
yum  install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear \
php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel
    
四、配置PHP-FPM
1、我們需要配置 php-fpm 與 Nginx 協同運行。php7-fpm將使用nginx 用戶來運行,並監聽 9000 端口。
編輯默認的php7-fpm配置文件。
vi /etc/php-fpm.d/www.conf
------------------------------------------------------------------------------
user = nginx                          //將用戶和組都改為nginx
group = nginx
.....
listen = 127.0.0.1:9000                            //php-fpm所監聽的端口為9000
......
env[HOSTNAME] = $HOSTNAME                     //去掉下面幾行注釋366行
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
-------------------------------------------------------------------------------

2、需要在 /var/lib/ 目錄下創建一個新的文件夾 session,並將其擁有者變更為 nginx 用戶。
最后啟動 php-fpm 和 Nginx,並且將它們設置為隨開機啟動的服務。
mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/

(注:如果沒建nginx用戶:useradd -s /sbin/nologin -M nginx)


3、vi/etc/php.d/opcache.ini 去掉以下行注釋,修改為對應的配置值:
vi/etc/php.d/opcache.ini
---------------------------------
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.revalidate_freq=1
opcache.save_comments=1
----------------------------------
注:未裝opcache的請看下面
php -v         #查詢php版本
yum search opcache php        #YUM查詢並安裝對應PHP版本的opcache
yum install php70w-opcache.x86_64

4、啟動php-fpm 和nginx服務  
systemctl start php-fpm
systemctl enable php-fpm
systemctl start redis   
systemctl enable redis  
chkconfig nginx on

五、配置MariaDB, 創建數據庫、創建庫用戶、用戶授權
mysql -u root -p
create database nextclouddb;
create user ncuser@'%' identified by'ncdb1234';
grant all privileges on nextclouddb.* to ncuser@'%' identified by 'ncdb1234';
flush privileges;
exit

六、安裝SSL證書我們可以自己生成SSL證書,也可以申請專業的SSL證書。
自簽名的SSL證書在使用的時候會報錯,建議使用有資質的SSL證書。 安裝過程如下:
1、為SSL 文件創建新目錄:
mkdir -p /etc/nginx/cert/
2、創建證書(生產環境請購買公網SSL證書)
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/misemcloud.crt -keyout /etc/nginx/cert/misemcloud.key
3、在該目錄下儲存申請過的SSL證書,並設置證書的權限:
chmod 700 /etc/nginx/cert
chmod 600 /etc/nginx/cert/*    

七、初步安裝 Nextcloud
1、下載解壓到瀏覽器(apache或者nginx)
unzip nextcloud-13.0.5.zip
mv mv nextcloud /usr/local/nginx/html/    
chown -R nginx:nginx /usr/local/nginx/html/  

2、為NextCloud創建文件儲存文件夾,並授予一定的權限
/usr/local/nginx/
mkdir /datacloud
chown -R nginx:nginx /datacloud/

八、配置Nginx轉發規則
1、創建文件: (只改如下三處)
# 注意:server_name 需要改為域名
# ssl_certificate和ssl_certificate_key需要改為SSL證書對應的文件
# root 需要改為nextcloud文件夾所在路徑

vi /usr/local/nginx/conf/nginx.conf
----------------------------------------------------------------------------------------
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

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

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

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

server {
    listen 80;
    server_name www.misem.top;
    # enforce https
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name www.misem.top;

    ssl_certificate /etc/nginx/cert/misemcloud.crt;
    ssl_certificate_key /etc/nginx/cert/misemcloud.key;

    # 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;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    # Path to the root of your installation
    root /usr/local/nginx/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;

    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;
    }
}
}
--------------------------------------------------------------------------------------------------
#附:官方配置文檔 https://docs.nextcloud.com/server/13/admin_manual/installation/nginx.html


九,網頁安裝Nextcloud
vi /etc/hosts
139.159.182.198 www.misem.top
#在台式機HOSTS也加本地域名

瀏覽器輸入https://139.159.182.198/nextcloud

nextcloud一定要改為nginx所屬,有時不注意就會報錯


數據庫主機:139.159.182.198:5945  (查看實際端口號)

GRANT ALL PRIVILEGES ON *.* TO 'ncuser'@'139.159.182.198' IDENTIFIED BY 'ncdb1234' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'ncuser'@'%' IDENTIFIED BY 'ncdb1234' WITH GRANT OPTION;

第一次沒有反應,
重新運行第二次可以了

重要,重要,重要:

關閉selinux和防火牆
# vi /etc/selinux/config
//SELINUX=enforcing改為SELINUX=disabled



十:一些優化設置:

1 . HTTP 請求頭“Strict-Transport-Security”沒有配置為至少“15552000”秒,出於增強安全性考慮,
我們推薦按照安全提示中的說明啟用HSTS
---------------------------------------------
vim /application/nginx/conf/nginx.conf
#在server {
      listen 443 ssl;
#下面加入一行:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
----------------------------------------------------------------------------------------------------------------

2、內存緩存未配置. 如果可用, 請配置 memcache 以增強性能. 更多信息請查看我們的…

nextcloud支持內存加速,它可支持3種方式APCu,Memcached,Redis。這里只展示APCu的配置

安裝apcu
yum search apcu php
yum install 文件名(對應php版本)

vim /application/nginx/html/nextcloud/config/config.php
以下內容加入倒數第二行
'memcache.local' => '\\OC\\Memcache\\APCu',
----------------------------------------------------------------------------------------------------------------

結束語:
       至此,一台私有雲盤搭建完畢,歡迎交流指正。

















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM