We present a systematic study on the influence of epitaxial strain and hole concentration on the magnetic anisotropy in (Ga,Mn)As at 4.2 K. The strain was gradually varied over a wide range from tensile to compressive by growing a series of (Ga,Mn)As layers with 5% Mn on relaxed graded (In,Ga)As/GaAs templates with different In concentration. The hole density, the Curie temperature, and the relaxed lattice constant of the as-grown and annealed (Ga,Mn)As layers turned out to be essentially unaffected by the strain. Angle-dependent magnetotransport measurements performed at different magnetic field strengths were used to probe the magnetic anisotropy. The measurements reveal a pronounced linear dependence of the uniaxial out-of-plane anisotropy on both strain and hole density. Whereas the uniaxial and cubic in-plane anisotropies are nearly constant, the cubic out-of-plane anisotropy changes sign when the magnetic easy axis flips from in-plane to out-of-plane. The experimental results for the magnetic anisotropy are quantitatively compared with calculations of the free energy based on a mean-field Zener model. An almost perfect agreement between experiment and theory is found for the uniaxial out-of-plane and cubic in-plane anisotropy parameters of the as-grown samples. In addition, magnetostriction constants are derived from the anisotropy data.