The best defensive structure is the repair bay, build these at planets the pirates are attacking and put a capital ship there and the cap will gain a lot of experience for killing the pirates.
Also, repair bays allow a smaller fleet to take on a much larger one. Use them at planets your opponent likes to attack to help put your smaller fleet.
Against the AI you can usually get away with heavily fortifying every planet they like to attack to buy time to build up your economy and fleet, just remember that defenses are a method to delay your opponent and not defeat them, you will need a fleet for any defense to be effective.