apt-get install apache2 php unzip libapache2-mod-php php-mcrypt php-mysql
apt-get install php-ctype php-dom php-gd php-iconv php-json php-mbstring php-posix php-common
apt-get install php-simplexml php-xmlreader php-xmlwriter php-zip php-json php-mbstring
apt-get install php-curl php-fileinfo php-bz2 php-intl php-smbclient php-ftp php-imap php-xml
apt-get install php-exif php-gmp php-imagick

a2enmod rewrite
a2enmod headers
a2enmod env
a2enmod dir
a2enmod mime
a2enmod setenvif 
a2enmod ssl

service apache2 restart

Disable default sites

a2dissite 000-default.conf
a2dissite default-ssl.conf

Setup Apache2 config

nano /etc/apache2/sites-available/cloud.conf
LoadModule headers_module modules/mod_headers.so

<VirtualHost *:80>
     DocumentRoot "/var/www/nextcloud"
     ServerName cloud.example.com
     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined
     Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains;"
<IfModule mod_dav.c>
      Dav off
<Directory /var/www/nextcloud>
     AllowOverride None
     Order deny,allow
     deny from all
     SetEnv HOME /var/www/nextcloud
     SetEnv HTTP_HOME /var/www/nextcloud
     Satisfy Any
<Directory /var/www/nextcloud>
     AllowOverride All
     Order allow,deny
     allow from all
<VirtualHost *:443>
     ServerName cloud.example.com:443
     DocumentRoot /var/www/nextcloud
     SSLEngine on
     SSLCertificateFile /mnt/certbot/letsencrypt/live/example.com/fullchain.pem
     SSLCertificateKeyFile  /mnt/certbot/letsencrypt/live/example.com/privkey.pem
     Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains;"
ln -s /etc/apache2/sites-available/cloud.conf /etc/apache2/sites-enabled/cloud.conf
service apache2 restart

Download Nextcloud 12

cd /var/www
mkdir nextcloud
cd nextcloud
wget https://download.nextcloud.com/server/installer/setup-nextcloud.php
chown -R www-data:www-data /var/www/nextcloud
chmod -R g+s /var/www/nextcloud

Setup Nextcloud

Point your browser to your server and finish the setup of NextCloud.

Setup Redis Cache

apt-get install redis-server
systemctl start redis-server
systemctl enable redis-server
apt install php-redis
php --ri redis
 See if redis is enabled.
systemctl restart apache2
nano /var/www/nextcloud/config/config.php

Add the below before the last );

'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.local' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => array(
     'host' => 'localhost',
     'port' => 6379,

Enable PHP Opcache

nano /etc/php/7.0/apache2/php.ini
ctrl+w and find [opcache]

put the below directly after [opcache]

systemctl restart apache2

occ command for updating NextCloud

sudo -u www-data php occ upgrade