$_SERVER['DOCUMENT_ROOT'] directs to /var/www not ~/public_html

Hi all,

Exactly like my title says.
I am learning PHP and MySQL and I used to use /var/www/ to host (contain or store) my files (.htm/.php) for testing. I could configure, finally, apache2 to use ~/public_html instead.

Now I when I tried to use $_SERVER['Document_ROOT'] it still directs (I used echo to show its path) to /var/www and not the expected ~/public_html.
My question is: how to make variable ($_SERVER['Document_ROOT']) directs to ~/public_html and not the default /var/www. I found this but I did not understand it. I am totally new to servers/networks.

I use Ubuntu 12.04 on a Dell Desktop.

Thank you in advance,

~faizlo

$_SERVER['DOCUMENT_ROOT'] is a PHP global variable for the entire PHP (and web server) configuration set in the the core web server config files (for example the global Apache2 conf file). You can change it there if you have root access.

Sounds like you are on a shared server? Yes? No?

1 Like

Hi Neo

The box I use is my own box at home. I am using it to learn web programming with PHP and MySQL, so I can do anything I want with it - no problem.
That said, I could configure apache2 to use ~/public_html via commenting few lines in /etc/apache2/mods_available/php5.conf. The instructions were in the file itself.

One other thing is that my httpd.conf file (in /etc/apache2/) has nothing in it, it's empty (of size 0.)

I hope these info can help you figure out how to use ~/public_html/

You can use the Apache2 DocumentRoot directive to set the document root.

Perhaps you should search on line and find a basic working example of setting up your Apache2 config files?

Most Linux distributions have easy step-by-step LAMP set up instructions. Which Linux distribution are you using?

1 Like

I use UBUNTU 12.04 LTS on a Dell Machine (Core i5, 8GB RAM, NVIDIA Graphics.)
Where can I change this $_SERVER["DOCUMENT_ROOT"] directive?

As said by Neo, you have to change DocumentRoot directive

  1. Copy default configuration file safety point of view $ sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mynewsite

  2. Edit $ vi /etc/apache2/sites-available/mynewsite

  3. Change DocumentRoot /var/www to DocumentRoot /your/new/path/to/site

  4. Replace <Directory /var/www/> with <Directory /your/new/path/to/site/>

  5. Deactivate old and activate new site $ sudo a2dissite default && sudo a2ensite mynewsite

  6. Restart Apache $ sudo service apache2 restart

2 Likes

Akshay_Hedge

Thank you so much. That did the trick.
Now, if I can take the chance, would you please explain me what I (we) just did?

Thanks again to all of you who replied

~faizlo

I think Akshay's well thought out reply is explanatory.