Change localhost directory for Yosemite Apache 2.4
Asked Answered
F

2

31

I have clean installed Mac OSX Yosemite but I cant configure the Apache 2.4 like I have always done on older OSX versions.

Here is what I want to do : set the "localhost" directory to "/Users/username/Public/". But, everything I tried won't work, I always get a "Forbiden, can't access to /" or I get the default "It works!" page...

How to simply reroot my localhost ?

Thx

EDIT (thanks to Krister Andersson for the answer)

For Mac OSX 10.10 Yosemite

I also post the changes I had to do to keep things running.

In "/etc/apache2/users/", I created a file named by my username like this "myUsername.conf".

You can get your username by typing "id" in terminal. You should find your username at start in "uid=501(myUsername)".

In this new "myUsername.conf" file, just copy past this:

<Directory "/Users/myUsername/Sites/">
    AllowOverride All
    Options Indexes MultiViews
    Options +FollowSymLinks
    Require all granted
</Directory>

Dont forgive to change the myUsername value.

Then, in the "/etc/apache2/httpd.conf" file, uncomment all these two lines:

167 #LoadModule userdir_module libexec/apache2/mod_userdir.so
169 #LoadModule php5_module libexec/apache2/libphp5.so

Line 236, change the directory of "DocumentRoot" to whatever you want. Line 250, set "Options" to "Options "Options Indexes FollowSymLinks Multiviews". Line 258, set "AllowOverride None" to "AllowOverride All". Line 263, set "Require all denied" to "Require all granted"

In Terminal, restart apache by typing "sudo apachectl restart".

It work's for me on Mac OS X 10.10 Yosemite clean install.

Forbid answered 21/10, 2014 at 12:30 Comment(5)
possible duplicate of Apache localhost/~username/ not workingCommunal
No, it's not a duplicate.Forbid
While I appreciate this has been answered, future readers may instead want to look into configuring Apache Virtual Hosts on Mac OS X.Flak
It also work on OS X El Capitan and SierraForbid
And High Sierra nowForbid
S
43

I've just installed Yosemite and I managed to change the DocumentRoot without any problems. First I modified the following lines in /private/etc/apache2/httpd.conf:

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
Options FollowSymLinks Multiviews

AllowOverride None
</Directory>

to:

DocumentRoot "<CUSTOM_PATH>"
<Directory "<CUSTOM_PATH>">
Options Indexes FollowSymLinks Multiviews

AllowOverride All
</Directory>

The above will set a custom DocumentRoot, enable directory listing and allow configurations to be overridden by .htaccess files.

Then I restarted apache by executing sudo apachectl restart.

Another approach would be to set up a virtual host. First make sure so that the following line is uncommented in your /private/etc/apache2/httpd.conf file:

# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf

Then you can add the following in the httpd-vhosts.conf file:

<VirtualHost *:80>
   ServerAdmin [email protected]
   DocumentRoot "/Library/WebServer/Documents"
   ServerName example.local
   ErrorLog "/private/var/log/apache2/example.local-error_log"
   CustomLog "/private/var/log/apache2/example.local-access_log" common

   <Directory "/Library/WebServer/Documents">
     Options Indexes FollowSymLinks Multiviews
     AllowOverride All
     Order allow,deny
     Allow from all
   </Directory>
</VirtualHost>

The above will setup a document root for a new virtual host named example.local and enable directory listing and allow configurations to be overridden by .htaccess files. Of course your also will need to restart apache for the changes to take effect:

sudo apachectl restart
Shoa answered 21/10, 2014 at 15:36 Comment(9)
Nice ! It works for me too. I'm bad in 'server configuration'. Thanks a lot, I also answer my question to post my configuration (I add some changes from other sources). Bonus: Do you know how to set localhost to root "/" only available for 127.0.0.1 and the 127.0.0.1 requested from outside redirected to another folder ?Forbid
@Forbid - I might be able to help, but since this seems to be a different question I think you first should create a new one and you should also try to add more clear information about what you're trying to do.Shoa
This isn't working for me anymore under Apache 2.4. It doesn't seem to be honoring DocumentRoot for some reason.Dairyman
@Dairyman - Have you restarted apache? And are you sure that you are editing the correct configuration file?Shoa
I'm using the config file indicated by httpd -V: /private/etc/apache2/httpd.conf, and yes, I've done apachectl restart, and stop and start, etc.Dairyman
Also I've enabled server-info, and looking at that, and navigating to configuration files, shows my changed DocumentRoot setting - but I'm still getting the "It works" file, not my index.htmlDairyman
Very strange, could it be that you are running two different apache servers on the same machine?Shoa
I don't see anything else that looks like an apache server process in the process list, and /localhost/server-info shows me a config that /localhost/index.html doesn't seem to reflectDairyman
Hmm, well, nix my comments. It's working now. I noticed the access log was showing a 304 for index.html, so I dumped my browser cache - I was looking at a cached version of index.html. A previous iteration of my config was giving me 404's for my other files, so I assumed the problem was with DocumentRoot, but apparently that wasn't it, and the current iteration (which includes permissions changes, etc.) has fixed the problem.Dairyman
G
10

On El Capitan you should restart apache with "-k" flag: sudo apachectl -k restart

Grub answered 3/11, 2015 at 18:54 Comment(1)
This solved my problem with root directory not updating :)Ardell

© 2022 - 2024 — McMap. All rights reserved.