Ruling on building a mosque close to a non-Muslim graveyard
Question

                Are mulsims allowed to build or purchase a mosque next to non muslim graveyard?.
Praise be to Allah.
    Firstly: 

    One of the rulings of sharia which is aimed at cutting off 
    all possible fitnah, blocking all means that may lead to shirk and closing 
    the door to exaggeration is the prohibition on taking graves as places of 
    worship, or praying facing towards them, or taking graveyards as a place of 
    prayer. 

    There is no differentiation in that between graves of Muslims 
    or graves of mushrikeen; in fact with regard to the graves of Muslims and 
    righteous people, it is more important to keep away from them, because shirk 
    and fitnah are more likely concerning them. 

    Al-Bukhaari (436) and Muslim (531) narrated that 'Aa'ishah 
    and ‘Abd-Allaah ibn ‘Abbaas (may Allaah be pleased with him) said: When the 
    Messenger of Allaah (peace and blessings of Allaah be upon him) was dying, 
    he drew the blanket over his face, then when the pains of death grew too 
    intense, he uncovered his face and said: “May Allaah curse the Jews and the 
    Christians; they took the graves of their Prophets as places of worship,” 
    warning against doing what they had done. 

    And it was narrated that Abu Sa’eed al-Khudri (may Allaah be 
    pleased with him) said: The Messenger of Allaah (blessings and peace of 
    Allaah be upon him) said: “All the Earth is a mosque except graveyards and 
    washrooms.” 

    Narrated by Imam Ahmad (11379), Abu Dawood (492) and 
    al-Tirmidhi (317). Classed as saheeh by al-Haakim, and al-Dhahabi agreed 
    with him. Shaykh al-Islam Ibn Taymiyah classed its isnaad as jayyid, but 
    al-Daraqutni and al-Tirmidhi thought it most likely to be mursal. 

    And there are many well-known hadeeths about this matter. 

    Ibn al-Qayyim (may Allaah have mercy on him) said concerning 
    the lessons learned from the battle of Tabook and what is mentioned in it of 
    Masjid al-Diraar, which Allaah forbade His Prophet (blessings and peace of 
    Allaah be upon him) from praying in: 

    [One of the lessons we learn]: is that a waqf is not valid if 
    it involves something other than an act of righteousness or of worship, just 
    as the waqf for this mosque [meaning Masjid al-Diraar] was not valid. Based 
    on this, a mosque is to be demolished if it is built over a grave, and the 
    deceased is to be exhumed if he is buried inside a mosque. This was stated 
    by Imam Ahmad and others. 

    A mosque and a grave cannot be combined in the religion of 
    Islam, rather whichever one of them comes after the other should be 
    prevented; the ruling favours the one that was there first. If they are 
    built at the same time, that is not permissible, and this waqf is not valid 
    and is not permissible, and it is not valid to pray in this mosque, because 
    the Messenger of Allaah (blessings and peace of Allaah be upon him) forbade 
    that, and cursed those who take graves as mosques or who set up lamps over 
    them. 

    This is the religion of Islam with which Allaah sent His 
    Messenger and Prophet, but it has become as a stranger among the people, as 
    you see! End quote.

    Zaad al-Ma’aad fi Huda Khayr il-‘Abaad 
    (3/572) 

    But if the graves or graveyard are close to the place where 
    it is intended to build a mosque, there is nothing wrong with building a 
    mosque, subject to 3 conditions: 

    1.That the mosque should not be 
    built with the aim of venerating those graves or seeking blessing from them.

    2.That the graves should not be 
    in the direction of the qiblah of the mosque, because of the hadeeth of Abu 
    Marthad al-Ghanawi (may Allaah be pleased with him), according to which the 
    Messenger of Allaah (blessings and peace of Allaah be upon him) said: “Do 
    not sit on graves and do not pray towards them.” narrated by Muslim, number 
    972.

    3.The mosque should be separated 
    from them in a clear manner, so that these graves will not form any part of 
    the halls or courtyard of the mosque, and so that it will clearly appear to 
    the onlooker that the mosque is completely separate from the graves, such as 
    a path, a road, a large open space or the like.

    Abu Bakr al-Athram said: I heard Abu ‘Abd-Allaah – meaning 
    Ahmad ibn Hanbal (may Allaah have mercy on him) – when he was asked about 
    praying in graveyards. He disapproved of prayer in graveyards. It was said 
    to him: The mosque is among the graves, can I pray in it? And he disapproved 
    of that. It was said to him: It is a mosque and between it and the graves 
    there is a barrier. He disapproved of offering obligatory prayers therein, 
    but he granted a concession with regard to offering funeral prayers there, 
    and he mentioned the hadeeth of Abu Marthad al-Ghanawi (may Allaah be 
    pleased with him), according to which the Prophet (blessings and peace of 
    Allaah be upon him) said: “Do not pray towards graves.” He said: Its isnaad 
    is jayyid. End quote. Fath al-Baari by Ibn Rajab, 2/398. 

    See also the answers to question number
    7875 and
    13490. 

    Conclusion: 

    There is no reason why a mosque cannot be built in an area 
    close to the graveyards of mushrikeen -- or of Muslims -- on condition that 
    the mosque is not within the boundary of the graveyard, rather it should be 
    clearly separated from it by a path or the like. 

    But if it is possible for you to find another place, far away 
    from the graveyard, that is better and is more on the safe side lest any 
    expansion of the graveyard, with the passage of time, reach the boundaries 
    of the mosque. 

    And Allaah knows best.