Having the default Apache document root to /etc/www
can be sometimes annoying because of permissions. I strongly recommend to use a folder into your home
folder.
For the sake of this post, I will use the folder Sites
(that I created under my personal folder /home/shprink/Sites/
).
PS: The Apache version used for this post is: 2.4.10 (Ubuntu), you can see yours using apache2 -v
command.
Changing apache2 document root
The default document root is set in the 000-default.conf
file that is under /etc/apache2/sites-available
folder.
$ cd /etc/apache2/sites-available
$ sudo nano 000-default.conf
While the file is opened change DocumentRoot /var/www/
with your new folder e.g DocumentRoot /home/shprink/Sites/
Set the right Apache configuration
The configuration of the /etc/www
folder is under /etc/apache2/apache2.conf
. Edit this file to add the configuration of your new document root.
$ sudo nano /etc/apache2/apache2.conf
Copy the following:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
and change the directory path:
<Directory /home/shprink/Sites/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Restart Apache
$ sudo service apache2 restart
Set the right permission
All of your document root parent folders must be executable by everyone. To know if it is the case you can use the utility namei
that list the permissions along each component of the path:
$ namei -m /home/shprink/Sites/
f: /home/shprink/Sites/
drwxr-xr-x /
drwxr-xr-x home
drwx------ shprink
drwx------ Sites
Here as you can see that shprink
and Sites
permissions are not set properly.
Open http://localhost/
in your browser, you should get the following message:
Forbidden You don’t have permission to access / on this server.
Open the apache error log to see the exact error code e.g AH00035
. It might help you to get more information.
$ sudo tail -f /var/log/apache2/error.log
[Mon Apr 06 09:04:26.518260 2015] [core:error] [pid 22139] (13)Permission denied: [client 127.0.0.1:45121] AH00035: access to / denied (filesystem path '/home/shprink/Sites') because search permissions are missing on a component of the path
To fix the permission problem for good, using chmod +755
should be enough.
$ chmod +755 /home/shprink/
$ chmod +755 /home/shprink/Sites/
Re run namei
to make sure everything is ok.
$ namei -m ~/Sites/
f: /home/shprink/Sites/
drwxr-xr-x /
drwxr-xr-x home
drwxr-xr-x shprink
drwxr-xr-x Sites
Now opening http://localhost/
should work as expected. If you are having trouble please leave a comment.