Bark beetles are an economically and ecologically important insect group, with aggregation behavior and thus host colonization success depends on pheromone-mediated communication. For some species, such as the major invasive forest pest in China, red turpentine beetle (Dendroctonus valens), gut microbiota participates in pheromone production by converting tree monoterpenes into pheromone products. However, how variation in gut microenvironment, such as pH, affects the gut microbial composition, and consequently pheromone production, is unknown. In this study, we fed wild caught D. valens with 3 different pH media (main host diet with natural pH of 4.7; a mildly acidic diet with pH 6 mimicking the beetle gut pH; and highly acidic diet with pH 4), and measured their effects on the gut pH, bacterial community and production of the main aggregation and anti-aggregation pheromone (verbenone). We further tested the verbenone production capacity of 2 gut bacterial isolates in different pH environments (pH 6 and 4). Compared to natural state or main host diet, feeding on less acidic diet (pH 6) diluted the acidity of the gut, whereas feeding on highly acidic diet (pH 4) enhanced it. Both changes in gut pH reduced the abundance of dominant bacterial genera, resulting in decreased verbenone production. Similarly, the highest pheromone conversion rate of the bacterial isolates was observed in pH mimicking the acidity in beetle gut. Taken together, these results indicate that changes in gut pH can affect gut microbiota composition and pheromone production, and may therefore have the potential to affect host colonization behavior.