Ubuntu – How to limit apache memory via cgroups

Apache2cgroup

I'd like to limit the total memory used by user www-data in Ubuntu Server 14.04 LTS. I've read that I can do this via cgroups.

This is what I've done so far:

# install
apt-get install cgroup-bin

# create group for user www-data
cgcreate -a 'www-data' -g memory,cpu:'www-data'

# validate that the cgroup fs was created
ll /sys/fs/cgroup/cpu/www-data
ll /sys/fs/cgroup/memory/www-data

# adjust cpu and memory
echo 100 > /sys/fs/cgroup/cpu/www-data/cpu.shares
echo 100000000 > /sys/fs/cgroup/memory/www-data/memory.limit_in_bytes

Now what? How do I enforce these settings to user www-data?

edit: Note that if there is a better/best way to do this via other means (e.g systemd) then please suggest it.

Best Answer

Enforcing the setting to the process would make more sense

sudo cgexec -g cpu:cpulimited /usr/sbin/apachectl start