1

I have an dedicated server where i have to create multiple virtual hosts.So for this i have managed to create .conf file in /apache2/sites-available folder using php,but next step is to add that virtual host entry in host file which is at /etc/hosts.

I can do this using Linux command by editing hosts file in nano editor,But i want to do this using php.

I found this command which add entry in host file using terminal which i will execute via shell_exec() programmatically,

sudo echo "192.168.xx.xx example.com" >> /etc/hosts

but this is not working,it throws permission denied error.

So,can i edit host file dynamically using php or can i append a line into it?
CyberAbhay
  • 494
  • 6
  • 17
  • 1
    Allowing `shell_exec` is a very bad thing for your system as it may get hacked. – Mostafa Hussein May 22 '17 at 04:45
  • Why do you need something in the hosts file? And allowing something to be written to web server or system configuration on a server open to public is quite bad idea – Sami Kuhmonen May 22 '17 at 04:47
  • yes i know so i am not using it anymore.used to create .conf file in /xyz directory and them move using cron. So want to do same for hosts.create .txt with host entries and cron will move this .txt contains to hosts ie,host entries – CyberAbhay May 22 '17 at 04:48
  • the permission error because the webserver which is executing the script probably using nobody or httpd/www-data user which has no write access to `/etc/hosts` thats the main problem. also is the user of your web application added to sudoers ? as it might be another issue. – Mostafa Hussein May 22 '17 at 04:52
  • @Mostafa Hussein i fire command using user who have sudo access – CyberAbhay May 22 '17 at 04:55
  • does /etc/hosts allowing write access from others ? for example on my laptop i am using Fedora where /etc/hosts is not writable by my account and i needed to change its permission in order to modify it without sudo. check [this answer](http://stackoverflow.com/a/18241395/2336650) – Mostafa Hussein May 22 '17 at 04:59
  • no .it accessible to root only. Now my above command works if i su to root.But now i set this command in cron where ip in it will fix but domain name will be dynamic(can i use text to save domain and then cron will copy domain name from txt each time) – CyberAbhay May 22 '17 at 05:02

0 Answers0