I am writing a web frontend for configuring OpenWRT. I'm using PHP to execute commands and all the uci commands are executing without issues. However, OpenVPN configurations cannot be done via UCI and I have to use direct Unix commands on its config file.

These commands run fine in my terminal, but not in PHP:

shell_exec(" cd /etc/openvpn && sed -i '2s/.*/remote 5-nl.cg-dialup.net 443/' openvpn.ovpn && cd'");
shell_exec(" cd /etc/openvpn && sed -i '30s/.*/ca /etc/openvpn/Nethelands/ca.crt/' openvpn.ovpn && cd'");
shell_exec(" cd /etc/openvpn && sed -i '32s/.*/cert /etc/openvpn/Nethelands/client.crt/' openvpn.ovpn && cd'");
shell_exec(" cd /etc/openvpn && sed -i '34s/.*/key /etc/openvpn/Nethelands/client.key/' openvpn.ovpn && cd'");

How can I get the tasks above done in PHP? Do I need to write a shell script, or do I need to change the uhttpd permissions?

I am running php on top of uhttpd web server in Openwrt Chaos calmer. with raspberry pi2

  • "those commands ... execute fine in terminal but not inside the php"  Can you be more precise?  Are they executing at all?  Are you getting any error messages?  Try shell_exec(" date > /tmp/charith && cd /etc/openvpn && echo 'Kilroy was here' > wall").  That will tell you whether the commands are running, and as what user, and whether that user has permission to write in /etc/openvpn.  If that looks good, try "sed -i '…' openvpn.ovpn > /tmp/charith.log 2>&1.  … … … … … … … … … …  P.S.  I'm 99.44% sure that you don't need the && cd at the end. Oct 25, 2015 at 21:21
  • Your sed command won't work too. Use '34s@.*@key /etc/openvpn/Nethelands/client.key@' and alike. P.S. I am 96.2% sure you can replace those four sed` invocations using sed -i.bak -e '2s@.*@remote 5-nl.cg-dialup.net 443@' -e '30s@.*@ca /etc/openvpn/Nethelands/ca.crt@' -e '32s@.*@cert /etc/openvpn/Nethelands/client.crt@' -e '34s@.*@key /etc/openvpn/Nethelands/client.key@' openvpn.ovpn. Oct 25, 2015 at 22:05
  • 1
    Ah, sed BLAH BLAH /etc/openvpn/openvpn.ovpn actually. You don't even need the initial cd. Now it's only a matter of permissions. Oct 25, 2015 at 22:14
  • Thanks all for helping me, My sed commands are wrong and uhttpd doesn't have permissions to edit the files in /etc/openvpn. I fixed it with Arthur2e5 answer.
    – sameera
    Oct 26, 2015 at 4:25

1 Answer 1


Fast and crude guess:

  • Permission denied. Files in /etc are often not changeable by ordinary users, especially web users like your httpd. Run chmod -R +w /etc/openvpn to open up a hole for that, or do some chown thing.
  • sed command invalid. In commands number 2-4, multiple slashes appeared. Since s is followed by a slash, slashes are used as operand separator and sed won't work with so many invalid operands. Change your sed thing to this:

    <?php /* For Syntax */ // cd avoided.
    shell_exec("sed -i.php_sed_bak ".
        "-e '2s@.*@remote 5-nl.cg-dialup.net 443@' ".
        "-e '30s@.*@ca /etc/openvpn/Nethelands/ca.crt@' ".
        "-e '32s@.*@cert /etc/openvpn/Nethelands/client.crt@' ".
        "-e '34s@.*@key /etc/openvpn/Nethelands/client.key@' ".
        "/etc/openvpn/openvpn.ovpn"); ?>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .