Set up NextCloud Server on Ubuntu 16.04 with Apache, MariaDB and PHP7

NextCloud 11 is out! It’s a drop-in replacement for ownCloud. In this tutorial, we are going to look at how to set up NextCloud personal cloud storage on a Ubuntu 16.04 VPS with Apache, MariaDB and PHP7.

Differences between NextCloud and ownCloud

NextCloud is a fork of the open source ownCloud. Major differences are:

  • It is 100% open source. All features including the enterprise part is open source. NextCloud charges fees for support rather than selling enterprise product.
  • It values the community more than ownCloud does.

Prerequisites

To follow this tutorial, you will need:

Once the above prerequisites are fulfilled, come back here and read on.

Step 1: Install NextCloud 11 Server on Ubuntu 16.04

Download the NextCloud server zip archive onto your server. The latest stable version is 11.0.1 at the time of this writing. You may need to change the version number. Go to https://nextcloud.com/install and click the download button to check out the latest version.

wget https://download.nextcloud.com/server/releases/nextcloud-11.0.1.zip

Extract it.

sudo apt install unzip

unzip nextcloud-11.0.1.zip

A new directory named nextcloud will be created in the current working directory. Copy the new directory and all of its content to the document root of Apache server.

sudo cp -r nextcloud /var/www/

Then you also need to give the Apache user (www-data) write permission.

sudo chown www-data:www-data /var/www/nextcloud/ -R

Step 2: Create a Database and User in MariaDB

Log into MariaDB database server with the following command:

mysql -u root -p

Then create a database for Nextcloud. This tutorial name the database nextcloud. You can use whatever name you like.

create database nextcloud;

Create the database user. Again, you can use your preferred name for this user. Replace your-password with your preferred password.

create user nextclouduser@localhost identified by 'your-password';

Grant this user all privileges on the nextcloud database.

grant all privileges on nextcloud.* to nextclouduser@localhost identified by 'your-password';

Flush privileges and exit.

flush privileges;

exit;

Step 3: Enable Binary Logging in MariaDB

Edit the mysqld configuration file.

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Add the following three lines in [mysqld] section.

log-bin        = /var/log/mysql/mariadb-bin
log-bin-index  = /var/log/mysql/mariadb-bin.index
binlog_format  = mixed

The format of binary log must be mixed. Save and close the file. Then restart MariaDB service.

sudo systemctl restart mysql

Now binary log is enabled in MariaDB.

Step 4: Create an Apache Virtual Host File for Nextcloud

We will create a nextcloud.conf in /etc/apache2/sites-available directory.

sudo nano /etc/apache2/sites-available/nextcloud.conf

Copy and paste the following lines in the file. Replace the red text with your actual domain name. You also need to point your domain name to the IP address of your Ubuntu server in DNS.

<VirtualHost *:80>
 DocumentRoot "/var/www/nextcloud"
 ServerName nextcloud.your-domain.com

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined

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

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

 SetEnv HOME /var/www/nextcloud
 SetEnv HTTP_HOME /var/www/nextcloud
 Satisfy Any

</Directory>

</VirtualHost>

Save and close the file. Then create a symbolic link to /etc/apache2/sites-enabled/

sudo ln -s /etc/apache2/sites-available/nextcloud.conf /etc/apache2/sites-enabled/nextcloud.conf

Enable the following Apache modules.

sudo a2enmod rewrite headers env dir mime setenvif ssl

Install needed PHP modules

sudo apt install php7.0-common php7.0-gd php7.0-json php7.0-curl  php7.0-zip php7.0-xml php7.0-mbstring

Restart Apache so that the above Apache and PHP modules are loaded.

sudo systemctl restart apache2

Step 5: Finish the Installation in your Web Browser

Now in your web browser, type your domain name. For instance

nextcloud.your-domain.com

You will be asked to create an admin account. The data folder is where user’s files are stored. For security, it’s better to place the data directory outside of Nextcloud web root, such as /var/www/nextcloud-data. which can be created with the following command:

sudo mkdir /var/www/nextcloud-data/

Then make sure the Apache user (www-data) has write permission to the data directory.

sudo chown www-data:www-data /var/www/nextcloud-data -R

If you are concerned with username and password sniffing, then you can first enable HTTPS (see the steps below) and then enter username, password and database details.

install nextcloud on ubuntu 16.04 16.10

Then enter the database username, database name and password you created earlier to connect Nextcloud to MariaDB database.

nextcloud ubuntu 16.04

Once it’s done, you will see the Web interface of Nextcloud. Congrats! You can now start using it as your private cloud storage.

Nextcloud LAMP

Secure it with HTTPS

Skip this step if you installed nextCloud on your local computer.

HTTPS helps us prevent man-in-the-middle attack and password sniffing. We can obtain a free TLS/SSL certificate from Let’s Encrypt CA. First Let’s install the certbot client. The client is still named letsnecrypt in Ubuntu repository. The following command will install the client and apache plugin.

sudo apt install letsencrypt python-letsencrypt-apache

Now issue the following command to obtain a free TLS/SSL certificate. Replace the red-colored text with your actual data.

sudo letsencrypt --apache --agree-tos --email your-email-address -d nextcloud.your-domain.com

You will be asked to choose easy or secure. It’s recommended to choose secure so that all http requests will be redirected to https.

let's encrypt apache

Once you hit the OK button, a free TLS/SSL certificate is obtained and installed on your Apache server.

let's encrypt apache auto

Now visit your Nextcloud site again. You will see that it’s automatically redirected to https.

By default, the Apache SSL configuration did not add HSTS security header. To add HSTS header, edit the SSL config file.

sudo nano /etc/apache2/sites-available/nextcloud-le-ssl.conf

Paste the following lines in the file after SSLCertificateKeyFile line.

<IfModule mod_headers.c>
     Header always set Strict-Transport-Security "max-age=15768000; preload"
 </IfModule>

Save and close the file. Then reload Apache.

sudo systemctl reload apache2

Linux Desktop Client

You can install NextCloud desktop client on Ubuntu 16.04/Ubuntu 17.10 from official PPA by using the following commands.

sudo add-apt-repository ppa:nextcloud-devs/client

sudo apt update

sudo apt install nextcloud-client

If you are using Gnome desktop environment, then simply go to Settings > Online account > NextCloud (ownCloud) to set it up. You can also easily access your Nextcloud files via WebDAV.

Configuring Redis Cache for nextCloud

Note: Redis cache is optional. You can safely skip this part.

If you go to your nextCloud admin page, you might see the following warning:

No memory cache has been configured. To enhance your performance please configure a memcache if available.

We will enable memory caching for nextCloud by using Redis. Install Redis server from Ubuntu 16.04 repository.

sudo apt install redis-server

You can check the version with:

redis-server -v

Sample output:

Redis server v=3.0.6 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=687a2a319020fa42

Now we can check if redis server is running.

systemctl status redis

ubuntu-16.04-redis-server-nextcloud-caching

From the above screenshot, we can see that it’s running and auto-start is enabled. If for any reason it’s not running, execute the following command:

sudo systemctl start redis-server

And if auto-start at boot time is not enabled, you can use the following command to enable it:

sudo systemctl enable redis-server

Now let’s configure Redis as a cache for nextCloud. Install the PHP extension for interfacing with Redis.

sudo apt install php-redis

Check if the extension is enabled or not.

php --ri redis

ubuntu 16.04 php7 redis extension

We can see that Redis extension is enabled and the version is 2.2.8 which is higher than the required 2.2.5+ by nextCloud. You may need to manually enable Redis extension either by restarting Apache

sudo systemctl restart apache2

or by running the following commmand:

sudo phpenmod redis

Next, edit nextCloud configuration file.

sudo nano /var/www/nextcloud/config/config.php

Add the following lines above the ); line.

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

ubuntu 16.04 redis cache nextcloud

Save and close the file. Now go to nextCloud admin page again, the warning about memory caching should be gone.

Congrats! You have successfully set up NextCloud personal cloud storage on a Ubuntu 16.04 VPS with Apache, MariaDB and PHP7.

Comments, questions or suggestions are always welcome. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+Twitter or like our Facebook page.

Rate this tutorial
[Total: 93 Average: 4]