Mustafa Can Yücel
blog-post-1

Setting Up Your Debian Server 6: NextCloud

Nextcloud is a suite of client-server software for creating and using file hosting services. Nextcloud provides functionality similar to Dropbox, Office 365, or Google Drive when used with integrated office suites Collabora Online or OnlyOffice. It can be hosted in the cloud or on-premises. It is scalable from home office software based on the low-cost Raspberry Pi all the way through to full-sized data centers that support millions of users. Translations in 60 languages exist for web interfaces and client applications.

Nextcloud requires a LAMP stack to run (Linux, Apache, MySQL, PHP). It has a snap package that includes all the dependencies, but it will mess up with the existing Apache configurations. It is also much more fun to install it manually, so let's get started. Nextcloud requires PHP version 8.0 or higher and my server was running PHP 7.3. To check the current version, use the following command:

php -v
So, let's upgrade PHP to version 8.+:
sudo apt install apt-transport-https lsb-release ca-certificates wget -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg 
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update
Then let's install the dependencies:
sudo apt update && sudo apt upgrade
sudo apt install apache2 mariadb-server libapache2-mod-php php-gd php-mysql \
php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-imagick php-zip
The steps above will add the PPA as a trusted source and update the package list that contains all PHP packages and their dependencies. All PHP packages follow the same naming convention: php{version}-{package}. For example, php8.2-mysql is the package name for the MySQL extension for PHP 8.2. So, let's install the latest PHP now:
sudo apt install php8.2-common php8.2-cli -y
After the installation is complete, verify it by checking the PHP version as above.

Then we need to set up the MySQL database for the NextCloud user. First, we need to log in to the MySQL server:

sudo mysql
Then create a new database and user for NextCloud (change the password):
CREATE DATABASE nextcloud;
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';
FLUSH PRIVILEGES;
Then exit the MySQL server:
quit;

Now we download NextCloud files, extract them and move them to the Apache root directory:

wget https://download.nextcloud.com/server/releases/latest.zip
unzip latest.zip
sudo cp -r nextcloud /var/www

Note From the Future

I strongly advise you to use Caddy instead of Apache. It is much easier to set up and use. For setting up Caddy, you can skip to Switching to Caddy Server post. If you still want to use Apache, you can continue reading this post.

Then we need to change the ownership of the NextCloud files to the Apache user:

sudo chown -R www-data:www-data /var/www/nextcloud
Then we need to create a new Apache configuration file for NextCloud. We will serve Nextcloud in a subdomain, such as nextcloud.mysite.com:
sudo vi /etc/apache2/sites-available/nextcloud.mysite.com.conf
Then we need to add the following lines to the file:
<VirtualHost *:80>
    DocumentRoot /var/www/nextcloud/
    ServerName  your.server.com
  
    <Directory /var/www/nextcloud/>
      Satisfy Any
      Require all granted
      AllowOverride All
      Options FollowSymLinks MultiViews
  
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
    </Directory>
  </VirtualHost>
Then we need to enable the new configuration file:
sudo a2ensite nextcloud.mysite.com.conf
Then we need to enable the Apache rewrite module, as well as some other modules (some of which should already be enabled anyway):
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime
If you’re running mod_fcgi instead of the standard mod_php also enable:
sudo a2enmod setenvif
Then we need to restart Apache:
sudo systemctl restart apache2

Now we need to add a DNS record for the subdomain; go to your DNS provider and add an A record for the subdomain. Note that it may take some time for the DNS record to propagate. Now we can access the NextCloud installation page by going to nextcloud.mysite.com in a browser. The first time we visit the page, it will ask us for a username, a password, a data directory, a MySQL username, and a password. Enter a username, a strong password, a data directory, and the MySQL username and password that we created earlier. Then click on the Finish Setup button. The system should be working now, but it does not run on a secure connection. To enable HTTPS, we need to install a certificate. Luckily, Let's Encrypt rushes to our aid once more.

We should have a running certbot installation by now. If not, install it as described in the previous part. Then you might remember that we need port 80, so we disable the Apache server for a moment:

sudo systemctl stop apache2
Then we run certbot:
sudo certbot certonly --standalone
Follow the directions on the terminal and enter the subdomain that the certificate will be issued (nextcloud.mydomain.com). Once it is completed, we can enable Apache again:
sudo systemctl start apache2
The standalone certonly certificates are by default stored under /etc/letsencrypt/live/domain.com/ directory. Then we need to create a new Apache configuration file for the HTTPS connection, and redirect the HTTP connection to HTTPS:
sudo vi /etc/apache2/sites-available/nextcloud.mydomain.com.conf
Then we need to add the following lines to the file:
<VirtualHost *:80>
    ServerName  nextcloud.mydomain.com
    ServerAlias nextcloud.mydomain.com
    Redirect permanent / https://nextcloud.mydomain.com/
    </VirtualHost>
    <VirtualHost *:443>
    ServerAdmin admin@mail.me
    ServerName nextcloud.mydomain.com
    ServerAlias nextcloud.mydomain.com
    DocumentRoot /var/www/nextcloud
    <Directory /var/www/nextcloud/>
        Satisfy Any
        Require all granted
        AllowOverride All
        Options FollowSymLinks MultiViews
        <IfModule mod_dav.c>
        Dav off
        </IfModule>
    </Directory>
    SSLCertificateFile /etc/letsencrypt/live/nextcloud.mydomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/nextcloud.mydomain.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
  </VirtualHost>
Then we need to enable the new configuration file:
sudo a2ensite nextcloud.mydomain.com.conf
Then we need to restart Apache:
sudo systemctl restart apache2
Now we can access the NextCloud installation page by going to https://nextcloud.mysite.com in a browser. The system should be working now, and it should be running on a secure connection. Nextcloud is a great alternative to Dropbox, Google Drive, and other cloud storage services. It also has a calendar, contacts, and other features such as document editing, online meetings (with recording)...etc.

Next, we will install Firefly III, a personal finance manager.