1、背景
javascript
最近多家雲盤相繼關停,費了不少時間才把上面的東西下載到本地,百度雲盤也早已開始限速,技術宅豈能容忍?是時候搭建本身的私有云盤了!php
一、搭建本身的私有云有什麼好處呢?css
首先沒有什麼容量、下載速度的限制,並且本地訪問速度很快。而後能夠和本地的ftp配合使用來實現多個設備文件共享:好比能夠在電視、手機等等智能設備上掛載雲盤中的文件來實現播放電影、看照片、聽歌等需求。最後能夠防止泄密和和諧。html
說到私有云,其實有不少現成的產品可使用,好比羣暉、鐵威馬、西數等。買過來,插上一塊硬盤就能夠用,十分適合小白。可是成本略高,僅僅主機就須要1000多元,再加上一塊硬盤,這種解決方案的成本通常都要超過2000元。本身搭建私有云的話,不只成本很低,並且能夠本身定製不少功能,好比在線筆記、郵件等等功能。可是須要會折騰linux哦!java
二、搭建難度怎麼樣?node
本身搭建私有云其實很簡單,首先須要一臺主機,而後須要選擇一個私有云軟件(好比ownCloud、Nextcloud、seafile)。在這裏我仍是用個人Linux服務器做爲主機,大概上傳下載爲2MB/s,在對比幾個不一樣的私有云軟件以後,最終採用了Nextcloud 12,感受這個功能更爲強大。mysql
2、Nextcloud 12概述
linux
Nextcloud 是一款自由(開源)的類Dropbox軟件,由ownCloud分支演化造成。它使用PHP和JavaScript編寫,支持多種數據庫系統,好比 MySQL/MariaDB、PostgreSQL、Oracle 數據庫和 SQLite。它可使你的桌面系統和雲服務器中的文件保持同步,Nextcloud 爲 Windows、Linux、Mac、安卓以及蘋果手機都提供了客戶端支持。Nextcloud 並不是只是 Dropbox 的克隆,它還提供了不少附加特性,如日曆、聯繫人、計劃任務以及流媒體 Ampache。nginx
在這篇文章中,我將向你展現如何在CentOS 7.3服務器中安裝和配置最新版本的 Nextcloud 12。我會經過Nginx和PHP7-FPM來運行Nextcloud,同時使用MariaDB作爲數據庫系統。web
一、系統需求
官方推薦參數以下:
內存 | 最少512MB |
系統 | Red Hat Enterprise Linux 7 / Ubuntu 16.04 LTS |
數據庫 |
MySQL/MariaDB |
PHP版本 | PHP 7.0 + |
WEB服務器 | Apache 2.4 with mod_php |
二、安裝須要最低版本及支持平臺
Server: Linux (Debian 7, SUSE Linux Enterprise Server 11 SP3 & 12, Red Hat Enterprise Linux/CentOS 6.5 and 7 (7 is 64-bit only), Ubuntu 14.04 LTS, 16.04 LTS)
Web server: Apache 2 (mod_php, php-fpm) or Nginx (php-fpm)
Databases: MySQL/MariaDB 5.5+; PostgreSQL; Oracle 11g (currently only possible if you contact us <https://nextcloud.com/enterprise> as part of a subscription)
PHP 5.6 + required
Hypervisors: Hyper-V, VMware ESX, Xen, KVM
Desktop: Windows XP SP3 (EoL Q2 2015), Windows 7+, Mac OS X 10.7+ (64-bit only), Linux (CentOS 6.5, 7 (7 is 64-bit only), Ubuntu 12.04 LTS, 14.04 LTS, 14.10, Fedora 20, 21, openSUSE 12.3, 13, Debian 7 & 8).
Mobile apps: iOS 7+, Android 4+
Web browser: IE11+, Microsoft Edge, Firefox 14+, Chrome 18+, Safari 7+
3、Nextcloud 12環境準備
一、安裝nginx和PHP7
首先安裝epel和webtatic最新包倉庫,具體參照個人置頂博文。
#安裝nginx yum install nginx -y #安裝php7及其nextcloud須要的包 yum -y install php70w-fpm php70w-cli php70w-opcache php70w-gd php70w-mcrypt php70w-mysql php70w-pear php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel
二、驗證安裝的php版本
[root@pan ~]# php -v PHP 7.0.20 (cli) (built: Jun 10 2017 06:34:07) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
三、配置 PHP7-FPM
在這一個步驟中,咱們將配置php-fpm與Nginx協同運行。Php7-fpm將使用nginx用戶來運行,並監聽9000端口。
使用vim編輯默認的php7-fpm配置文件。
#vim /etc/php-fpm.d/www.conf user = nginx group = nginx #在第8行和第10行,user和group賦值爲nginx。 listen = 127.0.0.1:9000 #在第 22 行,確保 php-fpm 運行在指定端口。 env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp #取消第366-370行的註釋,啓用php-fpm的系統環境變量。
下一步,就是在/var/lib/目錄下建立一個新的文件夾session,並將其擁有者變動爲nginx用戶。
mkdir -p /var/lib/php/session chown nginx:nginx -R /var/lib/php/session/
而後啓動php-fpm和Nginx,而且將它們設置爲隨開機啓動的服務。
systemctl start php-fpm systemctl start nginx systemctl enable php-fpm systemctl enable nginx
四、安裝和配置MariaDB
我這裏使用MariaDB做爲Nextcloud的數據庫。能夠直接使用yum命令從CentOS默認遠程倉庫中安裝 mariadb-server包。
yum -y install mariadb mariadb-server
在配置文件/etc/my.cnf添加以下幾行配置
[mysqld] innodb_file_per_table=1 default-storage-engine = INNODB
啓動MariaDB,並將其添加到隨系統啓動的服務中去。
systemctl start mariadb systemctl enable mariadb
如今開始配置MariaDB的root用戶密碼,鍵入Y,而後設置MariaDB的root密碼。
mysql_secure_installation
這樣就設置好了密碼,如今登陸到 mysql shell併爲Nextcloud建立一個新的數據庫和用戶。這裏我建立名爲nextcloud的數據庫以及名爲nextcloud的用戶,用戶密碼爲nextcloud。固然了,要給你本身的系統選用一個更安全的密碼。
mysql -u root -p
輸入MariaDB的root密碼,便可登陸 mysql shell。
mysql> create database nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; mysql> grant all privileges on nextcloud.* to nextcloud@localhost identified by 'nextcloud'; mysql> flush privileges;
五、爲Nextcloud生成一個自簽名SSL證書
我會讓客戶端以https鏈接來運行Nextcloud,這裏我使用OpenSSL來建立本身的自簽名SSL證書。
mkdir -p /etc/nginx/cert/
以下,使用openssl生成一個新的SSL證書。
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
最後使用 chmod 命令將全部證書文件的權限設置爲 600。
chmod 700 /etc/nginx/cert chmod 600 /etc/nginx/cert/*
4、Nextcloud 12安裝配置
一、下載和安裝Nextcloud
我直接使用wget命令下載Nextcloud到服務器上。
wget https://download.nextcloud.com/server/releases/nextcloud-12.0.0.zip
解壓Nextcloud,並將其移動到/usr/share/nginx/html/目錄。
unzip nextcloud-12.0.0.zip mv nextcloud/ /usr/share/nginx/html/
下一步,轉到Nginx的web根目錄爲Nextcloud建立一個data文件夾,推薦選擇一個比較大的分區放置data目錄,由於之後上傳的數據都會放置在裏面。
cd /usr/share/nginx/html/ mkdir -p nextcloud/data/
變動nextcloud目錄的擁有者爲nginx用戶和組。
chown nginx:nginx -R nextcloud/
二、在Nginx中爲Nextcloud配置虛擬主機
咱們已經下載好了Nextcloud源碼,並配置好了讓它運行於Nginx服務器中,但咱們還須要爲它配置一個虛擬主機。在Nginx的conf.d目錄下建立一個新的虛擬主機配置文件nextcloud.conf。
vim /etc/nginx/conf.d/nextcloud.conf
將如下內容粘貼到虛擬主機配置文件中:
upstream php-handler { server 127.0.0.1:9000; #server unix:/var/run/php5-fpm.sock; } server { listen 80; server_name pan.wzlinux.com; # enforce https return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name pan.wzlinux.com; ssl_certificate /etc/nginx/cert/nextcloud.crt; ssl_certificate_key /etc/nginx/cert/nextcloud.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; # Path to the root of your installation root /usr/share/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-polic y; # Uncomment if your server is build with the ngx_pagespeed module # This module is currently not supported. #pagespeed off; location / { rewrite ^ /index.php$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$uri$is_args$args; 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$uri$is_args$args; # Optional: Don't log access to other assets access_log off; } }
下面測試一下該Nginx配置文件是否有錯誤,沒有的話就能夠重啓服務了。
# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # systemctl restart nginx.service
三、Nextcloud安裝
打開你的Web瀏覽器,輸入你爲Nextcloud設置的域名,我這裏設置爲pan.wzlinux.com,而後會重定向到安全性更好的https鏈接。
設置你的管理員用戶名和密碼,而後輸入數據驗證信息,點擊 '完成安裝(Finish Setup)'。
參考文檔:https://docs.nextcloud.com/server/12/admin_manual/installation/index.html