How to make OpenVPN autostart on Ubuntu Xenial (16.04)

I was used to OpenVPN on Ubuntu for a long time. However since the last upgrade from Trusty, OpenVPN didn't start at all and it had to be hand-started, so I had to find a solution (apparently no environment variables were passed to /etc/network/if.up.d/openvpn script, so it did not start).

1. Nuke OpenVPN autostart

Assuming your configuration file is named server.conf, I resorted to:
# sudo nano /etc/default/openvpn
and commented any line with AUTOSTART= in it with a leading # just leaving:

2. Replace your openvpn startup script

Then I replaced the openvpn script at /etc/network/if.up.d/openvpn with.



if [ ! -x $OPENVPN ]; then
exit 0

$SYSTEMCTL start openvpn@$MYOPENVPN.service

exit 0

MYOPNVPN is the name of your configuration file, stored in /etc/openvpn, and ommiting the .conf extension. Replace it by your own name
If you do it from scratch don't forget to:
# sudo chmod +x /etc/network/if.up.d/openvpn

Before you proceed it may be advisable to make a copy of your present /etc/network/if.up.d/openvpn script, maybe with:
# sudo cp /etc/network/if.up.d/openvpn /etc/network/if.up.d/openvpn.old

3. Reboot, it should work

I have other solutions for the client side, so as not to start unless remotely, using network configuration

Installation of the above is entirely on your risk.