Thursday, January 2, 2014

Owncloud with dedicated php-fpm service

Owncloud on Raspberry PI is pretty slow. With Owncloud 6, takes the page loading from 20 to 60 seconds (this is much worse than with Owncloud 5). So there is an option to run php-fpm on other machine with more CPU power. Here is an howto to setup this scenario.


There are several things you need to consider. First you need to share owncloud data directory which needs to be the same on both machines. Second thing is to share database for owncloud.
So after few scenarios, which didn't work very well here is one that works:





First thing to setup are NFS mount points and IPtables to allow connection for NFS and database. Configuration for NFS is covered in Setting up NFS  post. You need to setup 2 mount points:
  • /mnt/owncloud 172.16.0.2(rw,sync,no_subtree_check)  
  • /usr/share/webapps/owncloud 172.16.0.2(rw,sync,no_subtree_check) 
For the database you need to enable port in IPtables and allow access to the owncloud's database from the remote machine:
GRANT ALL PRIVILEGES ON owncloud.* TO 'krisko'@172.16.0.2 identified by 'password;
or
GRANT ALL PRIVILEGES ON owncloud.* TO 'krisko'@krisko identified by 'password;
if you have setup /etc/hosts

Now you should be able to mount NFS (mount points have to be in the same paths as on Raspberry) and you should be able to connect to mysql database from remote host.

Next step is to install php-fpm on the remote host, then in php-fpm.conf file alter the listener directive:
listen = 172.16.0.2:9000

Also I have set dynamic process manager and set numbers for childs (this is not required):
pm = dynamic
;pm = static

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 20

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 5

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 5

Now you shold be able to start php-fpm deamon. Final step is to change nginx configuration (nginx.conf) and add another php-fpm connector. In the http {} section add:
    upstream php_cluster {
        #ip_hash;
        server 172.16.0.2:9000;
        server unix:/run/php-fpm/php-fpm.sock backup;
    }
and in the php configuration section for owncloud comment out existing fastcgi_pass and set it to php_cluster:
#fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_pass php_cluster;

Now php-fpm requests will be sent and processed on the remote machine.
With this setup I get page load in about 12 seconds.

4 comments:

  1. its probably not cpu power that is the problem on the raspberry pi ..

    it would be ram (php5 is VERY ram-hungry and was probably sending the raspberry pi into swap)
    .
    for a standalone personal server on a raspi using php it might be better to restrict concurrency right down to 1. Sure it will force concurrent requests into a queue, but it would help avoid hitting swap and give it more chance to actually handle those requests.

    I'm actually curious about doing the remote fpm thing the other way round - I have a vps which is having trouble handling traffic hitting php and an external helper server even on a raspi might help it out until I can find ways to trim down those bloated php processes. ... and php processed are much smaller on the 32 bit raspi than on the 64 bit vps so it might be possible to allow it to even have two or three worker processes on there!

    ReplyDelete
  2. Hi,
    I have over 300MB RAM free, so this is not the issue. In this case, CPU is the cause. It would be helpful, if Raspberry had 2 cores... but it doesn't.

    I have done some performance testing with php-fpm child counts.
    1. I have set pm to static. This should be slightly faster, as php-fpm doesn't need to start additional childs when needed.
    2. There were almost no differences with different child counts. Currently I am running fpm with 2 child processes.

    Settings in my php-fpm.conf:
    pm = static
    pm.max_children = 2
    pm.start_servers = 2
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3

    ReplyDelete
  3. Thanks for taking time to share this post. It is really useful. Continue sharing more like this. PHP Developers
    Web Data Extraction Services
    magento development services

    ReplyDelete
  4. The great information that you shared. It will help all of them. Thanks for posting. Keep maintain the updates
    PHP web development services|
    ERP Software|
    Ecommerce website builder|

    ReplyDelete