1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
---
title: "Nextcloud"
date: 2021-06-30
icon: 'nextcloud.svg'
tags: ['service']
short_desc: 'A free and private Google Drive-like cloud storage system.'
---
## What is Nextcloud? {#whatis}
[Nextcloud](https://nextcloud.com)
is a free and open source solution for cloud storage. However it can
also do other things, such as manage your email, notes, calender, tasks,
and can even connect to the Fediverse (think Mastodon and Pleroma).
Pretty much every service that Google has to offer has a much better
alternative as a Nextcloud app and this is a must-have for anyone
wanting to get away from Google services but still wants a traditional
cloud experience (in the likes of Google Services, anyways).
## Instructions
Before beginning this tutorial, it is required that you have
[set up NGINX](/basic/nginx/) and [obtained SSL certificates](/basic/certbot/).
We should upgrade the system and then install the MariaDB server. Run the following commands:
```sh
apt update
apt full-upgrade -y
apt install mariadb-server -y
```
In order for Nextcloud to operate properly, we need PHP version 7.4 or higher. Since Debian 10 does not include PHP 7.4, we need to add the community ran SURY repository which includes several versions of PHP for use on Debian 10.
```sh
apt -y install lsb-release apt-transport-https ca-certificates && wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg && echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list && apt update
```
Next, we need PHP 7.4 and several server side dependencies for Nextcloud. Run the following command:
```sh
apt install php7.4 php7.4-{fpm,bcmath,bz2,intl,gd,mbstring,mysql,zip,xml,curl}
```
*Optionally*, you can improve the performance of your Nextcloud server by adjusting the child processes that are used to execute PHP scripts. That way, more PHP scripts can be executed at once. Make the following adjustments to `/etc/php/7.4/fpm/pool.d/www.conf`:
```
pm = dynamic
pm.max_children = 120
pm.start_servers = 12
pm.min_spare_servers = 6
pm.max_spare_servers = 18
```
We're going to need to use the MariaDB commandline utility, which uses a Unix Socket to connect. Add the following line to `/etc/mysql/conf.d/mysql.cnf` under the `[mysql]` section:
```
socket=/var/lib/mysql/mysql.sock
```
Start the MariaDB server:
```sh
systemctl enable mariadb --now
```
Next, we need to set up our SQL database by running a Secure
Installation and creating the tables that will store data that Nextcloud
will need. Run the following command:
```sh
mysql_secure_installation
```
When it asks for root a password, say yes and input a new and secure
password. The root password here is just for the SQL database, not for
the GNU/Linux system.
Answer the rest of the questions as follows:
```sh
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y
```
Next, sign into the SQL database with the new and secure password you
chose before. Run the following command:
```sh
mysql -u root -p
```
We need to create a database for Nextcloud. Follow the instructions
below and change some of the placeholders as you wish:
```mysql
CREATE DATABASE nextcloud;
GRANT ALL ON nextcloud.* TO 'username'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
EXIT;
```
Now we need to configure PHP. Let\'s start my making sure that the PHP
user is set to `www-data` and if that is not the case, add the
`www-data` user if needed and set the correct variable in `nginx.conf`.
Make sure this line is at the beginning of `/etc/nginx/nginx.conf`.
```nginx
user www-data;
```
Check for the `www-data` user by running `id -u www-data`. If a number
is output from that command, then the www-data user exists. If not. add
the user simply by running `useradd www-data`
Next, we need to ensure that we have SSL certificates generated for your
website. If you have not already done this, refer to [this
guide](/basic/certbot).
In `/etc/nginx/sites-available/` we need to make a new configuration for
Nextcloud (example: `/etc/nginx/sites-available/nextcloud`). Create it
and open it, add the following lines, and *modify* the configuration as needed (most importantly, the lines that include **example.org**):
```nginx
upstream php-handler {
server unix:/var/run/php/php7.4-fpm.sock;
server 127.0.0.1:9000;
}
server {
listen 80;
listen [::]:80;
server_name example.org;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.org;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem ;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem ;
root /var/www;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ^~ /.well-known {
location = /.well-known/carddav { return 301 /nextcloud/remote.php/dav/; }
location = /.well-known/caldav { return 301 /nextcloud/remote.php/dav/; }
location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
location /.well-known/pki-validation { try_files $uri $uri/ =404; }
return 301 /nextcloud/index.php$request_uri;
}
location ^~ /nextcloud {
client_max_body_size 512M;
fastcgi_buffers 64 4K;
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
add_header Referrer-Policy "no-referrer" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Download-Options "noopen" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Permitted-Cross-Domain-Policies "none" always;
add_header X-Robots-Tag "none" always;
add_header X-XSS-Protection "1; mode=block" always;
fastcgi_hide_header X-Powered-By;
index index.php index.html /nextcloud/index.php$request_uri;
location = /nextcloud {
if ( $http_user_agent ~ ^DavClnt ) {
return 302 /nextcloud/remote.php/webdav/$is_args$args;
}
}
location ~ ^/nextcloud/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; }
location ~ ^/nextcloud/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; }
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $path_info $fastcgi_path_info;
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS on;
fastcgi_param modHeadersAvailable true;
fastcgi_param front_controller_active true;
fastcgi_pass php-handler;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ \.(?:css|js|svg|gif)$ {
try_files $uri /nextcloud/index.php$request_uri;
expires 6M;
access_log off;
}
location ~ \.woff2?$ {
try_files $uri /nextcloud/index.php$request_uri;
expires 7d;
access_log off;
}
location /nextcloud/remote {
return 301 /nextcloud/remote.php$request_uri;
}
location /nextcloud {
try_files $uri $uri/ /nextcloud/index.php$request_uri;
}
}
}
```
Enable the site by running this command:
```sh
ln -s /etc/nginx/sites-available/nextcloud /etc/nginx/sites-enabled/
```
Next, we need to download the latest release tarball of Nextcloud. To download the latest version of Nextcloud, run the following command:
```sh
wget https://download.nextcloud.com/server/releases/latest.tar.bz2
```
Now we need to extract the Nextcloud tarball. Run the following command:
```sh
tar -xjf latest.tar.bz2 -C /var/www
```
Let\'s correct the ownership and permissions of those files. Run the
following commands:
```sh
chown -R www-data:www-data /var/www/nextcloud
chmod -R 755 /var/www/nextcloud
```
Start and enable php-fpm and reload nginx:
```sh
systemctl enable php7.4-fpm --now
systemctl reload nginx
```
Now we need to head to Nextcloud\'s web interface. Go to your web
browser and go to your website, but go to the subdirectory \"nextcloud\"
instead. Go to `https://example.org/nextcloud`. This will launch the
configuration wizard.
- Choose an admin username and secure password.
- Leave Data folder at the default value unless it is incorrect.
- For Database user, enter the user you set for the SQL database.
- For Database password, enter the password you chose for the new user
in MariaDB.
- For Database name, enter: `nextcloud`
- Leave \"localhost\" as \"localhost\".
- Click Finish.
Congratulations, you have set up your own Nextcloud instance.
## What\'s Next? {#whatsnext}
Now you may be wondering: What do I do now? Here are some suggestions:
- Rice your Nextcloud instance by changing your themeing and
installing new themes and plugins in Settings in the Nextcloud Web
Interface.
- Install the Nextcloud Client on your personal computer and sync your
files to your instance.
- Install the Nextcloud App on your mobile device and sync your files
to your instance.
- Set up your email account on the Nextcloud Mail app on the web
interface to view and sync your email there (just like Gmail).
- Schedule events with Nextcloud Calender.
- Write notes in Markdown inside the Nextcloud Notes web and mobile
app.
- Set the Nextcloud Dashboard as your web browser\'s homepage (it is
pretty nice).
In the event that anything goes wrong with the web interface of Nextcloud, Nextcloud has a commandline utility bundled with it called `occ`. You can use it with the following command:
```sh
sudo -u www-data php /var/www/nextcloud/occ
```
You can make this an alias by putting it in your `~/.bashrc` file for ease of use with the following alias:
```sh
alias occ="sudo -u www-data php /var/www/nextcloud/occ"
```
Enjoy your cloud services in freedom.
------------------------------------------------------------------------
*Written by [Matthew \"Madness\" Evan](https://github.com/MattMadness)*
|