Optimally doped YBa2Cu3O7 (YBCO) has a high critical temperature, at 92 K. It is largely believed that Cooper pairs form in YBCO and other cuprates because of spin fluctuations, but the issue and the detailed mechanism are far from settled. In the present work, we employ a state-of-the-art first-principles ability to compute both the low- and high-energy spin fluctuations in optimally doped YBCO. We benchmark our results against recent inelastic neutron scattering and resonant inelastic x-ray scattering measurements. Further, we use strain as an external parameter to modulate the spin fluctuations and superconductivity. We disentangle the roles of barium-apical oxygen hybridization, interlayer coupling, and orbital symmetries by applying an idealized strain, and also a strain with a fully relaxed structure. We show that shortening the distance between Cu layers is conducive to enhanced Fermi surface nesting, which increases spin fluctuations and drives up Tc. However, when the structure is fully relaxed, electrons flow to the dz2 orbital as a consequence of a shortened Ba-O bond, which is detrimental for superconductivity.