26

I had web sharing enabled in Lion and a web site at http://localhost/~user, where user is my User directory. When I upgraded to Mountain Lion, web sharing (Apache) remained enabled and I can go to localhost and get the "It works!" default web page, but I cannot access my user page anymore. The error is

Forbidden

You don't have permission to access /~user/ on this server.

How do I re-enable my user directory's web page?

18

Here is how you can re-enable the web page with the command line using Terminal.

First copy this and paste it into Terminal. You may have to press enter after pasting to run it. It will ask for your password because it is adding a file to your system directory.

USER_DIR=$(basename $(echo ~))
sudo bash -c "cat > /etc/apache2/users/$USER_DIR.conf" <<TEXT
<Directory "/Users/$USER_DIR/Sites">
     Options Indexes MultiViews
     AllowOverride None
     Order allow,deny
     Allow from all
</Directory>
TEXT

Then run this command to restart the web server:

sudo apachectl restart
  • Sorry, my mistake, you need it for the name of the conf file as well. Nevertheless, $(basename ~) should work as well – nohillside Jul 28 '12 at 18:35
  • 1
    I did something slightly different than this. Thanks indiv for the info that helped me create a custom solution for my setup. I added the "Directory" block you have to /etc/apache2/extra/httpd-userdir.conf with a "*" in place of "$USER_DIR". This enables user directories for all users that have a "Sites" directory. – Jason Jan 7 '13 at 15:13
1

I had to add FollowSymLinks as follows to my /etc/apache2/users/username.conf :

<Directory "/Users/username/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>

(username has to be replaced by your real username)

1

Here is a one-line terminal command that will enable macOS's built-in apache server, and allow you to use the Sites directory in your User folder. It is compatible with the latest version of macOS as of this writing (Mojave), and has also been tested to work with Sierra and High Sierra. I suspect it will work with other versions as well—I did my best to write it in a future-proof manner.

mkdir ~/Sites ; sudo bash -c "printf '<Directory \"/Users/`whoami`/Sites/\">\n\tAddLanguage en .en\n\tAllowOverride All\n\tOptions Indexes MultiViews FollowSymLinks\n\tRequire all granted\n</Directory>' > /etc/apache2/users/`whoami`.conf ; echo 'AddDefaultCharset utf-8' >> /etc/apache2/httpd.conf ; sed -i '' '/LoadModule userdir_module libexec\/apache2\/mod_userdir.so/s/^#*//g' /etc/apache2/httpd.conf ; sed -i '' '/LoadModule php[0-9]_module libexec\/apache2\/libphp[0-9].so/s/^#*//g' /etc/apache2/httpd.conf ; sed -i '' '/Include \/private\/etc\/apache2\/extra\/httpd-userdir.conf/s/^#*//g' /etc/apache2/httpd.conf ; sed -i '' '/Include \/private\/etc\/apache2\/users\/\*.conf/s/^#*//g' /etc/apache2/extra/httpd-userdir.conf ; apachectl start"

† Well, it's technically one line, even if it's really really long...

0

See https://apple.stackexchange.com/a/57555/9058, basically you have to enable per-user web sharing manually (with Terminal.app).

0

Mountain Lion removes the configuration file that gives web access to your user directory. The configuration files are in /etc/apache2/users/. The missing one is user.conf, where user is your short username.

If you don't want to fiddle with the command line, here's an Applescript you can run to create a configuration file for your user. It will ask for your password because it has to create the file in a system-level directory and needs elevated privileges.

In your applications folder or Launchpad, open Other > Applescript Editor. Copy the script below and paste it into the text area of the Applescript editor. Then click the Run button.

set userHome to (short user name of (system info))
set configFile to "/etc/apache2/users/" & userHome & ".conf"
set configFileContents to "<Directory \"/Users/" & userHome & "/Sites/\">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>"

do shell script "echo '" & configFileContents & "' > " & configFile with administrator privileges
do shell script "/usr/sbin/apachectl restart" with administrator privileges

When the script finishes, you should be able to access your user-level web page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .