How to run a Gentoo Nextcloud server on your local network
searchforzero
Table of Contents
This is a step-by-step tutorial for setting up your own Nextcloud server on a fresh Gentoo install. Portage, Gentoo's fantastic package manager, does a lot of the heavy lifting for us so there's barely any configuring to do by hand.
Choices
Not every OS install or server config has to be the same. Here are the choices I've made but feel free to try others. I'm using:
Install all required software
Install a MySQL server and have it start at boot.
emerge dev-db/percona-server rc-update add mysql default
Install Apache and Nextcloud, adding Apache to the default runlevel.
# You may not need all these USE flags, depending on what Nextcloud apps you use echo "dev-lang/php apache2 pdo mysql mysqli zip xmlreader xmlwriter sqlite sockets mhash intl imap ftp gd curl truetype" > /etc/portage/package.use/php echo "app-eselect/eselect-php apache2" >> /etc/portage/package.use/php echo "www-apps/nextcloud mysql -sqlite" >> /etc/portage/package.use/nextcloud emerge www-servers/apache www-apps/nextcloud rc-update add apache2 default
Create the database
Log in to the database server
mysql -u root -p
then create a new database and set up the user (replacing [PASSWORD]
with a new password for nextclouduser).
CREATE DATABASE nextclouddb; CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY '[PASSWORD]'; GRANT ALL PRIVILEGES ON nextclouddb.* TO 'nextclouduser'@'localhost'; FLUSH PRIVILEGES;
Apache Configuration
Let Apache execute PHP
Ensure APACHE2_OPTS
contains the following in /etc/conf.d/apache2
. Simply append "-D PHP"
to the variable.
APACHE2_OPTS="-D PHP"
Then restart Apache
rc-service apache2 restart
Now you can navigate to the server's IP in a web browser to create the admin account and attach the database, but first you'll need a place for your data.
(Optional) Redirect /
to /nextcloud
This is just so that users can navigate to the machine's IP without adding /nextcloud
to get to Nextcloud.
You could create another virtual host for Nextcloud, but since this Apache server is just going to serve Nextcloud, I just alter the default virtual host config.
In /etc/apache2/vhosts.d/default_vhost.include
, add
RedirectMatch ^/$ /nextcloud/
Some clients might still need to be pointed to <IP>/nextcloud
though.
Set up Nextcloud data
(Optional) RAID10 + btrfs
Skip this section if you don't want to use RAID to store your nextcloud data. You might just use one separate device or keep it on the same storage device as you OS, but that makes it harder to replace any failed drives in the future.
In my case there are 6 hard drives so with sys-fs/btrfs-progs
installed, I can run the following command. WARNING: just like creating any filesystem, this will destroy any existing data on the drives - be very careful to specify the right devices.
mkfs.btrfs -L nextclouddata -m raid10 -d raid10 /dev/sda /dev/sdb /dev/sdc /dev/sde /dev/sdf /dev/sdg
Notice I add the label nextclouddata
, this is so I can mount the RAID using the label and swap it out down the line for a different device labeled nextclouddata
with ease.
Mounting with fstab
Typically you'll need to mount your Nextcloud data storage since it is most likely not in the root partition.
While the rest of our Nextcloud installation lives in /var/www/localhost/
by default, I'd like to put the user data in /var/www/nextclouddata
. So we need to create the directory and give the apache user ownership. This will be the mountpoint for our RAID.
mkdir /var/www/nextclouddata chown apache:apache /var/www/nextclouddata
Now edit your /etc/fstab
to automatically mount it there
LABEL=nextclouddata /var/www/nextclouddata btrfs noatime,auto 0 2
Notice I've used the nextclouddata
label, you can mount any way you like if you didn't use a label to create your filesystem.
Create a Nexcloud admin & connect the database
Go to your server's IP in a web browser and follow the instructions in the web form to create an admin account. You'll need to enter the machine IP, database credentials, and data directory from before.
You should configure a static IP address for the server machine so that clients and users can navigate to the same place. Also this will prevent a recurring error where Nextcloud refuses to log you in because you haven't trusted the current IP in /var/www/localhst/htdocs/nextcloud/config/config.php
Done!
That's it! Without port forwarding your Nextcloud should only be accessible from your local network. Enjoy!