Previous Page

nihilist - 07 / 11 / 2022

Nginx Nextcloud Server Setup (on arch linux)

Rent a VPS with debian 10+ (or just run it yourself, but make sure it is correctly port forwarded so that public ip points to the machine like a vps).

click here for the debian version

Once you have ssh'd into your debian server, we can start:

Setting up php7.3 and pgsql



First we get every package we need:


[ nihilism ] [ /dev/pts/12 ] [~]
→ pacman -Syy
:: Synchronizing package databases...
 core                                                                                                                                                                                           157.4 KiB   283 KiB/s 00:01 [------------------------------------------------------------------------------------------------------------------------------------------] 100%
 extra                                                                                                                                                                                         1720.9 KiB  2.45 MiB/s 00:01 [------------------------------------------------------------------------------------------------------------------------------------------] 100%
 community                                                                                                                                                                                        6.7 MiB  7.82 MiB/s 00:01 [------------------------------------------------------------------------------------------------------------------------------------------] 100%
 multilib                                                                                                                                                                                       172.6 KiB   528 KiB/s 00:00 [------------------------------------------------------------------------------------------------------------------------------------------] 100%

[ nihilism ] [ /dev/pts/12 ] [~]
→ pacman -S nextcloud
resolving dependencies...
:: There are 2 providers available for php-interpreter 8.2:
:: Repository extra
   1) php  2) php7

Enter a number (default=1):
looking for conflicting packages...

Package (9)          New Version       Net Change  Download Size

extra/gd             2.3.3-4             0.67 MiB       0.15 MiB
community/libavif    0.10.1-2            0.32 MiB       0.11 MiB
extra/libde265       1.0.8-2             1.06 MiB       0.34 MiB
extra/libheif        1.12.0-3            0.77 MiB       0.24 MiB
extra/libxpm         3.5.13-3            0.14 MiB       0.05 MiB
community/libyuv     r2322+3aebf69d-1    1.65 MiB       0.26 MiB
extra/php            8.1.8-1            24.04 MiB       4.21 MiB
extra/php-gd         8.1.8-1             0.10 MiB       0.03 MiB
community/nextcloud  24.0.2-1          345.65 MiB      98.34 MiB

Total Download Size:   103.73 MiB
Total Installed Size:  374.39 MiB

:: Proceed with installation? [Y/n] y

[ nihilism ] [ /dev/pts/12 ] [~]
→ pacman -S php-imagick php-intl

resolving dependencies...
:: There are 8 providers available for ttf-font:
:: Repository extra
   1) gnu-free-fonts  2) noto-fonts  3) ttf-bitstream-vera  4) ttf-croscore
:: Repository community
   5) ttf-dejavu  6) ttf-droid  7) ttf-ibm-plex  8) ttf-liberation

Enter a number (default=1):
looking for conflicting packages...

Package (6)            New Version  Net Change  Download Size

extra/gnu-free-fonts   20120503-8     6.65 MiB       3.23 MiB
extra/imagemagick      7.1.0.43-1    10.89 MiB       2.84 MiB
extra/liblqr           0.4.2-3        0.09 MiB       0.03 MiB
extra/libraqm          0.9.0-1        0.15 MiB       0.03 MiB
community/php-imagick  3.7.0-2        0.51 MiB       0.12 MiB
extra/php-intl         8.1.8-1        0.47 MiB       0.14 MiB

Total Download Size:    6.39 MiB
Total Installed Size:  18.75 MiB


[ nihilism ] [ /dev/pts/12 ] [~]
→ vim /etc/php/php.ini

[ nihilism ] [ /dev/pts/12 ] [~]
→ cat /etc/php/php.ini

[...]

memory_limit = 512M

[...]

extension=pdo_mysql
extension=bcmath
extension=bz2
extension=exif
extension=gd
extension=iconv
; in case you installed php-imagick (as recommended)
extension=imagick
; in case you also installed php-intl (as recommended)
extension=intl

[...]

[Date]
date.timezone = America/New_York

:wq

[ nihilism ] [ /dev/pts/12 ] [~]
→  cp /etc/php/php.ini /etc/webapps/nextcloud/php.ini

[ nihilism ] [ /dev/pts/12 ] [~]
→ export NEXTCLOUD_PHP_CONFIG=/etc/webapps/nextcloud/php.ini

[ nihilism ] [ /dev/pts/12 ] [~]
→ echo 'export NEXTCLOUD_PHP_CONFIG=/etc/webapps/nextcloud/php.ini' >> ~/.zshrc

[ nihilism ] [ /dev/pts/12 ] [~]
→ install --owner=nextcloud --group=nextcloud --mode=700 -d /var/lib/nextcloud/sessions


Then we setup the database:


[ nihilism ] [ /dev/pts/12 ] [~]
→ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 632
Server version: 10.8.3-MariaDB Arch Linux

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'xxxxxxxx';
Query OK, 0 rows affected (0.257 sec)

MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.012 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES on nextcloud.* to 'nextcloud'@'localhost';
Query OK, 0 rows affected (0.039 sec)

MariaDB [(none)]> FLUSH privileges;
Query OK, 0 rows affected (0.051 sec)

MariaDB [(none)]> exit
Bye

Now to configure php-fpm:


[ nihilism ] [ /dev/pts/12 ] [~]
→ pacman -S php-fpm

[ nihilism ] [ /dev/pts/12 ] [~]
→ cp /etc/php/php.ini /etc/php/php-fpm.ini

[ nihilism ] [ /dev/pts/12 ] [~]
→ vim /etc/php/php-fpm.ini

[...]

zend_extension=opcache

[...]

[opcache]
opcache.enable = 1
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 10000
opcache.memory_consumption = 128
opcache.save_comments = 1
opcache.revalidate_freq = 1

[...]

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ cat /etc/php/php-fpm.d/nextcloud.conf
[nextcloud]

user = nextcloud
group = nextcloud

listen = /run/php/nextcloud.sock

listen.owner = nextcloud
listen.group = http
listen.mode = 0660
pm = dynamic

pm.max_children = 5

pm.start_servers = 2

pm.min_spare_servers = 1

pm.max_spare_servers = 3

;pm.max_spawn_rate = 32

;pm.process_idle_timeout = 10s;

access.log = /var/log/php-fpm/access/$pool.log

access.format = "%{%Y-%m-%dT%H:%M:%S%z}t %R: \"%m %r%Q%q\" %s %f %{milli}d %{kilo}M %C%%"

chdir = /usr/share/webapps/$pool

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

php_value[date.timezone] = Europe/Berlin

php_value[open_basedir] = /var/lib/$pool:/tmp:/usr/share/webapps/$pool:/etc/webapps/$pool:/dev/urandom:/usr/lib/php/modules:/var/log/$pool:/proc/meminfo

php_value[session.save_path] = /var/lib/$pool/sessions
php_value[session.gc_maxlifetime] = 21600
php_value[session.gc_divisor] = 500
php_value[session.gc_probability] = 1

php_flag[expose_php] = false
php_value[post_max_size] = 1000M
php_value[upload_max_filesize] = 1000M

php_flag[output_buffering] = off
php_value[max_input_time] = 120
php_value[max_execution_time] = 60

php_value[memory_limit] = 768M

php_value[apc.ttl] = 7200
php_flag[apc.enable_cli] = 1

php_value[extension] = bcmath
php_value[extension] = bz2
php_value[extension] = exif
php_value[extension] = gd
php_value[extension] = gmp
; uncomment if php-imagick is installed and used
php_value[extension] = imagick
; uncomment if php-imap is installed and used
; php_value[extension] = imap
; recommended to enable
php_value[extension] = intl
php_value[extension] = iconv
; uncomment if php-memcached is installed and used
; php_value[extension] = memcached
; uncomment exactly one of the pdo extensions
php_value[extension] = pdo_mysql
; php_value[extension] = pdo_pgsql
; php_value[extension] = pdo_sqlite
; uncomment if php-igbinary is installed and used
; php_value[extension] = igbinary
; uncomment if php-redis is installed and used (requires php-igbinary)
; php_value[extension] = redis
; uncomment if php-xsl is installed and used
; php_value[extension] = xsl

[ nihilism ] [ /dev/pts/12 ] [~]
→ mkdir -p /etc/systemd/system/php-fpm.service.d/

[ nihilism ] [ /dev/pts/12 ] [~]
→ vim /etc/systemd/system/php-fpm.service.d/override.conf

[ nihilism ] [ /dev/pts/12 ] [~]
→ cat /etc/systemd/system/php-fpm.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/php-fpm --nodaemonize --fpm-config /etc/php/php-fpm.conf --php-ini /etc/php/php-fpm.ini
ReadWritePaths=/var/lib/nextcloud
ReadWritePaths=/etc/webapps/nextcloud/config

[ nihilism ] [ /dev/pts/12 ] [~]
→ mkdir /var/log/php-fpm/access/ -p

[ nihilism ] [ /dev/pts/12 ] [~]
→ systemctl enable --now php-fpm

[ nihilism ] [ /dev/pts/12 ] [~]
→ systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
    Drop-In: /etc/systemd/system/php-fpm.service.d
             └─override.conf
     Active: active (running) since Mon 2022-07-11 16:01:22 UTC; 6s ago
   Main PID: 3396418 (php-fpm)
     Status: "Ready to handle connections"
      Tasks: 5 (limit: 11902)
     Memory: 13.1M
        CPU: 713ms
     CGroup: /system.slice/php-fpm.service
             ├─3396418 "php-fpm: master process (/etc/php/php-fpm.conf)"
             ├─3396435 "php-fpm: pool nextcloud"
             ├─3396436 "php-fpm: pool nextcloud"
             ├─3396437 "php-fpm: pool www"
             └─3396438 "php-fpm: pool www"

Jul 11 16:01:15 nihilism systemd[1]: Starting The PHP FastCGI Process Manager...
Jul 11 16:01:22 nihilism php-fpm[3396418]: [NOTICE] fpm is running, pid 3396418
Jul 11 16:01:22 nihilism php-fpm[3396418]: [NOTICE] ready to handle connections
Jul 11 16:01:22 nihilism systemd[1]: Started The PHP FastCGI Process Manager.
Jul 11 16:01:22 nihilism php-fpm[3396418]: [NOTICE] systemd monitor interval set to 10000ms

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ chown nextcloud:www-data /run/php/nextcloud.sock

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ vim config/config.php

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ cat config/config.php
<?php
$CONFIG = array (
  'datadirectory' => '/var/lib/nextcloud/data',
  'logfile' => '/var/log/nextcloud/nextcloud.log',
  'apps_paths' =>

[...]

  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => 'cloud.nihilism.network',
  ),
  'dbtype' => 'mysql',

[...]




[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ occ
Nextcloud is not installed - only a limited number of commands are available
Nextcloud 24.0.2

Usage:
  command [options] [arguments]

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ occ maintenance:install \
    --database=mysql \
    --database-name=nextcloud \
    --database-host=localhost:/run/mysqld/mysqld.sock \
    --database-user=nextcloud \
>   --database-pass=xxxxx \
>   --admin-pass=xxxxx \
>   --admin-email=nihilist@nihilism.network \
>   --data-dir=/var/lib/nextcloud/data

Nextcloud was successfully installed

[ nihilism ] [ /dev/pts/12 ] [share/webapps/nextcloud]
→ systemctl restart nginx php-fpm mysql

Then just login with the admin credentials you specified above













Certbot Certificate and Nginx Configuration



From here we need to install our letsencrypt certificate. If you don't have a domain name yet, go get one, or just go for the free alternative DuckDNS and get one, mine currently is ech2.duckdns.org





















Post-Installation



Now from here you can make backups just in case if the server goes down or harddrive gets corrupted, etc. You could use a script like this:


#!/bin/bash
#this must run as root !
if [ "$EUID" -ne 0 ]
then
        echo 'MUST RUN AS ROOT!'
        exit
fi

cd /var/www/nextcloud/data/nothing/files/
#make sure the path to your  user is correct!

#run it at 3AM
cooldate=$(date --iso-8601)
echo $cooldate

rm backup*.zip
rm backup-$cooldate.zip
zip -r backup-$cooldate.zip /var/www/nextcloud/data/nothing/files/

#rsync backup-$cooldate.zip nothing@10.0.0.10:/home/nothing/backup/
rsync backup-$cooldate.zip nothing@mainpc:/home/nothing/backup/

rm backup*.zip

#crontab -e
#0 3 * * * /bin/bash /var/www/nextcloud/data/nothing/files/backup.sh

#chmod u+x backup.shg

#BACKUP_SERVER (here its 10.0.0.10)
#https://git.void.yt/nothing/serverside/blob/master/ssh/ssh.sh
#use this script to setup the key based ssh authentication, and then make sure your nextcloud server's root user has the private ssh key.

Here i can make rsync login via ssh to my mainpc host thanks to the private key ssh authentication specified in ~/.ssh/config:


root@home:/var/www/nextcloud/data/nothing/files# apt install rsync -y
root@home:/var/www/nextcloud/data/nothing/files# cat ~/.ssh/config
Host mainpc
        Hostname 10.0.0.10
        IdentityFile ~/.ssh/mainpc-10.pkey
        User nothing

of course you would have created the ssh keys on your remote host (in this case : 192.168.0.18) and placed the private key in the server's /root/.ssh/ folder. as comments at the end of the script imply, you can setup the cronjob to run backup.sh every day at 3 AM.g

Now in order to mount your files as a webdav share on linux you can do the following:


[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ apt-get install davfs2

 [ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo mkdir /mnt/cloud.void.yt

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo chown -R nothing:nothing /mnt/cloud.void.yt

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo mount -t davfs -o noexec https://cloud.void.yt/remote.php/webdav/ /mnt/cloud.void.yt/
Please enter the username to authenticate with server
https://cloud.void.yt/remote.php/webdav/ or hit enter for none.
  Username: nothing
Please enter the password to authenticate user nothing with server
https://cloud.void.yt/remote.php/webdav/ or hit enter for none.
  Password:
/usr/bin/mount.davfs: warning: the server does not support locks

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ cd /mnt/cloud.void.yt

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [/mnt/cloud.void.yt]
→ ls
 backup.sh   Caldera   Certs   Cours   Crypto   Documents   id_ed25519   KEEPASS.txt   lost+found   Notes   nothing.ovpn   Passwords.kdbx   Photos   Random_Files   Readme.md   SSH   Templates  'void.yt setup'

Now in order to make it persistant accross reboots, you need to make a fstab entry:


[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo vim /etc/fstab

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ cat /etc/fstab

#webdav entry
https://cloud.void.yt/remote.php/webdav/ /mnt/cloud.void.yt davfs _netdev,noauto,user,uid=nothing,gid=nothing 0 0

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo vim /etc/davfs2/secrets

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo cat /etc/davfs2/secrets | tail -n2
# personal webdav, nextcloud application password
/mnt/cloud.void.yt nothing "mypassword"

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ sudo mount /mnt/cloud.void.yt/
/usr/bin/mount.davfs: warning: the server does not support locks

And that's it ! your nextcloud files have been mounted on a linux host.


[ 10.55.55.2/32 ] [ /dev/pts/42 ] [~]
→ cd /mnt/cloud.void.yt

[ 10.55.55.2/32 ] [ /dev/pts/42 ] [/mnt/cloud.void.yt]
→ ls -l
total 46
-rw-r--r-- 1 nothing nothing   859 Apr  7  2021  backup.sh
drwxr-xr-x 3 nothing nothing     0 Feb 16 13:14  Caldera
drwxr-xr-x 9 nothing nothing     0 Jan 20 20:54  Certs
drwxr-xr-x 8 nothing nothing     0 Mar 21 20:34  Cours
drwxr-xr-x 2 nothing nothing     0 Oct 27 09:05  Crypto
drwxr-xr-x 2 nothing nothing     0 Apr  7  2021  Documents
-rw-r--r-- 1 nothing nothing   411 Apr  7  2021  id_ed25519
-rw-r--r-- 1 nothing nothing    55 Apr  7  2021  KEEPASS.txt
drwx------ 2 nothing nothing     0 Mar 27 14:07  lost+found
drwxr-xr-x 2 nothing nothing     0 Aug 23  2021  Notes
-rw-r--r-- 1 nothing nothing  2914 Apr  7  2021  nothing.ovpn
-rw-r--r-- 1 nothing nothing 40510 Mar 26 21:40  Passwords.kdbx
drwxr-xr-x 2 nothing nothing     0 Apr  7  2021  Photos
drwxr-xr-x 9 nothing nothing     0 Mar 25 09:42  Random_Files
-rw-r--r-- 1 nothing nothing     1 May 27  2021  Readme.md
drwxr-xr-x 7 nothing nothing     0 Jul  1  2021  SSH
drwxr-xr-x 2 nothing nothing     0 Apr  7  2021  Templates
drwxr-xr-x 2 nothing nothing     0 Jun  6  2021 'void.yt setup'

Special thanks to skid9000 from the anjara.eu staff for helping me update this tutorial. (23/09/2020)

Nihilism

Until there is Nothing left.

About nihilist

Donate XMR: 8AUYjhQeG3D5aodJDtqG499N5jXXM71gYKD8LgSsFB9BUV1o7muLv3DXHoydRTK4SZaaUBq4EAUqpZHLrX2VZLH71Jrd9k8


Contact: nihilist@nihilism.network (PGP)