要把Windows上的习惯改掉。
Nginx和PHP-FPM运行用户都使用Nginx,这个用户是一个不能登录的普通用户,yum安装时会自动新建,自己编译安装的话需要手动新建:
addgroup nginx --system
adduser nginx --system --disabled-login --ingroup nginx --no-create-home --home /nonexistent --gecos "nginx user" --shell /bin/false
网站根目录的所有者不能设为Nginx用户,可以是你上传文件的用户。
网站目录基本的权限设置(目录755,文件644):
find -type d -exec chmod 755 {} \\;
find -type f -exec chmod 644 {} \\;
也就是说Nginx和PHP-FPM只能读文件和访问目录。
对于特别的目录,比如文件上传目录,需要给Nginx写权限,这时可以设为777:
chmod 777 www/uploads
但注意配置该目录不做PHP解析,比如:
location ~ \\.php$ { include fastcgi_params; if ($uri !~ "^/uploads/") { fastcgi_pass 127.0.0.1:9000; } fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /path/to/www$fastcgi_script_name; }
另外还有像缓存目录需要写权限的目录同样可以这样配置。
其实PHP可以很安全,只要你想去保护她。