The back of my neck hurts. I can’t turn my head without pain. My lymph nodes might be swollen. Can a viral infection cause this?

YES. however, I would never ignore neck pain: it could be muscular, inflammatory, nerver issues, infection or just a low grade viral infection. Never ignore these symptoms. Get checked today.
Neck pain. Probable cervical muscle spasm. Do range of motion exercises. apply hot compress, take ibuprofen.