Make Tech Easier published a tutorial about how to prevent Mac from sleeping when you close the lid.
How to Prevent Mac From Sleeping When You Close the Lid
Want to prevent your Mac from sleeping even if the lid is closed? You can use this free "keep-awake" application to prevent Mac from sleeping.
How to Prevent Mac From Sleeping When You Close the Lid