Online defamation was already a crime, but the new law makes specific provisions for the president.
Offenders using fake accounts could face a 1,500 manats (Â£676; $855) fine and up to three years in jail for insults against Ilham Aliyev.
Azerbaijan's traditional media is strictly controlled by the government.
Social media had been a way for citizens to vent their frustration with the increasingly authoritarian government and failing economy - that is now complicated by the new law.
Online insults against Mr Aliyev that trace back to an authentic account will get a more lenient two year sentence and pay a smaller fine.
The BBC's South Caucasus correspondent, Rayhan Demytrie, says what constitutes an insult will be decided by domestic courts, which are not independent according to human rights groups.
In the past few years, dozens of journalists, youth and pro-democracy activists have found themselves behind bars for being critical of the country's leadership.
Around the world, several other countries have imposed harsh controls on the internet and social media, barring their citizens from criticising government officials.
In Thailand, strict lese majeste laws protect the royal family from insult or threat. Those who defame or insult the king, queen or heirs face up to 15 years in prison.
Turkey has repeatedly blocked social media in the run-up to elections and jailed journalists for critical coverage of the government. Russia recently blocked the social network LinkedIn and has jailed internet activists who have posted material critical of the government, Ukraine or Crimea, the peninsula it annexed in 2014.
On Thursday, The Gambia banned the internet and international phone calls during its presidential elections.
Azerbaijan's parliament has approved a new law which makes insulting the country's president on social media a criminal offence.