cmc/cleberg.net
My personal web garden & blog.
clone: git clone https://gitbay.org/cmc/cleberg.net.git
main: content/blog/2022-03-23-nextcloud-on-ubuntu.org · raw
1#+date: [2022-03-23 Wed 00:00:00]
2#+title: Self-Hosting Guide: Nextcloud
3#+description: How to install and configure Nextcloud on Ubuntu.
4#+slug: nextcloud-on-ubuntu
5#+filetags: :linux:self-hosting:
6
7* What is Nextcloud?
8:PROPERTIES:
9:CUSTOM_ID: what-is-nextcloud
10:END:
11[[https://nextcloud.com/][Nextcloud]] is a self-hosted solution for storage, communications, editing,
12calendar, contacts, and more.
13
14This tutorial assumes that you have an Ubuntu server and a domain name
15configured to point toward the server.
16
17* Install Dependencies
18:PROPERTIES:
19:CUSTOM_ID: install-dependencies
20:END:
21To start, you will need to install the packages that Nextcloud requires:
22
23#+begin_src sh
24sudo apt install apache2 mariadb-server libapache2-mod-php7.4
25sudo apt install php7.4-gd php7.4-mysql php7.4-curl php7.4-mbstring php7.4-intl
26sudo apt install php7.4-gmp php7.4-bcmath php-imagick php7.4-xml php7.4-zip
27#+end_src
28
29* Set Up MySQL
30:PROPERTIES:
31:CUSTOM_ID: set-up-mysql
32:END:
33Next, you will need to log in to MySQL as the =root= user of the machine.
34
35#+begin_src sh
36sudo mysql -uroot -p
37#+end_src
38
39Once you've logged in, you must create a new user so that Nextcloud can manage
40the database. You will also create a =nextcloud= database and assign privileges:
41
42#+begin_src sql
43CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
44CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
45GRANT ALL PRIVILEGES ON nextcloud.** TO 'username'@'localhost';
46FLUSH PRIVILEGES;
47quit;
48#+end_src
49
50* Download & Install Nextcloud
51:PROPERTIES:
52:CUSTOM_ID: download-install-nextcloud
53:END:
54To download Nextcloud, go the [[https://nextcloud.com/install/#instructions-server][Nextcloud downloads page]], click on =Archive File=
55and right-click the big blue button to copy the link.
56
57Then, go to your server and enter the following commands to download, unzip, and
58move the files to your destination directory. This example uses =example.com= as
59the destination, but you can put it wherever you want to server your files from.
60
61#+begin_src sh
62wget https://download.nextcloud.com/server/releases/nextcloud-23.0.3.zip
63sudo apt install unzip
64unzip nextcloud-23.0.3.zip
65sudo cp -r nextcloud /var/www/example.com
66#+end_src
67
68* Configure the Apache Web Server
69:PROPERTIES:
70:CUSTOM_ID: configure-the-apache-web-server
71:END:
72Now that the database is set up and Nextcloud is installed, you need to set up
73the Apache configuration files to tell the server how to handle requests for
74=example.com/nextcloud=.
75
76First, open the following file in the editor:
77
78#+begin_src sh
79sudo nano /etc/apache2/sites-available/nextcloud.conf
80#+end_src
81
82Once the editor is open, paste the following information in. Then, save and
83close the file.
84
85#+begin_src config
86<VirtualHost *:80>
87 DocumentRoot /var/www/example.com
88 ServerName example.com
89 ServerAlias www.example.com
90 ErrorLog ${APACHE_LOG_DIR}/error.log
91 CustomLog ${APACHE_LOG_DIR}/access.log combined
92
93 <Directory /var/www/example.com/nextcloud/>
94 Require all granted
95 AllowOverride All
96 Options FollowSymLinks MultiViews
97 Satisfy Any
98
99 <IfModule mod_dav.c>
100 Dav off
101 </IfModule>
102 </Directory>
103</VirtualHost>
104#+end_src
105
106Once the file is saved, enable it with Apache:
107
108#+begin_src sh
109sudo a2ensite nextcloud.conf
110#+end_src
111
112Next, enable the Apache modules required by Nextcloud:
113
114#+begin_src sh
115sudo a2enmod rewrite headers env dir mime
116#+end_src
117
118Finally, restart Apache. If any errors arise, you must solve those before
119continuing.
120
121#+begin_src sh
122sudo systemctl restart apache2
123#+end_src
124
125For the app to work, you must have the correct file permissions on your
126=nextcloud= directory. Set the owner to be =www-data=:
127
128#+begin_src sh
129sudo chown -R www-data:www-data /var/www/example.com/nextcloud/
130#+end_src
131
132* DNS
133:PROPERTIES:
134:CUSTOM_ID: dns
135:END:
136If you do not have a static internet protocol (IP) address, you will need to
137update your DNS (Domain Name System) settings (at your DNS provider) whenever
138your dynamic IP address changes.
139
140For an example on how I do that with Cloudflare, see my other post: [[https://cleberg.net/blog/cloudflare-dns-api.html][Dynamic DNS
141Record Updates via Cloudflare API]].
142
143* Certbot
144:PROPERTIES:
145:CUSTOM_ID: certbot
146:END:
147If you want to serve Nextcloud from HTTPS (Hypertext Transfer Protocol Secure)
148rather than plain HTTP (Hypertext Transfer Protocol), use the following commands
149to issue Let's Encrypt SSL (Secure Socket Layer) certificates:
150
151#+begin_src sh
152sudo apt install snapd
153sudo snap install core
154sudo snap refresh core
155sudo snap install --classic certbot
156sudo ln -s /snap/bin/certbot /usr/bin/certbot
157sudo certbot --apache
158#+end_src
159
160* Results
161:PROPERTIES:
162:CUSTOM_ID: results
163:END:
164Voilà! You're all done and should be able to access Nextcloud from your domain
165or IP address.