Nextcloud on Alibaba Cloud の構築メモ

# はじめに

個人用のメモとして半年ほど残していましたが、下書きスペースの不足により公開。
Alibaba Cloud の CentOS へ Nextcloud を構築する簡易メモです。
最後に実験として中国国内サーバーから[CEN](https://www.alibabacloud.com/ja/product/cen)を使っての接続も試しています。

## Middleware Install

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/171badf4-da07-1399-2cdc-1b1334ac4e52.png)


`sudo yum install -y epel-release yum-utils unzip curl wget bash-completion policycoreutils-python mlocate bzip2`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/2cd3fdd8-efc1-a4f4-f4a9-6a2693cc4303.png)

`sudo yum update -y`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/ee8a309b-34ed-dc44-ebd4-54734b0a7804.png)

### Apache

`sudo yum install -y httpd`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/650a98b8-a867-ad3b-482e-e776666bf1df.png)

`sudo systemctl enable httpd.service`
`sudo systemctl start httpd.service`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/66dcc5d7-3378-a5ed-e1ad-dca1a68fc6df.png)

### PHP

`sudo yum install -y centos-release-scl`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/cc3d4045-de5c-cf82-54c8-dbd1164985e1.png)


`sudo yum install -y rh-php72 rh-php72-php rh-php72-php-gd rh-php72-php-mbstring rh-php72-php-intl rh-php72-php-pecl-apcu rh-php72-php-mysqlnd rh-php72-php-pecl-redis rh-php72-php-opcache rh-php72-php-imagick`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/dc681c09-33f0-dfb2-da93-80e7a89eb456.png)

`sudo ln -s /opt/rh/httpd24/root/etc/httpd/conf.d/rh-php72-php.conf /etc/httpd/conf.d/`

`sudo ln -s /opt/rh/httpd24/root/etc/httpd/conf.modules.d/15-rh-php72-php.conf /etc/httpd/conf.modules.d/`

`sudo ln -s /opt/rh/httpd24/root/etc/httpd/modules/librh-php72-php7.so /etc/httpd/modules/`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/e8decd01-0fc1-265b-7576-65f1fc679c1e.png)

シンボリックリンクを確認します。

`ls -la /etc/httpd/conf.d/rh-php72-php.conf`
`ls -la /etc/httpd/conf.modules.d/15-rh-php72-php.conf`
`ls -la /etc/httpd/modules/librh-php72-php7.so`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/aa62853a-76da-612a-e26a-be4da8ff4f0b.png)

`sudo ln -s /opt/rh/rh-php72/root/bin/php /usr/bin/php`
`ls -la /usr/bin/php`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/79220377-922a-1a47-f1c6-669a7839340b.png)

### Database

`curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/5e87c58c-ecc0-661f-d2c1-fd5791ab337c.png)

`cat /etc/yum.repos.d/mariadb.repo`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/afff0600-12ba-f00d-05a0-7762e5ab5623.png)

`sudo yum install MariaDB-server MariaDB-client`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/940189f2-4645-6099-a075-59c3b6b4350d.png)

`sudo systemctl enable mariadb.service`
`sudo systemctl start mariadb.service`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/6b657afd-2dbc-d038-a33f-4bb4927d34fa.png)

:warning: もしかしたら以下が必要・・・？
`sudo mkdir -p /etc/mysql/mariadb.conf.d/`
`sudo mkdir -p /etc/mysql/conf.d/`

### Redis

`sudo yum install -y redis`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/59d7c7e8-1451-4046-48b7-f27f9181c1f0.png)

`sudo systemctl enable redis.service`
`sudo systemctl start redis.service`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/660010cb-5f6d-d190-e457-876a8cfa8d6e.png)

## Database Configuration

### MariaDB

[こちら](https://docs.nextcloud.com/server/16/admin_manual/configuration_database/linux_database_configuration.html#database-configuration)参照

`sudo cp -p /etc/my.cnf /etc/my.cnf.bak`

Original File

``` 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
```

`sudo vi /etc/my.cnf`

Edited File

```
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
transaction_isolation = READ-COMMITTED
binlog_format = ROW
innodb_large_prefix=on
innodb_file_format=barracuda
innodb_file_per_table=1

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

[server]
skip-name-resolve
innodb_buffer_pool_size = 128M
innodb_buffer_pool_instances = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 32M
innodb_max_dirty_pages_pct = 90
query_cache_type = 1
query_cache_limit = 2M
query_cache_min_res_unit = 2k
query_cache_size = 64M
tmp_table_size= 64M
max_heap_table_size= 64M
slow-query-log = 1
slow-query-log-file = /var/log/mariadb/slow.log
long_query_time = 1

[client-server]
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

[client]
default-character-set = utf8mb4
```

`sudo systemctl restart mariadb.service`

### PHP configuration

`sudo cp -p /etc/opt/rh/rh-php72/php.ini /etc/opt/rh/rh-php72/php.ini.bak`

`sudo vi /etc/opt/rh/rh-php72/php.ini`

```
# configuration for PHP MySQL module
extension=pdo_mysql.so

[mysql]
mysql.allow_local_infile=On
mysql.allow_persistent=On
mysql.cache_size=2000
mysql.max_persistent=-1
mysql.max_links=-1
mysql.default_port=
mysql.default_socket=/var/lib/mysql/mysql.sock  # Debian squeeze: /var/run/mysqld/mysqld.sock
mysql.default_host=
mysql.default_user=
mysql.default_password=
mysql.connect_timeout=60
mysql.trace_mode=Off
```


### 

パスワード設定　Zaq12wsx
`mysql -uroot -p`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/8d2db52a-00f9-701d-c020-47a41f90e854.png)

`CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'password';`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/f8161e72-4bf4-fe81-1c46-fc2635fc7f8b.png)

`CREATE DATABASE IF NOT EXISTS nextcloud;`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/ef13bdfb-35a1-b54f-d3db-63d7aa5cc08d.png)

`GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'password';`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/17039318-346c-a264-2310-dfda3668d45a.png)

`FLUSH privileges;`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/683575c9-1a88-1b33-69c3-490596e44705.png)

`quit`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/c15c9663-8206-c2ef-a85d-459fa8eab147.png)

## Installing Nextcloud

[ココ](https://nextcloud.com/install/#instructions-server)からダウンロードする。

`wget https://download.nextcloud.com/server/releases/nextcloud-16.0.1.zip`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/1f77eaca-8ee7-6862-811c-9de34fd59d49.png)

`unzip nextcloud-16.0.1.zip`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/1ca13b5a-102a-3137-4c24-5402d90141b4.png)

`sudo cp -R nextcloud/ /var/www/html/`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/30b110a7-dcf7-3a90-2271-5a9199fbf491.png)

`sudo mkdir /var/www/html/nextcloud/data`
`sudo chown -R apache:apache /var/www/html/nextcloud`

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/6be525bc-b9b3-6a49-0b8a-77f0b97418d6.png)

`systemctl restart httpd.service`

## Nextcloud Access

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/fd38403c-de19-3fe7-12a0-504d47ae4cde.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/54394d82-9af8-e660-784e-84f20a070b66.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/dd19ac79-7041-ed59-c728-fd8865893a80.png)

[コチラ](https://nextcloud.com/install/#install-clients)からダウンロード可能です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/292b567a-24e1-0060-bc56-8e9749c59b8b.png)

初期状態だとこんな感じです。  

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/e4fd759f-d820-d822-d8e0-67d6a2d3d944.png)

実際にサーバーのdataディレクトリを見に行くと同じ様にファイルが置かれています。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/d9d38147-7e83-7b65-ac0e-f1166c4a17f1.png)

新しいファイルをNextcloudの画面からアップロードするとサーバー側のディレクトリが増えてファイルもちゃんと増えていました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/2861f9c1-8b16-7a67-9692-bf5d4dfc213d.png)

## 中国からCEN経由でのVPC間アクセス

こんな感じの構成で中国のサーバーからのアクセス実験をしてみました。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/01f310ae-defd-e874-4ecb-f275bd26561e.png)


ちなみにこのままだと`trusted_domains`にPrivateのIPが設定されていないので`信頼できないドメイン`と判断されます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/325fe3a0-1e18-d8ec-c4a4-3b038956aebe.png)

画面の指示の通り`config.php`を編集します。
最初にバックアップを作成します。 
`sudo cp -p /var/www/html/nextcloud/config/config.php /var/www/html/nextcloud/config/config.php.bak`

config.phpを編集します。 
`sudo vi /var/www/html/nextcloud/config/config.php`

編集前のconfig.php

```
<?php
$CONFIG = array (
  'instanceid' => '一意の識別子（さわるな危険）',
  'passwordsalt' => '自動生成（さわるな危険）',
  'secret' => '自動生成（さわるな危険）',
  'trusted_domains' => 
  array (
    0 => '47.xxx.xxx.xxx',
  ),
  'datadirectory' => '/var/www/html/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '16.0.1.1',
  'overwrite.cli.url' => 'http://47.xxx.xxx.xxx/nextcloud',
  'dbname' => 'データベース名',
  'dbhost' => 'localhost:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'データーベースユーザー',
  'dbpassword' => 'データーベースユーザーパスワード',
  'installed' => true,
);
```

編集箇所

```
  'trusted_domains' => 
  array (
    0 => '47.xxx.xxx.xxx',
    1 => '10.10.1.36',
  ),
``` 

ファイル保存後に再度アクセスすればログイン可能な状態になります。
:information_source: サービスの再起動などは不要です。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/1f5cb17a-08a4-8a1a-8c96-809d7a81bd37.png)

日本からアップロードしたファイルを中国（上海）のマシンから取得してみます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/652a1c47-e3b6-fdc3-64fe-a3fc8e7d653d.png)

問題なくファイルが取得できました😁
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/11547/940d6451-6baf-1b47-67a2-c977e1226cb2.png)

# おわりに

ちょっとした Nextcloud の Alibaba Cloud での構築とアクセスの実験でした。
CENに関しては[コチラ](https://www.sbcloud.co.jp/entry/2019/08/15/cen-first-part)も参考にどうぞ