In this work, the parasitic conductive channel in a silicon substrate is compensated using in-situ doping for the first time. The experimental results show that in-situ doping of tert-butylphosphorus (TBP) has little adverse effect on the mobility and the density of two-dimensional electron gas but the compensation of the parasitic channel in the substrate is effective, and the aluminum nitride (AlN) / Si template based on the optimized conditions reduces the radio frequency loss by more than 60%. In addition, it was found that a suitable preflow of TBP can enhance the migration of Al atoms on the substrate surface and improve the crystal quality of gallium nitride (GaN), which reduces the dislocation density of GaN by 25% compared to the sample without TBP preflow. The hypothesis is that TBP preflow can affect the strain of the substrate, which makes the dislocations more likely to annihilate during the subsequent growth of AlN nucleation layer, resulting in the eventual improvement of GaN crystal quality.