SexyMF SexyMF - 22 days ago 10
PHP Question

Create a new virtual host via php fails on Permission denied

I have 2 problems:


  1. when I run:

    file_put_contents("/etc/apache2/sites-enabled/www.mydomain.com.conf",$vh);


    I get the error:
    failed to open stream: Permission denied

  2. How do I safely
    reload
    apache (after adding a domain) from PHP.
    I don't think
    $res = shell_exec("sudo service apache2 reload")
    is working, I dont get anything in return.


Answer

Its a bad practice, why don't you go and create a default virtual host that will answer to any domain.
In the app level you can do some protections about it. This way you are shortening your code to the minimum.
He is an example (dont use it as it is, needs to be tested):

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    <Directory /var/www/the_site>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Comments