St. John’s wort (Hypericum perforatum L.) is a medicinal plant that alleviates depression and other disorders due to its abundance of active ingredients. Hyperforin, rutin, and melatonin are the main active, and important, ingredients in St. John’s wort that alleviate depression. In order to investigate the optimal conditions for accumulating these active ingredients, design of experiments and response surface methodology (RSM) was employed in this study. Two-month-old St John’s wort plants were cultivated in growth chambers at varying temperatures, light intensities, and nutrient solution concentrations before analysis by HPLC, for determining differences in hyperforin, rutin, and melatonin content. The results showed that hyperforin and rutin contents were significantly influenced by temperature (18–23 °C) and light intensity (49–147 μmol m−2 s−1 photosynthetic photon flux density (PPFD)), whereas Hoagland’s nutrient solution concentration (25–75%) had little effect. The accumulation of melatonin might not be influenced by cultivation conditions. Light intensity and temperature are easily controlled environmental factors in artificial cultivation, both of which are related to secondary metabolite production in the plant. Based on RSM, the optimal conditions for the accumulation of hyperforin and rutin were obtained. The maximum content of hyperforin was 5.6 mg/g, obtained at a temperature of 19 °C, a nutrient solution concentration of 45%, and a light intensity of 49 μmol m−2 s−1 PPFD. The maximum content of rutin was 3.8 mg/g obtained at a temperature of 18 °C, a nutrient solution concentration of 50%, and a light intensity of 147 μmol m−2 s−1 PPFD. This evaluation of suitable conditions for the accumulation of bioactive compounds in St. John’s wort can be applied to plant factories on a large scale.