【2024年最新版】Ubuntu + Nginx環境でNextCloudをインストール・公開する方法

MySQL

NextCloudは、自分専用のクラウドストレージを構築できる人気のオープンソースソフトウェアです。この記事では、Ubuntu 22.04 + Nginx + MySQL + PHP環境にNextCloudをインストールし、公開する手順をわかりやすく解説します。

必要な前提条件

  • Ubuntuサーバー
  • NginxMySQLPHP 8.1がインストール済み
  • Vimエディタを使用
  • ドメインが設定済み(例: example.com

NextCloudのダウンロード

公式サイトから最新版のNextCloudをダウンロードします。

cd /var/www/
sudo wget https://download.nextcloud.com/server/releases/nextcloud-27.0.5.zip
sudo unzip nextcloud-27.0.5.zip
sudo chown -R www-data:www-data nextcloud
sudo chmod -R 755 nextcloud

Nginxの設定

ドメインでアクセスできるようにNginxの設定を変更します。

sudo vim /etc/nginx/sites-available/nextcloud

以下の設定を追加してください(適宜example.comを置き換えてください)。

server {
    listen 80;
    server_name example.com;

    root /var/www/nextcloud;
    index index.php index.html;

    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    location / {
        rewrite ^ /index.php;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

設定ファイルを保存し、シンボリックリンクを作成します。

sudo ln -s /etc/nginx/sites-available/nextcloud /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

データベースの設定

MySQLでNextCloud用のデータベースを作成します。

sudo mysql -u root -p

以下を入力(nextcloudusersecure_passwordは任意で設定)。

CREATE DATABASE nextcloud;
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

NextCloudの初期設定

ブラウザで http://example.com にアクセスし、画面の指示に従って設定します。

  • データフォルダ: /var/www/nextcloud/data
  • データベース情報:
    • データベース: MySQL/MariaDB
    • ユーザー: nextclouduser
    • パスワード: secure_password
    • データベース名: nextcloud
    • サーバー: localhost

HTTPS対応(Let’s Encrypt)

Let’s EncryptでSSL証明書を取得し、セキュアに公開します。

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -m meiladdress

SSL証明書の自動更新を有効化。

sudo systemctl enable certbot.timer

nginxの設定例

server {
    listen 80;
    server_name example.com;

    # HTTPSへリダイレクト
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    # SSL証明書の設定
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;

    # SSL設定(セキュリティ強化の推奨設定)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    root /var/www/nextcloud;
    index index.php index.html;

    client_max_body_size 512M;
    fastcgi_buffers 64 4K;

    location / {
        rewrite ^ /index.php;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    # セキュリティヘッダー(推奨)
    add_header Referrer-Policy "no-referrer" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;

    # WebDAVの設定(NextCloud用)
    location /.well-known/carddav {
        return 301 $scheme://$host/remote.php/dav;
    }
    location /.well-known/caldav {
        return 301 $scheme://$host/remote.php/dav;
    }

  location ~* \.(?:css|js|woff|svg|gif)$ {
    try_files $uri /index.php$request_uri;
      expires 6M;
      access_log off;
  }

}

まとめ

これで、NextCloudのインストールと公開が完了です。あとは必要に応じてプラグインを追加したり、ユーザーごとの設定を行いましょう!

今回使用したVPS

コメント

タイトルとURLをコピーしました