Setup a Secure Installation of OwnCloud

Setup a Secure Installation of OwnCloud

celeriummind-owncloud-header

Start Sharing Files Securely:

Recently I have been designing a secure file sharing platform for a company. The requirements were: a secure environment where people could come together to share files quickly and safely.  In comes OwnCloud, OwnCloud is a popular open source file sync and sharing platform founded in 2011 to give corporate IT greater control of their data.

To read some more about OwnCloud you can check out their site here

Machine Overview:

For this guide I will be using OwnCloud 7 Community edition. I would have loved to setup this install with their enterprise edition but when I submitted a request for a quote they came back with a $10,000 bill.  You should expand your installation to fit your needs and check out the OwnCloud forums and official documentation as they have some great information that I would highly suggest you reference when setting up your own OwnCloud instance.

Basic Setup:

  • CentOS 7 Minimal Install
  • 80GB HDD
  • 2GB Memory

Initial Install:

Once you are done with the initial install of CentOS 7 you will need to grab a few tools that do not come pre-installed with the minimal installation.

yum -y install VIM net-tools wget epel-release

Then run an update on your machine and reboot.

yum -y update

reboot 

Install the LAMP stack:

Run the following commands to install Apache, Mariadb, PHP, and extra packages that OwnCloud will need later on.

yum -y install mariadb-server mariadb httpd php php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel

Once you install thoses packages you will need to start mariadb and apache, then enable them so they auto start on reboots.

systemctl start httpd
systemctl enable httpd
systemctl start mariadb
systemctl enable mariadb

Mariadb:

Run the initial setup of mariadb and configure you database.

mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):     ## Press Enter ## 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n]        ## Press Enter ##
New password:                   ## Enter new password ##
Re-enter new password:          ## Re-enter new password ##
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]    ## Press Enter ##
... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]     ## Press Enter ##
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]     ## Press Enter ##
- Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]     ## Press Enter ##
... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Create a OwnCloud database:

Head into mariadb and create a new database and assign it an “sa” account with all permissions. Do not use your root user as the sa account.

mysql -u root -p

create database owncloud;

CREATE USER 'SAYOURUSER'@'localhost' IDENTIFIED BY 'YOURPASSWORD';

GRANT ALL PRIVILEGES ON owncloud. * TO 'SAUSERNAME'@'localhost';

FLUSH PRIVILEGES;

Installing Owncloud 7:

If you run into permission errors with CentOS and OwnCloud then you probably have an SELinux issue. The following are SELinux permission commands that will allow Apache to read/write into specified OwnCloud directories.

ATTENTION: Do not disable SELinux. Instead temp place SELinux in permissive mode (0). This allows SELinux to log permission information but not block.  If you disable SELinux you will need to reboot, then make your changes, then re-enable, reboot again, wait 30+min while the entire system is recontexted, just to see if your changes took affect. DO NOT DISABLE SELINUX. Do not disable SELinux.

setenforce 0

sestatus (to view the status of SELinux)

chcon -R -t httpd_sys_rw_content_t /var/www/owncloud/config

chcon -R -t httpd_sys_rw_content_t /var/www/owncloud/data

chcon -R -t httpd_sys_rw_content_t /var/www/owncloud/apps 

OwnCloud 7 Setup Finalization:

Navigate to your OwnCloud instance via “http://x.x.x.x/owncloud” and you will be prompted with the following.  The first box is where you will setup an administrative user.  Then the box after is where you will supply the OwnCloud 7 instance with the database information that you created earlier. After this information is entered you can finish the setup and you will be redirected to the OwnCloud 7 user homepage where you can begin your journey into OwnCloud.

Adding SSL communications:

For an SSL encrypted web server you will need a few things. Depending on your install you may or may not have OpenSSL and mod_ssl, Apache’s interface to OpenSSL.  Use yum to get them if you need them.

yum -y install mod_ssl openssl 

Using OpenSSL we will generate a self-signed certificate. If you are using this on a production server then get a key from a Trusted Certificate Authority, but if you are just using this on a personal site or for testing purposes a self-signed certificate is fine.

# Generate private key

openssl genrsa -out ca.key 2048

# Generate CSR

openssl req -new -key ca.key -out ca.csr

# Generate Self Signed Key

openssl x509 -req -days 365 -in ca.csr -signkey ca.key -out ca.crt

# Copy the files to the correct locations

cp ca.crt /etc/pki/tls/certs

cp ca.key /etc/pki/tls/private/ca.key

cp ca.csr /etc/pki/tls/private/ca.csr 

Then we need to update the Apache SSL configuration file

vim +/SSLCertificateFile /etc/httpd/conf.d/ssl.conf

###Change the Following lines to match where your keys are###

SSLCertificateFile /etc/pki/tls/certs/ca.crt

SSLCertificateKeyFile /etc/pki/tls/private/ca.key
systemctl restart httpd 

All being well you should now be able to connect over https to your server. As the certificate is self-signed browsers will generally ask you whether you want to accept the certificate.

Creating Virtual Host for OwnCloud:

Next you will want to navigate to /etc/httpd/conf.d/owncloud.conf and create a virtual host

vim /etc/httpd/conf.d/owncloud.conf 

This will allow you to go to https://x.x.x.x and it will take you directly to OwnCloud instead of using https://x.x.x.x/owncloud.

#Listen 80 http (only enable this is you need to listen on non native port)

NameVirtualHost *:80

AllowOverride All
DocumentRoot /var/www/owncloud

ServerName x.x.x.x

#Listen 443 https (only need to enable this listen if it is a non native port)

NameVirtualHost *:443

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/ca.crt

SSLCertificateKeyFile /etc/pki/tls/private/ca.key

AllowOverride All

DocumentRoot /var/www/owncloud

ServerName x.x.x.x
systemctl restart httpd 

Now you can navigate to https://x.x.x.x and it will auto redirect you to your OwnCloud site.

Redirect port 443:

You will need to install some SELinux  tools before you can continue on.

yum -y install policycoreutils-python 

Next you can run a semanage port -l to get an idea for some port options you can allow and create. As an example lets have Apache bind to port 5443

semanage port -a -t http_port_t -p tcp 5443 

Then after allowing this port you will need to change your owncloud.conf  virtual host file located in /ect/http/conf.d. And replace anything with the standard 443 port should be read a 5443.

Listen 5443 https

NameVirtualHost *:5443

SSLEngine on

SSLCertificateFile /etc/pki/tls/certs/ca.crt

SSLCertificateKeyFile /etc/pki/tls/private/ca.key

AllowOverride All

DocumentRoot /var/www/owncloud

ServerName x.x.x.x:5443 

After changing theses settings you will need to change a couple more .conf files to get back into OwnCloud.  The first thing you will need to do is open port 5443 in your firewall. With newere versions of CentOS firewall-cmd has been removed so you will need to use iptables instead.

firewall-cmd --permanent --zone=public --add-port=5443/tcp

Next you will need to edit the /var/www/owncloud/config/config.php file to be able to access the OwnCloud application via its new address. So find the line that looks like the one below and change it.

OwnCloud.conf Trusted Domain

'trusted_domains' =>
array(
0 => 'x.x.x.x',

CHANGE TO

'trusted_domains' =>
array (
0 => 'x.x.x.x.x:5443', 

After these files and firewall ports have been edited you can now navigate to https://x.x.x.x:5443 and you will be direct to your OwnCloud site.Congratulations, you made it to the end.  By now you should have a secure OwnCloud instance running on https://x.x.x.x:5443.  This is a great step to understanding OwnCloud and what possibilities it can contain for you.