LoginSignup
3
2

ConoHa VPSとSwiftでNextcloudをサクッと構築する

Posted at

この記事はConoHa Advent Calendar 2023の8日目です!

手頃なオンラインストレージが欲しかったので、ConoHa VPSと、ストレージにConoHaのオブジェクトストレージ(Swift)を使ってNextcloudを構築してみました。
NextcloudはConoHaのアプリケーションイメージにもありますが、今回は使わずに素のUbuntu Serverにマニュアルでインストールしました。

おことわり
この記事の著者はLinux初心者 / Advent Calendar初参加です。間違いなどあったらやさしく指摘してほしいです...。

サーバーの用意

私が普段使っているOSがUbuntuなので、この記事ではUbuntuを使用します。
image.png
しばらく待つと構築が完了して起動するので、IPアドレスをコピーしてsshで接続します。

$ ssh root@xxx.xxx.xxx.xxx

この時のパスワードは先程設定したrootパスワードです。

Nextcloudのインストール

まずはApache、MariaDB、phpなどの必要なソフトウェアをインストールします。

# apt update && apt upgrade -y
# apt install apache2 mariadb-server libapache2-mod-php php-gd php-mysql php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip php-apcu php-opcache libmagickcore-6.q16-6-extra certbot

次に、Nextcloud本体をダウンロードして配置します。

# wget https://download.nextcloud.com/server/releases/latest.tar.bz2
# tar xf latest.tar.bz2
# mv nextcloud /var/www/
# chown www-data:www-data /var/www/nextcloud/ -R

Apacheの設定 (http)

Nextcloudに必要なモジュールを有効化します。

# a2enmod rewrite headers env dir mime ssl

ついでにここでAPCuも有効化しておきます。

cat << EOF >> /etc/php/8.1/mods-available/apcu.ini
apc.enable_cli=1
EOF

/etc/apache2/sites-availableにサイトの設定ファイルnextcloud.confを作り、有効化します。
最初に書く設定はhttpの設定なので、Let's encryptの認証に必要なパス以外はhttpsにリダイレクトさせています。
nextcloud.example.comの部分は設定したいホスト名に変えてください。

# cat << EOF > /etc/apache2/sites-available/nextcloud.conf
<VirtualHost *:80>
  DocumentRoot /var/www/nextcloud/
  ServerName  nextcloud.example.com

  RewriteEngine On
  RewriteCond %{REQUEST_URI} !/.well\-known/acme\-challenge/.*
  RewriteCond %{HTTPS} off
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</VirtualHost>
EOF

# a2enmod nextcloud
# systemctl restart apache2

SSL化

Let's encryptの認証に必要なパスはhttpでアクセスできるようになったので、HTTP-01認証でSSL証明書を取得します。
example@example.comの部分は自分のメールアドレスに変えてください。

# certbot certonly --webroot -w /var/www/nextcloud -d nextcloud.example.com --agree-tos --email example@example.com --manual-public-ip-logging-ok

取得できたらhttpsでアクセスできるように設定ファイルに追記します。

# cat << EOF >> /etc/apache2/sites-available/nextcloud.conf
<IfModule mod_ssl.c>
  <VirtualHost *:443>
    ServerName nextcloud.example.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/nextcloud

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    SSLEngine on

    SSLCertificateFile /etc/letsencrypt/live/nextcloud.example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/nextcloud.example.com/privkey.pem

    <Directory /var/www/nextcloud/>
      Require all granted
      AllowOverride All
      Options FollowSymLinks MultiViews
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
    </Directory>

  </VirtualHost>
</IfModule>
EOF

# systemctl reload apache2

これで、httpsでNextcloudにアクセスできるようになったはずです。

MariaDBの設定

MariaDBのユーザーとデータベースを作成します。
Passwordの部分はユーザーのパスワードになるので、適切なものに変更してください。

# mysql
> create user nextcloud@localhost identified by "Password";
> create database nextcloud;
> grant all on nextcloud.* to nextcloud@localhost;
> exit;

Nextcloudの初期設定

Nextcloudの管理用のアカウントを作成します。
データベースの欄は先程実行したSQL文のものを入力します。
Screenshot from 2023-12-08 11-54-58.png
「インストール」をクリックし、しばらく待つとNextcloudのインストールが完了します。

URLの変更

今のままでは、URLがhttps://nextcloud.example.com/index.php/apps/dashboard/のようになっているので、これをhttps://nextcloud.example.com/apps/dashboard/に変えます。
そのために/var/www/nextcloud/config/config.phpを編集します。

  'overwrite.cli.url' => 'https://nextcloud.example.com',
+ 'htaccess.RewriteBase' => '/',
  'dbname' => 'nextcloud',

この設定に基づいて.htaccessの中身を変更させます。

# sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccess

オブジェクトストレージの用意

実際にファイルを保存するオブジェクトストレージを用意します。
工具のアイコンをクリックしてディスク容量を適切な容量に増やします。
image.png
image.png

APIユーザーの追加

オブジェクトストレージを利用するためには、APIユーザーを追加する必要があります。
image.png
パスワードを設定して保存します。
image.png
テナント名と、Identity Serviceのエンドポイントと、APIユーザーのユーザー名を使うのでコピーしておきます。
image.png

Nextcloudの設定

APCuの設定と、ファイルをオブジェクトストレージに保存する設定を/var/www/nextcloud/config/config.phpの下から2行目に挿入します。
gncu00000000はAPIユーザー名、gnct00000000はテナント名、https://identity.c3j1.conoha.io/v3はIdentity Serviceのエンドポイント、c3j1はエンドポイントのidentity..conohaの間の文字列を入力すると動くはずです。

/var/www/nextcloud/config/config.php
  'memcache.local' => '\OC\Memcache\APCu',
  'objectstore' => [
    'class' => 'OC\\Files\\ObjectStore\\Swift',
    'arguments' => [
      'catalogName' => 'swift',
      'autocreate' => true,
      'user' => [
        'name' => 'gncu00000000',
        'password' => 'Password',
        'domain' => [
          'name' => 'Default',
        ],
      ],
      'scope' => [
        'project' => [
          'name' => 'gnct00000000',
          'domain' => [
            'name' => 'Default',
          ],
        ],
      ],
      'serviceName' => 'swift',
      'region' => 'c3j1',
      'url' => 'https://identity.c3j1.conoha.io/v3',
      'bucket' => 'nextcloud',
    ],
  ],

動作確認

ここまでできたらNextcloudを開いているブラウザのタブをリロードしてアップロードしてみてください。
きちんとオブジェクトストレージにアップロードできているか、ローカルから確認してみます。

$ sudo apt install python3-swiftclient -y
$ cat << EOF > conoharc
export OS_AUTH_URL="https://identity.c3j1.conoha.io/v3"
export OS_TENANT_NAME="gnct00000000"
export OS_USERNAME='gncu00000000'
export OS_PASSWORD='Password'
export OS_IDENTITY_API_VERSION=3
EOF
$ . conoharc
$ swift list

最後のswift listを実行してnextcloudが表示されれば成功しています。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2