如何在 CentOS 8 上使用 Nginx 和 PHP 7.3 安装 Nextcloud
本教程适用于这些操作系统版本
- 中央操作系统 8
- 中央操作系统 7
在此页
- 第 1 步 - 安装 Nginx
- 第 2 步 - 安装 PHP-FPM
- 第 3 步 - 配置 PHP-FPM 7.3
- 第 4 步 - 安装和配置 MariaDB
- 第 4 步 - 生成 SSL Letsencrypt
- 第 5 步 - 下载并安装 Nextcloud
- 第 6 步 - 为 Nextcloud 设置 Nginx 虚拟主机
- 第 7 步 - 为 Nextcloud 设置 SELinux
- 第 8 步 - Nextcloud 安装向导
- 参考资料
Nextcloud 是一个免费(开源)的类似 dropbox 的软件,是 ownCloud 项目的一个分支。 Nextcloud 是用 PHP 和 JavaScript 编写的,它支持许多数据库系统,如 MySQL/MariaDB、PostgreSQL、Oracle 数据库和 SQLite。
为了让您的文件在桌面和服务器之间保持同步,Nextcloud 提供了适用于 Windows、Linux 和 Mac 桌面的应用程序以及适用于 Android 和 iOS 的移动应用程序。
在本教程中,我们将向您展示如何在 CentOS 8 服务器上安装带有 Nginx Web 服务器、PHP 7.3 和 MariaDB 数据库的 Nextcloud 17。我们将安装 Nextcloud 并使用免费的 Lets Encrypt SSL 证书对其进行保护。
先决条件
对于本指南,我们将在具有 2GB RAM、25GB 可用空间和 2 个 CPU 的 CentOS 8 服务器上安装 Nextcloud。
我们将要做什么:
- 安装 Nginx 网络服务器
- 安装 PHP-FPM 7.3
- 配置 PHP-FPM 7.3
- 安装和配置 MariaDB 数据库
- 生成 SSL Letsencrypt
- 下载 Nextcloud 17
- 为 Nextcloud 设置 Nginx 虚拟主机
- 为 Nextcloud 设置 SELinux
- Nextcloud 安装后
第 1 步 - 安装 Nginx
首先,我们将 Nginx 网络服务器安装到 CentOS 8 服务器上,并在 firewalld 上打开 HTTP 和 HTTPS 端口。
使用下面的 dnf 命令从 AppStream 存储库安装 Nginx。
sudo dnf install nginx
安装完成后,启动nginx服务并将其添加到系统引导。
systemctl start nginx
systemctl enable nginx
现在使用下面的命令检查 nginx 服务状态。
systemctl status nginx
您将在 CentOS 8 服务器上启动并运行 nginx 服务。

接下来,我们将 HTTP 和 HTTPS 服务添加到 firewalld。
使用下面的 firewall-cmd 命令将 HTTP 和 HTTPS 服务添加到 firewalld。
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
之后,重新加载 firewalld 服务。
firewall-cmd --reload
至此,您已经成功安装了 Nginx Web 服务器并在 CentOS 8 服务器上打开了 HTTP 和 HTTPS 端口。

第 2 步 - 安装 PHP-FPM
根据 Nextcloud 系统要求,建议使用 PHP 7.2 或 PHP 7.3 进行安装。
对于本指南,我们将使用可以从 REMI 存储库安装的 PHP 7.3。
在继续之前,我们将启用 PowerTools 存储库并为 CentOS 8 服务器添加 EPEL 和 REMI 存储库。
运行下面的 dnf 命令。
sudo dnf config-manager --set-enabled PowerTools
sudo dnf install epel-release
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
现在检查系统上所有可用的存储库。
dnf repolist
你会得到如下结果。

您已经为 CentOS 8 启用了 PowerTools 存储库并添加了 EPEL 和 REMI 存储库。
接下来,我们将启用 PHP 7.3 REMI 存储库。
检查 PHP 包的所有可用模块。
dnf module list php
现在启用 PHP 7.3 REMI 存储库的模块。
dnf module enable php:remi-7.3

之后,使用下面的 dnf 命令为 Nextcloud 安装 PHP 和 PHP-FPM 7.3 包。
sudo dnf install php-fpm php-cli php-devel php-gd php-mysqlnd php-pear php-xml php-mbstring php-pdo php-json php-pecl-apcu php-pecl-apcu-devel php-pecl-imagick-devel php-intl php-opcache php-zip
并且你已经在CentOS 8系统上安装了PHP和PHP-FPM 7.3。
第 3 步 - 配置 PHP-FPM 7.3
在此步骤中,我们将为 Nextcloud 部署设置 PHP-FPM。
使用以下命令编辑 php.ini 配置。
vim /etc/php.ini
取消注释并更改配置,如下所示。
memory_limit = 512M
date.timezone = Asia/Jakarta
cgi.fixpathinfo = 0
保存并关闭。
现在编辑 PHP opcache 配置 /etc/php.d/10-opcache.ini。
vim /etc/php.d/10-opcache.ini
更改配置如下。
opcache.enable=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1
保存并关闭。
接下来,编辑 PHP-FPM 配置 /etc/php-fpm.d/www.conf。
vim /etc/php-fpm.d/www.conf
将用户和组更改为 nginx。
user = nginx
group = nginx
将监听配置更改为 sock 文件,如下所示。
listen = /run/php-fpm/www.sock
取消注释下面的 PHP 环境变量。
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
取消注释最后一行的 opcache 配置。
php_value[opcache.file_cache] = /var/lib/php/opcache
保存并关闭。
现在为 PHP 会话和 opcache 创建一个新目录,然后将这些目录的所有者更改为 nginx 用户和组。
mkdir -p /var/lib/php/{session,opcache}
chown -R nginx:nginx /var/lib/php/{session,opcache}
您已经完成了 Nextcloud 安装的 PHP-FPM 配置。
启动 PHP-FPM 服务并将其添加到系统引导。
systemctl enable php-fpm
systemctl start php-fpm

现在检查 PHP-FPM sock 文件和服务状态。
netstat -pl | grep php
systemctl status php-fpm
你会得到如下结果。

这样一来,PHP-FPM 就运行起来了,在sock 文件/run/php-fpm/www.sock 下。
第 4 步 - 安装和配置 MariaDB
在这一步中,我们将安装 MariaDB 数据库服务器,设置 root 密码身份验证,并为 Nextcloud 创建一个新的数据库和用户。
使用下面的 dnf 命令安装 MariaDB 数据库。
sudo dnf install mariadb mariadb-server
安装完成后,启动MariaDB服务并将其添加到系统引导。
systemctl start mariadb
systemctl enable mariadb
MariaDB 服务已启动并正在运行。

接下来,我们将使用下面的 mysql_secure_installation 命令设置 root 密码身份验证。
mysql_secure_installation
输入您的 root 密码并为其余配置输入 Y。
Set a root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
并且 MariaDB root 密码已经配置。
现在使用下面的 mysql 命令登录到 MySQL shell。
mysql -u root -p
TYPE YOUR ROOT PASSWORD
现在创建一个新数据库 nextcloud_db 并使用下面的查询创建一个新用户 nextclouduser,密码为 nextcloudpassdb。
create database nextcloud_db;
create user identified by 'nextcloudpassdb';
flush privileges;
您已经为 Nextcloud 安装创建了数据库和用户。

第 4 步 - 生成 SSL Letsencrypt
在此步骤中,我们将使用 certbot 生成 SSL letsencrypt。 SSL 证书将用于保护 Nextcloud 访问。
使用下面的 dnf 命令从 EPEL 存储库安装 certbot。
sudo dnf install certbot
安装完成后,使用以下命令为 Nextcloud 域名生成 SSL 证书,并确保将域名和电子邮件地址更改为您自己的。
certbot certonly --webroot --webroot-path /usr/share/nginx/html --agree-tos -m -d cloud.hakase-labs.io
完成后,所有生成的 SSL 证书都位于 /etc/letsencrypt/live/cloud.hakase-labs.io 目录中。
使用以下命令检查它。
ls -lah /etc/letsencrypt/live/cloud.hakase-labs.io/
您已经使用 certbot 工具生成了 SSL letsencrypt。
第 5 步 - 下载并安装 Nextcloud
在此步骤中,我们将下载最新版本的 Nextcloud 17。
在下载nextcloud源码之前,先将zip包安装到系统中。
sudo dnf install unzip
现在转到 /var/www/ 目录并使用 wget 命令下载 Nextcloud 源代码,如下所示。
cd /var/www/
wget https://download.nextcloud.com/server/releases/nextcloud-17.0.2.zip
使用以下命令提取 Nextcloud 源代码。
unzip nextcloud-17.0.2.zip
您将获得一个名为 nextcloud 的新目录。
现在为 Nextcloud 创建一个新的数据目录。数据目录将用于存储用户数据。
mkdir -p /var/www/nextcloud/data/
之后,将 nextcloud 目录的所有者更改为 nginx 用户和组。
sudo chown -R nginx:nginx /var/www/nextcloud
并且您已经将最新的 Nextcloud 17 下载到 /var/www 目录。

第 6 步 - 为 Nextcloud 设置 Nginx 虚拟主机
下载Nextcloud源码后,我们将为Nextcloud搭建Nginx虚拟主机。
进入/etc/nginx/conf.d目录,新建配置nextcloud.conf。
cd /etc/nginx/conf.d/
vim nextcloud.conf
现在将域名和 SSL 证书路径更改为您自己的,并将以下配置粘贴到其中。
upstream php-handler {
#server 127.0.0.1:9000;
server unix:/run/php-fpm/www.sock;
}
server {
listen 80;
listen [::]:80;
server_name cloud.hakase-labs.io;
# enforce https
return 301 https://$server_name:443$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name cloud.hakase-labs.io;
# Use Mozilla's guidelines for SSL/TLS settings
# https://mozilla.github.io/server-side-tls/ssl-config-generator/
# NOTE: some settings below might be redundant
ssl_certificate /etc/ssl/nginx/fullchain.pem;
ssl_certificate_key /etc/ssl/nginx/privkey.pem;
# 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;" always;
#
# WARNING: Only add the preload option once you read about
# the consequences in https://hstspreload.org/. This option
# will add the domain to a hardcoded list that is shipped
# in all major browsers and getting removed from this list
# could take several months.
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Download-Options "noopen" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "none" always;
add_header X-XSS-Protection "1; mode=block" always;
# Remove X-Powered-By, which is an information leak
fastcgi_hide_header X-Powered-By;
# Path to the root of your installation
root /var/www/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;
# The following rule is only needed for the Social app.
# Uncomment it if you're planning to use this app.
#rewrite ^/.well-known/webfinger /public.php?service=webfinger last;
location = /.well-known/carddav {
return 301 $scheme://$host:$server_port/remote.php/dav;
}
location = /.well-known/caldav {
return 301 $scheme://$host:$server_port/remote.php/dav;
}
# set max upload size
client_max_body_size 512M;
fastcgi_buffers 64 4K;
# Enable gzip but do not remove ETag headers
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
# Uncomment if your server is built with the ngx_pagespeed module
# This module is currently not supported.
#pagespeed off;
location / {
rewrite ^ /index.php;
}
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\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
fastcgi_split_path_info ^(.+?\.php)(\/.*|)$;
set $path_info $fastcgi_path_info;
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS on;
# Avoid sending the security headers twice
fastcgi_param modHeadersAvailable true;
# Enable pretty urls
fastcgi_param front_controller_active true;
fastcgi_pass php-handler;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
try_files $uri/ =404;
index index.php;
}
# Adding the cache control header for js, css and map files
# Make sure it is BELOW the PHP block
location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
try_files $uri /index.php$request_uri;
add_header Cache-Control "public, max-age=15778463";
# 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;" always;
#
# WARNING: Only add the preload option once you read about
# the consequences in https://hstspreload.org/. This option
# will add the domain to a hardcoded list that is shipped
# in all major browsers and getting removed from this list
# could take several months.
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Download-Options "noopen" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "none" always;
add_header X-XSS-Protection "1; mode=block" always;
# Optional: Don't log access to assets
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
try_files $uri /index.php$request_uri;
# Optional: Don't log access to other assets
access_log off;
}
}
保存并关闭。
之后,测试nginx配置并重启Nginx服务。并确保没有错误。
nginx -t
systemctl restart nginx
现在 Nginx 服务将在系统上打开一个新的 HTTPS 端口,使用以下命令检查它。
netstat -plntu
你会得到如下结果。

因此,您已经为 Nextcloud 添加了 Nginx 虚拟主机配置,并在其上启用了安全 HTTPS。
第 7 步 - 为 Nextcloud 设置 SELinux
对于本教程,我们将在强制模式下使用 SELinux。我们将为 Nextcloud 安装设置 SELinux。
使用下面的 dnf 命令安装 SELinux 管理工具。
sudo dnf install policycoreutils-python-utils
现在在您的服务器上以 root 身份执行以下命令。
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/data(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/config(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/apps(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/assets(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/.htaccess'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/nextcloud/.user.ini'
restorecon -Rv '/var/www/nextcloud/'
Nextcloud 的 SELinux 配置已经完成。

第 8 步 - Nextcloud 安装向导
现在打开您的网络浏览器并在地址栏中输入您的 Nextcloud 域名。
https://cloud.hakase-labs.io/
现在您将获得如下所示的 Nextcloud 安装页面。

输入您的管理员用户和密码,然后选择 MySQL/MariaDB 作为您的数据库,并输入有关您在顶部创建的数据库的详细信息。
现在单击完成安装按钮,安装将开始。
安装完成后,您将获得如下所示的 Nextcloud 仪表板。

因此,您已经在 CentOS 8 服务器上成功安装了最新的 Nextcloud 17 以及 Nginx Web 服务器、PHP-FPM 7.3 和 MariaDB 数据库。
参考
- https://docs.nextcloud.com/