如何在 CentOS 7 上使用 Nginx 和 PHP7-FPM 安装 Nextcloud如何在 CentOS 7 上使用 Nginx 和 PHP7-FPM 安装 Nextcloud如何在 CentOS 7 上使用 Nginx 和 PHP7-FPM 安装 Nextcloud如何在 CentOS 7 上使用 Nginx 和 PHP7-FPM 安装 Nextcloud
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 CentOS 7 上使用 Nginx 和 PHP7-FPM 安装 Nextcloud

Nextcloud 是一个免费(开源)的类似 Dropbox 的软件,是 ownCloud 项目的一个分支。 Nextcloud 是用 PHP 和 JavaScript 编写的,它支持许多数据库系统,例如 MySQL/MariaDB、PostgreSQL、Oracle 数据库和 SQLite。为了让您的文件在桌面和您自己的服务器之间保持同步,Nextcloud 提供适用于 Windows、Linux 和 Mac 桌面的应用程序以及适用于 Android 和 iOS 的移动应用程序。 Nextcloud 不仅仅是一个 Dropbox 克隆,它还提供了附加功能,如日历、联系人、计划任务和 Ampache 的流媒体。

在本教程中,我将向您展示如何在 CentOS 7 服务器上安装和配置最新的 Nextcloud 10 版本。我将使用 Nginx Web 服务器和 PHP7-FPM 运行 Nextcloud,并使用 MariaDB 作为数据库系统。

先决条件

  • CentOS 7 64 位
  • 服务器的根权限

第 1 步 - 在 CentOS 7 上安装 Nginx 和 PHP7-FPM

在我们开始安装 Nginx 和 php7-fpm 之前,我们必须添加 EPEL 包存储库。使用这个 yum 命令安装它。

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

接下来,为 Nextcloud 安装安装 PHP7-FPM 和一些额外的包。

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

第 2 步 - 配置 PHP7-FPM

在此步骤中,我们将配置 php-fpm 以与 Nginx 一起运行。 php7-fpm 将在用户 nginx 下运行并监听端口 9000。

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

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

在第 8 行和第 10 行中,将用户和组更改为 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/目录下为session路径新建一个目录,并将owner修改为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

PHP7-FPM 配置完成。

第 3 步 - 安装和配置 MariaDB

我将为 Nextcloud 数据库使用 MariaDB。使用 yum 从 CentOS 存储库安装 mariadb-server 软件包。

yum -y install mariadb mariadb-server

启动 MariaDB 服务并将其添加为在启动时运行。

systemctl start mariadb
systemctl enable mariadb

现在配置 MariaDB root 密码。

mysql_secure_installation

在请求时输入您的 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

MariaDB root 密码已设置,现在我们可以登录 mysql shell 为 Nextcloud 创建一个新数据库和一个新用户。我将创建一个名为 nextcloud_db 的新数据库和一个密码为 [email 的用户 nextclouduser。为您的安装选择一个安全密码!

mysql -u root -p
Type Password

在下面输入 mysql 查询以创建新数据库和新用户。

create database nextcloud_db;
create user ';
flush privileges;

已创建用户为 nextclouduser 的 nextcloud_db 数据库。

第 4 步 - 为 Nextcloud 生成自签名 SSL 证书

在本教程中,我将为客户端运行带有 https 连接的 nextcloud。您可以使用免费的 SSL,例如让我们加密或创建自签名 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/*

第 5 步 - 下载并安装 Nextcloud

我们会直接用wget下载Nextcloud到服务器,所以我们要先安装wget。此外,我们需要解压缩程序。使用 yum 安装这两个应用程序。

yum -y install wget unzip

转到 /tmp 目录并使用 wget 从 Nextcloud 网站下载最新稳定的 Nextcloud 10 版本。

cd /tmp
wget https://download.nextcloud.com/server/releases/nextcloud-10.0.2.zip

提取 nextcloud zip 文件并将其内容移动到 /usr/share/nginx/html/ 目录。

unzip nextcloud-10.0.2.zip
mv nextcloud/ /usr/share/nginx/html/

接下来,转到 Nginx web 根目录并为 Nextcloud 创建一个新的数据目录。

cd /usr/share/nginx/html/
mkdir -p nextcloud/data/

将 nextcloud 目录的所有者更改为 nginx 用户和组。

chown nginx:nginx -R nextcloud/

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

在第 5 步中,我们下载了 Nextcloud 源代码并将其配置为在 Nginx Web 服务器下运行。但是我们仍然需要为 Nextcloud 配置一个虚拟主机。在Nginx的conf.d目录下新建一个虚拟主机配置文件nextcloud.conf。

cd /etc/nginx/conf.d/
vim nextcloud.conf

粘贴下面的 Nextcloud 虚拟主机配置。

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'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'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't log access to other assets
        access_log off;
    }
}

保存文件并退出 vim。

现在测试Nginx配置,确保没有错误,s- 然后重启服务。

nginx -t
systemctl restart nginx

第 7 步 - 为 Nextcloud 配置 SELinux 和 FirewallD

在本教程中,我们将 SELinux 保持在强制模式,因此我们需要一个新的 SELinux 管理工具包来为 Nextcloud 配置 SELinux。

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

yum -y install policycoreutils-python-utils

然后以 root 用户身份执行以下命令,以允许 Nextcloud 在 SELinux 下运行。如果您使用不同的目录,请记住更改 Nextcloud 目录。

semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/data(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/apps(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/assets(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.htaccess'
semanage fcontext -a -t httpd_sys_rw_content_t '/usr/share/nginx/html/nextcloud/.user.ini'

restorecon -Rv '/usr/share/nginx/html/nextcloud/'

接下来,我们将启用 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

所有服务器配置已完成。

第 8 步 - Nextcloud 安装向导

打开您的网络浏览器并输入您的 Nextcloud 域名,我的是:cloud.nextcloud.co。您将被重定向到安全的 https 连接。

输入您想要的管理员用户名和密码,然后输入您的数据库凭据。点击完成设置。

Nextcloud 管理仪表板(文件管理器)出现。

Nextcloud 用户设置。

管理设置。

Nextcloud 已经在 CentOS 7 服务器上安装了 Nginx、PHP7-FPM 和 MariaDB。

参考

  • https://docs.nextcloud.com/

©2015-2025 艾丽卡 support@alaica.com