LAMPnextcloudVPS

How to install APCu Object Cache in php7 and integrate in Nextcloud

Today we want to optimize our Nextcloud installation with PHP-APCu, it does not matter which Nextcloud or PHP7 version we use. Depending on the version we replace the „x“ in the commands.

VPS Preparation:

Optional:
If you have big but slow disks like me, you can work with a RAM disk. Condition is, of course, enough RAM in the system.
For this purpose, we create a RAM disk under /tmp/ before we start:

sudo mkdir /tmp/apc

and mount this disk:

sudo mount -t ramfs ramfs /tmp/apc

To make this available after each reboot, we edit the file /etc/fstab:

sudo nano /etc/fstab

and we add the following line:

ramfs   /tmp/apc     ramfs   defaults        0       0

As always, we bring our system up to date:

sudo apt -y update && apt -y upgrade && apt -y full-upgrade 

then we install PHP-APCu

sudo apt install php-apcu

Depending on the version, edit the php.ini (replace the x with the version here)

for php-fpm:

sudo nano /etc/php/7.x/fpm/php.ini

for php-apache-modul:

sudo nano /etc/php/7.x/apache2/php.ini

and enter the following values:

apc.enabled=1
apc.file_update_protection=2
apc.optimization=0
apc.shm_size=256M
apc.include_once_override=0
apc.shm_segments=1
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl=3600
apc.num_files_hint=1024
apc.enable_cli=0
apc.max_file_size=5M
apc.cache_by_default=1
apc.use_request_time=1
apc.slam_defense=0
apc.mmap_file_mask=/tmp/apc/apc.XXXXXX
apc.stat_ctime=0
apc.canonicalize=1
apc.write_lock=1
apc.report_autofilter=0
apc.rfc1867=0
apc.rfc1867_prefix =upload_
apc.rfc1867_name=APC_UPLOAD_PROGRESS
apc.rfc1867_freq=0
apc.rfc1867_ttl=3600
apc.lazy_classes=0
apc.lazy_functions=0

Now we have to teach Nextcloud to work with the APCu, we adapt the config.php:

sudo nano /var/www/html/nextcloud/config/config.php

Here we adjust the values „memcache.distributed“ and „memcache.local“ as follows:

'memcache.distributed' => '\\OC\\Memcache\\APCu',
'memcache.local' => '\\OC\\Memcache\\APCu',

Next we restart Apache2 and php-fpm (if used):

sudo systemctl restart php7.x-fpm
sudo systemctl restart apache2

We can verify the success with a php script that we have to load in our webspace

cd /var/www/html/nextcloud
sudo wget https://raw.githubusercontent.com/krakjoe/apcu/master/apc.php

Then call the script in our domain via our browser, call https://nextcloud.deine-domain.tld/apc.php:

If we want to look at the „User Cache Entries“, we have to replace the default password in the script „apc.php“:

sudo nano /var/www/html/nextcloud/apc.php

In the script „apc.php“ we have to find this line and adjust accordingly:

defaults('ADMIN_PASSWORD','password');          // Admin Password - CHANGE THIS TO ENABLE!!!

That’s it and have fun… 🙂

4 Gedanken zu „How to install APCu Object Cache in php7 and integrate in Nextcloud

  1. Hallo, Danke auch von mir. Die Installation klappt, Nextcloud meckert nicht mehr, aber nur, wenn ich die Zeilen
    „apc.enabled=1
    apc.file_update_protection=2

    apc.lazy_functions=0“
    NICHT in die PHP.ini eintrage. Tue ich es, ist der Webserver nicht mehr erreichbar.
    Bin jetzt etwas unsicher…
    Schönen Gruß, Thomas

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.