记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

How to install NextCloud 16 server on CentOS 7.x


NextCloud is a Dropbox-like solution for self-hosted file sharing and syncing. Installing NextCloud 16 on CentOS is quite simple. Whether you want to backup, have file-syncing or just have a Google Calendar alternative, this guide is for you.

What is NextCloud? Is it like a “cloud”?


If you stumbled here by chance and don’t know what NextCloud is, here is an article explaining its principal features and advantages/disadvantages. In this other article you can find NextCloud 16 new features. To tell you the truth, NextCloud is a SaaS cloud, if you want to know more about cloud types you can read this article.

In this article we will cover the installation of the server (not the client).

What’s the newest version?

The newest version of this tutorial is the following:

Looking for an earlier version of this tutorial?

Step1: Install software

Important
I take absolutely NO responsibility of what you do with your machine; use this tutorial as a guide and remember you can possibly cause data loss if you touch things carelessly.

The first step in order to install NextCloud 16 is to install a web server and PHP. Since CentOS 7 ships with PHP 5.4 by default but NextCloud 16 requires at least PHP 7 we’ll also be installing PHP 7 from a third-party repository. The following procedure will install apache as webserver. Input the commands one by one to avoid errors!

CentOS 7

If you’d rather use PHP 7.3, you can follow this tutorial: how to install PHP 7.3 on CentOS 7. PHP 7.3 isn’t yet available in this repository.

Warning!
If you decided to use PHP 7.3 rather than PHP 7.2 using the past tutorial, replace each instance of php72w with php73w in all the successive commands.

Open a terminal and input the following commands:

  1. # yum install epel-release
  2. # rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  3. # yum install httpd php72w php72w-dom php72w-mbstring php72w-gd php72w-pdo php72w-json php72w-xml php72w-zip php72w-curl php72w-pear php72w-intl setroubleshoot-server bzip2 php72w-pecl-imagick

Step 2: Database selection

Now that you got the software, you need to choose a database that will support the installation. You have three choices:

  • SQLite: is a single-file database. It is suggested only for small installations since it will slow NextCloud down sensibly.
  • MariaDB/MySQL: are popular open source databases especially amongst web developers. It is the suggested choice.
  • PostgreSQL: a popular enterprise-class database. More complicated than MySQL/MariaDB.

Now, this choice won’t really alter the functionality of NextCloud (except if you use SQLite), so pick whatever you know best. If you’re unsure pick MariaDB/MySQL.

SQLiteMySQL/MariaDBPostgreSQL

No additional steps are required if you choose SQLite.

Install the software:

  1. # yum install mariadb-server php72w-mysql

Start (and enable at boot) the service:

  1. # systemctl start mariadb
  2. # systemctl enable mariadb

Next step is to configure the database management system. During the configuration you will be prompted to choose a root password, pick a strong one.

  1. # mysql_secure_installation

Now you need to enter the database (you will be asked the password you just set):

  1. $ mysql -u root -p

Now that you are in create a database:

  1. CREATE DATABASE nextcloud;

Now you need to create the user that will be used to connect to the database:

  1. CREATE USER 'nc_user'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD_HERE';

The last step is to grant the privileges to the new user:

  1. GRANT ALL PRIVILEGES ON nextcloud.* TO 'nc_user'@'localhost';
  2. FLUSH PRIVILEGES;

When you’re done type Ctrl-D to exit.

Install the software:

  1. # yum install postgresql postgresql-server php72w-pgsql

Run the setup:

  1. # postgresql-setup initdb

Start (and enable at boot) the service:

  1. # systemctl start postgresql
  2. # systemctl enable postgresql

Now you need to enter the database:

  1. $ sudo -u postgres psql

Now that you are in create a database:

  1. CREATE DATABASE nextcloud;

Now you need to create the user that will be used to connect to the database:

  1. CREATE USER nc_user WITH PASSWORD 'YOUR_PASSWORD_HERE';

The last step is to grant the privileges to the new user:

  1. GRANT ALL PRIVILEGES ON DATABASE nextcloud to nc_user;

When you’re done type \q and press enter to exit.

Warning!
You may experience difficulties in authenticating NextCloud with PostgreSQL since the local authentication method is set to ident by default. If you want to change it keep reading.

The configuration file for PostgreSQL is a file located in /var/lib/pgsql/data/pg_hba.conf . Open it with your favourite editor and look for the marked line (line 5):

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all peer
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 ident
  6. # IPv6 local connections:
  7. host all all ::1/128 ident
  8. # Allow replication connections from localhost, by a user with the
  9. # replication privilege.
  10. #local replication postgres peer
  11. #host replication postgres 127.0.0.1/32 ident
  12. #host replication postgres ::1/128 ident

Replace ident with md5 on that line and restart PostgreSQL:

  1. # systemctl restart postgresql

Step 3: Install NextCloud

This step involves getting the software and configure Apache to run it.

CentOS 7

With these step we download the software and extract it:

  1. # cd /var/www/html
  2. # curl -o nextcloud-16-latest.tar.bz2 https://download.nextcloud.com/server/releases/latest-16.tar.bz2
  3. # tar -xvjf nextcloud-16-latest.tar.bz2
  4. # mkdir nextcloud/data
  5. # chown -R apache:apache nextcloud
  6. # rm nextcloud-16-latest.tar.bz2

Now we need to create a new file in /etc/httpd/conf.d/nextcloud.conf . Feel free to use whatever editor you feel comfortable with and add the following lines:

  1. Alias /nextcloud "/var/www/html/nextcloud/"

  2. <Directory /var/www/html/nextcloud/>
  3. Options +FollowSymlinks
  4. AllowOverride All

  5. <IfModule mod_dav.c>
  6. Dav off
  7. </IfModule>

  8. SetEnv HOME /var/www/html/nextcloud
  9. SetEnv HTTP_HOME /var/www/html/nextcloud

  10. </Directory>

Step 4: Setting Apache and SELinux

In this step we’ll start (and enable) the webserver and we’ll set SELinux up. Now, many tutorials will tell you to disable SELinux (because it is a difficult component to manage). Instead, I suggest you to keep it on and add the rules for NextCloud:

CentOS 7
  1. # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/data(/.*)?'
  2. # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/config(/.*)?'
  3. # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/apps(/.*)?'
  4. # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/.htaccess'
  5. # semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/html/nextcloud/.user.ini'
  6. # restorecon -Rv '/var/www/html/nextcloud/'

If you decided to use a Mariadb/MySQL/PostgreSQL, you also need to allow apache to access it:

  1. # setsebool -P httpd_can_network_connect_db

In case you chose PostgreSQL you also need to enable httpd_execmem (I’m still investigating why this is needed):

  1. # setsebool -P httpd_execmem

Another important thing to do is to raise PHP’s memory limit:

  1. # sed -i '/^memory_limit =/s/=.*/= 512M/' /etc/php.ini

Now that you’ve configured SELinux let’s start and enable Apache:

  1. # systemctl start httpd
  2. # systemctl enable httpd

Step 5: Configuring firewall

This step is essential when your firewall is enabled. If your firewall is enabled you won’t be able to access your NextCloud 14 instance; on the other hand if it isn’t enabled you shouldn’t have any problems and you can simply skip this step. 

Tip!
Keep in mind having a firewall enabled is a good security practice and you should already have one enabled.

In order for the firewall to work, it must be enabled. This guide will not include this part. When you enable a firewall many things can go wrong, e.g. you’re using SSH, you enable the firewall and your connection is cut and can’t connect otherwise, hence you should carefully review the documentation from your distribution.

To open the ports needed by NextCloud 16 follow these steps:

FirewallDIPtables

FirewallD is a newer firewall used to simplify firewall management. If you’re using it you can simply do:

  1. # firewall-cmd --add-service http --permanent
  2. # firewall-cmd --add-service https --permanent
  3. # firewall-cmd --reload

IPtables is an older firewall (still widely used), if you have disabled firewallD you can use IPtables directly.

  1. # iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
  2. # iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

Step 6: Install

Once you’re done, it’s time to install everything. Head to http://YOUR_IP_ADDRESS/nextcloud/ and you will be facing the following screen:

Nextcloud 16 Installation
Nextcloud 16 Installation

Select an administrator username and password. Then click on “Storage & Database“, here you can select the data folder, but if you don’t know what you’re doing it’s best if you leave it with the default value. Then select the database you chose during step 2. Fill everything and if you’ve followed all the steps correctly you should be seeing the following screen:

NextCloud 16 Files app as viewed when installing for the first time
NextCloud 16 Files app

Step 7: Enable Caching (suggested)

NextCloud is good but it can be very slow if you don’t configure a caching solution. There are two caching solutions covered in this guide:

  • PHP OPcache: a PHP inbuilt cache solution that speeds up scripts execution.
  • Redis server: a fast in-memory key-value store that speeds up everything in NextCloud.

Enabling OPcache

CentOS

Open a terminal and input the following commands:

  1. # yum install php-opcache

Now you need to edit a file located at /etc/php.d/10-opcache.ini . With your favorite editor, edit the file and make it look like this:

  1. ; Enable Zend OPcache extension module
  2. zend_extension=opcache.so
  3. opcache.enable=1
  4. opcache.enable_cli=1
  5. opcache.interned_strings_buffer=8
  6. opcache.max_accelerated_files=10000
  7. opcache.memory_consumption=128
  8. opcache.save_comments=1
  9. opcache.revalidate_freq=1

These values are suggested by NextCloud, but you’re free to tweak them to suit your needs. Once you’re done you can restart apache:

  1. # systemctl restart httpd

Installing and configuring Redis

CentOS

Open a terminal and input the following commands:

  1. # yum install redis php72w-pecl-redis

Now you must configure NextCloud to use Redis. To do so you need to edit the NextCloud configuration file located at /var/www/html/nextcloud/config/config.php . The file will look like this, add the highlighted lines:

  1. <?php
  2. $CONFIG = array (
  3. 'instanceid' => '',
  4. 'passwordsalt' => '',
  5. 'secret' => '',
  6. 'trusted_domains' =>
  7. array (
  8. 0 => 'YOUR_IP',
  9. ),
  10. 'datadirectory' => '/var/www/html/nextcloud/data',
  11. 'dbtype' => 'mysql',
  12. 'version' => '15.0.0.10',
  13. 'overwrite.cli.url' => 'http://YOUR_IP/nextcloud',
  14. 'dbname' => 'nextcloud',
  15. 'dbhost' => 'localhost',
  16. 'dbport' => '',
  17. 'dbtableprefix' => 'oc_',
  18. 'dbuser' => 'nc_user',
  19. 'dbpassword' => 'YOUR_PASSWORD_HERE',
  20. 'installed' => true,
  21. 'memcache.locking' => '\OC\Memcache\Redis',
  22. 'memcache.distributed' => '\OC\Memcache\Redis',
  23. 'memcache.local' => '\OC\Memcache\Redis',
  24. 'redis' => [
  25. 'host' => 'localhost',
  26. 'port' => 6379,
  27. 'timeout' => 3,
  28. ],
  29. );

These settings will enable NextCloud to use Redis for caching and file locks. Of course these settings are just an example, you can tweak them to suit your needs.

Now you need to modify (for some reason) the Redis port SELinux label in order to enable Apache to access Redis:

  1. # semanage port -m -t http_port_t -p tcp 6379

Lastly, enable and start Redis and restart the webserver:

  1. # systemctl restart redis
  2. # systemctl enable redis
  3. # systemctl restart httpd

Step 8: Expose NextCloud to Internet (optional)

Important
Hosting applications available to the Internet is potentially dangerous. In order to keep your applications safe you need to be proficient in system security and to follow security best practices.

Most people will want to access their files from whatever location they are. To do so, your newly created NextCloud instance needs to be connected to the Internet.

Given that you need to take care of port-forwarding (if you’re a home user) and domain configuration (which varies according to your provider), here you can find the instructions to create a virtual host with Apache.

CentOS

Using your favorite text editor, edit the file we created previously at /etc/httpd/conf.d/nextcloud.conf . And make it look like this:

  1. <VirtualHost *:80>
  2. ServerName YOURDOMAIN.TLD
  3. ServerAdmin YOUR@EMAIL.TLD
  4. DocumentRoot /var/www/html/nextcloud

  5. <directory /var/www/html/nextcloud>
  6. Require all granted
  7. AllowOverride All
  8. Options FollowSymLinks MultiViews
  9. SetEnv HOME /var/www/html/nextcloud
  10. SetEnv HTTP_HOME /var/www/html/nextcloud
  11. </directory>
  12. </VirtualHost>

It is important to set ServerName according to a domain you own and have configured correctly. Now you need to add YOURDOMAIN.TLD to the trusted domains in the NextCloud config file. You can do so with the following command:

  1. $ sudo -u apache php /var/www/html/nextcloud/occ config:system:set trusted_domains 2 --value=YOURDOMAIN.TLD

Once you complete this step you won’t be able to access NextCloud through http://YOUR_IP_ADDRESS/nextcloud anymore. Instead you will be able to access it through http://YOURDOMAIN.TLD (notice /nextcloud is gone).

Lastly, restart the webserver:

  1. # systemctl restart httpd

Step 9: Get a free SSL certificate with Let’s Encrypt! (SUGGESTED!

Now that you have your NextCloud instance up and running you’re good to go, but beware: you’re not safe. Internet is a dangerous place for your data and you will most likely need an SSL certificate to ensure your communications are encrypted. Provided you own a domain name you can get one for free using Let’s Encrypt! No catches, free forever.

Warning!
Let’s Encrypt has rate limits in place to prevent inappropriate usage of the CA. There’s a limit on the numbers of attempts you can do before getting a temporary ban. During this setup, if things go wrong, I suggest you to use the –staging option to avoid the temporary ban. The –staging option will use a testing server and will not issue valid certificates. When you have completed the procedure against the test server successfully, you can remove the –staging option to obtain the real certificate.
CentOS

Open a terminal and input the following commands:

  1. # yum install certbot certbot-apache

Now you will run the command to install a certificate, follow the procedure and you will get everything configured out of the box:

  1. $ sudo certbot --apache

Lastly, restart the webserver:

  1. # systemctl restart httpd

If you need further help you can follow my other tutorial on Let’s Encrypt on CentOS (the apache part).

Image courtesy of mark | marksei
The following two tabs change content below.
The IT guy with a little boredom look in his eyes, fond of computers since forever he now works as a freelancer in the IT and shares his experiences through this blog.


转载请标明出处【How to install NextCloud 16 server on CentOS 7】。

《www.92cto.com》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论