How to get the order of a group generator in DH? For a DH parameter prime, if the generator $g$ is 2, how do I get the order $q$?
To extend kelalaka's answer, if $p$ is a safe prime (that is, if $p = 2q+1$ with $q$ prime), then:

If $p \equiv 7 \pmod 8$, then the order of $g=2$ will be $q$

If $p \equiv 3 \pmod 8$, then the order of $g=2$ will be $2q$

If $p = 5$ (the only other possibility), then the order of $g=2$ is 4 (that is, $2q$)