- RTFM
- Uncategorized
- Nextcloud Installation on Debian 12
Nextcloud Installation on Debian 12
Nextcloud is open-source software for creating public and private file storage. It allows you to create your self-hosted services like Dropbox, Google Drive, or Mega.nz. Originally, it's created by the original owncloud developer Frank Karlitschek. In 2016, he forks the Owncloud project and creates a new project with the new name "Nextcloud"
By this time, the Nextcloud project growing rapidly and becoming more than file hosting software, it's more like a file sync and content collaboration platform. Backed with a lot of plugins, Nextcloud becomes such a powerful collaboration software. You can install plugins for project management, video conferencing, collaborative editing, note-taking, email client, etc.
In this guide, you will learn how to install Nextcloud on the latest Debian 12 Bookworm. You will be installing Nextcloud under the LAMP Stack (Linux, Apache2/httpd, MySQL/MariaDB, and PHP).
Prerequisites
- A Debian 12 server. Ensure all packages are updated to the latest version.
- A root user or a user with root privileges. This user will be used for installing new packages and editing system configurations.
Get Root and Update Repositories
First, execute the following command to get the root privileges.
- If you have sudo installed, execute the sudo command below.
Code
sudo su |
Now type the password login for your user.
- After that, update all your Debian repositories using the apt command below.
Code
apt update |
- Install wget if missing
Code
apt install wget |
Installing and Configuring PHP
For this guide, you will be deploying Nextcloud with the latest stable version of PHP 8.2.
By default, the Debian repository provides packages for PHP 8.1. But, Nextcloud requires some additional PHP packages that can be installed from a 3rd-party repository. And you will be adding a new repository to your Debian system.
1. Execute the command below to add a PHP repository for the Debian system.
- Install the Sury php Repository
Code
bash -c "$(wget -qLO - https://packages.sury.org/php/README.txt)" |
Installing and Configuring MariaDB
For this stage, you will be installing the mariadb database server, securing mariadb deployment, and creating a new database and user for Nextcloud.
- To install the mariadb database server, run the command below and the MariaDB Repository via thew install script
Code
bash -c "$(wget -qLO - https://r.mariadb.com/downloads/mariadb_repo_setup)" |
Install the needed packages
Now install all the packages for the system LAMP & Redis
- To install all the needed packages, run the command below.
Code
apt install -y mc unzip ffmpeg apache2 redis-server memcached libapache2-mod-php8.2 php-common php8.2 \ | |
php8.2-{gd,curl,xml,zip,intl,mbstring,bz2,ldap,apcu,bcmath,gmp,imagick,igbinary,mysql,redis,smbclient,cli,common,opcache,readline,soap,memcached} \ | |
imagemagick libmagickcore-6.q16-6-extra --allow-change-held-packages |
Adapt the PHP 8.2 ini Values for Netxtcloud
The following ini settings should be adapted for Nextcloud.
- Uncomment the date.timezone parameter and input the proper timezone for PHP.
Code
sed -i "s/\;date.timezone =.*/date.timezone = Europe\/Vienna/" /etc/php/8.2/apache2/php.ini |
- Increase the default value of parameters memory_limit, upload_max_filesize, post_max_size, and max_execution_time. Change the value as you need.
Code
sed -i "s/memory_limit =.*/memory_limit = 512M/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 500M/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/post_max_size =.*/post_max_size = 600M/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/max_execution_time =.*/max_execution_time = 300/" /etc/php/8.2/apache2/php.ini |
- Enable file_uploads and allow_url_fopen by changing the default value to On.
Code
sed -i "s/file_uploads =.*/file_uploads = on/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/allow_url_fopen =.*/allow_url_fopen = On/" /etc/php/8.2/apache2/php.ini |
- Disable the parameter display_errors and output_buffering by changing the default value to Off.
Code
sed -i "s/display_errors =.*/display_errors = Off/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/output_buffering =.*/output_buffering = Off/" /etc/php/8.2/apache2/php.ini |
- APCu is disabled by default on CLI which could cause issues with nextcloud’s cron jobs. so activate it
Code
sed -i '$aapc.enable_cli=1' /etc/php/8.2/mods-available/apcu.ini |
- Uncomment the zend_extension parameter and change the value to opcache. This will enable PHP OPcache, which is needed for Nextcloud.
Code
sed -i "s/\;zend_extension=opcache/zend_extension=opcache/" /etc/php/8.2/apache2/php.ini |
- Add the following lines to the [opcache] section. The OPCache configuration is recommended by Nextcloud.
Code
sed -i "s/\;opcache.enable=.*/opcache.enable = 1/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/\;opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer = 8/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/\;opcache.max_accelerated_files=.*/opcache.max_accelerated_files = 10000/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/\;opcache.memory_consumption=.*/opcache.memory_consumption = 128/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/\;opcache.save_comments=.*/opcache.save_comments = 1/" /etc/php/8.2/apache2/php.ini | |
sed -i "s/\;opcache.revalidate_freq=.*/opcache.revalidate_freq = 1/" /etc/php/8.2/apache2/php.ini |
- Lastly, enter the systemctl command below to restart the apache2 service. Every time you make changes to the PHP configuration, restart the apache2 service to apply the changes that you've made.
Code
sudo systemctl restart apache2 |
memcached configuration
We install the memcached server to increase Nextcloud performance, as memcached reduces the load on the MariaDB Nextcloud database.
- Customize the memcached configuration by customizing the configuration by running the commands below
Code
sed -i "s/-m 64/-m 128/" /etc/memcached.conf |
- Lastly, enter the systemctl command below to restart the redis service.
Code
systemctl restart memcached.service |
You can verify that the Memcached daemon is running with ps ax
Code
root@nextcloud:~# ps xa | grep memcached
23830 ? Ssl 0:00 /usr/bin/memcached -m 128 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
Installing and Configuring MariaDB
For this stage, you will be installing the mariadb database server, securing mariadb deployment, and creating a new database and user for Nextcloud.
- To install the mariadb database server, run the command below.
Code
apt install mariadb-server mariadb-client |
- Type 'y' to confirm and install mariadb packages.
- Once the installation is complete, check the mariadb service using the following command.
Code
systemctl is-enabled mariadb | |
systemctl status mariadb |
- The mariadb service is active and running, and it's enabled to start automatically at system startup.
- Next, you need to secure your mariadb deployment by setting up the root password for mariadb and remove some default configuration. To do that, you can use the command-line tool 'mysql_secure_installation', which is included on the default mariadb installation.
- Execute the "mysql_secure_installation" command below.
Code
mysql\_secure\_installation |
- At the first, you will be asked to enter the mariadb root password. Just press 'Enter' to continue.
Code
Enter current password for root (enter for none): | |
OK, successfully used password, moving on... |
- Type 'Y' to enable the 'unix_socket' authentication for the user 'root'.
Code
Switch to unix\_socket authentication \[Y/n\] Y | |
Enabled successfully! | |
Reloading privilege tables.. | |
... Success! |
- Type your strong mariadb root password and repeat, then press "Enter" to continue.
Code
Change the root password? \[Y/n\] Y | |
New password: | |
Re-enter new password: | |
Password updated successfully! | |
Reloading privilege tables.. | |
... Success! |
- Now type "Y" and press "Enter" to remove the default anonymous user from the mariadb server.
Code
By default, a MariaDB installation has an anonymous user, allowing anyone | |
to log into MariaDB without having to have a user account created for | |
them. This is intended only for testing, and to make the installation | |
go a bit smoother. You should remove them before moving into a | |
production environment. | |
| |
Remove anonymous users? \[Y/n\] Y | |
... Success! |
- After that, disable the remote login for the default user 'root'. Type "Y" and press "Enter" to continue.
Code
Normally, root should only be allowed to connect from 'localhost'. This | |
ensures that someone cannot guess at the root password from the network. | |
| |
Disallow root login remotely? \[Y/n\] Y | |
... Success! |
- Type "Y" again to remove the default database "test" and press "Enter".
Code
By default, MariaDB comes with a database named 'test' that anyone can | |
access. This is also intended only for testing, and should be removed | |
before moving into a production environment. | |
| |
Remove test database and access to it? \[Y/n\] Y | |
- Dropping test database... | |
... Success! | |
- Removing privileges on test database... | |
... Success! |
- And the last, type "Y" again to reload all tables privileges to apply a new configuration.
Code
Reloading the privilege tables will ensure that all changes made so far | |
will take effect immediately. | |
| |
Reload privilege tables now? \[Y/n\] Y | |
... Success! |
- Now the process is complete and you will see the following output.
Code
Cleaning up... | |
| |
All done! If you've completed all of the above steps, your MariaDB | |
installation should now be secure. | |
| |
Thanks for using MariaDB! |
Creating Database and User for Nextcloud
- log in to the mariadb shell using the mysql command below.
Code
mysql -u root -p |
- Now execute the following mysql query to create a new database "nextcloud".
Code
CREATE DATABASE nextcloud; |
- Execute the following query to create a new database user "nextcloud". Change the "strongpassword" with your strong password.
Code
CREATE USER nextcloud@localhost IDENTIFIED BY 'StrongPassword'; |
- Allow the user "nextcloud" to access and write the "nextcloud" using the following query.
Code
GRANT ALL PRIVILEGES ON nextcloud.\* TO nextcloud@localhost; |
- Now reload all tables privileges to apply the new database configuration.
Code
FLUSH PRIVILEGES; |
- Then you can type "quit" and press "Enter" to exit from the mariadb shell.
Generating SSL Letsencrypt
In this stage, you will be installing the certbot tool and generate the SSL certificates for the Nextcloud installation. You will be generating SSL Letsencrypts with the webroot plugin.
- Execute the following command to install the certbot tool for generating SSL Letsencrypt.
Code
apt install certbot |
Type 'y' and press 'Enter' to continue the installation.
[image:78::Install Certbot:9-install-certbot.png]- Once the installation is complete, create a new directory for letsencrypt authorization using the following commands.
Code
mkdir -p /var/lib/letsencrypt/.well-known | |
chgrp www-data /var/lib/letsencrypt | |
chmod g+s /var/lib/letsencrypt |
- Next, change the working directory to the "/etc/apache2/conf-available/" and create a new configuration "well-known.conf" using nano.
Code
cd /etc/apache2/conf-available/ | |
mcedit well-known.conf |
Copy and paste the following configuration.
Code
Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" | |||
="amc_code_odd"> | AllowOverride None | ||
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec | |||
Require method GET POST OPTIONS | |||
tr> |
Now activate the new configuration by creating a symlink of the 'well-known.conf' file to the directory 'conf-enabled' using the 'ln' command below.
Code
ln -s /etc/apache2/conf-available/well-known.conf /etc/apache2/conf-enabled/ |
- Now execute the following commands to verify the apache configuration and restart the apache service.
Code
apachectl configtest | |
systemctl restart apache2 |
If you've no error, you're ready to generate SSL Letsencrypt with the webroot plugin.
[image:81::Setup SSL Verification for Letsencrypt:10-setting-ssl-verification-letsencrypt.png]- Before generating SSL Letsencrypt, ensure your domain name is resolved to the server IP address. After that, you can generate SSL Letsencrypt with the webroot plugin by running the certbot command below. Also, change the email address and domain name to your own.
Code
sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d files.domain-name.io |
When the process is complete, your SSL certificates are available at the "/etc/letsencrypt/live/files.domain-name.io/" directory.
Apache Web server configuration
Configuring Apache requires the creation of a single configuration file. On Debian, Ubuntu, and their derivatives, this file will be /etc/apache2/sites-available/nextcloud.conf.
- Change the working directory to "/etc/apache2/sites-available/" and create new configuration "nextcloud.conf" using mcedit.
Code
mcedit /etc/apache2/sites-available/nextcloud.conf |
-
if you want http/2 & hsts support uncomment the Protocol & Header line
Code
nd exit the editor.
amc_code_even">DocumentRoot /var/www/nextcloud/
"amc_line"> ServerName your.server.com
SSLEngine On
SSLCertificateFile /etc/letsencrypt/live/files.domain-name.io/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/files.domain-name.io/privkey.pem
="amc_code_even">Require all granted
"amc_line"> AllowOverride All
Options +FollowSymLinks
Dav off
"amc_line"> SetEnv HOME /var/www/nextcloud
SetEnv HTTP_HOME /var/www/nextcloud
tr>tr>
Now enter the following systemctl command to restart the apache2 service and apply the Nextcloud virtual host configuration.
Code
systemctl restart apache2 |
Additional Apache configurations
- For Nextcloud to work correctly, we need the module mod_rewrite. Enable it by running:
Code
a2enmod rewrite | |
a2enmod ssl |
- Additional recommended modules are mod_headers, mod_env, mod_dir and mod_mime:
Code
a2enmod headers | |
a2enmod env | |
a2enmod dir | |
a2enmod mime |
Next, run the a2ensite command below to enable the virtual host configuration nextcloud.conf. Then verify the overall Apache2 configuration via the apachectl command below.
Code
a2ensite nextcloud.conf | |
apachectl configtest |
You should see the output Syntax OK if you have correct and proper Apache configurations. Now lets start with the nextcloud install
Code
AH00112: Warning: DocumentRoot [/var/www/nextcloud/] does not exist | |
Syntax OK |
- Lastly, enter the systemctl command below to restart the apache2 service. Every time you make changes to the PHP configuration, restart the apache2 service to apply the changes that you've made.
Code
systemctl restart apache2.service |
Installing Nextcloud from command line
It is now possible to install Nextcloud entirely from the command line. This is convenient for scripted operations, headless servers, and sysadmins who prefer the command line. There are three stages to installing Nextcloud via the command line:
Download and Install Nextcloud
- Change the working directory to "/var/www" and download the latest version of Nextcloud source code using the wget command as below.
Code
cd /var/www/ | |
wget https://download.nextcloud.com/server/releases/latest.zip | |
unzip latest.zip && chown -R www-data:www-data /var/www/nextcloud && rm -f latest.zip |
- Change the ownership of your nextcloud directory to your HTTP user, like this example for Debian/Ubuntu.
Code
mkdir -p /var/lib/nextcloud /var/log/nextcloud | |
chown -R www-data:www-data /var/lib/nextcloud /var/www /var/log/nextcloud | |
```bash | |
* Use the occ command to complete your installation. This takes the place of running the graphical Installation Wizard | |
| |
after the install we set some defaults and correct some values. | |
```bash | |
sudo -u www-data php /var/www/nextcloud/occ maintenance:install --database "mysql" --database-name "nextcloud" --database-user "nextcloud" --database-pass "StrongPassword" --database-host "localhost:3306" --admin-user "Admin" --admin-pass "SecurePassword" --data-dir "/var/lib/nextcloud" | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set trusted_domains 0 --value=nextcloud.disconnected-by-peer.at | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set overwritehost --value=nextcloud.disconnected-by-peer.at |
Memory caching
You can significantly improve your Nextcloud server performance with memory caching, where frequently-requested objects are stored in memory for faster retrieval. There are two types of caches to use: a PHP opcode cache, which is commonly called opcache, and data cache for your web server, commonly called “memcache”.
- Activate Memcached for local and distributed caching, as well as transactional file locking.
Code
sudo -u www-data php /var/www/nextcloud/occ config:system:set 'memcache.local' --value='\OC\Memcache\APCu' | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set 'memcache.distributed' --value='\OC\Memcache\Memcached' | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set 'memcache.locking' --value='\OC\Memcache\Memcached' | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set 'memcached_servers' --value='NextCloudMemcached' | |
sudo -u www-data sed -e 's!.NextCloudMemcached.![\n [ "localhost", "11211" ],\n ]!' -i /var/www/nextcloud/config/config.php |
Set up a cron job for Nextcloud as “www-data” user:
Code
crontab -u www-data -e |
Insert this line at the bottom
Code
*/5 * * * * php -f /var/www/nextcloud/cron.php > /dev/null 2>&1 |
Then save and close the file and reconfigure the Nextcloud job from “Ajax” to “Cron” using the Nextclouds CLI:
Code
sudo -u www-data php /var/www/nextcloud/occ background:cron |
Pretty URLs
Pretty URLs remove the index.php-part in all Nextcloud URLs, for example in sharing links like https://example.org/nextcloud/index.php/s/Sv1b7krAUqmF8QQ, making URLs shorter and thus prettier.
- mod_env and mod_rewrite must be installed on your webserver and the .htaccess must be writable by the HTTP user
Code
sudo -u www-data php /var/www/nextcloud/occ config:system:set overwrite.cli.url --value=https://nextcloud.disconnected-by-peer.at | |
sudo -u www-data php /var/www/nextcloud/occ config:system:set 'htaccess.RewriteBase' --value='/' |
- It isn’t installed in a subfolder. Finally run this occ-command to update your .htaccess file:
Code
sudo -u www-data php /var/www/nextcloud/occ maintenance:update:htaccess |
- After each update, these changes are automatically applied to the .htaccess-file.
Modify the “.user.ini”
Code
sudo -u www-data sed -i "s/output_buffering=.*/output_buffering=0/" /var/www/nextcloud/.user.ini |
- and customize the Nextcloud apps as user www-data
Code
sudo -u www-data php /var/www/nextcloud/occ app:disable survey_client | |
sudo -u www-data php /var/www/nextcloud/occ app:disable firstrunwizard | |
sudo -u www-data php /var/www/nextcloud/occ app:enable admin_audit | |
sudo -u www-data php /var/www/nextcloud/occ app:enable files_pdfviewer |
Optional Nextcloud Office:
Code
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ app:install richdocuments | |
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ app:install richdocumentscode |
Add qOwnNote API to the Nextcloud install
Code
sudo -u www-data /usr/bin/php /var/www/nextcloud/occ app:install qownnotesapi |
Conclusion
Congratulation! You have successfully installed Nextcloud on Debian 12 Bullseye. The Nextcloud server is running under the LAMP Stack with SSL enabled. Also, your Nextcloud installation is running with memory caching enabled with PHP APC and opcache extensions.