`Apache` `localhost/~username/` not working
here's a script i wrote because i got tired of googling how to do this every time i upgrade os x.
#!/bin/sh
# edit httpd.conf to allow home directories.
# some day just rip out httpd and replace with nginx
bin=$(basename "${0}")
conf="/etc/apache2/httpd.conf"
if [ $(id -u) -ne 0 ]
then
echo "ERROR: ${bin} must run as root. goodbye."
exit 1
fi
# make backup file
n=1
while [ -f "${conf}.bak.${n}" ]
do
let n=${n}+1
done
cp "${conf}" "${conf}.bak.${n}"
# edit httpd.conf in place - uncomment out two lines
sed -i '' \
-e '/^#.*[[:space:]]userdir_module[[:space:]]/s/^#*//' \
-e '/#Include \/private\/etc\/apache2\/extra\/httpd-userdir.conf/s/^#//' \
"${conf}"
echo "INFO: ${bin}:"
echo "INFO: ${bin}: check that this looks OK"
echo "INFO: ${bin}: % cat /private/etc/apache2/extra/httpd-userdir.conf"
echo "INFO: ${bin}:"
cat /private/etc/apache2/extra/httpd-userdir.conf
echo "INFO: ${bin}:"
echo "INFO: ${bin}: in particular, that '/private/etc/apache2/users/*.conf' files look OK"
ls /private/etc/apache2/users/*.conf
echo "INFO: ${bin}:"
echo "INFO: ${bin}: hint: check for /private/etc/apache2/users/${SUDO_USER}.conf"
if [ -f "/private/etc/apache2/users/${SUDO_USER}.conf" ]
then
echo "INFO: ${bin}: /private/etc/apache2/users/${SUDO_USER}.conf exists. See if it is OK."
else
echo "INFO: ${bin}: /private/etc/apache2/users/${SUDO_USER}.conf does NOT exist."
fi
echo "INFO: ${bin}:"
echo "INFO: ${bin}: here is a sample conf file (with php and perl enabled)."
echo "INFO: ${bin}:"
cat<<!
<Directory "/Users/${SUDO_USER}/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>
!
echo "INFO: ${bin}:"
echo "INFO: ${bin}: restarting apache httpd"
sudo apachectl restart
echo "INFO: ${bin}:"
Answer from user3814420 is correct if you use path to you project.
But if you still have issue like I had, when using virtual host:
One caveat to note about virtual hosts is that once set up you lose your older document root previously at /LIbrary/WebServer/Documents or accessed in the browser at localhost what happens is that you get a 403 Forbidden Error. But the ~/username document root is still compatible.
To get around this, you need to add in a vhost for localhost and declare this vhost before any of the others, in the same file:
sudo nano /etc/apache2/extra/httpd-vhosts.conf
Add in:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>
Restart apache
sudo apachectl restart
Changing the WebServer Default User
One of the frustrations of using the Users/username/Sites folder for vhosts is the permissions issues with things like updates and authentication.
This is because the default webserver user which runs httpd is known as _www, which will not be the user in your local account. If your machine is only in use by you and the webserver will run only under your account then you can change the user.
To Find Your User and Group In the Terminal use the id command to see your username and group
id
You will get a bunch of user groups, you need your primary user uid and group gid names
uid=502(your_user_name) gid=20(staff)
After edit /etc/apache2/httpd.conf
sudo nano /etc/apache2/httpd.conf
find block
<IfModule unixd_module>
and comment
#User _www
#Group _www
after add new 2 line user uid and in my case group gid defined with id command above:
User your_user_name
Group staff
Source: http://coolestguidesontheplanet.com/set-virtual-hosts-apache-mac-osx-10-10-yosemite/
Looks like you need to uncomment the following:
#LoadModule userdir_module libexec/apache2/mod_userdir.so
and
#Include /private/etc/apache2/extra/httpd-userdir.conf
Then in httpd-userdir.conf you may need to uncomment:
#Include /private/etc/apache2/users/*.conf
Lastly you would need to create /private/etc/apache2/users/kevin.conf if it doesn't exist. I think it should look something like this:
<Directory "/Users/kevin/Sites/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
Make sure to restart the Apache server afterwards with:
sudo apachectl restart
For the rare person who spent as much time as I did wondering why none of the other resources or posts worked... this is for you. I spent 6 hours (at work) devoted entirely towards getting this damn permission issue solved and here was my solution. PLEASE NOTE: When I initially set out to solve this issue, I could access localhost
(in Chrome), getting the desired output of It Works!
. So make sure you can at least get that far before moving on.
I'm using PHP v5.5.14 on a Retina Macbook Pro (mid-2014) that is running OS X Yosemite (I tested this again using Apple's recent El Capitan update and confirmed to be a solution!). My problem was that I recieived a 403 ERROR stating that permission was denied whenever attempting to access localhost/~userDirName/
. My solution involves 3 simple steps:
STEP #1:
Load the userdir
module by finding the following two lines in /etc/apache2/httpd.conf
and uncommenting them by removing the leading hash (#):
LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf
STEP #2:
Navigate to /etc/apache2/extra/httpd-userdir.conf
and uncomment the following line (in the same manner as step 1):
Include /private/etc/apache2/users/*.conf
STEP #3:
Edit /etc/apache2/users/fileYouMade.conf
. You must add Require local
and a +
character before each argument that is option-specific. The result will look like this:
<Directory "/Users/user/Sites/">
Options +Indexes +MultiViews +FollowSymLinks +SymLinksIfOwnerMatch +ExecCGI
AllowOverride All
Require local
Order allow,deny
Allow from all
</Directory>