You can purchase your tickets at the Visitor centre entry, alongside your Petra entry ticket, noting that it's a separate entry fee and ticket. You may also be able to purchase your Petra by Night tickets from your hotel concierge and there are local tour agencies permitted to sell the tickets in Wadi Musa.