Apache 错误的本地主机位置
Apache wrong localhost location
经过多次尝试并收到 403 禁止错误:
http://localhost/
Forbidden
You don't have permission to access / on this server.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80
我终于检查了 apachectl -S
并意识到本地主机文件夹的位置设置不正确:
$ apachectl -S
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
它清楚地表明我的本地主机文件夹是 /var/www 而我的设置是其他的:
/etc/apache2/sites-available/mynewsite.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/user/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
和
/etc/apache2/apache2.conf
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/user/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
ServerName localhost
对于文件权限:
$ ls -la /home/user/
total 3097944
drwx------ 9 user user 4096 Jan 3 19:28 .
drwxr-xr-x 45 user user 4096 Jan 3 20:46 ..
drwxr-xr-x 48 user www-data 4096 Jan 3 20:30 www
Apache重启也没有问题:
$ sudo service apache2 restart
* Restarting web server apache2 [ OK ]
我想知道出了什么问题?
您必须执行 chmod a+x /home/user/
,因为如果 /home/user/
无法访问 www-data 将无法访问 /home/user/www/
。
经过多次尝试并收到 403 禁止错误:
http://localhost/
Forbidden
You don't have permission to access / on this server.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80
我终于检查了 apachectl -S
并意识到本地主机文件夹的位置设置不正确:
$ apachectl -S
Main DocumentRoot: "/var/www"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
它清楚地表明我的本地主机文件夹是 /var/www 而我的设置是其他的:
/etc/apache2/sites-available/mynewsite.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/user/www
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
和
/etc/apache2/apache2.conf
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/user/www>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
ServerName localhost
对于文件权限:
$ ls -la /home/user/
total 3097944
drwx------ 9 user user 4096 Jan 3 19:28 .
drwxr-xr-x 45 user user 4096 Jan 3 20:46 ..
drwxr-xr-x 48 user www-data 4096 Jan 3 20:30 www
Apache重启也没有问题:
$ sudo service apache2 restart
* Restarting web server apache2 [ OK ]
我想知道出了什么问题?
您必须执行 chmod a+x /home/user/
,因为如果 /home/user/
无法访问 www-data 将无法访问 /home/user/www/
。