Nextcloud with Docker and NGINX on Ubuntu 22.04
Stand: 10. Februar 2024, Details können veraltet sein.
There are a ton of cloud storage providers out there including the large players: Dropbox, OneDrive, Apple Cloud and so on. Though some of these have pretty good security, the files in those storages are still accessible to the admins of said services – and in extension to governments and other third party actors.
Do I have anything to hide? No. Do I want the government or random hackers and script kiddies looking at my personal picture archives? No again. So a self-hosted solution is the way to go.
Enter Nextcloud, an ownCloud fork. I decided to run the Docker version behind an NGINX proxy server.
It’s pretty simple this time, let’s jump into it!
Step 1 – Install Docker and docker-compose
We will use sudo, apt and apt-get for this. All these tools usually come with Ubuntu, so no need to install them. Let’s grab us some nice Docker stuff.
# Make sure everything is up to date
sudo apt update
# Get stuff
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
# Get ourselves some sweet gpg keys for docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add the Docker repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# Get Docker
sudo apt update && sudo apt-get install docker-ce docker-ce-cli containerd.io
# Get docker-compose (you may need to adjust the version number, 'v2.24.5') …
sudo curl -L "https://github.com/docker/compose/releases/download/v2.24.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# … and make it writeable
sudo chmod +x /usr/local/bin/docker-composeIn addition, you might need to add the server user for the website to the Docker group (and create a docker group, if not in place already):
sudo groupadd docker
sudo usermod -aG docker usernameobviouslyuseyourwebsiteusernamehereThat’s all the installation stuff server-wise. Next, let’s set up NGINX.
Step 2 – Set up NGINX
Let’s create a configuration file for the webpage (or add a new server to an existing configuration file).
server {
server_name cloud.example.com; # Website URL
root "/var/www/example.com/cloud.example.com"; # Website Folder
location / {
proxy_pass http://localhost:3000/; # Port as set in docker-compose (we'll do this later)
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 0;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}
location = /.well-known/carddav {
return 301 $scheme://$host/remote.php/dav;
}
location = /.well-known/caldav {
return 301 $scheme://$host/remote.php/dav;
}
listen 80;
}At this point you’d typically make sure to add a certificate, e. g. utilising certbot --nginx to do so. I won’t cover that here.
Step 3 – Add docker-compose.yml and db.env
Let’s start with the docker-compose.yml file. You can copy it from here, just make sure to use a free port. 30xx should be fine. Use netstat -tunlep | grep LISTEN | awk '{print $4}' to make sure.
version: '3'
services:
db:
image: mariadb
command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
restart: always
volumes:
- ./db:/var/lib/mysql
env_file:
- db.env
redis:
image: redis
restart: always
app:
image: nextcloud:latest
build: ./app
restart: always
ports:
- 127.0.0.1:3000:80
# remember the port (3000) in the nginx file?
# Make sure this one is the same. And it's not in use, of course.
volumes:
- ./nextcloud:/var/www/html
environment:
- MYSQL_HOST=db
env_file:
- db.env
depends_on:
- db
- redis
cron:
build: ./app
restart: always
volumes:
- ./nextcloud:/var/www/html
entrypoint: /cron.sh
depends_on:
- db
- redis
volumes:
db:
nextcloud:Save and add another file, this time db.env:
MYSQL_ROOT_PASSWORD=UseSomeCrypticPasswordHere
MYSQL_PASSWORD=UseAnotherCrypticPasswordHere
MYSQL_DATABASE=nextcloud
MYSQL_USER=nextcloudStep 4 – Add the Dockerfile and the redis configuration file
Create a folder named app in your site directory, and add two files to it.
mkdir app
cd app
touch Dockerfile
touch redis.conf.phpAdd some content to those files.
FROM nextcloud:apache
COPY redis.config.php /usr/src/nextcloud/config/redis.config.php<?php
$CONFIG = array (
'memcache.local' => '\\OC\\Memcache\\Redis',
'memcache.locking' => '\\OC\\Memcache\\Redis',
'filelocking.enabled' => 'true',
'redis' => array(
'host' => 'redis',
'port' => 6379,
),
);Step 5 – Cross your fingers and …
… run docker-compose.
docker-compose upCheck the output for errors. If you don’t see any, terminate the containers with Ctrl-C (once!).
Step 6 – Fire up a detached container
Run docker-compose again, so it starts some detached containers. Head to your website, and you should be able to follow the frontend installation process.
docker-compose up -dIf everything went well, you should have a running Nextcloud instance. If not, let me know and I might add more to this tutorial.