Introduction
Nextcloud is an open-source network disk tool that allows users to build their own cloud network disk on their private server.
Nextcloud provides both server-side and client-side software. It supports desktop-level operating systems such as Windows, Linux, and macOS, as well as mobile-level operating systems such as Android and iOS. Therefore, personal users can easily use their private cloud network disk after building it.
Docker is a tool that creates containers that can virtualize the operating system layer, isolating a separate environment for running one or more software applications. It avoids dependency and runtime environment conflicts for the software and allows for easy deployment of certain software on a server.
Prerequisites
- docker and docker-compose
- Domain name (second-level domain is sufficient)
- acme.sh (generating SSL certificate)
- nginx (reverse proxy)
Preparation
Install Docker and Docker Compose
Debian-based systems can directly install docker using
apt
:1
sudo apt-get install docker-ce
Install Docker Compose:
1
sudo apt-get install docker-compose
Docker Compose uses a file named
docker-compose.yml
to “synthesize” a docker container.docker-compose.yml
is the formula for synthesizing this docker container.
Prepare Domain Name
Create a second-level domain on the website where you purchased your domain name or on your domain management website, and configure domain name resolution. For specific instructions, please refer to “Building a Personal Website (Part One): Purchasing a Personal Domain Name and Configuring Dynamic Domain Name Resolution”.
For example, if you have a primary domain name jinli.cyou, you can create a second-level domain “cloud.jinli.cyou” for your personal cloud network disk. Then, bind the domain name and IP address on the Alibaba Cloud domain management platform.
Use acme.sh to Generate SSL Certificates
Many online tutorials use Let’s Encrypt to generate SSL certificates, but since I have already downloaded the acme.sh tool when building this website, I will use acme.sh to generate a certificate for “cloud.jinli.cyou”. For specific instructions, please refer to “Building a Personal Website (Part Two): Building a Personal Website Using the Hugo Framework”.
The process is as follows:
Find the Access key for your domain management account. You can use the previous one or obtain a new one and export the Access key as system variables
Ali_Key
andAli_Secret
.Use the following command to generate the certificate:
1
acme.sh --issue --dns dns_ali -d your.domain.com
Use the following command to copy the certificate to your Nextcloud directory. For example, I created a folder named
cert
in the Nextcloud installation directory to store the certificate files:1 2 3
acme.sh --install-cert -d your.domain.com \ --key-file /media/nextcloud/cert/key.pem \ --fullchain-file /media/nextcloud/cert
nginx reverse proxy configuration
Since I installed nginx when building this website, I used nginx as the reverse proxy tool for Nextcloud (although Apache is mainly used for installation instructions on the Nextcloud website).
Create a new configuration file named nextcloud.conf
as the reverse proxy configuration file for the cloud drive in the nginx configuration directory, and write the following content:
|
|
Here, I used port 7080 as the service port for Nextcloud and did not limit the size of uploaded files for users. The SSL certificate is stored in the location specified in the previous step.
After the configuration is completed, restart the nginx service to make the modification effective:
|
|
Install Nextcloud
Configure docker-compose
Since we have generated SSL certificates and configured nginx above, we only need to configure two containers, nextcloud and database, in docker. Here we choose to use MariaDB as the database.
Create a docker-compose.yml
file in the Nextcloud installation root directory and write the following content:
|
|
Here you need to:
- Change “MYSQL_ROOT_PASSSWORD” and “MYSQL_PASSWORD” to the database password you set, or change the names of “MYSQL_DATABASE” and “MYSQL_USER”.
- Change the domain name in “VIRTUAL_HOST” to your own cloud drive domain name.
Generate containers using docker-compose
Execute the following command in the Nextcloud installation root directory to generate containers:
|
|
Install and initialize Nextcloud
After completing the above steps, you can access your cloud drive from the browser! Enter the cloud drive URL in the browser, for example, mine is https://cloud.jinli.cyou. Then you will see the following page:
Here you need to:
- Create an administrator account and remember the username and password.
- Click “Storage & databases”, change the default database “SQLite” to “MySQL/MariaDB”, enter the user, name, and password of the database, and the last field (default “localhost”) should be the address of the database. Since we defined the database container service as
db
in thedocker-compose.yml
file above and placed it in the same network as the Nextcloud container, the address here should bedb
, so that the Nextcloud container can access the database container by this name. If yourdocker-compose.yml
file defines a different name for the database container service, you need to fill in that name here.
Finally, click “Finish Setup” to install Nextcloud, which may take several minutes. After installation, you can log in to Nextcloud with the administrator account!
Add new users and other settings
After logging into Nextcloud as an administrator, click the administrator account avatar in the upper right corner and you will see the “Users” option in the dropdown menu. Click this option to add user groups and new users.
Troubleshooting
“file/directory is locked” error
After a failed file upload, I wanted to re-upload the file but it kept failing. I tried to delete the entire folder but received an error message saying “file/directory is locked” and could not delete it.
To resolve this issue, you can try the following steps:
Log in to the Nextcloud container:
1
docker exec -it nextcloud-app bash
You should be in
/var/www/html
directory. And you can find an executable file namedocc
in this directory, which is the command-line tool for Nextcloud.Enter the maintenance mode:
1
./occ maintenance:mode --on
Install MySQL client:
1
apt-get update && apt-get install -y mariadb-client
Connect to the database:
1
mysql -h db -u ${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DATABASE}
Here,
${MYSQL_USER}
and${MYSQL_PASSWORD}
are the database user and password you set in thedocker-compose.yml
file.Execute the following SQL command to unlock the file/directory:
1
DELETE FROM oc_file_locks;
Exit the MySQL client:
1
exit;
Exit the maintenance mode:
1
./occ maintenance:mode --off
Finally go back to the Nextcloud web interface and try to delete the file or directory again. It should work now.
Note: There is another solution online, which is to enter the Nextcloud container and use the command ./occ files:scan --all
to scan all files. This command can indeed remove the lock, but it will refresh the last modified time of all files, causing all files’ modification times to change to the current time. If you want to keep the original modification times of the files, do not use this command.