A trick some people do is to write the words to an existing tune, which ensures that the lines scan and so forth.And then when the words are there, give it a new tune.
That makes sense, but I don't think I've ever done it that way. For me words and tune tend to come together. Or rather, as I get the words right, the tune is there as well when I try to sing it..