NextCloud on your shared VPS

Nextcloud is a suite of client-server software for creating and using file hosting services. It is functionally similar to Dropbox, although Nextcloud is free and open-source, allowing anyone to install and operate it on a private server.

In contrast to proprietary services like Dropbox, the open architecture allows adding additional functionality to the server in form of applications and enables the user to have full control of their data.

The original ownCloud developer Frank Karlitschek forked ownCloud and created Nextcloud, which continues to be actively developed by Karlitschek and other members of the original ownCloud team.

Assumptions

  • your server is example.com and it is running Ubuntu 16.04;
  • you already have a configured subdomain in the dns: nextcloud.example.com;
  • your sudo user is admin;
  • the user you want to create for NextCloud is nextcloud;
  • the database main user is root and the password is mysqlpassword;
  • the database you will use is named nextclouddbwith the user nextclouddbuser with the password password

Create a new user and add it to Apache group

  • sudo adduser nextcloud
  • sudo usermod -a -G www-data nextcloud

Create the virtualhost for Apache

Apache specials

Please be sure to have an Apache configuration similar to what the following commands give:

  • sudo a2enmod rewrite
  • ​sudo a2enmod headers
  • sudo a​2enmod env
  • ​sudo a2enmod dir
  • ​sudo a2enmod mime
  • sudo a2enmod ssl
  • sudo a2ensite default-ssl
  • sudo service apache2 restart

Actually creating the host

  • su - nextcloud
  • cd ~
  • mkdir public_html
  • chmod 755 ./public_html
  • exit
  • sudo chgrp -R www-data /home/nextcloud/public_html
  • sudo chmod -R g+w /home/nextcloud/public_html
  • sudo nano /etc/apache2/sites-available/nextcloud.example.com.conf
  • <VirtualHost *:80>
      <Directory "/home/nextcloud/public_html">
        Allow from all
        Options +Indexes
        <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /
          AllowOverride All
        </IfModule>
        
        <IfModule mod_dav.c>
          Dav off
        </IfModule>
      </Directory>
    
      DocumentRoot /home/nextcloud/public_html
      ServerName nextcloud.example.com
      
      SetEnv HOME /home/nextcloud/public_html
      SetEnv HTTP_HOME /home/nextcloud/public_html
    </VirtualHost>
  • sudo a2ensite nextcloud.example.com.conf
  • sudo service apache2 reload

Create the database

  • mysql -uroot -pmysqlpassword -e "CREATE DATABASE nextclouddb /*\!40100 DEFAULT CHARACTER SET utf8 */;"
  • mysql -uroot -pmysqlpassword -e "CREATE USER nextclouddbuser@localhost IDENTIFIED BY 'password';"
  • mysql -uroot -pmysqlpassword -e "GRANT ALL PRIVILEGES ON nextclouddb.* TO 'nextclouddbuser'@'localhost';"
  • mysql -uroot -pmysqlpassword -e "FLUSH PRIVILEGES;"
  • sudo nano /etc/mysql/my.cnf
    Add binlog-format = MIXED

Install other packages

  • sudo apt-get install apache2 mariadb-server libapache2-mod-php7.0
  • ​sudo apt-get install php7.0-gd php7.0-json php7.0-mysql php7.0-curl php7.0-mbstring
  • ​sudo apt-get install php7.0-intl php7.0-mcrypt php-imagick php7.0-xml php7.0-zip
  • ​sudo apt-get install unzip

NextCloud

  • sudo su - nextcloud
  • cd ~/public_html
  • wget https://download.nextcloud.com/server/releases/latest-13.tar.bz2 -O nextcloud-13-latest.tar.bz2
  • tar -xvjf nextcloud-13-latest.tar.bz2
  • rm nextcloud-13-latest.tar.bz2
  • cd nextcloud
  • mv ./* ../
  • cd ..
  • rm -rf nextcould
  • sudo chmod 0770 /home/nexcloud/public_html/data

Access http://nextcloud.example.com to finish the setup. Also, be sure to check:

Also, do remember to check the firewall rules.

Activate SSL

  • sudo add-apt-repository ppa:certbot/certbot
  • sudo apt-get update
  • sudo apt-get install python-certbot-apache
  • sudo certbot --apache -d nextcloud.example.com
  • sudo su - nextcloud
  • nano /home/nextcoud/public_html/config/config.php
    Change the option for overwrite.cli.url from http:// to https://

Activate opcache

  • sudo su - nextcloud
  • nano /home/nextcloud/php.ini
  • 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
  • Ctrl+x and Y to save

  • exit
  • sudo nano /etc/apache2/sites-available/nextcloud.example.com.conf
  • add PHPINIDir /home/nextcloud below the ServerName line
  • sudo nano /etc/apache2/sites-available/nextcloud.example.com-le-ssl.conf

Sources