We have already commented that modular exponentiation is easy to compute but very difficult to invert. But why don’t we do exponentiation modulo a non-prime number?
The protocol itself does not depend on the modulus being prime. To break Diffie–Hellman, you might try to compute discrete logarithms; discrete logarithms over a prime modulus is very very hard; if your modulus is composite, then the Chinese Remainder Theorem can be used to reduce the discrete logarithm problem to the prime factors of the modulus. So Diffie–Hellman over a prime modulus has only one avenue of attack: computing the discrete logarithm of a big prime. In contrast, Diffie–Hellman over a composite modulus has two avenues of attack: computing the discrete logarithm of a big number, or factoring the big number and then computing the discrete logarithms of some little primes.