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;
}
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
add a comment |
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
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
add a comment |
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
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
16.04 permissions apache2 php
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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 thesudo nano
by agksu gedit
.
Taken from: https://stackoverflow.com/a/23175981
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
add a comment |
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.
@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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 thesudo nano
by agksu gedit
.
Taken from: https://stackoverflow.com/a/23175981
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
add a comment |
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 thesudo nano
by agksu gedit
.
Taken from: https://stackoverflow.com/a/23175981
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
add a comment |
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 thesudo nano
by agksu gedit
.
Taken from: https://stackoverflow.com/a/23175981
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 thesudo nano
by agksu gedit
.
Taken from: https://stackoverflow.com/a/23175981
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
add a comment |
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
add a comment |
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.
@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
add a comment |
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.
@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
add a comment |
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.
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.
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
add a comment |
@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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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