content/blog/2022-03-23-nextcloud-on-ubuntu.org
170 lines · 4886 bytes
1#+date: <2022-03-23 Wed 00:00:00>
2#+title: Installation and Configuration of Nextcloud on Ubuntu Server
3#+description: Comprehensive guide for implementing Nextcloud as a self-hosted solution on an Ubuntu server, including installation steps, configuration parameters, and basic security measures.
4#+slug: nextcloud-on-ubuntu
5#+filetags: :nextcloud:ubuntu:server:
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
12storage, communications, editing, calendar, 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
34machine.
35
36#+begin_src sh
37sudo mysql -uroot -p
38#+end_src
39
40Once you've logged in, you must create a new user so that Nextcloud can
41manage the database. You will also create a =nextcloud= database and
42assign privileges:
43
44#+begin_src sql
45CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
46CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
47GRANT ALL PRIVILEGES ON nextcloud.** TO 'username'@'localhost';
48FLUSH PRIVILEGES;
49quit;
50#+end_src
51
52* Download & Install Nextcloud
53:PROPERTIES:
54:CUSTOM_ID: download-install-nextcloud
55:END:
56To download Nextcloud, go the
57[[https://nextcloud.com/install/#instructions-server][Nextcloud
58downloads page]], click on =Archive File= and right-click the big blue
59button to copy the link.
60
61Then, go to your server and enter the following commands to download,
62unzip, and move the files to your destination directory. This example
63uses =example.com= as the destination, but you can put it wherever you
64want to server your files from.
65
66#+begin_src sh
67wget https://download.nextcloud.com/server/releases/nextcloud-23.0.3.zip
68sudo apt install unzip
69unzip nextcloud-23.0.3.zip
70sudo cp -r nextcloud /var/www/example.com
71#+end_src
72
73* Configure the Apache Web Server
74:PROPERTIES:
75:CUSTOM_ID: configure-the-apache-web-server
76:END:
77Now that the database is set up and Nextcloud is installed, you need to
78set up the Apache configuration files to tell the server how to handle
79requests for =example.com/nextcloud=.
80
81First, open the following file in the editor:
82
83#+begin_src sh
84sudo nano /etc/apache2/sites-available/nextcloud.conf
85#+end_src
86
87Once the editor is open, paste the following information in. Then, save
88and close the file.
89
90#+begin_src config
91<VirtualHost *:80>
92 DocumentRoot /var/www/example.com
93 ServerName example.com
94 ServerAlias www.example.com
95 ErrorLog ${APACHE_LOG_DIR}/error.log
96 CustomLog ${APACHE_LOG_DIR}/access.log combined
97
98 <Directory /var/www/example.com/nextcloud/>
99 Require all granted
100 AllowOverride All
101 Options FollowSymLinks MultiViews
102 Satisfy Any
103
104 <IfModule mod_dav.c>
105 Dav off
106 </IfModule>
107 </Directory>
108</VirtualHost>
109#+end_src
110
111Once the file is saved, enable it with Apache:
112
113#+begin_src sh
114sudo a2ensite nextcloud.conf
115#+end_src
116
117Next, enable the Apache mods required by Nextcloud:
118
119#+begin_src sh
120sudo a2enmod rewrite headers env dir mime
121#+end_src
122
123Finally, restart Apache. If any errors arise, you must solve those
124before continuing.
125
126#+begin_src sh
127sudo systemctl restart apache2
128#+end_src
129
130For the app to work, you must have the correct file permissions on your
131=nextcloud= directory. Set the owner to be =www-data=:
132
133#+begin_src sh
134sudo chown -R www-data:www-data /var/www/example.com/nextcloud/
135#+end_src
136
137* DNS
138:PROPERTIES:
139:CUSTOM_ID: dns
140:END:
141If you do not have a static IP address, you will need to update your DNS
142settings (at your DNS provider) whenever your dynamic IP address
143changes.
144
145For an example on how I do that with Cloudflare, see my other post:
146[[../updating-dynamic-dns-with-cloudflare-api/][Updating Dynamic DNS
147with Cloudflare API]]
148
149* Certbot
150:PROPERTIES:
151:CUSTOM_ID: certbot
152:END:
153If you want to serve Nextcloud from HTTPS rather than plain HTTP, use
154the following commands to issue Let's Encrypt SSL certificates:
155
156#+begin_src sh
157sudo apt install snapd
158sudo snap install core
159sudo snap refresh core
160sudo snap install --classic certbot
161sudo ln -s /snap/bin/certbot /usr/bin/certbot
162sudo certbot --apache
163#+end_src
164
165* Results
166:PROPERTIES:
167:CUSTOM_ID: results
168:END:
169Voilà! You're all done and should be able to access Nextcloud from your
170domain or IP address.