OwnCloud with NginX and PostgreSQL

This quick tutorial will help you run the OwnCloud with NginX and PostgreSQL on Ubuntu 14.04.

  1. Add the necessary repo to /etc/apt/sources.list:

    [bash]# nginx stable
    deb http://nginx.org/packages/ubuntu/ trusty nginx
    deb-src http://nginx.org/packages/ubuntu/ trusty nginx
    # nginx mainline
    deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx
    deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx[/bash]

  2. Update system:

    [bash]apt-get update
    apt-get dist-upgrade[/bash]

  3. Install nginx:

    [bash]apt-get install nginx[/bash]

  4. Install dependencies for owncloud:

    [bash]apt-get install acl at-spi2-core ca-certificates-java colord cpp cpp-4.8 dbus-x11 dconf-gsettings-backend dconf-service default-jre default-jre-headless desktop-file-utils dictionaries-common fontconfig fontconfig-config fonts-dejavu fonts-dejavu-core fonts-dejavu-extra fonts-font-awesome fonts-liberation fonts-opensymbol fonts-sil-gentium fonts-sil-gentium-basic gconf-service gconf-service-backend gconf2 gconf2-common gdisk ghostscript gsfonts gvfs gvfs-common gvfs-daemons gvfs-libs heirloom-mailx hicolor-icon-theme hunspell-en-us icedtea-7-jre-jamvm imagemagick-common java-common javascript-common libao-common libao4 libapache2-mod-php5 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libasound2 libasound2-data libasyncns0 libatasmart4 libatk-bridge2.0-0 libatk-wrapper-java libatk-wrapper-java-jni libatk1.0-0 libatk1.0-data libatspi2.0-0 libav-tools libavahi-client3 libavahi-common-data libavahi-common3 libavahi-glib1 libavcodec54 libavdevice53 libavfilter3 libavformat54 libavresample1 libavutil52 libbonobo2-0 libbonobo2-common libboost-date-time1.54.0 libboost-system1.54.0 libcaca0 libcairo-gobject2 libcairo2 libcanberra0 libcdio-cdda1 libcdio-paranoia1 libcdio13 libcdr-0.0-0 libcloog-isl4 libclucene-contribs1 libclucene-core1 libcmis-0.4-4 libcolamd2.8.0 libcolord1 libcolorhug1 libcups2 libcupsfilters1 libcupsimage2 libdatrie1 libdc1394-22 libdconf1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libexif12 libexttextcat-2.0-0 libexttextcat-data libfftw3-double3 libflac8 libfontconfig1 libfontenc1 libgconf-2-4 libgconf2-4 libgd3 libgdk-pixbuf2.0-0 libgdk-pixbuf2.0-common libgif4 libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libglu1-mesa libgmp10 libgnome2-0 libgnome2-bin libgnome2-common libgnomevfs2-0 libgnomevfs2-common libgomp1 libgphoto2-6 libgphoto2-l10n libgphoto2-port10 libgraphite2-3 libgs9 libgs9-common libgsm1 libgstreamer-plugins-base1.0-0 libgstreamer1.0-0 libgtk-3-0 libgtk-3-bin libgtk-3-common libgtk2.0-0 libgtk2.0-bin libgtk2.0-common libgudev-1.0-0 libgusb2 libharfbuzz-icu0 libharfbuzz0b libhsqldb1.8.0-java libhunspell-1.3-0 libhyphen0 libice6 libicu52 libidl-common libidl0 libieee1284-3 libijs-0.35 libisl10 libjack-jackd2-0 libjasper1 libjbig0 libjbig2dec0 libjpeg-turbo8 libjpeg8 libjs-chosen libjs-dojo-core libjs-dojo-dijit libjs-dojo-dojox libjs-jcrop libjs-jquery libjs-jquery-metadata libjs-jquery-minicolors libjs-jquery-mousewheel libjs-jquery-tablesorter libjs-jquery-timepicker libjs-jquery-ui libjs-pdf libjs-sphinxdoc libjs-twitter-bootstrap libjs-underscore liblangtag-common liblangtag1 liblcms2-2 libldb1 libllvm3.4 liblqr-1-0 libltdl7 libmagickcore5 libmagickwand5 libmcrypt4 libmhash2 libmp3lame0 libmpc3 libmpfr4 libmspub-0.0-0 libmysqlclient18 libmythes-1.2-0 libneon27-gnutls libnspr4 libnss3 libnss3-1d libnss3-nssdb libntdb1 liboauth-php libogg0 libopenjpeg2 libopus0 liborbit-2-0 liborbit2 liborc-0.4-0 liborcus-0.6-0 libpango-1.0-0 libpangocairo-1.0-0 libpangoft2-1.0-0 libpaper-utils libpaper1 libpciaccess0 libphp-pclzip libphp-phpmailer libpixman-1-0 libpoppler44 libpulse0 libpython3.4 libraptor2-0 librasqal3 libraw1394-11 librdf0 libreoffice libreoffice-avmedia-backend-gstreamer libreoffice-base libreoffice-base-core libreoffice-base-drivers libreoffice-calc libreoffice-common libreoffice-core libreoffice-draw libreoffice-gnome libreoffice-gtk libreoffice-impress libreoffice-java-common libreoffice-math libreoffice-pdfimport libreoffice-report-builder-bin libreoffice-sdbc-firebird libreoffice-sdbc-hsqldb libreoffice-style-galaxy libreoffice-style-human libreoffice-writer libsamplerate0 libsane libsane-common libschroedinger-1.0-0 libsdl1.2debian libsecret-1-0 libsecret-common libservlet3.0-java libsm6 libsmbclient libsndfile1 libspeex1 libswscale2 libtalloc2 libtdb1 libtevent0 libthai-data libthai0 libtheora0 libtidy-0.99-0 libtiff5 libtxc-dxtn-s2tc0 libudisks2-0 libv4l-0 libv4lconvert0 libva1 libvisio-0.0-0 libvorbis0a libvorbisenc2 libvorbisfile3 libvpx1 libwayland-client0 libwayland-cursor0 libwbclient0 libwpd-0.9-9 libwpg-0.2-2 libwps-0.2-2 libx11-xcb1 libx264-142 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-render0 libxcb-shape0 libxcb-shm0 libxcb-sync1 libxcomposite1 libxcursor1 libxdamage1 libxfixes3 libxfont1 libxft2 libxi6 libxinerama1 libxkbcommon0 libxmu6 libxpm4 libxrandr2 libxrender1 libxshmfence1 libxslt1.1 libxt6 libxtst6 libxv1 libxvidcore4 libxxf86dga1 libxxf86vm1 libyajl2 libzend-framework-php lp-solve mediawiki-classes mysql-common openjdk-7-jre openjdk-7-jre-headless owncloud-doc php-aws-sdk php-crypt-blowfish php-doctrine-common php-dompdf php-dropbox php-font-lib php-getid3 php-google-api-php-client php-guzzle php-irods-prods php-opencloud php-patchwork-utf8 php-pear php-phpdocx php-sabre-dav php-sabre-vobject php-seclib php-services-json php-symfony-console php-symfony-eventdispatcher php-symfony-routing php-tcpdf php-xml-parser php5 php5-apcu php5-cli php5-common php5-curl php5-gd php5-imagick php5-intl php5-json php5-ldap php5-mcrypt php5-mysql php5-oauth php5-readline php5-tidy php5-xsl policykit-1-gnome poppler-data python-crypto python-ldb python-ntdb python-samba python-talloc python-tdb python3-uno samba-common samba-common-bin samba-libs sdop smbclient sound-theme-freedesktop ssl-cert ttf-dejavu-core tzdata-java udisks2 uno-libs3 ure vorbis-tools x11-common x11-utils xfonts-encodings xfonts-mathml xfonts-utils zend-framework zend-framework-bin php5-pgsql postgresql-9.3 php5-fpm mc bzip2[/bash]

  5. Change php and nginx configurations for large file processing: Change max_execution_time in /etc/php5/fpm/php.ini, client_body_buffer_size in nginx.conf file, and remember to change .user.ini file in owncloud root folder to overcome 513MB limit which is set by owncloud.
  6. Create database and user for owncloud:

    [bash]su postgres #switch to postgres user
    psql template1
    template1=# create user cloud with password ‘YOURPASSWORD’;
    template1=# create database cloud template template0 encoding ‘UNICODE’;
    template1=# alter database cloud owner to cloud;
    template1=# grant all privileges on database cloud to cloud;
    template1=# \q
    $ exit[/bash]

  7. Download and move owncloud to www folder:

    [bash]cd /tmp
    wget https://download.owncloud.org/community/owncloud-8.0.0.tar.bz2
    tar jxvf owncloud-8.0.0.tar.bz2
    mv owncloud /var/www/owncloud[/bash]

  8. Modify nginx main configuration file as follows:

    [bash]nano -wc /etc/nginx/nginx.conf
    ##Configuration file might be as follows:
    user www-data;
    worker_processes 4; # Set this to your number of CPUs

    error_log /var/log/nginx/error.log error;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    use epoll;
    }

    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    log_format main ‘$remote_addr – $remote_user [$time_local] "$request" ‘
    ‘$status $body_bytes_sent "$http_referer" ‘
    ‘"$http_user_agent" "$http_x_forwarded_for"’;

    #access_log /var/log/nginx/access.log main;
    access_log off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_min_length 1000;
    gzip_buffers 4 32k;
    gzip_types text/plain application/x-javascript text/xml text/css application/xml;

    include /etc/nginx/conf.d/*.conf;

    upstream php-handler {
    server unix:/var/run/php5-fpm.sock;
    }

    }
    [/bash]

    . Then, create a new configuration file for owncloud (in my case, I use https so remember to copy your certificate to /etc/ssl/nginx/):

    [bash]mkdir /etc/nginx/conf.d
    nano -wc /etc/nginx/conf.d/cloud.conf
    ### Configuration file is as follows
    server {
    listen 80;
    server_name DOMAIN.com;
    return 301 https://$server_name$request_uri; # enforce https
    }

    server {
    listen 443 ssl spdy; # Activate SPDY for SSL connections
    server_name DOMAIN.com;
    ssl_certificate /etc/ssl/nginx/nginx.crt;
    ssl_certificate_key /etc/ssl/nginx/nginx.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    # Path to the root of your installation
    root /var/www/owncloud/;

    client_max_body_size 10G; # set max upload size
    fastcgi_buffers 64 4K;
    client_body_buffer_size 2M;
    rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
    rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
    rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;

    index index.php;
    error_page 403 /core/templates/403.php;
    error_page 404 /core/templates/404.php;

    location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }

    location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README) {
    deny all;
    }

    location / {
    # The following 2 rules are only needed with webfinger
    rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
    rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
    rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
    rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
    try_files $uri $uri/ index.php;
    }

    location ~ \.php(?:$|/) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS on;
    fastcgi_pass php-handler;
    fastcgi_read_timeout 600; # Increase this to allow larger uploads
    access_log off; # Disable logging for performance
    }

    # Optional: set long EXPIRES header on static assets

    location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
    expires 365d;
    # Optional: Don’t log access to assets
    access_log off;
    }

    }[/bash]

  9. Remember to change the owner of the owncloud folder to www-data user:

    [bash]chown -R www-data:www-data /var/www/owncloud[/bash]

  10. Continue installing on the web interface. Remember to select PostgreSQL for database and enter the above configuration.
  11. Done.

Leave a Reply