Minecraft wiki entry for Insomnia, associated text: Phantoms - phantoms may only spawn when a player has insomnia.
Sleeping - Sleeping or entering a bed are methods to reset the insomnia counter.
/gamerule doInsomnia - A gamerule that enables or disables insomnia, and thus, phantom spawning.