Long range (LoRa) is a wireless communication standard specifically targeted for resource-constrained Internet of Things (IoT) devices. LoRa is a promising solution for smart city applications as it can provide long-range connectivity with a low energy consumption. The number of LoRa-based networks is growing due to its operation in the unlicensed radio bands and the ease of network deployments. However, the scalability of such networks suffers as the number of deployed devices increases. In particular, the network performance drops due to increased contention and interference in the unlicensed LoRa radio bands. This results in an increased number of dropped messages and, therefore, unreliable network communications. Nevertheless, network performance can be improved by appropriately configuring the radio parameters of each node. To this end, in this article we formulate integer linear programming models to configure LoRa nodes with the optimal parameters that allow all devices to reliably send data with a low energy consumption. We evaluate the performance of our solutions through extensive network simulations considering different types of realistic deployments. We find that our solution consistently achieves a higher delivery ratio (up to 8% higher) than the state of the art with minimal energy consumption. Moreover, the higher delivery ratio is achieved by a large percentage of nodes in each network, thereby resulting in a fair allocation of radio resources. Finally, the optimal network configurations are obtained within a short time, usually much faster than the state of the art. Thus, our solution can be readily used by network operators to determine optimal configurations for their IoT deployments, resulting in improved network reliability.