Php nginx本地web服务器的所有权和权限
我在设置本地nginx环境时遇到问题。我在网上读了无数的教程,所有的东西似乎都让我的大脑有点混乱 OS:OSX 10.11.4 El Capitan Nginx:1.8.1 PHP-FPM:5.5.31 目前,我的web根目录的文件目录如下:Php nginx本地web服务器的所有权和权限,php,nginx,permissions,Php,Nginx,Permissions,我在设置本地nginx环境时遇到问题。我在网上读了无数的教程,所有的东西似乎都让我的大脑有点混乱 OS:OSX 10.11.4 El Capitan Nginx:1.8.1 PHP-FPM:5.5.31 目前,我的web根目录的文件目录如下: /webserver /webverver/phpinfo.php /webserver/example /webserver/example/index.php 我可以使用curl或在访问localhost的web浏览器中访问默认的“欢迎使用Nginx
/webserver
/webverver/phpinfo.php
/webserver/example
/webserver/example/index.php
我可以使用curl或在访问localhost的web浏览器中访问默认的“欢迎使用Nginx”页面。如果我浏览,我会得到文件的索引,但是PHP文件会尝试下载而不是执行。如果我尝试访问我在local.example.com
上创建的示例站点(我已将其添加到我的主机文件中),那么我将使用curl获得403个禁止的头返回,并使用web浏览器获得一个漂亮、简单的“拒绝访问”
我不太了解文件权限和目录所有权,有人能告诉我应该如何配置所有内容吗?有人建议我运行以下命令,但到目前为止没有任何更改:
sudo chmod-R 755/Users/nickcorin/webserver
除了“信号已启动”日志之外,我的错误日志中没有任何日志
以下是我目前的配置:
nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /Users/nickcorin/webserver;
autoindex on;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
user yourusername staff;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log warn;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /Users/yourusername/webserver;
autoindex on;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
服务器/示例
upstream php {
server 127.0.0.1:9000;
}
server {
listen 80;
root /Users/nickcorin/webserver/example;
server_name local.example.com;
index index.php index.html index.htm;
autoindex on;
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name local.example.com;
root /Users/yourusername/webserver/example;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
**编辑-NGINX文件夹权限和进程所有者**
我在日志文件夹/usr/local/var/nginx
上运行了ls-la
,结果如下:
drwxr-xr-x 4 nickcorin admin 136 Apr 20 23:53 .
drwxr-xr-x 5 nickcorin admin 170 Apr 20 21:47 ..
-rw-r--r-- 1 root admin 4718 Apr 21 08:06 access.log
-rw-r--r-- 1 nickcorin admin 480 Apr 21 10:28 error.log
这是我的Web服务器根目录上的结果:
drwxr-xr-x 4 nickcorin staff 136 Apr 22 12:23 .
drwx-----x+ 54 nickcorin staff 1836 Apr 22 10:01 ..
drwxr-xr-x 3 nickcorin staff 102 Apr 20 22:14 example
-rw-r--r--@ 1 nickcorin staff 23 Apr 19 11:58 info.php
这是ps aux | grep nginx的结果:
root 756 0.0 0.0 2466616 480 ?? Ss 12:24PM 0:00.00 nginx: master process nginx
nickcorin 759 0.0 0.0 2445080 820 s000 S+ 12:24PM 0:00.00 grep nginx
nobody 757 0.0 0.0 2475832 1044 ?? S 12:24PM 0:00.00 nginx: worker process
**编辑#2-虚拟主机配置文件**
我设法解决了我的问题,现在事情似乎进展顺利。我必须将虚拟主机配置文件编辑为:
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name local.example.com;
root /Users/nickcorin/webserver/example;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我还通过以下两行修改了我的权限:
sudo chown -R nickcorin /Users/nickcorin/webserver (Web Server Root)
sudo chmod 755 /Users/nickcorin/webserver/example (Virtual Host Root)
感谢所有帮助我解决这个问题的人:)案例清单:
echo'hi there';
如果错误仍然存在,那么可能是我们错误地配置了web服务器
error_log /var/log/nginx/example.error.log notice
但是根据您的nginx.conf,您已经完成了这项工作,因此请检查日志文件以查找有关权限相关问题的信息
通常,您应该会发现问题的具体描述-文件无效权限、套接字无效权限或上游问题
nginx
)和所有者和组。对于nginx
用户(用户、nickcorin和web服务器),每个父目录(自身)都应该(至少)是可执行的(--x)
2) 而示例
目录及其所有内容也应该是可读的(r-x)。要实现这一点,可以使用以下命令:
# cd example
# find . -type d | xargs chmod 755
# find . -type f | xargs chmod 644
(这样做不会使文件像sudo chmod-R 755/Users/nickcorin/webserver那样可执行)
b。上游故障排除。检查防火墙(如果有的话)是否与上游php{Server127.0.0.1:9000;}
注1。“欢迎使用Nginx”html文档通常存储在/usr/share/中,需要授权
注2。最好使用系统中的某个位置,您将为您的环境手动创建和设置所有必需的访问权限,而不是使用具有700权限的用户目录(并导致一些额外的步骤来设置权限相关的内容)
注3。请记住,当目录中没有索引文件时,
403禁止
也会响应。我遇到的问题是虚拟主机的配置文件和Web服务器根目录的权限。这些是我当前的工作配置设置,适用于其他遇到类似问题的人:
nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /Users/nickcorin/webserver;
autoindex on;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
user yourusername staff;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log warn;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root /Users/yourusername/webserver;
autoindex on;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
服务器/示例
upstream php {
server 127.0.0.1:9000;
}
server {
listen 80;
root /Users/nickcorin/webserver/example;
server_name local.example.com;
index index.php index.html index.htm;
autoindex on;
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
server {
listen 80;
listen [::]:80 ipv6only=on;
server_name local.example.com;
root /Users/yourusername/webserver/example;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
一旦这些设置正确,我就运行这些命令来修复Web服务器中的权限错误:
sudo chown -R yourusername /Users/yourusername/webserver
sudo chmod 755 /Users/yourusername/webserver/example
别忘了将
127.0.0.1 local.example.com
添加到您的主机文件中。您的php fpm池中的用户和组是否与您的web文件的用户/组所有者匹配?您完全打开了一个新思路,我对nginx非常陌生,我不知道我必须这样做?我只是把一切都留给了php fpm默认值ps aux | grep php fpm
显示我的用户‘nickcorin’处理进程。ps aux | grep php fpm
表示php fpm池配置中的listen.owner
和listen.group
属性。确保池配置中的用户
和组
与您的文件权限匹配。编辑默认池配置,例如:/etc/php5/fpm/pool.d/www.conf
,或者将其复制到一个新的池中,如/etc/php5/fpm/pool.d/mysite.conf
,并通过listen=127.0.0.1:9001
在另一个端口进行侦听。哇!谢谢你的回答!:D 1。My index.php已包含phpinfo()回显。2.My error.log仅包含启动的nginx信号的条目。这应该指向一个权限错误,如您所说,我使用了ps aux | grep nginx
,并且“root”处理nginx主进程,而“nobody”处理工作进程。我将尝试更改权限!非常感谢你的回答。在第三个注意事项中,我确实看到了这一点——我在网站根目录中有一个index.php,并启用了自动索引以备不时之需。运行chmod命令并没有改变任何事情,我感觉这与用户和组有关。”没有人处理nginx公关