如何使用Nginx和Https在Debian 8上设置OwnCloud 9

时间:2020-03-05 15:29:34  来源:igfitidea点击:

OwnCloud是一个开源文件同步和共享软件,适用于所有操作免费OwnCloud Server版本的人,也适用于大型企业和操作OwnCloud Enterprise Subscription的服务提供商。
它提供了安全,可靠且合规的文件同步和共享解决方案。

我们可以共享我们计算机上的一个或者多个文件和文件夹,并将它们与OwnCloud服务器同步。
我们可以将文件放在本地共享目录中,然后这些文件将立即使用OwnCloud Desktop Sync Client,Android应用程序或者iOS应用程序同步到服务器和其他设备。

在本教程中,我将说明如何在带有LEMP设置的Debian 8系统上设置OwnCloud。

1)安装Nginx

与Apache相比,Nginx是一种轻量级且快速的Web服务器。
要在Debian上安装Nginx,我们可以在下面运行以下命令:

root@debian-theitroad:~# cat /etc/debian_version
8.6
root@debian-theitroad:~# apt-get install nginx -y
root@debian-theitroad:/# nginx -v
nginx version: nginx/1.6.2
root@debian-theitroad:~# service nginx status
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
Active: active (running) since Tue 2015-10-25 03:00:54 UTC; 24s ago
Main PID: 1422 (nginx)
CGroup: /system.slice/nginx.service
├─1422 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─1423 nginx: worker process
├─1424 nginx: worker process
├─1425 nginx: worker process
└─1426 nginx: worker process

安装后请确认服务状态。
我们甚至可以浏览IP http://IP来查看默认的Nginx欢迎页面,如下所示:

Welcome to nginx on Debian!
If you see this page, the nginx web server is successfully installed and working on Debian. Further configuration is required.
For online documentation and support please refer to nginx.org
Please use the reportbug tool to report bugs in the nginx package with Debian. However, check existing bug reports before reporting a new bug.
Thank you for using debian and nginx.

2)安装MariaDB

MariaDB是数据库管理的流行选择之一。
它是MySQL的直接替代品。
我们可以使用此命令安装MariaDB。

root@debian-theitroad:~# apt-get install -y mariadb-server mariadb-client

在安装过程中,它将要求我们设置MariaDB“ root”用户密码。
我们可以设置所需的MariaDB root用户密码并继续安装。

我们可以运行MySQL安全安装来保护MariaDB安装。

root@debian-theitroad:~# mysql --version
mysql Ver 15.1 Distrib 10.0.27-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
root@debian-theitroad:~# 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):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
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
... 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
... 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
... skipping.
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!

3)安装PHP7

在进行这些安装之前,我们需要将dotdeb.org存储库添加到debian源列表/etc/apt/sources.list文件中并安装其GnuPG密钥。

deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all
root@debian-theitroad:~# wget https://www.dotdeb.org/dotdeb.gpg
root@debian-theitroad:~# apt-key add dotdeb.gpg
OK

现在,我们需要更新存储库软件包并安装所需的PHP。

root@debian-theitroad:~# apt-get update
root@debian-theitroad:~# apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl php7.0-xml php7.0-zip php7.0-mbstring -y

安装后,我们需要在PHP-FPM配置文件/etc/php/7.0/fpm/php.ini中将cgi.fix_pathinfo修改为“ 0”。

请注意,cgi.fix_pathinfo对CGI提供* real * PATH_INFO/PATH_TRANSLATED支持。

修改后重新启动php-fpm实例。

root@debian-theitroad:~# service php7.0-fpm restart
root@debian-theitroad:~# service nginx restart

4)安装ownCloud 9服务器

我们可以下载官方的OwnCloud存储库文件,安装ownCloud签名密钥,然后将存储库文件添加到源列表中。
完成后,我们可以更新并安装软件包。

Download and install the Owncloud signing key.
root@debian-theitroad:~# wget -nv https://download.owncloud.org/download/repositories/stable/Debian_8.0/Release.key -O Release.key
2015-10-25 03:42:41 URL:https://download.owncloud.org/download/repositories/stable/Debian_8.0/Release.key [4502/4502] -> "Release.key" [1]
root@debian-theitroad:~# apt-key add - < Release.key
OK
Download the OwnCloud offical repository and add it to the source list.
root@debian-theitroad:~# sh -c "echo 'deb http://download.owncloud.org/download/repositories/stable/Debian_8.0//' >> /etc/apt/sources.list.d/owncloud.list"

我们可以安装两个OwnCloud软件包:owncloud和owncloud文件。
通过安装owncloud,它将自动安装apache2,MySQL和PHP。
但是其中我们已经在Debian 8服务器上配置了LEMP堆栈,因此我们只需要安装独立的owncloud文件。

Build the repository packages and install owncloud files.
root@debian-theitroad:~# apt-get update
root@debian-theitroad:~# apt-get install owncloud-files

安装后,我们的OwnCloud文件将存储在/var/www/owncloud目录中。

5)为OwnCloud创建数据库和用户

接下来,我们需要创建一个OwnCloud数据库和一个用户来管理OwnCloud数据库。

root@debian-theitroad:/var/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.0.27-MariaDB-0+deb8u1 (Debian)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database owncloud;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create user ownclouduser@localhost identified by 'password';
Query OK, 0 rows affected (0.00 sec)
Grant this user all privileges on owncloud database.
MariaDB [(none)]> grant all privileges on owncloud.* to ownclouduser@localhost identified by 'password';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;

6)如何启用MariaDB二进制日志记录

OwnCloud正在使用TRANSACTION_READ_COMMITTED事务隔离来避免高负载情况下的数据丢失。
使用MySQL或者MariaDB时,这需要正确配置的二进制日志记录。

我们需要将以下行添加到MySQL配置文件/etc/mysql/my.cnf中,以启用二进制日志记录。

log-bin = /var/log/mysql/mariadb-bin
log-bin-index = /var/log/mysql/mariadb-bin.index
binlog_format = mixed

完成这些修改后,重新启动MySQL服务。

service mysql reload

7)使用Letsencrypt为OwnCloud域创建SSL

在安装“Let’s Encrypt ”之前,需要安装其中两个软件包。
BC是“任意精度语言计算器”。
它用于Let's Encrypt软件中的自动更新脚本。
我们可以使用以下命令安装这些软件包:

root@debian-theitroad:~# apt-get install git bc -y

完成后,我们可以通过从GitHub克隆存储库轻松下载加密文件。

root@debian-theitroad:~# git clone https://github.com/letsencrypt/letsencrypt
Cloning into 'letsencrypt'...
remote: Counting objects: 41688, done.
remote: Compressing objects: 100% (61/61), done.
remote: Total 41688 (delta 30), reused 0 (delta 0), pack-reused 41626
Receiving objects: 100% (41688/41688), 11.56 MiB | 19.50 MiB/s, done.
Resolving deltas: 100% (29708/29708), done.
Checking connectivity... done.

现在,我们可以使用单个命令发布SSL证书。

root@debian-theitroad:~/letsencrypt# ./letsencrypt-auto certonly --standalone --email <your-email-address> --agree-tos -d nodenixbox.com
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/nodenixbox.com/fullchain.pem. Your cert will
expire on 2016-01-25. To obtain a new or tweaked version of this
certificate in the future, simply run letsencrypt-auto again. To
non-interactively renew *all* of your certificates, run
"letsencrypt-auto renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG/Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

8)为OwnCloud域创建虚拟主机

现在,我们可以为OwnCloud域创建虚拟主机。
请查看我的虚拟主机

root@debian-theitroad:~# nano /etc/nginx/sites-enabled/nodenixbox.com.conf
upstream php-handler {
server unix:/run/php/php7.0-fpm.sock;
}
server {
listen 80;
server_name nodenixbox.com;
}
server {
listen 443 ssl;
server_name nodenixbox.com;
ssl_certificate /etc/letsencrypt/live/nodenixbox.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/nodenixbox.com/privkey.pem;
# Path to the root of your installation
root /var/www/owncloud/;
# set max upload size
client_max_body_size 10G;
fastcgi_buffers 64 4K;
# Disable gzip to avoid the removal of the ETag header
gzip off;
# Uncomment if your server is build with the ngx_pagespeed module
# This module is currently not supported.
#pagespeed off;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
rewrite ^/.well-known/carddav /remote.php/dav/permanent;
rewrite ^/.well-known/caldav /remote.php/dav/permanent;
# 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 = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/{
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location/{
rewrite ^/remote/(.*) /remote.php last;
rewrite ^(/core/doc/[^\/]+/)$/index.html;
try_files $uri $uri/=404;
}
location ~ \.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;
fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
fastcgi_pass php-handler;
fastcgi_intercept_errors on;
}
# Adding the cache control header for js and css files
# Make sure it is BELOW the location ~ \.php(?:$|/) { block
location ~* \.(?:css|js)${
add_header Cache-Control "public, max-age=7200";
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
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;
}
# Optional: Don't log access to other assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)${
access_log off;
}
}

我们可以根据自己的cloud域名和证书详细信息,用实际数据替换突出显示的文本。
出于安全原因,我强烈建议为OwnCloud URL强制使用https。

9)创建和管理OwnCloud管理帐户

最后,我们可以创建我们的OwnCloud管理帐户,并将其服务连接到我们的MariaDB数据库。
让我们通过浏览我们的OwnCloud域(在我的情况下为https://nodenixbox.com)来访问OwnCloud存储。
我们可以创建管理员登录凭据,指定存储目录和数据库详细信息以完成安装。