Self-host Nextcloud – Part 1 – Installation

I used to trust Google more when they were under the leadership of Larry Page and Sergey Brin and held up their “Don’t Do Evil” motto, and had the balls to not cave to China’s censorship and pulled their business out of mainland China and moved to Hong Kong. But nowadays Google has become more of a censorship giant itself implementing the radical left’s communist political agenda, and its ever increasing intrusion to our privacy has been too concerning. Luckily, there are plenty open source alternatives.

Installation:

  • Environment: Windows Server 2019 + Hyper-V
  • Linux: Ubuntu 20.04 LTS
  • LAMP Stack: PHP8.0, MariaDB, Apache

My main reference sources are from this article and this article, however, some commands are slightly modified to fit my needs.

Server configuration:

# Become root
sudo -i

# Install Apache
apt-get update -y
apt-get install -y apache2 apache2-utils
systemctl status apache2
# After installation, visit http://localhost/ to verify that Apache is running.

# Install MariaDB
apt-get install -y mariadb-server mariadb-client
systemctl status mariadb
mysql_secure_installation

# Install PHP8.0 (the default PHP version might be PHP7.4, I completely skipped what was in the original article and went with PHP8.0)
apt-get install software-properties-common -y
add-apt-repository ppa:ondrej/php
apt-get update -y
apt-get install php8.0 libapache2-mod-php8.0 -y
systemctl restart apache2

apt-get update -y
apt-get install php8.0-fpm libapache2-mod-fcgid -y
a2enmod proxy_fcgi setenvif
a2enconf php8.0-fpm
systemctl restart apache2

# Install other modules for Nextcloud
apt-get install -y php8.0-gd php8.0-mysql php8.0-curl php8.0-mbstring php8.0-intl
apt-get install -y php8.0-gmp php8.0-bcmath php-imagick php8.0-xml php8.0-zip

# Create MySQL database
sudo /etc/init.d/mysql start
mysql -u root -p

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

GRANT ALL PRIVILEGES ON nextcloud.* TO 'username'@'localhost';
FLUSH PRIVILEGES;

quit;

Install Nextcloud:

Download Nextcloud server package from here.

# My Downloads folder path is /home/*username*/Downloads, and the current version Nextcloud build I use is nextcloud-22.1.1.zip .

cd /home/*username*/Downloads
unzip nextcloud-22.1.1.zip

# Copy to Apache document root
cp -r nextcloud /var/www/html

Configure Apache web server. Reference article.

# Use your favorite text editor in Linux to create the nextcloud.conf file under /etc/apache2/sites-available/nextcloud.conf .

# I used directory based installation so the following was added to nextcloud.conf .

Alias /nextcloud "/var/www/html/nextcloud/"

<Directory /var/www/html/nextcloud/>
  Require all granted
  AllowOverride All
  Options FollowSymLinks MultiViews

  <IfModule mod_dav.c>
    Dav off
  </IfModule>
</Directory>


# Enable modules and sites
a2ensite nextcloud.conf
a2enmod rewrite
a2enmod headers
a2enmod env
a2enmod dir
a2enmod mime
a2enmod setenvif
systemctl reload apache2


# Enable SSL
a2enmod ssl
a2ensite default-ssl
systemctl reload apache2

# Change directory permission
chown -R www-data:www-data /var/www/html/nextcloud/

Now, Nextcloud should be working, the next few steps are very straightforward through Nextcloud’s setup page.

Post-setup configs and troubleshooting:

  • Add Trusted Domain in Nextcloud’s config files. Ref.
  • Increase PHP memory_limit.
  • View phpinfo.
# 1. In your nextcloud directory, /var/www/html/nextcloud/config , find the config.php file and edit the line with 'trusted_domain'. Assuming you'll access your nextcloud using 'localhost', '192.168.0.2', and 'my_nextcloud' , write the trusted_domain like below.

  'trusted_domains' =>
  array (
    0 => 'localhost',
    1 => '192.168.0.29',
    2 => 'my_nextcloud',
  ),

# 2. Change line memory_limit to a desired value in both /etc/php/8.0/fpm/php.ini and /etc/php/8.0/apache2/php.ini , e.g. memory_limit=512M, and then restart the services.

service php8.0-fpm restart
service apache2 restart

# 3. Create a info.php file in document root to verify php info.

cd /var/www/html/
touch info.php

# Add the following to info.php using your favorite text editor.

<?php phpinfo(); ?>

# Visit info.php in a browser to view phpinfo.

Useful MariaDB/MySQL commands:

# Find out usernames, databases and ports
mysql -u root -p
SELECT User FROM mysql.user;
SHOW GLOBAL VARIABLES LIKE 'PORT';
SHOW DATABASES;

Disable (to not enforce) 2FA in Nextcloud:

# In file /var/www/html/nextcloud/config/config.php, change the following.

‘twofactor_enforced’ => ‘false’