I have created a web app in python3 which is hosted on an apache2 web server using the WSGI module. Now one of the requirements of the web application is that it needs to restart a systemd service based on some user inputs. Now I know there is os library in python that can help me but as my web application is running as user www-data, the commands are executed by www-data which is not allowed to start or stop systemd services. After some research, I found we can accomplish that by editing sudoers file but I am still not sure if www-data should be given sudoers permission or not.
What would be the best way to accomplish this? and what security measures will I have to keep in mind as this would go on a live production environment.
Service which needs to be controlled is a custom made service:
sudo systemctl restart kodi.service
The platform is Ubuntu 18.04 and the web server used is apache2.