博文記錄
默认分类 2019-06-14 11:00:21 1825 0

方案前文:建立一个家庭私有云盘方案系列教程+N2n+Nextcloud

前一篇:家庭私有云盘系列教程-建立公网服务器实现外网访问

在安装NextCloud之前,我们需要将物理硬盘挂载到linux上,供使Nextcloud存储数据。如果是物理机是linux,会更方便一些。

挂载物理磁盘

新硬盘直接进行分区挂载就行,这里对已经在window上分区过,甚至已经有文件的物理硬盘进行挂载说明。

查看硬盘号顺序。

使用cmd运行命令> Diskpart

接着输入 List disk

如图所示,如果我们挂载第一块硬盘,即是磁盘0。

这里的状态是脱机的,如果是联机的,需要将其更改为脱机,只允许一个系统将其挂载读写,那即是nas-linux。

更改硬盘为脱机状态后,windows物理机将无法访问到磁盘。

这里示例更改方法,恢复一样,右键将其更改为 联机 即可。需要在centos上将其卸载掉,否则会产生冲突。

 

VMware添加一块物理磁盘映射

虚拟机右键设置

选择第三个,使用物理磁盘。

这里的设备即是刚才cmd列出来的顺序,莫要搞错。而且一定要是脱机状态,否则这里会报占用错误。

单选项,1.使用整块硬盘,2.使用单个分区。选择第二项,单个分区。

勾选自己的分区,如果只有一个分区,那就勾选一个,然后继续下一步。

完成后,单击OK保存退出。

Centos进行挂载分区

查看磁盘情况

fdisk -l

由此看到,我们这块硬盘有两块硬盘,一块128M的小分区(Microsoft Reserved Partition),剩下的才是我们的主要数据分区。这是因为我们是在windows上使用GPT模式分区硬盘产生的。

这里也看不清具体的分区格式,需要使用parted -l 查看,遇到提示,输入OK回车即可。

这里可以清楚地看到两个分区,第一个是没有文件系统类别的,而第二个是ntfs。我们只需要挂载第二个即可,第一个挂载不上去。

编号1即 /dev/sda1

编号2即 /dev/sda2

 

安装ntfs-3g

折腾了半夜,没有将ntfs-3g编译安装成功,最终放弃,选择了yum方式安装。简单干脆。

#增加阿里云epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

#安装
yum install -y ntfs-3g

 

继续挂载硬盘

mount -t ntfs -o iocharset=cp936 /dev/sda2 /mnt/hd1

为避免windows上文件名乱码,这里指定磁盘字符,-o iocharset=cp936

注意:cp936是指简体中文,cp950是指繁体中文。

卸载分区

umount /dev/sda2

自动挂载分区

vi /etc/fstab
#追加内容
/dev/sda2 /mnt/hd1      ntfs    defaults,iocharset=cp936,rw   0 0

除此之外,自动挂载可以通过开机启动脚本实现。

在 /etc/rc.d/rc.local 文件尾部增加挂载分区mount命令即可。

 

安装Nextcloud

安装部署环境,PHP、Mariadb、Nginx

编译安装PHP

yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel

mkdir /usr/local/php/
cd /usr/local/php/

wget http://cn2.php.net/distributions/php-7.2.2.tar.gz -O php-7.2.2.tar.gz
tar -xzf php-7.2.2.tar.gz -C ./
cd php-7.2.2
./configure --prefix=/usr/local/php/php7.2.2/\
 --with-config-file-path=/usr/local/php/php7.2.2/\
 --with-libdir=lib64\
 --enable-fpm\
 --with-fpm-user=php-fpm\
 --with-fpm-group=www\
 --enable-mysqlnd\
 --with-mysql=mysqlnd\
 --with-mysqli=mysqlnd\
 --with-pdo-mysql=mysqlnd\
 --enable-opcache\
 --enable-pcntl\
 --enable-mbstring\
 --enable-soap\
 --enable-zip\
 --enable-calendar\
 --enable-bcmath\
 --enable-exif\
 --enable-ftp\
 --enable-intl\
 --with-openssl\
 --with-zlib\
 --with-curl\
 --with-gd\
 --with-zlib-dir=/usr/lib\
 --with-png-dir=/usr/lib\
 --with-jpeg-dir=/usr/lib\
 --with-gettext\
 --with-mhash\
 --with-ldap

make && make install

创建配置文件

cd /usr/local/php/php-7.2.2/
cp php.ini-development /usr/local/php/php7.2.2/php.ini
cp /usr/local/php/php7.2.2/etc/php-fpm.conf.default /usr/local/php/php7.2.2/etc/php-fpm.conf

#复制php-fpm管理器脚本
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

vi /usr/local/php/php7.2.2/php.ini

#修改
cgi.fix_pathinfo=0

cd /usr/local/php/php7.2.2/etc/php-fpm.d
cp www.conf.default www.conf

关闭selinux

vi /etc/selinux/config

#将SELINUX=enforcing改为SELINUX=disabled,保存后退出
SELINUX=disabled


#执行生效
getenforce

通过php-fpm脚本,启动php服务(停止、重启、重载)。

service php-fpm start
service php-fpm restart
service php-fpm stop
service php-fpm reload

 

创建网站目录

#创建网站目录及网站产生的日志存放目录
mkdir /mnt/web/cloud/wwwroot -p
mkdir /mnt/web/cloud/log -p

#创建nginx加载的虚拟主机配置存放目录
mkdir /usr/local/nginx/vhost

#创建默认文件
echo "<?php phpinfo();?>" > /mnt/web/cloud/wwwroot/index.php
echo "hi example.com" > /mnt/web/cloud/wwwroot/index.html

#设置权限
chown -R php-fpm:www /mnt/web
chmod -R 775 /mnt/web

 

配置Nginx

此前nginx已经安装了, 这里只需要配置下即可。

vi /usr/local/nginx/nginx.conf

在 http  段尾部增加

include /usr/local/nginx/vhost/*.conf;

新增一个虚拟主机配置

vi /usr/local/nginx/vhost/cloud.conf

以下内容摘自官方文档部门,为HTTP访问。为避免HTTPS测试麻烦,如果后期需要部署HTTPS,参照官方配置即可。另外,配置HTTPS需要在公网入口配置,这台机器可以保持当前配置。

查看官方Nginx部署配置,点击这里

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

log_format cloud.log.format '$remote_addr - $remote_user [$time_local] $request'
        '$status $body_bytes_sent $http_referer '
        '$http_user_agent $http_x_forwarded_for';
server {
        listen       80;
        server_name cloud.cn.n2n.ee;
        index index.html index.htm index.php;
        root  /mnt/web/cloud/wwwroot;
		
		# 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;

		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;
			#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;
		}
		
        access_log  /mnt/web/cloud/log/access.log cloud.log.format;
        error_log  /mnt/web/cloud/log/error.log;
}

运行nginx

/usr/local/nginx/nginx

尝试访问,http://公网IP:10252/index.php,成功即可!

 

安装Mariadb

避免麻烦,直接使用yum安装,并启动设置自动运行。

yum -y install mariadb mariadb-server
systemctl start mariadb
systemctl enable mariadb

初始化数据库

>mysql_secure_installation

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


Remove anonymous users? [Y/n] y
 ... Success!



Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!


Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

 

开放root远程权限,方便操作,不需要的可以忽略。

mysql -u root -p

MariaDB [mysql]> update mysql.user set host='%' where user='root' and host='localhost';
MariaDB [mysql]> flush privileges;

为NextCloud创建一个用户名及所属数据库。

CREATE DATABASE IF NOT EXISTS db_cloud DEFAULT CHARSET utf8 COLLATE utf8_bin;

CREATE USER 'user_cloud'@'%' IDENTIFIED BY 'vuu01z4ztsdl0rmu';

GRANT SELECT, INSERT, UPDATE, REFERENCES, DELETE, CREATE, DROP, ALTER, INDEX, TRIGGER, CREATE VIEW, SHOW VIEW, EXECUTE, ALTER ROUTINE, CREATE ROUTINE, CREATE TEMPORARY TABLES, LOCK TABLES, EVENT ON `db\_cloud`.* TO 'user_cloud'@'%';

GRANT GRANT OPTION ON `db\_cloud`.* TO 'user_cloud'@'%';

 

安装NextCloud

获取最新 nextcloud

https://download.nextcloud.com/server/releases/

下载并解压到网站目录

cd /mnt/web/cloud/wwwroot/
wget https://download.nextcloud.com/server/releases/nextcloud-13.0.0.tar.bz2
tar -xjf nextcloud-13.0.0.tar.bz2

rm -f index.html
rm -f index.php
mv  nextcloud/* ./
rm -rf nextcloud

chown -R php-fpm:www /mnt/web
chmod -R 775 /mnt/web

至此,nextcloud基本就能使用了,至于后台的提醒错误,可以参照错误后面的链接去解决。

nextcloud项目的优化配置也可以参照官方的文档。

 

nextcloud外部存储

作为一个扩展插件,可以在后台应用中 搜索 External storage support,然后启用即可。

目录名称即是在文件管理界面展示的目录名,配置即是linux的目录路径,设置完毕后,保存即可。

另外设置下目录的权限,就可以正常使用了。

chown -R php-fpm:www /mnt/hd1
chmod -R 775 /mnt/hd1

 

(完)

media iamge
StudioEIM - 冒险者讲习所
0:00