首页 后端开发 php教程 详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

Mar 25, 2017 am 09:12 AM

这篇文章主要介绍了详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud,会通过 Nginx 和 PHP7-FPM 来运行 Nextcloud,同时使用 MariaDB 做为数据库系统。

Nextcloud 是一款自由 (开源) 的类 Dropbox 软件,由 ownCloud 分支演化形成。它使用 PHP 和 Javascript 编写,支持多种数据库系统,比如 MySQL/MariaDB、PostgreSQL、Oracle 数据库和 SQLite。它可以使你的桌面系统和云服务器中的文件保持同步,Nextcloud 为 Windows、Linux、Mac、安卓以及苹果手机都提供了客户端支持。Nextcloud 并非只是 Dropbox 的克隆,它还提供了很多附加特性,如日历、联系人、计划任务以及流媒体 Ampache。

在这篇文章中,我将向你展示如何在 CentOS 7 服务器中安装和配置最新版本的 Nextcloud 10。我会通过 Nginx 和 PHP7-FPM 来运行 Nextcloud,同时使用 MariaDB 做为数据库系统。

先决条件

  1. 64 位的 CentOS 7

  2. 服务器的 Root 权限

步骤 1 - 在 CentOS 7 中安装 Nginx 和 PHP7-FPM

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

yum -y install epel-release
登录后复制

现在开始从 EPEL 仓库来安装 Nginx:

yum -y install nginx
登录后复制

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

添加 PHP7-FPM webtatic 仓库:

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
登录后复制

然后就是安装 PHP7-FPM 以及 Nextcloud 需要的一些包。

代码如下:

yum -y install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear 
php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel
登录后复制

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

php -v
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

步骤 2 - 配置 PHP7-FPM

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

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

vim /etc/php-fpm.d/www.conf
登录后复制

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

user = nginx
group = nginx
登录后复制

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

listen = 127.0.0.1:9000
登录后复制

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

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
登录后复制

保存文件并退出 vim 编辑器

下一步,就是在 /var/lib/ 目录下创建一个新的文件夹 <a href="http://www.php.cn/php/php-tp-session.html" target="_blank">session</a>,并将其拥有者变更为 nginx 用户。

mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/
登录后复制

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

sudo systemctl start php-fpm
sudo systemctl start nginx
sudo systemctl enable php-fpm
sudo systemctl enable nginx
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

PHP7-FPM 配置完成

步骤 3 - 安装和配置 MariaDB

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

yum -y install mariadb mariadb-server
登录后复制

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

systemctl start mariadb
systemctl enable mariadb
登录后复制

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

mysql_secure_installation
登录后复制

键入 Y ,然后设置 MariaDB 的 root 密码。

Set root password? [Y/n] Y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
登录后复制

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

mysql -u root -p
登录后复制

输入 MariaDB 的 root 密码,即可登录 mysql shell。

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

create database nextcloud_db;
create user nextclouduser@localhost identified by &#39;nextclouduser@&#39;;
grant all privileges on nextcloud_db.* to nextclouduser@localhost identified by &#39;nextclouduser@&#39;;
flush privileges;
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

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

步骤 4 - 为 Nextcloud 生成一个自签名 SSL 证书

在教程中,我会让客户端以 https 连接来运行 Nextcloud。你可以使用诸如 let's encrypt 等免费 SSL 证书,或者是自己创建自签名 (self signed) SSL 证书。这里我使用 OpenSSL 来创建自己的自签名 SSL 证书。

为 SSL 文件创建新目录:

mkdir -p /etc/nginx/cert/
登录后复制

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

代码如下:

openssl req -new -x509 -days 365 -nodes -out /etc/nginx/cert/nextcloud.crt -keyout /etc/nginx/cert/nextcloud.key
登录后复制

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

chmod 700 /etc/nginx/cert
chmod 600 /etc/nginx/cert/*
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

步骤 5 - 下载和安装 Nextcloud

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

yum -y install wget unzip
登录后复制

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

cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-10.0.2.zip
登录后复制

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

unzip nextcloud-10.0.2.zip
mv nextcloud/ /usr/share/nginx/html/
登录后复制

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

cd /usr/share/nginx/html/
mkdir -p nextcloud/data/
登录后复制

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

chown nginx:nginx -R nextcloud/
登录后复制

步骤 6 - 在 Nginx 中为 Nextcloud 配置虚拟主机

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

cd /etc/nginx/conf.d/
vim nextcloud.conf
登录后复制

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

upstream php-handler {
  server 127.0.0.1:9000;
  #server unix:/var/run/php5-fpm.sock;
}
server {
  listen 80;
  server_name cloud.nextcloud.co;
  # enforce https
  return 301 https://$server_name$request_uri;
}
server {
  listen 443 ssl;
  server_name cloud.nextcloud.co;
  ssl_certificate /etc/nginx/cert/nextcloud.crt;
  ssl_certificate_key /etc/nginx/cert/nextcloud.key;
  # 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;";
  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;
  # Path to the root of your installation
  root /usr/share/nginx/html/nextcloud/;
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
  # The following 2 rules are only needed for the user_webfinger app.
  # Uncomment it if you&#39;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 = /.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;
  # 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;
  error_page 403 /core/templates/403.php;
  error_page 404 /core/templates/404.php;
  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/.+|core/templates/40[34])\.php(?:$|/) {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_param script_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS on;
    #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)$ {
    try_files $uri /index.php$uri$is_args$args;
    add_header Cache-Control "public, max-age=7200";
    # 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;";
    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&#39;t log access to assets
    access_log off;
  }
  location ~* \.(?:svg|gif|png|html|ttf|woff|ico|jpg|jpeg)$ {
    try_files $uri /index.php$uri$is_args$args;
    # Optional: Don&#39;t log access to other assets
    access_log off;
  }
}
登录后复制

保存文件并退出 vim。

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

nginx -t
systemctl restart nginx
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

步骤 7 - 为 Nextcloud 配置 SELinux 和 FirewallD 规则

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

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

yum -y install policycoreutils-python
登录后复制

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

semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/data(/.*)?&#39;
semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/config(/.*)?&#39;
semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/apps(/.*)?&#39;
semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/assets(/.*)?&#39;
semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/.htaccess&#39;
semanage fcontext -a -t httpd_sys_rw_content_t &#39;/usr/share/nginx/html/nextcloud/.user.ini&#39;
restorecon -Rv &#39;/usr/share/nginx/html/nextcloud/&#39;
登录后复制

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

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

systemctl start firewalld
systemctl enable firewalld
登录后复制

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

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
登录后复制

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

至此,服务器配置完成。

步骤 8 - Nextcloud 安装

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

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

Nextcloud 安装
详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程

Nextcloud 管理面板大致如下:

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程
Nextcloud 用户设置:

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程
管理设置:

详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程
至此,我们在 CentOS 7 服务器上通过使用 Nginx、PHP7-FPM、MariaDB 完成了 Nextcloud 的安装。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

以上是详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud的图文教程的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

tomcat服务器怎么让外网访问 tomcat服务器怎么让外网访问 Apr 21, 2024 am 07:22 AM

要让 Tomcat 服务器对外网访问,需要:修改 Tomcat 配置文件,允许外部连接。添加防火墙规则,允许访问 Tomcat 服务器端口。创建 DNS 记录,将域名指向 Tomcat 服务器公有 IP。可选:使用反向代理提升安全性和性能。可选:设置 HTTPS 以提高安全性。

html文件怎么生成网址 html文件怎么生成网址 Apr 21, 2024 pm 12:57 PM

要将 HTML 文件转换为网址,需要使用网络服务器,包括以下步骤:获取网络服务器。设置网络服务器。上传 HTML 文件。创建域名。路由请求。

nodejs项目怎么部署到服务器 nodejs项目怎么部署到服务器 Apr 21, 2024 am 04:40 AM

Node.js 项目的服务器部署步骤:准备部署环境:获取服务器访问权限、安装 Node.js、设置 Git 存储库。构建应用程序:使用 npm run build 生成可部署代码和依赖项。上传代码到服务器:通过 Git 或文件传输协议。安装依赖项:SSH 登录服务器并使用 npm install 安装应用程序依赖项。启动应用程序:使用 node index.js 等命令启动应用程序,或使用 pm2 等进程管理器。配置反向代理(可选):使用 Nginx 或 Apache 等反向代理路由流量到应用程

nodejs可以外网访问么 nodejs可以外网访问么 Apr 21, 2024 am 04:43 AM

是的,Node.js 可以外网访问。您可以使用以下方法:使用 Cloud Functions 部署函数并公开访问。使用 Express 框架创建路由并定义端点。使用 Nginx 反向代理请求到 Node.js 应用程序。使用 Docker 容器运行 Node.js 应用程序并通过端口映射公开。

如何使用 PHP 部署和维护网站 如何使用 PHP 部署和维护网站 May 03, 2024 am 08:54 AM

要成功部署和维护PHP网站,需要执行以下步骤:选择Web服务器(如Apache或Nginx)安装PHP创建数据库并连接PHP上传代码到服务器设置域名和DNS监控网站维护步骤包括更新PHP和Web服务器、备份网站、监控错误日志和更新内容。

如何使用 Fail2Ban 保护服务器免受暴力攻击 如何使用 Fail2Ban 保护服务器免受暴力攻击 Apr 27, 2024 am 08:34 AM

Linux管理员的一个重要任务是保护服务器免受非法攻击或访问。默认情况下,Linux系统带有配置良好的防火墙,比如iptables、UncomplicatedFirewall(UFW),ConfigServerSecurityFirewall(CSF)等,可以防止多种攻击。任何连接到互联网的机器都是恶意攻击的潜在目标。有一个名为Fail2Ban的工具可用来缓解服务器上的非法访问。什么是Fail2Ban?Fail2Ban[1]是一款入侵防御软件,可以保护服务器免受暴力攻击。它是用Python编程语

和我一起来学习Linux安装Nginx 和我一起来学习Linux安装Nginx Apr 28, 2024 pm 03:10 PM

而今天将来一起带领大家在Linux环境安装Nginx,这里用的Linux系统是CentOS7.2.准备安装工具1.从Nginx官网下载Nginx。这里用的版本为:1.13.6.2.将下载下来的Nginx上传到Linux上,这里以/opt/nginx目录为例。运行“tar-zxvfnginx-1.13.6.tar.gz”进行解压。3.切换到/opt/nginx/nginx-1.13.6目录下,运行./configure进行初始化配置。如出现下面的提示,说明该机器没有安装PCRE,而Nginx需要依

keepalived+nginx搭建高可用几个注意点 keepalived+nginx搭建高可用几个注意点 Apr 23, 2024 pm 05:50 PM

在yum安装完keepalived之后,配置keepalived的配置文件注意点在master和backup的keepalived的配置文件中,网卡名字为当前机器的网卡名称VIP的选择为可用的一个ip,通常在做高可用,局域网环境比较多,所以这个vip是和两台机器同网段的一个内网ip。如果用在外网环境下,无所谓在不在一个网段,只要客户端能访问到。停掉nginx服务,启动keepalived服务,会看到keepalived拉动nginx服务启动若是无法启动失败,基本都是配置文件和脚本的问题,或者是防

See all articles