Chapter 14: Owning Your Own Cloud with NextCloud
NextCloud is slightly more complex than some of the other projects. Although it can be installed as a Docker container, you lose out on some granular control that can make the program not fun to use.
Instead, we will install it as a LAMP stack. LAMP, for Linux, Apache, MySQL, PHP, is probably the most popular development stack that powers the majority of the most visited websites in the world. It's well established, scalable, and open source. The primary "competitor" to LAMP is MERN (or MEAN or MEVN), which stands for MongoDB, Express, React/Angular/Vue, Node.js. This website, for example, uses Express and Node.js.
Enough geeking out on web frameworks. The point is that by being a LAMP stack, we need to tackle all four of the components. But congratulations, as by using Raspberry Pi OS, you're already 25% through the stack. Sort of. We're already on Linux but we still need to setup our web server (Apache), database (MySQL), and backend scripting (PHP).
Step 1: Download NextCloud and Setup up your Linux Environment (The L in LAMP)
- Change into the directory that NextCloud will be run out of. Although not required to run from this directory, it's a standard Linux convention.
cd /var/www - Download the compressed NextCloud installer.
sudo wget https://download.nextcloud.com/server/releases/latest.tar.bz2 - Unpack the file.
sudo tar -xvf latest.tar.bz2 - Create a NextCloud directory and data subdirectory.
sudo mkdir -p /var/www/nextcloud/data - Nextcloud uses the www-data system account. We have to make sure that this account can access the new directories and that the files have the right permissions.
sudo chown -R www-data:www-data /var/www/nextcloud/sudo chmod 750 /var/www/nextcloud/data
Step 2: Configure The Web Server (The A in LAMP)
- Install Apache.
sudo apt install apache2 - When done installing, go to your Raspberry Pi's IP address in the browser. No need to specify a port. You should see the Apache welcome screen.
- Apache, like Lighttpd, runs on port 80 by default. You could keep it that way. Personally, I like to keep port 80 free, if only to not interfere with other services' default behavior that expect port 80 to be free. With that in mind, we're going to change Apache's port. Change into the Apache directory.
cd /etc/apache2 - We have to change two configuration files. The first refers specifically to the ports Apache will use. Open it for editing.
sudo nano ports.conf - Change the references to port 80 to something else in our normal range. We're already using 8080 for Vaultwarden and 8090 for Pi Hole. Maybe go with 8070.
- Next, we have to adjust the default Apache Virtual Host. Open the associated file for editing:
sudo nano sites-available/000-default.conf - On the very first line, change 80 to the same number (e.g., 8070).
- Restart Apache.
sudo systemctl restart apache2 - Now go to your Pi's IP address, but specify the port number. You should see the Apache welcome screen again. If you can't, check your firewall.
- Next, we have to configure Apache to run NextCloud. We'll do that by creating a configuration file.
sudo nano /etc/apache2/sites-available/nextcloud.conf - We have two options. We can either access NextCloud similarly to how we access Pi Hole, with /nextcloud at the end of our Pi's IP address and port. Or we can configure it to run on a domain name. Much as with Vaultwarden, the latter is preferred. Copy and paste one of the configurations below into this file and then save and close it.
Step 2, Option 1: Access Through /nextcloud
Alias /nextcloud "/var/www/nextcloud/"
<Directory /var/www/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>Step 2, Option 2: Access Through a Domain Name
<VirtualHost *:YourPortNumber>
DocumentRoot /var/www/nextcloud/
ServerName cloud.MyAwesomePi.com
<Directory /var/www/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>
</VirtualHost>Step 3: Create a Database (The M in LAMP)
- We'll start by installing MariaDB, an open source fork of MySQL.
sudo apt install mariadb-server - By default, MySQL has no authentication. We need to change that as NextCloud expects and requires it.
sudo mysql_secure_installation - Follow the prompts, specifying the root password and removing the test database and anonymous users.
- Once complete, login to MySQL.
sudo mysql -u root -p - Create a database. We're going to call ours nextclouddb.
CREATE DATABASE nextclouddb; - Now create a user to access the database. We'll call ours nextclouduser. Make sure to specify a password.
CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY '<database password>'; - We grant all priviliges to the user and then flush the priviliges table to have them take effect.
GRANT ALL PRIVILEGES ON nextclouddb.* TO 'nextclouduser'@'localhost';FLUSH PRIVILEGES;
Step 4: Install PHP Modules (The P in LAMP)
- Install the long list of modules that NextCloud will need.
sudo apt install php8.2 php8.2-gd php8.2-sqlite3 php8.2-curl php8.2-zip php8.2-xmlsudo apt install php8.2-mbstring php8.2-mysql php8.2-bz2 php8.2-intl php8.2-imapsudo apt install php8.2-gmp php8.2-bcmath libapache2-mod-php8.2 - By default, PHP limits you to 2MB uploads. We're going to increase that manifold to 1GB. Open the PHP configuration file.
sudo nano /etc/php/8.2/apache2/php.ini - Look for
post_max_size = 8Mand change it to 1024M. - Do the same for
upload_max_filesize = 2M, also changing it to 1024M.
Step 5: Polish Up Apache
- With our PHP modules downloaded and our Apache configuration defined, we're ready to put on the finishing touches. We'll enable some other Apache modules that will help NextCloud run.
sudo a2enmod rewritesudo a2enmod headerssudo a2enmod envsudo a2enmod dirsudo a2enmod mime - Then we officially tell Apache to use the configuration file we created to act as its web server.
sudo a2ensite nextcloud.conf - Restart Apache
sudo systemctl restart apache2
Step 6: Firing Up NextCloud
- With all of our configurations set, we can now take our NextCloud for a test drive. Go to your NextCloud domain or Pi's IP address and port.
- You should see a sign in screen. Specify an admin user name and password. Change the database from SQLite to MySQL/MariaDB.
- For the databse information, leave the defaults as is. Enter nextclouduser, the database password, and nextclouddb for the MySQL credentials.
- Press Install. After a couple of minutes, you'll be greeted by a welcome page.
If accessing over your local network, you'll want to enable HTTPS. If using a domain name, your Caddy reverse proxy server should take care of that. Explore the NextCloud interface. There's a lot that you can see and adjust. You'll definitely want to enable two-factor authentication. Some particularly good apps are Memories, for auto-photo tagging and organizing, Collabra, for a Google Workspace replacement, and Cookbook, for recipe management.
