How to Install NextCloud 9 on CentOS 7

Updated on September 2, 2016
How to Install NextCloud 9 on CentOS 7 header image

NextCloud, just as its name suggests, is a promising alternative of another open source file hosting solution OwnCloud.

In this article, I will show you how to install NextCloud on a Vultr CentOS 7 server instance.

Prerequisites

  • A newly created Vultr CentOS 7 server instance.
  • A sudo user for log in.

Step 1: Update the system

Log into the CentOS 7 system as a sudo user, and then update the system to the latest stable status:

sudo yum install epel-release -y
sudo yum update -y
sudo shutdown -r now

After the reboot, still use the same sudo user to log in.

Step 2: Install Apache

Install Apache as the web server for NextCloud:

sudo yum install httpd -y

Disable the pre-set Apache welcome page:

sudo sed -i 's/^/#&/g' /etc/httpd/conf.d/welcome.conf

For security purposes, prevent Apache from displaying contents in the web root directory /var/www/html:

sudo sed -i "s/Options Indexes FollowSymLinks/Options FollowSymLinks/" /etc/httpd/conf/httpd.conf

Prevent Apache from loading WebDAV modules, as required by NextCloud:

sudo sed -i 's/^/#&/g' /etc/httpd/conf.modules.d/00-dav.conf

Start the Apache service and make it start on boot:

sudo systemctl start httpd.service
sudo systemctl enable httpd.service

Step 3: Install PHP and necessary PHP extensions

NextCloud recommends to use PHP 5.6. Here, you can install PHP 5.6 and necessary PHP extensions using the IUS YUM repository.

Install the IUS YUM repository:

cd
wget https://centos7.iuscommunity.org/ius-release.rpm
sudo rpm -Uvh ius-release.rpm

Install PHP 5.6 and necessary extensions using the IUS YUM repo:

sudo yum install php56u php56u-common php56u-xml php56u-gd php56u-mbstring php56u-process php56u-mysqlnd php56u-intl php56u-mcrypt php56u-imap php56u-cli -y

Increase the upload file size to an appropriate value, say 50 MB:

sudo cp /etc/php.ini /etc/php.ini.bak
sudo sed -i "s/post_max_size = 8M/post_max_size = 50M/" /etc/php.ini
sudo sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 50M/" /etc/php.ini

Restart Apache in order to apply above modifications:

sudo systemctl restart httpd.service

Step 4: Install MariaDB and setup a database for NextCloud

Install MariaDB:

sudo yum install mariadb mariadb-server -y

Start the MariaDB service and make it start on boot:

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Secure MariaDB installation:

sudo /usr/bin/mysql_secure_installation

During the process, answer questions one by one as below:

Enter current password for root (enter for none): Enter
Set root password? [Y/n]: Y
New password: <your-password>
Re-enter new password: <your-password>
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]: Y
Reload privilege tables now? [Y/n]: Y

Now, it's time to setup a database for NextCloud:

mysql -u root -p

Input the MariaDB root password you set earlier to log in.

In the MySQL shell, you need to create a database and a database user, and then grant privileges to this database user.

Use the following commands to finish the work. Be 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.

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;

Step 5: Install NextCloud

Download the latest stable version of NextCloud from its official website. At the time of writing, the latest stable version number is 9.0.53.

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

Unzip the NextCloud archive:

sudo yum install unzip -y
unzip nextcloud-9.0.53.zip

Move all of the NextCloud files to the web root directory, and then grant proper permissions to them:

sudo mv nextcloud/* /var/www/html && sudo chown apache:apache -R /var/www/html

Install NextCloud from command line interface with the following sample parameters. Be sure to replace them with your own ones.

  • database-name: nextcloud
  • database-user: nextclouduser
  • database-pass: yourpassword
  • admin-user: admin
  • admin-pass nextcloudadminpassword

Run:

cd /var/www/html/
sudo -u apache php occ maintenance:install --database "mysql" --database-name "nextcloud"  --database-user "nextclouduser" --database-pass "yourpassword" --admin-user "admin" --admin-pass "nextcloudadminpassword"

If everything goes well, the output will read:

Nextcloud is not installed - only a limited number of commands are available
ownCloud was successfully installed

Add your server IP (say it is 203.0.113.1) and domain name (say it is www.example.com) to NextCloud's trusted domains list:

sudo vi /var/www/html/config/config.php

Find the line:

0 => 'localhost',

Insert the following two lines right beneath it:

1 => '203.0.113.1',
2 => 'www.example.com',

Save and quit:

:wq!

For security purposes, you need to set strong permissions to Nextcloud files and directories after the installation:

sudo find /var/www/html -type f -print0 | sudo xargs -0 chmod 0640
sudo find /var/www/html -type d -print0 | sudo xargs -0 chmod 0750
sudo chown -R root:apache /var/www/html
sudo chown -R apache:apache /var/www/html/apps
sudo chown -R apache:apache /var/www/html/config
sudo chown -R apache:apache /var/www/html/data
sudo chown -R apache:apache /var/www/html/themes
sudo chown -R apache:apache /var/www/html/updater
sudo chmod 0644 /var/www/html/.htaccess
sudo chown root:apache /var/www/html/.htaccess
sudo chmod 0644 /var/www/html/data/.htaccess
sudo chown root:apache /var/www/html/data/.htaccess

Additionally, remember that there is a directory which hasn't been created named /var/www/html/assets. If you create this directory in the future, you also need to run the following command:

sudo chown -R apache:apache /var/www/html/assets

These settings help prevent NextCloud from unauthorized access.

Note: If you need to update NextCloud in the future, you can temporarily loosen permissions using the following command:

sudo chown apache:apache -R /var/www/html

After the update, still set strong permissions as above.

Restart Apache in order to put your changes into effect:

sudo systemctl restart httpd.service

Modify firewall rules in order to allow access for visitors:

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --zone=public --permanent --add-service=https
sudo firewall-cmd --reload

That's it. Now you can point your web browser to http://203.0.113.1, and use the NextCloud admin account to log in.

Thank you for reading.