Static port and Bernoulli
When aircraft is flying, there will be air flowing outside of static port.


No, that is not true if the aircraft flies without sideslip and at constant altitude (which it should do most of the time). Instead, there will be a pressure equilibrium between the air in the lines leading to the static port and the static pressure outside. The position of static ports is carefully chosen so the airflow around them is neither accelerated (which would lower local static pressure) nor retarded (which would increase local static pressure). Also, static ports are on both sides of the fuselage so the ram effect of the windward static port in a sideslip is equalised by the suction on the leeward static port. Note that in this case there is indeed some air flowing from the windward to the leeward side through the static line connecting both.

Now to the Bernoulli effect. It says in essence that the sum of static and dynamic pressure is constant (if we disregard the height term, which is not needed in this context). Since you want to measure the same pressure that a stationary observer would measure, you need to pick a place along the airframe where the dynamic part of the pressure is zero because all flow is tangential to the port opening. Small errors can be corrected by calibrating measurements in flight test.