How to install and configure Nextcloud on CentOS 7

Introduction

Nextcloud provides client applications for Windows, Linux, macOS, Android and iOS which are used to sync files between your Desktop and the Nextcloud file server. It also has a modern and easy-to-use web interface which enables you to access files from a web browser.

In this article, we will show you how to install NextCloud on a CentOS 7 server/VPS instance.

Prerequisites

  • an active KVM VPS
  • root user
1. Install PHP and httpd

You will have to install PHP and Apache to run Nextcloud on CentOS 7. For PHP, we will use PHP 7.3 since it is the latest stable release of PHP available for CentOS 7.

yum -y install epel-release yum-utils yum -y install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Disable default PHP 5.x enabled repository and enable one for PHP 7.3:

yum-config-manager --disable remi-php54
yum-config-manager --enable remi-php73

Then install Apache and PHP packages:

yum -y install vim httpd php php-cli php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-pdo php-pecl-apcu php-pecl-apcu-devel

Confirm your PHP version

php -v
2. Install and configure MariaDB / MySQL

Install MariaDB / MySQL database server on CentOS 7 following our previous guides here: 

How to Install MariaDB 10.4 on CentOS 7

After the installation, login as a root user to MySQL console and create a new database for Nextcloud.

$ mysql -u root -p
CREATE DATABASE nextcloud;  CREATE USER 'nextclouduser'@'localhost' IDENTIFIED BY 'yourpassword';  GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextclouduser'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;  FLUSH PRIVILEGES;  EXIT;

Make sure to replace the database name “nextcloud”, the database username “nextclouduser”, and the database user password “yourpassword” in each and every command with your own ones.

3. Download and install Nextcloud on CentOS 7

Download the latest stable version of NextCloud from its official website:

yum -y install wget unzip wget https://download.nextcloud.com/server/releases/latest-20.zip

Unzip the downloaded file:

unzip latest-20.zip rm -f latest-20.zip

Move all of the NextCloud files to /var/www/html directory

mv nextcloud/ /var/www/html/

Create data directory to store Nextcloud uploaded files. This can be any path e.g. NFS mount point, SAN mount point etc.

mkdir /var/www/html/nextcloud/data
chown apache:apache -R /var/www/html/nextcloud/data

Give apache user and group the ownership of Nextcloud folder.

chown apache:apache -R /var/www/html/nextcloud
4. Configure Apache VirtualHost – without SSL

Now create an Apache configuration file for Nextcloud.

vi /etc/httpd/conf.d/nextcloud.conf

Paste the content below to the file:

   
 ServerName files.example.com   
 ServerAdmin [email protected]   
 DocumentRoot /var/www/html/nextcloud   
       
    Require all granted     
    AllowOverride All     
    Options FollowSymLinks MultiViews     
    SetEnv HOME /var/www/html/nextcloud     
    SetEnv HTTP_HOME /var/www/html/nextcloud   
   

Set the correct ServerName and change other settings to suit your use. When done, save the file and start httpd service.

Run:

systemctl enable --now httpd
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
sudo restorecon -Rv /var/www/html

Configure Firewall:

If you have an active firewall service, allow http and https ports.

firewall-cmd --add-service={http,https} --permanent
firewall-cmd --reload
5. Configure Apache With Let’s Encrypt SSL

To use Let’s Encrypt SSL certificate, first install certbot

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
mv certbot-auto /usr/local/bin

Then request for SSL certificate:

export DOMAIN="files.example.com"
export EMAIL="[email protected]"
certbot-auto certonly --standalone -d $DOMAIN --preferred-challenges 
http --agree-tos -n -m $EMAIL --keep-until-expiring

Modify your VirtualHost configuration file to look like below:


   ServerName files.example.com
   ServerAdmin [email protected]
   RewriteEngine On
   RewriteCond %{HTTPS} off
   RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]



   
     ServerName files.example.com
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/nextcloud
     
        Require all granted
        AllowOverride All
        Options FollowSymLinks MultiViews
        SetEnv HOME /var/www/html/nextcloud
        SetEnv HTTP_HOME /var/www/html/nextcloud
    
     SSLEngine on
     SSLCertificateFile /etc/letsencrypt/live/files.example.com/fullchain.pem
     SSLCertificateKeyFile /etc/letsencrypt/live/files.example.com/privkey.pem
   
6. Access Nextcloud UI and finish installation

Open up your web browser, point a URL to

http://files.example.com or http://your VPS/Server's IP address, and use the NextCloud admin account to log in.

The browser will take you automatically to NextCloud setup page where you have to begin the setup of NextCloud.

You can choose either SQLite or MySQL/MariaDB. If you want to SQLite database, then you do not have to enter the database details (not recommended for production use). Whereas MariaDB requires database user, password, and database name.

For this tutorial, we will use MariaDB as a backend database.

Provide admin username and password. Also configure MySQL / MariaDB database.

When done click the “Finish Setup” button. You should get the Files dashboard of Nextcloud now.

That’s it! Enjoy GreenCloudVPS services!
On this page