Search code examples
phpapachevpsramsolid-state-drive

Apache serve file from RAM


I'd like to store the content on my VPS in RAM as apposed to using the disk. Is there any way I can do this using Apache and PHP? I want to do this to minimise the time it takes to retrieve the content.

Also, is storing in RAM typically faster than storing on an SSD?


Solution

  • I'm no expert on this topic, but here's what I would try as a basic approach:

    I'd keep my application code (including resources like CSS, JS, etc. - to keep it simple) in a location on the HDD. I'd have a RAM mount like:

    vi /etc/fstab
    tmpfs       /mnt/ramdisk tmpfs   nodev,nosuid,noexec,nodiratime,size=1024M   0 0
    

    and on system startup I'd copy from the HDD over to the RAM mount. Then I'd have the HTTP server (apache) point to the RAM mount, also everything inside of PHP will load code from the RAM mount, not the original location (for the purpose of rutime, the HDD source location does not exist).

    For actual content I'd use an in-memory DB. If the content is media-rich some RAM cache like varnish can be used for that part.

    There are of course variation to this strategy, or maybe even better solutions, but this also depends on your particular conditions, and you haven't said too much about those.