Why can't Apache2 look outside /var/www/html/ for a PHP file?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I have just started to learn about the whole deal of PHP, MySql, and it's connections to front-end development. While doing this I found that I cannot execute a PHP file unless and until it is inside the /var/www/html directory. I have found out about this solution from multiple sources including askubuntu, but I could never find the reason as to why this is so. So does this mean that a PHP file always has to be inside this specific directory? Can I do anything to make a PHP file inside say, my Documents folder to run successfully instead of being "Save"-ad by Mozilla?
Please help.










share|improve this question























  • I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

    – Erik
    Mar 31 at 4:44











  • "but I could never find the reason as to why this is so". S e c u r i t y.

    – Rinzwind
    Mar 31 at 9:07


















0















I have just started to learn about the whole deal of PHP, MySql, and it's connections to front-end development. While doing this I found that I cannot execute a PHP file unless and until it is inside the /var/www/html directory. I have found out about this solution from multiple sources including askubuntu, but I could never find the reason as to why this is so. So does this mean that a PHP file always has to be inside this specific directory? Can I do anything to make a PHP file inside say, my Documents folder to run successfully instead of being "Save"-ad by Mozilla?
Please help.










share|improve this question























  • I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

    – Erik
    Mar 31 at 4:44











  • "but I could never find the reason as to why this is so". S e c u r i t y.

    – Rinzwind
    Mar 31 at 9:07














0












0








0








I have just started to learn about the whole deal of PHP, MySql, and it's connections to front-end development. While doing this I found that I cannot execute a PHP file unless and until it is inside the /var/www/html directory. I have found out about this solution from multiple sources including askubuntu, but I could never find the reason as to why this is so. So does this mean that a PHP file always has to be inside this specific directory? Can I do anything to make a PHP file inside say, my Documents folder to run successfully instead of being "Save"-ad by Mozilla?
Please help.










share|improve this question














I have just started to learn about the whole deal of PHP, MySql, and it's connections to front-end development. While doing this I found that I cannot execute a PHP file unless and until it is inside the /var/www/html directory. I have found out about this solution from multiple sources including askubuntu, but I could never find the reason as to why this is so. So does this mean that a PHP file always has to be inside this specific directory? Can I do anything to make a PHP file inside say, my Documents folder to run successfully instead of being "Save"-ad by Mozilla?
Please help.







16.04 permissions apache2 php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 31 at 4:12









The ViperThe Viper

33




33













  • I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

    – Erik
    Mar 31 at 4:44











  • "but I could never find the reason as to why this is so". S e c u r i t y.

    – Rinzwind
    Mar 31 at 9:07



















  • I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

    – Erik
    Mar 31 at 4:44











  • "but I could never find the reason as to why this is so". S e c u r i t y.

    – Rinzwind
    Mar 31 at 9:07

















I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

– Erik
Mar 31 at 4:44





I'm not an expert on the issue, but apache will NEVER look outside of the document root, which is specified in the config files.

– Erik
Mar 31 at 4:44













"but I could never find the reason as to why this is so". S e c u r i t y.

– Rinzwind
Mar 31 at 9:07





"but I could never find the reason as to why this is so". S e c u r i t y.

– Rinzwind
Mar 31 at 9:07










2 Answers
2






active

oldest

votes


















2














Because it is not configured to do so, by default Apache will only serve php file inside /var/www/html directory.



You can set Apache to look content anywhere within your system, in order to achieve such thing you need to change Apache setting, they are mostly located on /etc/apache2.




Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.



In my Ubuntu 14.04 LTS, the document root was set to /var/www/html.
It was configured in the following file:



/etc/apache2/sites-available/000-default.conf


So just do a



sudo nano /etc/apache2/sites-available/000-default.conf


and change the following line to what you want:



DocumentRoot /var/www/html


Also do a



sudo nano /etc/apache2/apache2.conf


and find this



<Directory /var/www/html/>    Options Indexes FollowSymLinks  AllowOverride None  Require all granted
</Directory>


and change /var/www/html to your preferred directory



and save it.



After you saved your changes, just restart the apache2 webserver and
you'll be done :)



sudo service apache2 restart 




If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.


Taken from: https://stackoverflow.com/a/23175981






share|improve this answer
























  • Works like a dream! Thanks

    – The Viper
    Mar 31 at 6:08











  • @TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

    – Emmet
    Mar 31 at 6:12













  • It does not answer the "but I could never find the reason as to why this is so"

    – Rinzwind
    Mar 31 at 9:08



















1














The reason php files not possible to open outside of document root is security. Web server always limited to some folder and its subfolders.



You can setup apache to look at few different folders, depending on url, like http://localhost or http://someother.localhost pointing to different folders.

To do so, first you need to edit 000-defailt.conf, the change is:



ServerName  localhost
ServerAlias localhost


Then make copy of 000-default.conf to 001-someother.conf and edit it like following:



<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/leonid/Web/SomeotherRoot
ServerName someother.localhost
ServerAlias someother.localhost
<Directory "/home/leonid/Web/SomeotherRoot">
AllowOverride All
Require local
# ^ this will limit connections to only local
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
LogLevel error
</VirtualHost>


After that you need to make someother.localhost resolve to server ip, edit /etc/hosts:



127.0.0.1   localhost   *.localhost
...other lines


Finally you need to enable new configuration file by:



sudo a2ensite 001-someother.conf
sudo systemctl reload apache2


Note: Example above is only for local site, accessible only locally.

I wrote script what creates configurations like this in a few clicks, but better to understood how its done before using script. Github.






share|improve this answer


























  • @Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

    – LeonidMew
    Mar 31 at 9:16











  • +1 from me. Security is almost always the answer to how apache is set up ;)

    – Rinzwind
    Mar 31 at 9:23












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130051%2fwhy-cant-apache2-look-outside-var-www-html-for-a-php-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Because it is not configured to do so, by default Apache will only serve php file inside /var/www/html directory.



You can set Apache to look content anywhere within your system, in order to achieve such thing you need to change Apache setting, they are mostly located on /etc/apache2.




Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.



In my Ubuntu 14.04 LTS, the document root was set to /var/www/html.
It was configured in the following file:



/etc/apache2/sites-available/000-default.conf


So just do a



sudo nano /etc/apache2/sites-available/000-default.conf


and change the following line to what you want:



DocumentRoot /var/www/html


Also do a



sudo nano /etc/apache2/apache2.conf


and find this



<Directory /var/www/html/>    Options Indexes FollowSymLinks  AllowOverride None  Require all granted
</Directory>


and change /var/www/html to your preferred directory



and save it.



After you saved your changes, just restart the apache2 webserver and
you'll be done :)



sudo service apache2 restart 




If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.


Taken from: https://stackoverflow.com/a/23175981






share|improve this answer
























  • Works like a dream! Thanks

    – The Viper
    Mar 31 at 6:08











  • @TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

    – Emmet
    Mar 31 at 6:12













  • It does not answer the "but I could never find the reason as to why this is so"

    – Rinzwind
    Mar 31 at 9:08
















2














Because it is not configured to do so, by default Apache will only serve php file inside /var/www/html directory.



You can set Apache to look content anywhere within your system, in order to achieve such thing you need to change Apache setting, they are mostly located on /etc/apache2.




Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.



In my Ubuntu 14.04 LTS, the document root was set to /var/www/html.
It was configured in the following file:



/etc/apache2/sites-available/000-default.conf


So just do a



sudo nano /etc/apache2/sites-available/000-default.conf


and change the following line to what you want:



DocumentRoot /var/www/html


Also do a



sudo nano /etc/apache2/apache2.conf


and find this



<Directory /var/www/html/>    Options Indexes FollowSymLinks  AllowOverride None  Require all granted
</Directory>


and change /var/www/html to your preferred directory



and save it.



After you saved your changes, just restart the apache2 webserver and
you'll be done :)



sudo service apache2 restart 




If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.


Taken from: https://stackoverflow.com/a/23175981






share|improve this answer
























  • Works like a dream! Thanks

    – The Viper
    Mar 31 at 6:08











  • @TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

    – Emmet
    Mar 31 at 6:12













  • It does not answer the "but I could never find the reason as to why this is so"

    – Rinzwind
    Mar 31 at 9:08














2












2








2







Because it is not configured to do so, by default Apache will only serve php file inside /var/www/html directory.



You can set Apache to look content anywhere within your system, in order to achieve such thing you need to change Apache setting, they are mostly located on /etc/apache2.




Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.



In my Ubuntu 14.04 LTS, the document root was set to /var/www/html.
It was configured in the following file:



/etc/apache2/sites-available/000-default.conf


So just do a



sudo nano /etc/apache2/sites-available/000-default.conf


and change the following line to what you want:



DocumentRoot /var/www/html


Also do a



sudo nano /etc/apache2/apache2.conf


and find this



<Directory /var/www/html/>    Options Indexes FollowSymLinks  AllowOverride None  Require all granted
</Directory>


and change /var/www/html to your preferred directory



and save it.



After you saved your changes, just restart the apache2 webserver and
you'll be done :)



sudo service apache2 restart 




If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.


Taken from: https://stackoverflow.com/a/23175981






share|improve this answer













Because it is not configured to do so, by default Apache will only serve php file inside /var/www/html directory.



You can set Apache to look content anywhere within your system, in order to achieve such thing you need to change Apache setting, they are mostly located on /etc/apache2.




Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.



In my Ubuntu 14.04 LTS, the document root was set to /var/www/html.
It was configured in the following file:



/etc/apache2/sites-available/000-default.conf


So just do a



sudo nano /etc/apache2/sites-available/000-default.conf


and change the following line to what you want:



DocumentRoot /var/www/html


Also do a



sudo nano /etc/apache2/apache2.conf


and find this



<Directory /var/www/html/>    Options Indexes FollowSymLinks  AllowOverride None  Require all granted
</Directory>


and change /var/www/html to your preferred directory



and save it.



After you saved your changes, just restart the apache2 webserver and
you'll be done :)



sudo service apache2 restart 




If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.


Taken from: https://stackoverflow.com/a/23175981







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 31 at 4:46









EmmetEmmet

8,11822346




8,11822346













  • Works like a dream! Thanks

    – The Viper
    Mar 31 at 6:08











  • @TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

    – Emmet
    Mar 31 at 6:12













  • It does not answer the "but I could never find the reason as to why this is so"

    – Rinzwind
    Mar 31 at 9:08



















  • Works like a dream! Thanks

    – The Viper
    Mar 31 at 6:08











  • @TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

    – Emmet
    Mar 31 at 6:12













  • It does not answer the "but I could never find the reason as to why this is so"

    – Rinzwind
    Mar 31 at 9:08

















Works like a dream! Thanks

– The Viper
Mar 31 at 6:08





Works like a dream! Thanks

– The Viper
Mar 31 at 6:08













@TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

– Emmet
Mar 31 at 6:12







@TheViper Please do accept the answer if you think it's helpful, click the grey ☑ at the left of this text, which means Yes, this answer is valid

– Emmet
Mar 31 at 6:12















It does not answer the "but I could never find the reason as to why this is so"

– Rinzwind
Mar 31 at 9:08





It does not answer the "but I could never find the reason as to why this is so"

– Rinzwind
Mar 31 at 9:08













1














The reason php files not possible to open outside of document root is security. Web server always limited to some folder and its subfolders.



You can setup apache to look at few different folders, depending on url, like http://localhost or http://someother.localhost pointing to different folders.

To do so, first you need to edit 000-defailt.conf, the change is:



ServerName  localhost
ServerAlias localhost


Then make copy of 000-default.conf to 001-someother.conf and edit it like following:



<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/leonid/Web/SomeotherRoot
ServerName someother.localhost
ServerAlias someother.localhost
<Directory "/home/leonid/Web/SomeotherRoot">
AllowOverride All
Require local
# ^ this will limit connections to only local
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
LogLevel error
</VirtualHost>


After that you need to make someother.localhost resolve to server ip, edit /etc/hosts:



127.0.0.1   localhost   *.localhost
...other lines


Finally you need to enable new configuration file by:



sudo a2ensite 001-someother.conf
sudo systemctl reload apache2


Note: Example above is only for local site, accessible only locally.

I wrote script what creates configurations like this in a few clicks, but better to understood how its done before using script. Github.






share|improve this answer


























  • @Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

    – LeonidMew
    Mar 31 at 9:16











  • +1 from me. Security is almost always the answer to how apache is set up ;)

    – Rinzwind
    Mar 31 at 9:23
















1














The reason php files not possible to open outside of document root is security. Web server always limited to some folder and its subfolders.



You can setup apache to look at few different folders, depending on url, like http://localhost or http://someother.localhost pointing to different folders.

To do so, first you need to edit 000-defailt.conf, the change is:



ServerName  localhost
ServerAlias localhost


Then make copy of 000-default.conf to 001-someother.conf and edit it like following:



<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/leonid/Web/SomeotherRoot
ServerName someother.localhost
ServerAlias someother.localhost
<Directory "/home/leonid/Web/SomeotherRoot">
AllowOverride All
Require local
# ^ this will limit connections to only local
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
LogLevel error
</VirtualHost>


After that you need to make someother.localhost resolve to server ip, edit /etc/hosts:



127.0.0.1   localhost   *.localhost
...other lines


Finally you need to enable new configuration file by:



sudo a2ensite 001-someother.conf
sudo systemctl reload apache2


Note: Example above is only for local site, accessible only locally.

I wrote script what creates configurations like this in a few clicks, but better to understood how its done before using script. Github.






share|improve this answer


























  • @Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

    – LeonidMew
    Mar 31 at 9:16











  • +1 from me. Security is almost always the answer to how apache is set up ;)

    – Rinzwind
    Mar 31 at 9:23














1












1








1







The reason php files not possible to open outside of document root is security. Web server always limited to some folder and its subfolders.



You can setup apache to look at few different folders, depending on url, like http://localhost or http://someother.localhost pointing to different folders.

To do so, first you need to edit 000-defailt.conf, the change is:



ServerName  localhost
ServerAlias localhost


Then make copy of 000-default.conf to 001-someother.conf and edit it like following:



<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/leonid/Web/SomeotherRoot
ServerName someother.localhost
ServerAlias someother.localhost
<Directory "/home/leonid/Web/SomeotherRoot">
AllowOverride All
Require local
# ^ this will limit connections to only local
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
LogLevel error
</VirtualHost>


After that you need to make someother.localhost resolve to server ip, edit /etc/hosts:



127.0.0.1   localhost   *.localhost
...other lines


Finally you need to enable new configuration file by:



sudo a2ensite 001-someother.conf
sudo systemctl reload apache2


Note: Example above is only for local site, accessible only locally.

I wrote script what creates configurations like this in a few clicks, but better to understood how its done before using script. Github.






share|improve this answer















The reason php files not possible to open outside of document root is security. Web server always limited to some folder and its subfolders.



You can setup apache to look at few different folders, depending on url, like http://localhost or http://someother.localhost pointing to different folders.

To do so, first you need to edit 000-defailt.conf, the change is:



ServerName  localhost
ServerAlias localhost


Then make copy of 000-default.conf to 001-someother.conf and edit it like following:



<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/leonid/Web/SomeotherRoot
ServerName someother.localhost
ServerAlias someother.localhost
<Directory "/home/leonid/Web/SomeotherRoot">
AllowOverride All
Require local
# ^ this will limit connections to only local
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
LogLevel error
</VirtualHost>


After that you need to make someother.localhost resolve to server ip, edit /etc/hosts:



127.0.0.1   localhost   *.localhost
...other lines


Finally you need to enable new configuration file by:



sudo a2ensite 001-someother.conf
sudo systemctl reload apache2


Note: Example above is only for local site, accessible only locally.

I wrote script what creates configurations like this in a few clicks, but better to understood how its done before using script. Github.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 31 at 9:13

























answered Mar 31 at 7:39









LeonidMewLeonidMew

961623




961623













  • @Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

    – LeonidMew
    Mar 31 at 9:16











  • +1 from me. Security is almost always the answer to how apache is set up ;)

    – Rinzwind
    Mar 31 at 9:23



















  • @Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

    – LeonidMew
    Mar 31 at 9:16











  • +1 from me. Security is almost always the answer to how apache is set up ;)

    – Rinzwind
    Mar 31 at 9:23

















@Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

– LeonidMew
Mar 31 at 9:16





@Rinzwind Good point, I thought its not necessary with explanation how it working. Updated answer.

– LeonidMew
Mar 31 at 9:16













+1 from me. Security is almost always the answer to how apache is set up ;)

– Rinzwind
Mar 31 at 9:23





+1 from me. Security is almost always the answer to how apache is set up ;)

– Rinzwind
Mar 31 at 9:23


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1130051%2fwhy-cant-apache2-look-outside-var-www-html-for-a-php-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

數位音樂下載

When can things happen in Etherscan, such as the picture below?

格利澤436b