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

  • A fresh install of Ubuntu 18.04 with SSH enabled.

  • This guide assumes that you are connected as the root user.

Preparation

First, ensure that all of the installed packages are entirely up to date, and that PHP 7.3 is available in the APT repository. To do so, follow the instructions below:

apt install software-properties-common && \
  add-apt-repository ppa:ondrej/php && \
  apt update && \
  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

Make helper script executable:

chmod +x /usr/local/bin/occ

Install the Required Packages

apt install -y \
  apache2 \
  libapache2-mod-php7.3 \
  mariadb-server \
  openssl \
  php-imagick php7.3-common php7.3-curl \
  php7.3-gd php7.3-imap php7.3-intl \
  php7.3-json php7.3-mbstring php7.3-mysql \
  php-ssh2 php7.3-xml php7.3-zip \
  php-apcu php-redis redis-server \
  wget
apt install -y \
  ssh bzip2 rsync curl jq \
  inetutils-ping smbclient\
  php-smbclient coreutils php7.3-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

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

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

a2ensite owncloud.conf
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"

a2enmod dir env headers mime rewrite setenvif
service apache2 reload

Setup ownCloud

Download ownCloud

cd /var/www/
wget https://download.owncloud.org/community/owncloud-10.2.1.tar.bz2 && \
tar -xjf owncloud-10.2.1.tar.bz2 && \
chown -R www-data. 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
chown www-data.crontab /var/spool/cron/crontabs/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 "*/15  *  *  *  * /usr/bin/php -f /var/www/owncloud/cron.php" > /var/spool/cron/crontabs/www-data
chown www-data.crontab  /var/spool/cron/crontabs/www-data
chmod 0600  /var/spool/cron/crontabs/www-data

Configure Caching and File Locking

Execute these commands:

occ config:system:set \
   memcache.local \
   --value '\OC\Memcache\APCu'

occ config:system:set \
   memcache.locking \
   --value '\OC\Memcache\Redis'

occ config:system:set \
   redis \
   --value '{"host": "127.0.0.1", "port": "6379"}' \
   --type json

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="{install-directory}"
datadir="{install-directory}/data"
htuser="{webserver-user}"
htgroup="{webserver-group}"
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.