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

For the time being, an official Linux binary 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. 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: 75 Average: 4]
  • 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. nextcloud.mydomain.com 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 nextcloud.mydomain.com. I have create an “A record” that says nextcloud.mydomain.com which points to the IP address 1.2.3.4. of my server. You’re telling me this should work? Or shall I simply make an A record of “mydomain.com” which points to my IP address?

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

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

          A nextcloud.mydomain.com 1.2.3.4

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

          A mydomain.com 5.6.7.8

          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.

          A nextcloud.mydomain.com 1.2.3.4
          A mydomain.com 1.2.3.4

          • Ryan Berkeley

            So it looks like I was doing it right. However it wasn’t picking it up. I ended up doing 1.2.3.4/nextcloud 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 subdomain.mydomain.com 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 (mydomain.com), just enter @.

          • Yoel Gutierrez

            Hello I have a weird problem, I set up everything and is working good. But, the thing is if I access my cloud thru my domain, (cloud.mydomain.com) out of my local network, work perfectly. Now the problem is when I get home I cant do it that way. if I open the browser and type https://cloud.mydomain.com it never get to the nextcloud but If I type my local ip address in the address bar of the NextCloud Server it opens perfectly. witch it like anoying everytime I get home remove the domain name and login with my local ip address.

            Please HELP I am frustrated

          • Leo

            Hi Yoel, look for a configuration in your internet router. I suppose you did a Port NAT in the router and the NextCloud is in an internal PC/RPi, etc. It seems that the connection from an internal IP (ie 192.168.1.0/24) is not permitted to the port forwarding in the public IP.

  • 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
    Thanks

    • 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 mydomain.com just fine but when i try to access nextcloud.mydomain.com or mydomain.com/nextcloud neither works

    Not Found

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

    Apache/2.4.18 (Ubuntu) Server at 192.168.1.177 Port 80

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

      You can edit the hosts file of your OS to set nextcloud.mydomain.com pointing to 192.168.1.177.
      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:

      127.0.0.1 localhost
      192.168.1.177 nextcloud.mydomain.com

      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.

      • zakarra zakarra

        thanks for the guide!
        i followed your guide, my ubuntu server is only local in virtualbox to test, in the step 4, what name i put for the domain??? any name??? for example “nextcloud.local.com”???
        Then in point 5, i type nextcloud.local.com in the browser but find internet address, not my nextcloud server instance.
        Any ideas??? (sorry my bad english)

  • 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
    Thanks

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

  • MrAdminus

    Been running many different servers, Owncloud is just one of them. Landed here when looking for news about the forked version(Nextcloud)..
    Just wanted to say that you have created a really nice tutorial!

  • Jensens Ideal

    You should secure your domain with https before nextcloud setup because otherwise you submit your admin and database password unencrypted in the web installer.

  • Casey

    Followed this guide to a Tee but got hung up on the Redis server part. I’m getting this error now when trying to access my website:
    Memcache OCMemcacheRedis not available for local cache Is the matching PHP module installed and enabled?

    any thoughts?

    • Al

      I’m getting the same exact error message. Did you ever resolve it?

      • Restart apache.
        systemctl restart apache2

        • Al

          Funny thing is, I did try that a couple of times, and it didn’t work.

          However, after a full reboot of my server, Nextcloud was successfully using the memory caching.

  • xlman

    Hello. All process failed for me at the end of point 4, when I must restart apache2. I received error “Job for apache2.service failed because the control process exited with error code. See “systemctl status apache2.service” and “journalctl -xe” for details”

    Before the apache server was running ok, but after point 4, server goes down… I uninstall apache 2, and reinstall, and the server start again. Proceed with point 4, and the same error when restarting..

    When edit netcloud.conf, I change red text for 192.168.1.2, because I’m installing nextcloud in local PC Mint-KDE, and the apache server is running on 192.168.1.2… (before goes down..)

    First of all, I follow previous guide https://www.linuxbabe.com/linux-server/install-apache-mariadb-and-php7-lamp-stack-on-ubuntu-16-04-lts and all was OK.

    And now I’m try to follow this guide, but cant pass point 4.

    Thanks for help 🙂 (Sorry bad English..)

    • Hi, I tested this tutorial on my local Mint 18 computer and it worked like a charm with no errors at all.

      You can issue sudo journalctl -xe command to see if any errors are recorded. Also checking out the Apache error log file /var/log/apache2/error.log can also help.

      • xlman

        I have this error…

        apache2_reload: apache2: Syntax error on line 219 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/sites-enabled/nextcloud.conf: /etc/apache2/sites-enabled/nextcloud.conf:1:

        And the line 219 is “IncludeOptional sites-enabled/*.conf”

        So, I navigate to etc/apache2/sites-enabled/ and there is only a nexcloud.conf

        I supose there is something wrong in nextcloud.conf, like the error that mention “line 1” in this file, so this is what I have..

        DocumentRoot “/var/www/nextcloud”
        ServerName xlman.es

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

        Options +FollowSymlinks
        AllowOverride All

        Dav off

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

        Is there somenthing wrong in Line 1??

        Thanks!

        • xlman

          I found the solution….

          I need to close the tag by adding the line

          to the end of nextcloud.conf file

          It was not closed!!! :-O

          SOLVED!! Next step… 5 🙂

  • Michael B

    followed your instructions with no errors but in step 5 I am getting only the Apache2 Ubuntu Default Page
    any advise?

    /etc/apache2/sites-available# ll
    total 16K
    -rw-r–r– 1 root root 1.4K Sep 21 20:37 000-default.conf
    -rw-r–r– 1 root root 6.2K Apr 5 23:15 default-ssl.conf
    -rw-r–r– 1 root root 409 Sep 21 20:49 nextcloud.conf

    • Are you installing it on your local machine?

      • Michael B

        no, on virtual machine
        [email protected]:~# lsb_release -a
        No LSB modules are available.
        Distributor ID: Ubuntu
        Description: Ubuntu 16.04.1 LTS
        Release: 16.04
        Codename: xenial
        [email protected]:~# uname -a
        Linux nextcloud 4.4.0-38-generic #57-Ubuntu SMP Tue Sep 6 15:42:33 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

        • What did you type in browser address bar?

  • Alex Tupeck

    I followed your tutorial (AWESOME BY THE WAY!) However, web browsers connect just fine but using the Android App I get Malformed Server Configuration. Any ideas on how to fix that?

  • marioandluigi879

    I keep getting a problem loading page, reloading in 5 secs everytime think this could be fixed?

  • Rath

    Awesome tutorial! What needs to be changed for this to work on Nginx?

  • Jon

    Have you by chance looked into creating a tutorial on how to install the Collabora version of LibreOffice on a NextCloud server?

  • Jamie Lanniste

    Good tutorial. I’m stuck on step 5 tho. I did everything to the T and everything in stalled. I have a website name and I tryed using that and nothing came up. So I did a redo on step 4 with my local ip and nothing. And when I run -ls s it says there is a file so I deleted that file in enabled but still nothing. I bought a dns from go daddy. Will that work with this?

    • Any domain name with a correct A record will work.

  • Sean Villani

    I couldn’t get redis working until i ran:
    sudo phpenmod redis