Skip to main content

Setup NextCloud Server on Ubuntu 16.04 with Apache, MariaDB and PHP7

ubuntu nextcloud server

NextCloud 10 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. This tutorial assumes that you have already configured a LAMP stack on Ubuntu 16.04. If you haven’t done so, please check out the below easy-to-follow guide to see how to do it.

Install Apache, MariaDB and PHP7 (LAMP Stack) on Ubuntu 16.04 LTS

When you are finished setting up LAMP stack, come back here and read on.

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.

Step 1: Install NextCloud 10 Server on Ubuntu 16.04

Download the NextCloud server zip archive onto your server. The latest version is 10.0.0 at the time of this writing. You may need to change the version number. Go to and click the download button to check out the latest version.


Extract it.

sudo apt install unzip


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;


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 reload MariaDB service.

sudo systemctl reload mysql

If that command failed, then restart it instead.

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"

 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

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



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
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime
sudo a2enmod setenvif
sudo a2enmod 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

You will be asked to create an admin account. The data folder is where user’s files are. For security, it’s better to place the data directory outside of Nextcloud web root, such as /var/www/data. Make sure the Apache user has write permission to the data directory.

install nextcloud on Ubuntu 16.04

Then enter the database username, database name and password you created earlier to connect Nextcloud with 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

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; includeSubDomains; preload"

Save and close the file. Then reload Apache.

sudo systemctl reload apache2

Linux Desktop Client

For the time being, an official Linux client has not yet released. However, you can use the ownCloud client to log into your Nextcloud server. Install ownCloud client on Ubuntu 16.04 with the following command:

sudo apt install owncloud-client

Then open it from Unity Dash and configure it.

Nextcloud desktop client owncloud nextcloud

If you are using Gnome desktop environment, then simply go to Settings > Online account > ownCloud to set it up.

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

If for any reason it’s not running, execute the following command:

sudo systemctl start redis

We can also enable auto-start with:

sudo systemctl enable redis

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. 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. If you found this post useful, 🙂 please share it with your friends on social media! Stay tuned for more Linux tutorials.

Rate this tutorial
[Total: 11 Average: 4.2]
  • Ryan Berkeley

    I seem to be facing an issue with pointing the domain name to the IP address. Could you give some more information on this? I have created an A record for my domain i.e. that points to my IP address.

    • For this nextcloud setup, you only need an A record that points your domain name to the IP address of your server.

      • Ryan Berkeley

        So let’s say that my domain is I have create an “A record” that says which points to the IP address of my server. You’re telling me this should work? Or shall I simply make an A record of “” which points to my IP address?

        • Normally you want to use to host a website and use to host your nextcloud storage.

          In this tutorial, we configured an Apache virtual host for So to access your Nextcloud storage via in the browser address bar, you need to set an “A record” that says which points to the IP address of your server.


          If you configure another Apache virtual host for to host a website, then you need to set an “A record” that says which points to the IP address of your server.


          This tutorial didn’t show you how to host a website, so you don’t need to set this A record.

          You can of course create two virtual hosts on the same server. In that case, you need to set A record for each of your virtual hosts and the two A records points to the same IP address.


          • Ryan Berkeley

            So it looks like I was doing it right. However it wasn’t picking it up. I ended up doing for the setup which worked.

          • For security, try to access it via domain name and install a free TLS/SSL certificate from Let’s Encrypt. You can’t get a cert with IP address.

          • Ryan Berkeley

            @xiao_guoan:disqus I actually figured it out. So in my A records, I was adding instead of just “subdomain” and the pointing it to the IP. I think I will retry this installation.

          • Yes, you only need to enter the subdomain in DNS settings. If the subdomain is blank (, just enter @.

  • Few things are missing/need correction in this post, other than that good job.
    – Need to run for letsencrypt to work: a2enmod ssl
    – As a security measure the data dir should be outside the web server path(maybe here): /var/www/data OR /var/data

    • sites-available contains the actual config files. sites-enabled contains the symlink.
      The 1st argument of ln -s command should be the actual file, the 2nd argument should be the name of the symlink.

      • Sorry, my bad overlooked that part. I thought it was enabled.
        Edit: removed that part to not confuse users.

    • Oh, yes, the data directory should be outside the web root. I’ve added this recommendation in step 5. Thanks !

      • Jon

        Could you please provide the command to give the apache user write permissions to the data folder?

        • First create the data directory.

          sudo mkdir /var/www/data

          Then make Apache as the owner of this directory.

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

          • Jon

            Thank you for the quick reply and the excellent instructions.

    • The SSL module will automatically be enabled when you run the letsencrypt command. Anyway, I will add this command in my tutorial.

      • For some reasons it did not for me, and the process died half way while some files were created. Dig a bit about it and found that ssl issue. Afterwards it worked.

        • Then I must add it in this post. Thanks for this info.

      • Jon

        Do you know the best way to set up auto renewal of the SSL Cert?

        • You can set up a cron job.

          sudo crontab -e

          Then put the following line at the end of the file.

          @weekly letsencrypt renew

          The renew command will run once a week.

  • Ralphael Johnson

    followed your guide but when i get to the step to type in my domain name to finish the last step of the nextcloud setup (step 5) i get the below error any ideas?? I can get to just fine but when i try to access or neither works

    Not Found

    The requested URL /nextcloud was not found on this server.

    Apache/2.4.18 (Ubuntu) Server at Port 80

    • Hi, it seems that you are installing nextcloud in your own network, not on a public server, as indicated by which is a private IP address.

      You can edit the hosts file of your OS to set pointing to
      On Linux, the hosts file is /etc/hosts. So you can edit it by running

      sudo nano /etc/hosts

      And add a record like this: localhost

      Note that you can’t obtain TLS/SSL certificate from Let’s Encrypt if you install Nextcloud in your own network and you don’t need to.

  • BHSPitMonkey

    In the HTTPS section, the article says to edit an Apache config file that was never created in the previous steps (nextcloud-le-ssl.conf). Where did it come from?

    • Hi, nextcloud-le-ssl.conf will be automatically created after you get the TLS certificate from Let’s Encrypt.

  • jenkinsm74

    Hey. You should include a tutorial on installing the redis caching for nextcloud

    • Hi, I updated my tutorial to include the instructions of configuring Redis cache for nextCloud. 🙂