Spherical coordinates $(r,\theta,\phi)$ are defined by $$\mathbf{x}(r,\theta,\phi)=(r\cos\phi\sin\theta,r\sin\phi\sin\theta,r\cos\theta),$$
but it is important to understand that they do not cover the whole $\mathbb{R}^3$ because points must be in one-to-one correspondence with coordinates. Let me give as an example a much simpler situation. Consider $r=0$. This sets $\mathbf{x}(r,\theta,\phi)=(0,0,0)$ regardless of the values of $(\theta,\phi)$. So we have a whole continuum of coordinates $(\theta,\phi)$ mapping to the same point. For this reason we restrict the coordinates to $r>0$.
Your issue with the $z$ axis is of the same nature. The $z$ axis is defined by $\theta=0,\pi$, since this is how we get $x=y=0$. But now observe that gives $$\mathbf{x}(r,0,\phi)=(0,0,r),\quad \mathbf{x}(r,\pi,\phi)=(0,0,-r).$$
This means that for all values of $\phi$ we get the same point on the $z$ axis. Again, the one-to-one correspondence between points and coordinates is broken here. So in the same way as we restrict $r>0$ we also restrict $0 < \theta < \pi$ with strict inequalities.