I solved it like this.
create a file
sudo nano /etc/systemd/system/nginx-cache-setup.service
file contents
[Unit]
Description=Create nginx cache directories in /dev/shm
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/mkdir -p /dev/shm/nginx-cache/wp
ExecStartPost=/bin/chown -R www:www /dev/shm/nginx-cache/wp
ExecStartPost=/bin/chmod -R 755 /dev/shm/nginx-cache/wp
[Install]
WantedBy=multi-user.target
activate it to start automatically
sudo systemctl enable nginx-cache-setup.service
restart the server