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,这样可以解决带宽问题)
- 配置初始的超管账户名
- 配置用户数据存储文件/home/data
- 输入数据库超管用户root及密码
- 配置数据库名称cloud
- 主机不修改为默认的localhost