部署Nextcloud私有云
以CentOS 7、MariaDB、PHP 7、Nginx爲環境部署Nextcloud。
一、安裝操作系統
首先安裝操作系統,Nextcloud只支持Linux,由於個人習慣的原因,選擇了CentOS 7,使用最小化安裝(爲了保證之後的步驟能在只有最小化安裝的VPS上重現,也爲了節約硬件資源)。
最小化的CentOS 7安裝完畢後,默認是沒有啓用網卡的,在本地登錄系統後,首先進入網絡配置目錄,列出目錄中的網卡配置文件
cd /etc/sysconfig/network-scripts/
ll | grep ifcfg-
運行結果
[root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ll | grep ifcfg
-rw-r--r--. 1 root root 312 Aug 30 10:01 ifcfg-enp0s3
-rw-r--r--. 1 root root 254 Sep 12 2016 ifcfg-lo
除了ifcfg-lo以外的那個文件就是網卡配置文件,具體名稱可能會有所不同。
然後使用vi編輯該文件,將最後一行“ONBOOT=no”改成“ONBOOT=yes”並保存退出。
通過命令重啓網絡服務,是配置生效
service network restart
如果不想通過DHCP動態獲取IP地址,也可以在網絡配置文件中添加以下配置項指定網絡參數
IPADDR0=192.168.21.128 #設置IP地址
PREFIXO0=24 #設置子網掩碼
GATEWAY0=192.168.21.2 #設置網關
DNS1=8.8.8.8 #設置主DNS
DNS2=8.8.4.4 #設置備DNS
網卡啓用後,就可以通過SSH遠程操作、通過yum方便的安裝程序了。
查看ip地址,可以通過ip命令
ip addr
二、安裝配置環境
1、安裝基本工具
安裝yum額外源、wget、unzip、gcc等基本工具
yum -y install epel-release wget unzip gcc
yum -y install libsmbclient libsmbclient-devel redis
關閉SELinux,可先通過sestatus -v命令查看SELinux是否開啓
/usr/sbin/sestatus -v
修改/etc/selinux/config,將’SELINUX=enforcing’改爲’SELINUX=disabled’,重啓系統即可生效,或者本次可以使用’setenforce 0’臨時關閉。
2、安裝MariaDB
通過yum安裝MariaDB
yum -y install mariadb mariadb-server
開啓、啓動服務,運行管理工具
systemctl enable mariadb.service
systemctl start mariadb.service
mysql_secure_installation
mysql_secure_installation的輸入如下,牢記自己的數據庫root密碼
Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
登錄到mysql shell爲Nextcloud創建用戶和數據庫。
mysql -u root -p
驗證root密碼後,在mysql shell執行
create database nextcloud_db;
create user nextclouduser@localhost identified by 'nextclouduser@';
grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by 'nextclouduser@';
flush privileges;
exit
這樣就創建了一個nextcloud_db數據庫和nextclouduser用戶,用戶密碼爲’nextclouduser@’。
3、安裝Nginx
通過yum安裝Nginx
yum -y install nginx
mkdir /var/www
chown -R nginx:nginx /var/www
開啓、啓動Nginx服務
systemctl enable nginx.service
systemctl start nginx.service
使用nginx -s reload可以重載配置而不需要重啓nginx
開放防火牆HTTP、HTTPS端口
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
systemctl restart firewalld
4、安裝PHP
添加PHP7-FPM webtatic倉庫,安裝php7主體以及nextcloud需要的一些模塊。
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y install php71w-fpm php71w-cli php71w-gd php71w-mcrypt php71w-mysql php71w-pear php71w-xml php71w-mbstring php71w-pdo php71w-json php71w-opcache php71w-pecl-apcu php71w-pecl-apcu-devel php71w-pecl-igbinary php71w-pecl-igbinary-devel php71w-pecl-imagick php71w-pecl-imagick-devel php71w-pecl-redis php71w-pecl-redis-devel
vi /etc/php-fpm.d/www.conf
編輯/etc/php-fpm.d/www.conf
;修改user和group這兩行,大概在8行左右
user = nginx
group = nginx
;取消這幾行的註釋,大概在第370行左右
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
建立相關目錄,修改相關目錄權限
mkdir -p /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session/
vi /etc/php.d/opcache.ini
修改/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
安裝smbclient擴展模塊
yum -y install libsmbclient libsmbclient-devel
pecl install smbclient
vi /etc/php.d/smbclient.ini
新建/etc/php.d/smbclient.ini,添加如下內容
extension=smbclient.so
開啓、啓動php-fpm服務
systemctl enable php-fpm.service
systemctl start php-fpm.service
三、安裝Nextcloud
1、下載並解壓到www目錄
wget https://download.nextcloud.com/server/releases/nextcloud-12.0.2.zip
unzip nextcloud-12.0.2.zip
mv nextcloud /var/www/
chown -R nginx:nginx /var/www
2、生成SSL證書
mkdir -p /etc/nginx/cert/
openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
chmod 700 /etc/nginx/cert
chmod 600 /etc/nginx/cert/*
3、在Nginx配置
修改nginx服務配置文件/etc/nginx/nginx.conf爲以下內容,將“yourname.domain”替換爲自己的域名,修改client_max_body_size可以設置最大可上傳的文件大小
#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 logs/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 yourname.domain;
# enforce https
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name yourname.domain;
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 /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$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 -s reload
4、初始化
使用域名或者IP訪問,就會出現初始設置頁面,在這裏設置Nextcloud管理員用戶名和密碼,然後選擇使用的數據庫爲MySQL/MariaDB,填入之前設置數據庫時的用戶名(nextclouduser)、密碼(nextclouduser@)、數據庫名稱(nextcloud_db),然後確認進行初始化後就可以使用了。
5、添加信任域名
Nextcloud本身的安全機制,會檢查訪問的域名,如果沒有配置在信任域名中,會提示正在通過不信任的域名訪問。
Nextcloud初始化完畢後,會生成“/var/www/nextcloud/config/config.php”配置文件,裏面的’trusted_domains’配置項爲信任域名,初始化完畢後只有一項,爲主機的IP地址。可以修改該配置項,添加綁定的域名
'trusted_domains' =>
array (
0 => '192.168.56.101',
1 => 'yourname.domain',
),
6、開啓內存緩存
開啓內存緩存,可以提升響應速度。之前我們已經通過yum安裝了redis服務,通過pecl安裝了php的apcu、redis組件,下面先把redis設置爲系統服務,再修改Nextcloud的配置。
安裝、配置redis服務,設置服務自啓、啓動服務
yum -y install redis
systemctl enable redis
systemctl start redis
修改/var/www/nextcloud/config/config.php文件,在配置加入
'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
'host' => 'localhost',
'port' => 6379,
),
讓Nginx重新載入配置
nginx -s reload