使用ConoHa VPS和Swift快速搭建Nextcloud
此文是 ConoHa Advent Calendar 2023 的第八天!
因为想要一个实惠的在线存储,所以我试着使用ConoHa VPS和ConoHa对象存储(Swift)来构建Nextcloud。虽然Nextcloud也有ConoHa的应用程序映像,但我这次选择手动在原始的Ubuntu Server上进行安装。
本文作者为Linux初学者/Advent Calendar首次参与。若有错误,请温和指正…。
准备服务器

$ 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的初始设置

网址更改
由于目前的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
オブジェクトストレージの用意

添加API用户

下一个云的设置
請將APCu的設定以及將文件保存到對象存儲的設定插入到/var/www/nextcloud/config/config.php文件的第二行以下。
gncu00000000是API用戶名,gnct00000000是租戶名,https://identity.c3j1.conoha.io/v3是身份服務的終端點,c3j1應該是在identity.和.conoha之間的終端點輸入的字符串。
'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,则表示成功。