Blue light (BL), abundant in sunlight, is highly toxic to many insect species when exposure is excessive. While the physiological mechanisms of BL toxicity are being revealed, the evolutionary responses remain less explored. In this study, using Drosophila melanogaster as a model system, we conducted laboratory selection for over 60 generations under excessive BL exposure. The selected line (SL) flies exhibited enhanced BL-tolerance, and also increased body weight and lipid accumulation. Interestingly, we found elongated midgut in the SL flies, and increased microbiota, which was dominated by Acetobacter persici. The BL-tolerance and the lipid accumulation were dependent on the increment of gut microbiota. Genomic analysis identified mutations in the Hippo signaling pathway linked to midgut elongation, while transcriptome analysis showed downregulation of Tachykinin (Tk), a key suppressor of intestinal lipogenesis. Genetically induced lipid accumulation through manipulation of Tk or related lipogenic genes was sufficient to confer BL-tolerance. Moreover, our findings indicate that parental BL irradiation, along with accumulated mutations from laboratory selection, played a crucial role in midgut elongation and increased bacterial abundance. These findings reveal evolutionary responses to excessive BL exposure that shape host traits to maximize the benefits provided by gut microbiota.