php

阅读:389 2019-03-19 14:40:39 来源:新网

nextcloud是一款自由(开源)的类dropbox软件,由owncloud分支演化形成。它使用php和javascript编写,支持多种数据库系统,比如mysql/mariadb、postgresql、oracle数据库和sqlite。

nextcloud是一款自由(开源)的类dropbox软件,由owncloud分支演化形成。它使用php和javascript编写,支持多种数据库系统,比如mysql/mariadb、postgresql、oracle数据库和sqlite。它可以使你的桌面系统和云服务器中的文件保持同步,nextcloud为windows、linux、mac、安卓以及苹果手机都提供了客户端支持。nextcloud并非只是dropbox的克隆,它还提供了很多附加特性,如日历、联系人、计划任务以及流媒体ampache。

在这篇文章中,我将向你展示如何在centos7服务器中安装和配置最新版本的nextcloud10。我会通过nginx和php7-fpm来运行nextcloud,同时使用mariadb做为数据库系统。

先决条件

在开始安装nginx和php7-fpm之前,我们还学要先添加epel包的仓库源。使用如下命令:

yum-yinstallepel-release

现在开始从epel仓库来安装nginx:

yum-yinstallnginx

然后我们还需要为php7-fpm添加另外一个仓库。互联网中有很个远程仓库提供了php7系列包,我在这里使用的是webtatic。

添加php7-fpmwebtatic仓库:

rpm-uvhhttps://mirror.webtatic.com/yum/el7/webtatic-release.rpm

然后就是安装php7-fpm以及nextcloud需要的一些包。

yum-yinstallphp70w-fpmphp70w-cliphp70w-gdphp70w-mcryptphp70w-mysqlphp70w-pearphp70w-xmlphp70w-mbstringphp70w-pdophp70w-jsonphp70w-pecl-apcuphp70w-pecl-apcu-devel

最后,从服务器终端里查看php的版本号,以便验证php是否正确安装。

php-v

查看php版本号

在这一个步骤中,我们将配置php-fpm与nginx协同运行。php7-fpm将使用nginx用户来运行,并监听9000端口。

使用vim编辑默认的php7-fpm配置文件。

vim/etc/php-fpm.d/www.conf

在第8行和第10行,user和group赋值为nginx。

user=nginxgroup=nginx

在第22行,确保php-fpm运行在指定端口。

listen=127.0.0.1:9000

取消第366-370行的注释,启用php-fpm的系统环境变量。

env[hostname]=$hostnameenv[path]=/usr/local/bin:/usr/bin:/binenv[tmp]=/tmpenv[tmpdir]=/tmpenv[temp]=/tmp

保存文件并退出vim编辑器。

下一步,就是在/var/lib/目录下创建一个新的文件夹session,并将其拥有者变更为nginx用户。

mkdir-p/var/lib/php/sessionchownnginx:nginx-r/var/lib/php/session/

然后启动php-fpm和nginx,并且将它们设置为随开机启动的服务。

sudosystemctlstartphp-fpmsudosystemctlstartnginxsudosystemctlenablephp-fpmsudosystemctlenablenginx

启动php-fpm和nginx

php7-fpm配置完成

我这里使用mariadb作为nextcloud的数据库。可以直接使用yum命令从centos默认远程仓库中安装mariadb-server包。

yum-yinstallmariadbmariadb-server

启动mariadb,并将其添加到随系统启动的服务中去。

systemctlstartmariadbsystemctlenablemariadb

现在开始配置mariadb的root用户密码。

mysql_secure_installation

键入y,然后设置mariadb的root密码。

setrootpassword?[y/n]ynewpassword:re-enternewpassword:removeanonymoususers?[y/n]ydisallowrootloginremotely?[y/n]yremovetestdatabaseandaccesstoit?[y/n]yreloadprivilegetablesnow?[y/n]y

这样就设置好了密码,现在登录到mysqlshell并为nextcloud创建一个新的数据库和用户。这里我创建名为nextcloud_db的数据库以及名为nextclouduser的用户,用户密码为nextclouduser@。当然了,要给你自己的系统选用一个更安全的密码。

mysql-uroot-p

输入mariadb的root密码,即可登录mysqlshell。

输入以下mysql查询语句来创建新的数据库和用户。

createdatabasenextcloud_db;createusernextclouduser@localhostidentifiedby'nextclouduser@';grantallprivilegesonnextcloud_db.*to nextclouduser@localhostidentifiedby'nextclouduser@';flushprivileges;

为nextcloud创建一个新的数据库和用户

nextcloud_db数据库和nextclouduser数据库用户创建完成

在教程中,我会让客户端以https连接来运行nextcloud。你可以使用诸如let'sencrypt等免费ssl证书,或者是自己创建自签名(selfsigned)ssl证书。这里我使用openssl来创建自己的自签名ssl证书。

为ssl文件创建新目录:

mkdir-p/etc/nginx/cert/

如下,使用openssl生成一个新的ssl证书。

opensslreq-new-x509-days365-nodes-out/etc/nginx/cert/nextcloud.crt-keyout/etc/nginx/cert/nextcloud.key

最后使用chmod命令将所有证书文件的权限设置为600。

chmod700/etc/nginx/certchmod600/etc/nginx/cert/*

为nextcloud生成一个自签名ssl证书

我直接使用wget命令下载nextcloud到服务器上,因此需要先行安装wget。此外,还需要安装unzip来进行解压。使用yum命令来安装这两个程序。

yum-yinstallwgetunzip

先进入/tmp目录,然后使用wget从官网下载最新的nextcloud10。

cd/tmpwgethttps://download.nextcloud.com/server/releases/nextcloud-10.0.2.zip

解压nextcloud,并将其移动到/usr/share/nginx/html/目录。

unzipnextcloud-10.0.2.zipmvnextcloud//usr/share/nginx/html/

下一步,转到nginx的web根目录为nextcloud创建一个data文件夹。

cd/usr/share/nginx/html/mkdir-pnextcloud/data/

变更nextcloud目录的拥有者为nginx用户和组。

chownnginx:nginx-rnextcloud/步骤6-在nginx中为nextcloud配置虚拟主机

在步骤5我们已经下载好了nextcloud源码,并配置好了让它运行于nginx服务器中,但我们还需要为它配置一个虚拟主机。在nginx的conf.d目录下创建一个新的虚拟主机配置文件nextcloud.conf。

cd/etc/nginx/conf.d/vimnextcloud.conf

将以下内容粘贴到虚拟主机配置文件中:

upstreamphp-handler{server127.0.0.1:9000;#serverunix:/var/run/php5-fpm.sock;}server{listen80;server_namecloud.nextcloud.co;#enforcehttpsreturn301https://$server_name$request_uri;}server{listen443ssl;server_namecloud.nextcloud.co;ssl_certificate/etc/nginx/cert/nextcloud.crt;ssl_certificate_key/etc/nginx/cert/nextcloud.key;#addheaderstoservesecurityrelatedheaders#beforeenablingstrict-transport-securityheaderspleasereadintothis#topicfirst.add_headerstrict-transport-security"max-age=15768000;includesubdomains;preload;";add_headerx-content-type-optionsnosniff;add_headerx-frame-options"sameorigin";add_headerx-xss-protection"1;mode=block";add_headerx-robots-tagnone;add_headerx-download-optionsnoopen;add_headerx-permitted-cross-domain-policiesnone;#pathtotherootofyourinstallationroot/usr/share/nginx/html/nextcloud/;location=/robots.txt{allowall;log_not_foundoff;access_logoff;}#thefollowing2rulesareonlyneededfortheuser_webfingerapp.#uncommentitifyou'replanningtousethisapp.#rewrite^/.well-known/host-meta/public.php?service=host-metalast;#rewrite^/.well-known/host-meta.json/public.php?service=host-meta-json#last;location=/.well-known/carddav{return301$scheme://$host/remote.php/dav;}location=/.well-known/caldav{return301$scheme://$host/remote.php/dav;}#setmaxuploadsizeclient_max_body_size512m;fastcgi_buffers644k;#disablegziptoavoidtheremovaloftheetagheadergzipoff;#uncommentifyourserverisbuildwiththengx_pagespeedmodule#thismoduleiscurrentlynotsupported.#pagespeedoff;error_page403/core/templates/403.php;error_page404/core/templates/404.php;location/{rewrite^/index.php$uri;}location~^/(?:build|tests|config|lib|3rdparty|templates|data)/{denyall;}location~^/(?:.|autotest|occ|issue|indie|db_|console){denyall;}location~^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+|core/templates/40[34]).php(?:$|/){includefastcgi_params;fastcgi_split_path_info^(.+.php)(/.*)$;fastcgi_paramscript_filename$document_root$fastcgi_script_name;fastcgi_parampath_info$fastcgi_path_info;fastcgi_paramhttpson;#avoidsendingthesecurityheaderstwicefastcgi_parammodheadersavailabletrue;fastcgi_paramfront_controller_activetrue;fastcgi_passphp-handler;fastcgi_intercept_errorson;fastcgi_request_bufferingoff;}location~^/(?:updater|ocs-provider)(?:$|/){try_files$uri/=404;indexindex.php;}#addingthecachecontrolheaderforjsandcssfiles#makesureitisbelowthephpblocklocation~*.(?:css|js)${try_files$uri/index.php$uri$is_args$args;add_headercache-control"public,max-age=7200";#addheaderstoservesecurityrelatedheaders(itisintendedto#havethoseduplicatedtotheonesabove)#beforeenablingstrict-transport-securityheaderspleasereadinto#thistopicfirst.add_headerstrict-transport-security"max-age=15768000;includesubdomains;preload;";add_headerx-content-type-optionsnosniff;add_headerx-frame-options"sameorigin";add_headerx-xss-protection"1;mode=block";add_headerx-robots-tagnone;add_headerx-download-optionsnoopen;add_headerx-permitted-cross-domain-policiesnone;#optional:don'tlogaccesstoassetsaccess_logoff;}location~*.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)${try_files$uri/index.php$uri$is_args$args;#optional:don'tlogaccesstootherassetsaccess_logoff;}}

保存文件并退出vim。

下载测试以下该nginx配置文件是否有错误,没有的话就可以重启服务了。

nginx-tsystemctlrestartnginx

在nginx中为nextcloud配置虚拟主机

本教程中,我们将以强制模式运行selinux,因此需要一个selinux管理工具来为nextcloud配置selinux。

使用以下命令安装selinux管理工具。

yum-yinstallpolicycoreutils-python

然后以root用户来运行以下命令,以便让nextcloud运行于selinux环境之下。如果你是用的其他名称的目录,记得将nextcloud替换掉。

semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/data(/.*)?'semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/config(/.*)?'semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/apps(/.*)?'semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/assets(/.*)?'semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/.htaccess'semanagefcontext-a-thttpd_sys_rw_content_t'/usr/share/nginx/html/nextcloud/.user.ini'restorecon-rv'/usr/share/nginx/html/nextcloud/'

接下来,我们要启用firewalld服务,同时为nextcloud开启http和https端口。

启动firewalld并设置随系统启动。

systemctlstartfirewalldsystemctlenablefirewalld

现在使用firewall-cmd命令来开启http和https端口,然后重新加载防火墙。

firewall-cmd--permanent--add-service=httpfirewall-cmd--permanent--add-service=httpsfirewall-cmd--reload

为nextcloud配置firewalld规则

至此,服务器配置完成。

打开你的web浏览器,输入你为nextcloud设置的域名,我这里设置为cloud.nextcloud.co,然后会重定向到安全性更好的https连接。

设置你的管理员用户名和密码,然后输入数据验证信息,点击'完成安装(finishsetup)'。

nextcloud安装

nextcloud管理面板大致如下:

nextcloud管理面板

nextcloud用户设置:

nextcloud用户设置

管理设置:

管理设置

至此,我们在centos7服务器上通过使用nginx、php7-fpm、mariadb完成了nextcloud的安装。

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服