搭建私有云同步云盘Nextcloud

环境准备

本次搭建使用LAMP运行环境:

  • L: Open SUSE Linux 15
  • A: apache2
  • M: MariaDB
  • P: PHP7

系统安装命令准备

更新命令zypper的源:

1
zypper ref

添加源,使用命令 zypper addrepo -f [URL] [Alias] (Alias是自己给这个源随便定义的英文名)添加软件源并开启自动刷新,如:

1
zypper addrepo -f http://mirrors.163.com/openSUSE/update/11.1/ main
1
zypper addrepo -f http://mirrors.163.com/openSUSE/distribution/11.1/repo/non-oss/ nonoss
1
zypper addrepo -f http://mirrors.163.com/openSUSE/distribution/11.1/repo/oss/ oss

删除源,使用命令 zypper rs [name]

1
zypper rs name

安装apache2

切换为root用户:

1
sudo

安装 apache2:

1
zypper in apache2

启动 apache2:

1
systemctl start apache2

停止 apache2:

1
systemctl stop apache2

重启 apache2:

1
systemctl restart apache2

系统重启后自动启动 apache 服务:

1
systemctl enable apache2

测试安装

为了检查你的apache2服务是否正常工作,使用touch命令在/srv/www/htdocs/文件夹下创建一个文件名为index.html的文件,写入以下内容:

1
<html><body><h1>Welcome to my web site!</h1></body></html>

之后在浏览器访问 http://localhost ,如果正常可以看到大字标题 Welcome to my web site!

启用web服务可以外部访问

需要在防火墙中开启http(80)端口,apache默认使用80端口。

安装PHP7

root权限下,执行下面命令安装php7:

1
zypper in php7 php7-mysql apache2-mod_php7

启用mod-php通过运行:

1
a2enmod php7

安装php7的扩展

1
sudo zypper in php7-bcmath php7-opcache php7-bz2 php7-calendar php7-ctype php7-curl php7-dom php7-ftp php7-gd php7-gettext php7-gmp php7-iconv php7-imap php7-ldap php7-mbstring php7-mcrypt php7-odbc php7-openssl php7-pcntl php7-pgsql php7-posix php7-shmop php7-snmp php7-soap php7-sockets php7-sqlite php7-sysvsem php7-tokenizer php7-wddx php7-xmlrpc php7-xsl php7-zlib php7-exif php7-fastcgi php7-pear php7-sysvmsg php7-sysvshm php7-zip php7-intl

重启web服务器

1
systemctl restart apache2

测试安装

为了验证php是否已经在运行,在/srv/www/htdocs/文件夹下创建一个index.php的文件,并写入以下内容:

1
2
3
<?php
phpinfo();
?>

使用浏览器访问 http://localhost ,你应该可以看到一个页面包含一个表格显示所有的PHP设置。

安装MariaDB

MariaDB 是一个MySQL的代替品,而且也是使用的mysql名称。
root权限下,执行下面命令安装mariadbmariadb-tools

1
zypper in mariadb mariadb-tools

mariadb-tools 是MariaDB的管理工具集。
启动MariaDB服务,运行:

1
systemctl start mysql

停止 mysql:

1
systemctl stop mysql

重启 mysql:

1
systemctl restart mysql

系统重启后自动启动 mysql 服务:

1
systemctl enable mysql

配置 MariaDB/MySql 服务和 Leap 42.1

通过配置 MariaDB 服务提高安全性,请使用openSUSE提供的脚本mysql_secure_installation

1
mysql_secure_installation

执行后显示:

root # mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here.
Enter current password for root (enter for none):

按回车(Enter)键:

如果无法正常执行到下一步,则在 /etc/my.cnf 文件中加入 skip-grant-tables ,保存并重启mysql服务。

root #
... (output sequel of previous command)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation.
Set root password? [Y/n] y

输入y:

root #
... (output sequel of previous command)
New password:

输入给root用户的密码:

root #
... (output sequel of previous command)
Re-enter new password:

输入确认密码:

root #
... (output sequel of previous command)
Password updated successfully!

Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? [Y/n]

输入y来移除匿名用户:

root #
... (output sequel of previous command)
... Success!

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]

输入y:

root #
... (output sequel of previous command)
... Success!

By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.
Remove test database and access to it? [Y/n]

输入y:

root #
... (output sequel of previous command)
- Dropping test database...

... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
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!

安装 phpMyAdmin

执行下面命令安装:

1
zypper in phpMyAdmin

安装完成后重启apache2服务。
登录到phpMyAdmin:

1
http://localhost/phpMyAdmin

输入mysql服务的用户名root和root的密码,点击登录。

给Nextcloud创建数据库

在这里,以用户root登录phpMyAdmin后,创建一个新的数据库 ‘nextcloud’ 即可。

安装Nextcloud

官网下载Nextcloud,这里下载的是:https://download.nextcloud.com/server/releases/nextcloud-19.0.1.zip
上传下载好的压缩包nextcloud-19.0.1.zip到目录/srv/www/htdocs/下,并解压:

1
cd /srv/www/htdocs/
1
upzip nextcloud-19.0.1.zip

删除压缩包:

1
rm nextcloud-19.0.1.zip

新建一个data文件夹,用于存放文件:

1
mkdir /srv/www/htdocs/nextcloud/data

给nextcloud目录赋权限:

1
sudo chmod 777 nextcloud -Rf

访问Nextcloud

访问地址:

1
http://localhost/nextcloud

访问成功后:

  • 在页面的最上面输入需要给Nextcolud创建的管理员账号和密码
  • 选择数据存放的位置位刚才新建的data文件夹
  • 选择数据库MySQL/MariaDB,输入数据库的账号,密码,数据库名称
  • 本地搭建的最后一项一般都填localhost和端口

最后,点击安装完成。

如果安装时出现如下错误:

1
2
3
4
nextcloud install Error while trying to initialise the database: An exception 	occurred while executing a query: SQLSTATE[HY000]: General error: 4047 InnoDB 	refuses to write tables with ROW_FORMAT=COMPRESSED or KEY_BLOCK_SIZE.
```
解决方法:
1. 进入mariadb控制台,键入以下命令并回车;

mysql -u root -p
1
2
2. 输入mariadb的root密码,回车登录;  
3. 输入以下命令并回车;

SET GLOBAL innodb_read_only_compressed=OFF;
1
2
3
4
	4. 这时候再次注册nextcloud,发现成功解决。


> 如果出现如下错误:

Your Data directory is invalid 请确保文件根目录下包含有一个名为“.ocdata”的文件。
1
2
解决方法:  
使用以下命令更改目录权限即可

chown -R username:groupname /path/to/folder
1
2
3
4
5

待安装完成后,访问 `http://localhost/nextcloud` ,输入刚刚设置的管理员账号密码登录即可使用。

### 关闭数据目录权限检查
打开配置文件:

vim /srv/www/htdocs/nextcloud/config/config.php

1
在最后加入:

‘check_data_directory_permissions’ => false,

1
2
3
4
5
6


## Nextcloud优化
### 设置后台任务,cron执行
启用wwwrun用户(如已经启用可以忽略,根据自己web服务的用户组和用户而定)
在root用户下执行

crontab -u wwwrun -e

1
追加以下内容:

*/5 * * * * php -f /srv/www/htdocs/nextcloud/cron.php

1
2
> 注意 cron.php 的权限:chown -R wwwrun:www cron.php
测试执行任务:

sudo -u wwwrun php -f /srv/www/htdocs/nextcloud/cron.php

1
2
3

可参考crontab命令用法:
1)设置定时器的设置文件, 文件名称为mycronset.txt(名称可自行设定)

cat mycronset.txt

1
2
3
4
5

0 4 * * * root updatedb #每天早上4:00以root用户身份执行updatedb命令
0 4 * * * root sendmail -q #每天早上4:00以root用户身份执行updatedb命令

使用自定义的crontab设置,指定使用者为tom用户

crontab -u tom mycronset.txt

1
2

2)列出当前定时器的设置

crontab -l

1
2

3)编辑用户定时器的设置文件

crontab -e

1
2

4)删除定时器设置

crontab -r

1
2
### 配置缓存
此处利用APCu+memcache实现缓存,具体做法是在配置文件`/srv/www/htdocs/nextcloud/config/config.php`中,加入下面设置:

‘memcache.local’ => ‘\OC\Memcache\APCu’,
‘memcache.distributed’ => ‘\OC\Memcache\Memcached’,
‘memcached_servers’ =>
array(
array(‘localhost’, 11211),
),

1
2
3
如果尚未安装APCu或memcache,请参考下面内容安装:
#### 安装APCu
依次执行下面命令安装:

cd /tmp/

1
2
```
tar -zxvf apcu-5.1.18.tgz
1
cd apcu-5.1.18/ 
1
phpize
1
./configure --with-php-config=/usr/bin/php-config
1
make
1
make install

在执行phpize命令后,如果提示没有phpize命令,则执行下面命令安装所需的phpize:

1
sudo zypper in php7-devel

最后配置php.ini:

1
vim /etc/php7/apache2/php.ini

在php.ini的最后加入以下配置:

1
2
3
4
5
extension = apcu.so
apc.enabled = on
apc.shm_size = 64M
apc.ttl = 7200
apc.enable_cli = on

安装memcache

Memcached会用到libevent这个库来进行Socket的处理,所以先安装libevent。

1. 先装libevent

安装包的官网下载地址:https://libevent.org/
依次执行下面命令安装:

1
cd /tmp/
1
tar -zxvf libevent-2.1.12-stable.tar.gz
1
cd libevent-2.1.12-stable/ 
1
./configure -prefix=/usr
1
make
1
make install

在执行./configure -prefix=/usr命令后,如果报错:configure: error: openssl is a must but can not be found,则执行下面命令安装所需的openssl:

1
zypper in openssl-devel

测试libevent是否安装成功:

1
ls -al /usr/lib | grep libevent
2. 再装memcache
1
cd /tmp/
1
tar -zxvf memcached-1.6.6.tar.gz
1
cd memcached-1.6.6/
1
./configure -prefix=/usr  -with-libevent=/usr
1
make
1
make install

测试memcache是否安装成功:

1
ls -al /usr/local/bin/mem*
3. 启动Memcached服务
1
memcached -d -m 10 -u root -l 10.1.90.40 -p 12000 -c 256 -P /home/javadev/memcached_pid/

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.1.132,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /home/javadev/m emcached_pid/

使用命令查看启动的memcached服务进程

1
ps -ef | grep memcached

启动成功后如下:

root     26084     1  0 14:41 ?        00:00:00 memcached -d -m 10 -u root -l 10.1.90.40 -p 12000 -c 256 -P /home/javadev/memcached_pid/
root     26095  9798  0 14:41 pts/0    00:00:00 grep --color=auto memcached

OCC命令的用法

occ命令是ownCloud/Nextcloud的命令行界面。您可以使用occ执行许多常见的服务器操作,例如安装和升级ownCloud/Nextcloud,管理用户,加密,密码,LDAP设置等。
一般格式是这样的:

1
sudo -u www-data php occ [options] [arguments]

更多命令可参考:ownCloud/Nextcloud OCC命令行工具详解

重置密码

user相关命令:

1
2
3
4
5
6
7
8
9
10
user:add                            添加一个用户
user:delete 删除特定用户
user:disable 禁用特定用户
user:enable 激活特定用户
user:info 显示用户信息
user:lastseen 显示用户最后登陆了时间
user:list 列出所有用户
user:report 显示有多少用户访问
user:resetpassword 为一个用户重置密码
user:setting 读取和修改用户设置

假如管理员密码忘了,可利用OCC命令按以下步骤重置:

  1. 进入nextcloud目录下
  2. 查看已有用户名列表
    1
    sudo -u wwwrun php occ user:list
  3. 重置管理员账号admin的密码
    1
    sudo -u wwwrun php occ user:resetpassword admin
  4. 按提示输入新密码即可。