通过使用EC2和RDS构建Nextcloud环境,体验在AWS上进行环境搭建的过程
首先
AWS的解决方案架构师的更新期限逐渐临近,平时主要负责构建和调查与AWS几乎没有关系的Nextcloud环境,对于跟进AWS的服务几乎没什么时间,我开始感到有点棘手,觉得必须做些准备。
因此,我想是否可以将平时经常构建的Nextcloud环境与AWS合作,做些什么,于是决定将这篇标题一样的文章分几次写出来。
首先,作为论据,Nextcloud最大的好处是可以在自己的服务器设备上管理自己的数据和文件,而不依赖于Dropbox等云服务,所以在AWS上构建是否有意义是一个微妙的问题,但由于涉及到各种服务,我觉得这是一个相当好的学习素材。
随着时间的推移,AWS 的相关服务将会不断增加。希望能够涵盖自己曾经使用过或从未使用过的服务,并在写文章的过程中进行知识巩固、学习新内容,并且享受其中的乐趣。
请点击以下链接查看「通过搭建Nextcloud环境来体验在AWS上进行环境搭建」系列文章。
-
- 【第 1 回】EC2 と RDS を利用した Nextcloud 環境の構築
【第 2 回】ElastiCache サービスの導入
【第 3 回】EFS ファイルサーバーへの移行
【第 4 回】ALB を利用したサーバー負荷分散、可用性向上に向けた取り組み
【第 5 回】分散した EC2 インスタンスのログの集約
【第 6 回】Cron の外部実行とメール送信の追加
【第 7 回】AutoScaling の導入
在AWS上建立的Nextcloud环境。

使用亚马逊云服务 (AWS)
使用操作系统、中间件和应用程序。
-
- CentOS 7
-
- Nextcloud 17.0.2
-
- Nginx 1.16
-
- PHP 7.3
- Redis 5
建築程序
请注意,本次内容包括了Nextcloud及相关中间件的安装和配置,因此可能会稍微冗长,请谅解。
启动EC2实例
这次我们将使用CentOS 7作为服务器操作系统。

启动 RDS
这次我们将使用MariaDB 10.3作为数据库。在Nextcloud中,您也可以使用MySQL(5.5以上)、PostgreSQL(9.4以上)和Oracle(11g)作为数据库。

構築 Nextcloud 的環境
Nextcloud 環境は、SSH コンソールで構築します。
SSH 接続は、EC2 構築時に割り当てられた IP アドレスに対して行います。
認証は公開鍵方式となります。EC2 構築時に指定した(もしくは作成した)キーペアの秘密鍵ファイルを利用します。
操作系统的基本设置
-
- 安装所需的包
sudo yum install epel-release yum-utils unzip curl wget bash-completion -y
sudo yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm -y
将系统更新到最新状态
sudo yum update -y
设置日期和时间以及日本本地化和日语键盘
sudo timedatectl set-timezone Asia/Tokyo
sudo localectl set-locale LANG=ja_JP.utf8
sudo localectl set-keymap jp106
禁用 SELINUX
sudo vi /etc/selinux/config
※将 “SELINUX=enforcing” 修改为 “SELINUX=disabled”。
重新启动服务器
sudo reboot
再次通过 SSH 连接并确认设置状态
getenforce
※确保显示为 “Disabled”。
date
※确保当前时间以及显示为日本时间和日语。
安装和配置Nginx。
安装最新版本的Nginx。
リポジトリファイルの作成
sudo vi /etc/yum.repos.d/nginx.repo
/etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
Nginx のインストール
sudo yum install nginx -y
Nginx のインストール確認
nginx -v
※Nginx のバージョンが表示されることを確認。
Nginx の自動起動設定
sudo systemctl enable nginx
Nextcloud 向けの Nginx 設定ファイルの追加
sudo vi /etc/nginx/conf.d/nextcloud.conf
/etc/nginx/conf.d/nextcloud.conf
upstream php-handler {
#server 127.0.0.1:9000;
server unix:/var/run/php-fpm/php-fpm.sock;
}
server {
listen 80;
listen [::]:80;
server_name 【Nextcloud を動かすサーバーの FQDN】;
# enforce https
return 301 https://$server_name:443$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name 【Nextcloud を動かすサーバーの FQDN】;
# 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/letsencrypt/live/【Nextcloud を動かすサーバーの FQDN】/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/【Nextcloud を動かすサーバーの FQDN】/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;”;
#
# 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/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;
# 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 build 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;”;
#
# 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;
}
}
※【Nextcloud を動かすサーバーの FQDN】の部分はご自身の環境に合わせて適宜修正してください。
PHP的安装和配置
安装最新版的PHP 7.3系列。
-
- 安装最新版的 PHP 7.3 及相关软件包。
sudo yum install –enablerepo=remi,remi-php73 php php-fpm php-cli php-common php-curl php-gd php-mbstring php-mysqlnd php-process php-xml php-zip php-opcache php-pecl-apcu php-intl php-pecl-redis php-pecl-zip php-pear -y
确认 PHP 7.3 是否已安装。
php -v
※ 确认是否显示 PHP 7.3.xx。
修改 PHP 配置文件。
sudo cp -pi /etc/php.ini{,.orig}
sudo vi /etc/php.ini
※ 将 “memory_limit = 128M” 修改为 “memory_limit = 512M”。
修改 PHP-FPM 配置文件。
sudo cp -pi /etc/php-fpm.d/www.conf{,.orig}
sudo vi /etc/php-fpm.d/www.conf
※ 由于需要修改的地方较多,请参考下面的 diff 结果进行修改。
diff /etc/php-fpm.d/www.conf.orig /etc/php-fpm.d/www.conf
24c24
< user = apache — > user = nginx
26c26
< group = apache — > group = nginx
38c38
< listen = 127.0.0.1:9000 — > listen = /var/run/php-fpm/php-fpm.sock
48,50c48,50
< ;listen.owner = nobody
< ;listen.group = nobody
< ;listen.mode = 0660 — > listen.owner = nginx
> listen.group = nginx
> listen.mode = 0666
396,400c396,400
< ;env[HOSTNAME] = $HOSTNAME
< ;env[PATH] = /usr/local/bin:/usr/bin:/bin
< ;env[TMP] = /tmp
< ;env[TMPDIR] = /tmp
< ;env[TEMP] = /tmp — > env[HOSTNAME] = $HOSTNAME
> env[PATH] = /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin
> env[TMP] = /tmp
> env[TMPDIR] = /tmp
> env[TEMP] = /tmp
425c425,427
< ;php_admin_value[memory_limit] = 128M — > php_admin_value[memory_limit] = 512M
> php_admin_flag[session.cookie_secure] = on
> php_admin_flag[expose_php] = off
更改 PHP 会话文件目录的组信息。
sudo chown -R root:nginx /var/lib/php/session
设置 PHP-FPM 自动启动。
sudo systemctl enable php-fpm
获取 SSL 证书(Let’s Encrypt)
SSL 証明書は Let’s Encrypt を利用して取得します。
取得にあたっては、あらかじめ、アクセス FQDN に対して EC2 サーバーの IP アドレスに名前解決ができるように DNS 登録を済ませておく必要があります。
certbot のインストール。git もないのでついでにインストール。
sudo yum install git -y
cd /usr/local
sudo git clone https://github.com/certbot/certbot
Let’s Encrypt 証明書の取得。
cd certbot
./certbot-auto certonly –standalone -d 【Nextcloud を動かすサーバーの FQDN】 -m 【管理者のメールアドレス】–agree-tos -n
※実行結果として “- Congratulations! ・・・” が表示されれば作成は成功です。
cd
启动 PHP-FPM 和 Nginx
PHP-FPM を起動。
sudo systemctl start php-fpm
systemctl status php-fpm
※”Active: active (running)” が表示されることを確認します。
Nginx を起動。
sudo systemctl start nginx
systemctl status nginx
※”Active: active (running)” が表示されることを確認します。
MariaDB 客户端的安装
-
- 安装MariaDB客户端。
curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
sudo yum install -y MariaDB-client
确认能够连接到在RDS上创建的MariaDB数据库。
mysql -h 【RDS数据库端点】 -u 【主用户名称】 -p 【DB名称】
输入密码: ※ 输入主用户密码
※ 确保出现”MariaDB [【DB名称】]>”提示符。
创建用于使用Nextcloud数据库的DB用户,并授予对数据库的访问权限。
CREATE USER ‘【用户名】’@’%’ IDENTIFIED BY ‘【密码】’;
GRANT SELECT, UPDATE, DELETE, INSERT, CREATE, DROP, INDEX, ALTER, LOCK TABLES, EXECUTE, CREATE TEMPORARY TABLES, TRIGGER, CREATE VIEW, SHOW VIEW, EVENT ON nextcloud.* TO ‘【用户名】’@’%’;
退出MariaDB控制台。
exit
下面是一种可能的汉语表达方式:
安装并配置Nextcloud

安装和配置内存缓存(Redis)。
Redis をインストール。
sudo yum install –enablerepo=remi redis -y
Redis の自動起動を設定し、Redis サービスを起動。
sudo systemctl enable redis.service
sudo systemctl start redis.service
Nextcloud 設定ファイルの修正。
sudo cp -pi /var/www/html/nextcloud/config/config.php{,.orig}
sudo vi /var/www/html/nextcloud/config/config.php
※以下の diff 結果のように設定行を追加します。
sudo diff /var/www/html/nextcloud/config/config.php.orig /var/www/html/nextcloud/config/config.php
21a22,29
> ‘memcache.distributed’ => ‘\\OC\\Memcache\\Redis’,
> ‘memcache.locking’ => ‘\\OC\\Memcache\\Redis’,
> ‘memcache.local’ => ‘\\OC\\Memcache\\APCu’,
> ‘redis’ =>
> array (
> ‘host’ => ‘localhost’,
> ‘port’ => 6379,
> ),
PHP-FPM 設定ファイルの修正。
sudo cp -pi /etc/php-fpm.d/www.conf{,.orig2}
sudo vi /etc/php-fpm.d/www.conf
※以下の diff 結果のように設定を修正します。
diff /etc/php-fpm.d/www.conf.orig2 /etc/php-fpm.d/www.conf
437,438c437,438
< php_value[session.save_handler] = files
< php_value[session.save_path] = /var/lib/php/session — > php_value[session.save_handler] = redis
> php_value[session.save_path] = “tcp://localhost:6379/”
PHP-FPM を再起動。
sudo systemctl restart php-fpm
PHP OPcache 的设置。
PHP 設定ファイルの修正。
sudo cp -pi /etc/php.d/10-opcache.ini{,.orig}
sudo vi /etc/php.d/10-opcache.ini
※以下の diff 結果のように設定を修正します。
diff /etc/php.d/10-opcache.ini.orig /etc/php.d/10-opcache.ini
18c18
< opcache.max_accelerated_files=4000 — > opcache.max_accelerated_files=10000
36c36
< ;opcache.revalidate_freq=2 — > opcache.revalidate_freq=1
43c43
< ;opcache.save_comments=1 — > opcache.save_comments=1
PHP-FPM を再起動。
sudo systemctl restart php-fpm
ImageMagick 的安装
-
- 安装ImageMagick相关软件包。
sudo yum install https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-libs-7.0.9-14.x86_64.rpm -y
sudo yum install https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-7.0.9-14.x86_64.rpm -y
sudo yum install https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-devel-7.0.9-14.x86_64.rpm -y
sudo yum install –enablerepo=remi,remi-php73 php-devel -y
sudo pecl install imagick
※等待输入后,请按[ENTER]。
修改PHP配置文件。
sudo cp -pi /etc/php.ini{,.orig2}
sudo vi /etc/php.ini
※在最后一行添加以下2行。
[PECL]
extension=imagick.so
重新启动PHP-FPM。
sudo systemctl restart php-fpm
一部データベースカラムの big int 変換
big int 変換を行う Nextcloud occ コマンドを実行。
sudo -u nginx php /var/www/html/nextcloud/occ db:convert-filecache-bigint
※確認が求められたら [y] を入力します。
确认追加设置后的配置。

バックグラウンドジョブの設定
Nextcloud では、データの適正化を図るなどの目的でバックグラウンドジョブが動作しております。
デフォルトでは “AJAX” モードとなっており、各ぺーじの読み込みごとにバックグラウンドジョブが起動するようになっておりますが、これを CRON で動かすように変更します。

お疲れさまでした! これですべての設定が完了です。
最后的话
由于涉及到 Nextcloud 自身的初始设置,本次操作步骤变得较多。非常感谢那些一直阅读到底的朋友们。
下次,我們將嘗試將在 EC2 伺服器上安裝的 Redis 從 EC2 中分離出來。