In my experience, one of the first things your mind does after detoxing off of porn is to recreate it in your imagination. It's compensating for lack of stimulation and in the dark and quiet it's all too easy to do.
I took some advice from Joseph Pilates (yes, the dude who invented Pilates, hear me out). In his books he has some strange theories but he also claims that if you can't fall asleep in 20 minutes you need to get up and move around. Something that occupies your mind. Make yourself so tired you can't think. Jigsaw puzzles, crosswords, COLD SHOWER, NO JOKE! Just get out of bed!
Also, don't neglect your circadian rhythm! Sit in dim light for the last few hours before bed, to signal your brain that it's time to sleep. Kinda like a sunset. Works like a charm for me.