Install ownCloud on Ubuntu 18.04

This is an ultra-short guide to installing ownCloud on a fresh installation of Ubuntu 18.04. Run the following commands in your terminal to complete the installation.

Prerequisites

Preparation

First, ensure that all of the installed packages are entirely up to date.

sudo apt update && sudo apt upgrade -y

Create the occ Helper Script

Create a helper script to simplify running occ commands.

FILE="/usr/local/bin/occ"
/bin/cat <<EOM >$FILE
#! /bin/bash

cd /var/www/owncloud
sudo -u www-data /usr/bin/php /var/www/owncloud/occ "\$@"
EOM

sudo chmod +x /usr/local/bin/occ

Install the Required Packages

sudo apt install -y \
  apache2 \
  libapache2-mod-php7.2 \
  mariadb-server \
  openssl \
  php-imagick php7.2-common php7.2-curl \
  php7.2-gd php7.2-imap php7.2-intl \
  php7.2-json php7.2-mbstring php7.2-mysql \
  php-ssh2 php7.2-xml php7.2-zip \
  php-apcu php-redis redis-server \
  wget
sudo apt install -y \
  ssh bzip2 rsync curl jq \
  sudo inetutils-ping \
  smbclient php-smbclient coreutils php7.2-ldap
Ubuntu 18.04 includes smbclient 4.7.6, which has a known limitation of only using version 1 of the SMB protocol.

Installation

Configure Apache

Change the Document Root

sudo sed -i "s#html#owncloud#" /etc/apache2/sites-available/000-default.conf

sudo service apache2 restart

Create a Virtual Host Configuration

FILE="/etc/apache2/sites-available/owncloud.conf"
sudo /bin/cat <<EOM >$FILE
Alias /owncloud "/var/www/owncloud"

<Directory /var/www/owncloud>
  Options +FollowSymlinks
  AllowOverride All

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

 SetEnv HOME /var/www/owncloud
 SetEnv HTTP_HOME /var/www/owncloud
</Directory>
EOM

Enable the Virtual Host Configuration

sudo a2ensite owncloud.conf
sudo service apache2 reload

Configure the Database

mysql -u root -e "CREATE DATABASE IF NOT EXISTS owncloud; \
GRANT ALL PRIVILEGES ON owncloud.* \
  TO owncloud@localhost \
  IDENTIFIED BY 'password'";
echo "Enabling Apache Modules"

sudo a2enmod dir env headers mime rewrite setenvif
sudo service apache2 reload

Setup ownCloud

Download ownCloud

sudo -u www-data wget -P=/var/www https://download.owncloud.org/community/owncloud-10.2.0.tar.bz2 && \
  sudo tar -xjf owncloud-10.2.0.tar.bz2 && \
  sudo chown -R www-data /var/www/owncloud

Install ownCloud

occ maintenance:install \
    --database "mysql" \
    --database-name "owncloud" \
    --database-user "owncloud" \
    --database-pass "password" \
    --admin-user "admin" \
    --admin-pass "admin"

Configure ownCloud’s Trusted Domains

myip=$(hostname -I|cut -f1 -d ' ')
occ config:system:set trusted_domains 1 --value="$myip"

Set Up a Cron Job

echo "*/15  *  *  *  * /usr/bin/php -f /var/www/owncloud/cron.php" \
  > /var/spool/cron/crontabs/www-data
sudo -u www-data chown www-data.crontab /var/spool/cron/crontabs/www-data
sudo -u www-data chmod 0600 /var/spool/cron/crontabs/www-data

If you need to sync your users from an LDAP or Active Directory Server, add this additional Cron job.

echo "*  */6  *  *  * /usr/bin/php -f /var/www/owncloud/cron.php" \
  > /var/spool/cron/crontabs/www-data
sudo -u www-data chown www-data.crontab /var/spool/cron/crontabs/www-data
sudo -u www-data chmod 0600 /var/spool/cron/crontabs/www-data

Configure Caching and File Locking

Add this to the existing configuration in /var/www/owncloud/config/config.php.

<?php
// ...remaining configuration
'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
    'host' => 'localhost',
    'port' => 6379,
],

Configure Log Rotation

Execute this command to set up log rotation.

FILE="/etc/logrotate.d/owncloud"
sudo /bin/cat <<EOM >$FILE
/var/www/owncloud/data/owncloud.log {
  size 10M
  rotate 12
  copytruncate
  missingok
  compress
  compresscmd /bin/gzip
}
EOM

Finalise the Installation

FILE="/usr/local/bin/ocpermissions"

/bin/cat <<EOM >$FILE
#!/bin/bash

ocpath="/var/www/owncloud"
datadir="/var/www/owncloud/data"
htuser="www-data"
htgroup="www-data"
rootuser="root"

printf "Creating any missing directories"
sudo -u "${htuser}" mkdir -p "$ocpath/assets"
sudo -u "${htuser}" mkdir -p "$ocpath/updater"
sudo -u "${htuser}" mkdir -p "$datadir"

printf "Update file and directory permissions"
sudo find "${ocpath}/" -type f -print0 | xargs -0 chmod 0640
sudo find "${ocpath}/" -type d -print0 | xargs -0 chmod 0750

printf "Set web server user and group as ownCloud directory user and group"
sudo chown -R "${rootuser}:${htgroup}" "${ocpath}/"
sudo chown -R "${htuser}:${htgroup}" "${ocpath}/apps/"
sudo chown -R "${htuser}:${htgroup}" "${ocpath}/apps-external/"
sudo chown -R "${htuser}:${htgroup}" "${ocpath}/assets/"
sudo chown -R "${htuser}:${htgroup}" "${ocpath}/config/"
sudo chown -R "${htuser}:${htgroup}" "${datadir}"
sudo chown -R "${htuser}:${htgroup}" "${ocpath}/updater/"
sudo chmod +x "${ocpath}/occ"

printf "Set web server user and group as .htaccess user and group"
if [ -f "${ocpath}/.htaccess" ]; then
  sudo chmod 0644 "${ocpath}/.htaccess"
  sudo chown "${rootuser}:${htgroup}" "${ocpath}/.htaccess"
fi

if [ -f "${datadir}/.htaccess" ]; then
  sudo chmod 0644 "${datadir}/.htaccess"
  sudo chown "${rootuser}:${htgroup}" "${datadir}/.htaccess"
fi

EOM

# Make the script executable
sudo chmod +x /usr/local/bin/ocpermissions

ocpermissions

ownCloud is now installed. You can confirm that it is ready to use by pointing your web browser to your ownCloud installation.