How long does my passport need to be valid for? 

 If you don't need a visa to enter Morocco your passport only needs to be valid for the duration of your visit.   No additional period of validity beyond this is required.   

When you enter the country your passport is stamped with the date of entry plus a police control number, and you can stay in Morocco for up to 90 days.  Beyond that you need a residence permit issued by the local police department.  Or... exit the country to Ceuta or elsewhere, and re-enter for another 90 days.

If your passport is valid for less than 90 days you may be questioned on your plans for leaving the country and a valid airline flight ticket will help in this respect.

Who does this affect (i.e. who doesn't need a visa):  Citizens of the following countries - all European Economic Area countries which comprises all 28 EU countries plus Iceland, Liechtenstein, Norway, Switzerland.  

In addition, Australia, New Zealand, Canada, Hong Kong, Malta, USA, Japan, Russian Federation, Mali, Senegal, Algeria, Tunisia, Turkey, Saudi Arabia, Kuwait, Oman, Qatar, Bahrain, UAE, Turkey, Congo, Niger, Gabon, Guinea, Ivory Coast, Mexico, Argentina, Brazil, Chile, Peru, Venezuela, Indonesia, Philippines, Puerto Rico, South Korea.   Plus Singapore (limited to one month).

Citizens of any other country need a visa to enter Morocco and at least six months left on the passport.   The fact that you might be living in a country with some right of residence doesn't make you a citizen of that country, your citizenship is based on the passport that you hold.

Obtaining a visa for Morocco

If you are not a citizen of one of the countries listed above you need a visa to enter Morocco.  If you are living in the UK you could apply to the Moroccan Consultate in London, otherwise apply at the nearest consulate.  It is essential that you provide every piece of information requested in the application form, otherwise the application will be rejected.  You are advised not to make firm travel plans (flight and hotel bookings) before obtaining your visa. There are several threads running on the forum about obtaining a visa, you can use the search facility to locate these.  This is one thread about applying at the London Consulate.