apt-get -y install apache2 php unzip libapache2-mod-php php-mcrypt php-mysql
apt-get -y install php-ctype php-dom php-gd php-iconv php-json php-mbstring php-posix php-common
apt-get -y install php-simplexml php-xmlreader php-xmlwriter php-zip php-json php-mbstring
apt-get -y install php-curl php-fileinfo php-bz2 php-intl php-smbclient php-ftp php-imap php-xml
apt-get -y 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

service apache2 restart

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;"
</VirtualHost>
<IfModule mod_dav.c>
      Dav off
</IfModule>
<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>
<Directory /var/www/nextcloud>
     AllowOverride All
     Order allow,deny
     allow from all
</Directory>

## If you are not using an proxy use this 443 config ##

<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;"
</VirtualHost> 

## If you are not using an proxy use this 443 config ##

Pending changes to apache2 config...

ln -s /etc/apache2/sites-available/cloud.conf /etc/apache2/sites-enabled/cloud.conf
service apache2 restart

Download Nextcloud 13.0.5

cd /var/www
mkdir nextcloud
wget https://download.nextcloud.com/server/releases/nextcloud-13.0.5.zip && unzip nextcloud-13.0.5.zip
rm nextcloud-13.0.5.zip
chown -R www-data:www-data /var/www/nextcloud
chmod -R g+s /var/www/nextcloud

Download Nextcloud 14.0.1

cd /var/www
mkdir nextcloud
wget https://download.nextcloud.com/server/releases/nextcloud-14.0.1.zip && unzip nextcloud-14.0.1.zip
rm nextcloud-14.0.1.zip
chown -R www-data:www-data /var/www/nextcloud
chmod -R g+s /var/www/nextcloud

Install MySQL server

apt-get install mariadb-server -y
mysql -e "CREATE DATABASE nc_2018;"
mysql -e "GRANT ALL PRIVILEGES ON nc_2018.* TO 'ncuser'@'localhost' IDENTIFIED BY 'ncpass';"
mysql -e "FLUSH PRIVILEGES;"
mysql_secure_installation

Setup Nextcloud

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

OR

cd /var/www/nextcloud
sudo -u www-data php occ  maintenance:install --database "mysql" --database-name nc_2018 --database-user ncuser --database-pass ROOT_PASSWORD --admin-user admin --admin-pass password

You should see….

Nextcloud is not installed - only a limited number of commands are available

Setup Redis Cache

apt-get install redis-server php-redis -y
systemctl start redis-server
systemctl enable redis-server
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]

opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1
systemctl restart apache2

updating nextcloud

occ command for updating NextCloud

sudo -u www-data php occ upgrade

Fixes

Keepweb fix

nano /var/www/nextcloud/config/mimetypemapping.json
{
    "kdbx": ["x-application/kdbx"]
}
sudo -u www-data php /var/www/nextcloud/occ maintenance:mimetype:update-js

Slow login?

Add the following to the config file.

nano /var/www/nextcloud/config/config.php
'auth.bruteforce.protection.enabled' => false,