Debian8+Nginx+MariaDB+PHP7环境搭建ownCloud云存储

这篇教程介绍Debian8+Nginx+MariaDB+PHP7环境搭建ownCloud云存储的步骤。ownCloud的安装过程和WordPress,Drupal等网站程序的安装过程很相似。我使用128MB内存的VPS,跑ownCloud绰绰有余。这篇教程假设你已经在Debian 8 VPS搭建了LNMP服务器环境。如果还不知道如何搭建LNMP,请看这篇文章。

Debian8 Jessie安装LNMP架构(Nginx, MariaDB, PHP7)

LNMP环境搭建好后,请继续往下阅读。

第1步:Debian 8 VPS安装ownCloud 9 Server

首先,用wget下载ownCloud的公钥。

wget -nv https://download.owncloud.org/download/repositories/stable/Debian_8.0/Release.key -O Release.key

使用apt-key将公钥导入Debian VPS。

sudo apt-key add - < Release.key

然后运行下面的命令添加ownCloud官方软件库。

sudo sh -c "echo 'deb http://download.owncloud.org/download/repositories/stable/Debian_8.0/ /' >> /etc/apt/sources.list.d/owncloud.list"

更新本地软件包索引并安装owncloud-files

sudo apt-get update;sudo apt-get install owncloud-files

注意,ownCloud官方软件库里有两个软件包:owncloudowncloud-files。前者除了安装ownCloud程序外,还会自动安装Apache2,MySQL和PHP5,而后者只会安装ownCloud本身。由于我们自己搭建了Nginx+MariaDB+PHP7环境,所以我们要安装owncloud-files,以避免自动安装Apache等程序。

安装完成后,ownCloud的程序文件存放在/var/www/owncloud目录下。

第2步:为ownCloud创建一个数据库和用户

登录MariaDB数据库服务器:

mysql -u root -p

创建owncloud数据库:

create database owncloud;

在localhost创建一人用户,并设置用户密码。

create user ownclouduser@localhost identified by 'password';

赋予这个ownclouduser对owncloud数据库的所有操作权限。

grant all privileges on owncloud.* to ownclouduser@localhost identified by 'password';

刷新权限后退出MariaDB数据库服务器。

flush privileges;

exit;

第3步:MariaDB启用二进制日志(binary log)

ownCloud要求数据库启用二进制日志。打开MariaDB的配置文件my.cnf

sudo nano /etc/my.cnf

你的my.cnf文件也可能保存在/etc/mysql/my.cnf 。将下面三行文字添加到[mysqld]部分。

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

第一行表示启用二进制日志,第二行指定了二进制日志索引文件的名称,第三行指定二进制日志的格式为mixed。二进制日志的格式必须为mixed,否则之后ownCloud创建数据表时会出错。

保存文件后,重新加载MariaDB的配置文件。

sudo service mysql reload         或      sudo systemctl reload mysql

第4步:向Let’s Encrypt申请一个免费的SSL证书

由于我们要访问ownCloud网页管理界面创建管理员用户并设置密码,所以为了安全我们要用HTTPS加密网页。(加密应该要成为互联网的标准。)

首先从github安装Let’s Encrypt的客户端。

sudo apt-get install git

git clone https://github.com/letsencrypt/letsencrypt

cd进入letsencrypt目录。

cd letsencrypt/

停止Nginx进程。

sudo service nginx stop       或         sudo systemctl stop nginx

用下面的命令为你的域名申请一个免费的SSL证书。

./letsencrypt-auto certonly --standalone --email <your-email-address> --agree-tos -d owncloud.your-domain.com

红色部分需要你替换成自己的邮箱地址和域名。在运行这条命令之前,你需要在DNS里将你的域名A记录设置成服务器的IP。命令中的certonly表示只申请SSL证书,而不安装(配置)证书。因为Let’s Encrypt还不支持对Nginx自动安装SSL证书,需要我们手动安装SSL证书。

你的SSL证书会保存在/etc/letsencrypt/live/<你的域名>目录下.

第5步:为ownCloud创建一个Nginx配置文件

sudo nano /etc/nginx/conf.d/owncloud.conf

在文件中添加如下配置,红色文字需要你替换成自己的域名.

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

server {
 listen 80;
 server_name owncloud.your-domain.com;
 # enforce https
 return 301 https://$server_name$request_uri;
 }

server {
 listen 443 ssl http2;
 server_name owncloud.your-domain.com;

 ssl_certificate /etc/letsencrypt/live/owncloud.your-domain.com/fullchain.pem;
 ssl_certificate_key /etc/letsencrypt/live/owncloud.your-domain.com/privkey.pem;

# 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;

 # 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/carddav/ permanent;
 rewrite ^/.well-known/caldav /remote.php/caldav/ 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/[^\/]+/)$ $1/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;
 # 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;
 }
}

以上配置是假设你的php-fpm和Nginx使用Unix socket互相通信.如果你的php-fpm和Nginx使用TCP 127.0.0.1:9000通信,你需要在修改最前面的upstream设置.在这个配置文件中我也启用了http/2.

重新启动Nginx.

sudo service nginx restart            or            sudo systemctl restart nginx

第6步:数据库连接

在浏览器地址栏输入你的域名:

owncloud.your-domain.com

你需要设置一个管理员账号,输入之前创建的owncloud数据库,数据库用户名和密码,将ownCloud与MariaDB数据库服务器连接.

Debian8+Nginx+MariaDB+PHP7环境搭建ownCloud云存储

经过上面的步骤,ownCloud就安装好了!现在你可以进入网页管理界面,拥有自己的ownCloud私人云存储了.

Debian8+Nginx+MariaDB+PHP7环境搭建ownCloud云存储

为这篇文章评分
[Total: 9 Average: 3.2]

3 Responses to “Debian8+Nginx+MariaDB+PHP7环境搭建ownCloud云存储

  • JayRose
    7 years ago

    如果你的php-fpm和Nginx使用TCP 127.0.0.1:9000通信,你需要在修改最前面的upstream设置.在这个配置文件中我也启用了http/2. 请问这个如何修改?卡在这里了

    • 如果你是按照我的教程安装的LNMP,那么你就不需要修改了。默认是使用Unix Socket通信的。

      如果你用的是TCP Socket,那么去掉 server 127.0.0.1:9000前面的#注释符,并在server unix:/run/php/php7.0-fpm.sock;这一行前面添加#注释符。

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

Leave a Reply

Your email address will not be published.

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here