Menu Home

解决nginx权限问题failed(13:Permission denied)

使用nginx架设wordpress站点,nginx安装后测试页面显示正常,当将wordpres安装包部署好之后运行安装程序报错,查看nginx的error日志显示 failed(13:Permission denied)。排查过程如下:

查看 nginx 运行状态,确认运行于 www-data 用户下。

roy@localhost:/$ sudo vim /etc/nginx/nginx.conf
...
user www-data;
worker_processes auto;
...
roy@localhost:/$ sudo ps aux | grep nginx
root       69558  0.0  0.0  66276  1708 ?        Ss   10:14   0:00 
nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   69559  0.0  0.1  66516  5540 ?        S    10:14   0:00 nginx: worker process
www-data   69560  0.0  0.1  66516  6944 ?        S    10:14   0:00 nginx: worker process
root       69794  0.0  0.0   8168   672 pts/1    S+   10:19   0:00 grep --color=auto nginx

通常情况下 www-data 用户没有权限访问用户私人目录,而我将 wordpress 放到了 /home/roy/www 的私人目录内,因此导致了权限不足的情况。

From https://stackoverflow.com/questions/25774999/nginx-stat-failed-13-permission-denied

Usually www-data does not have permissions to cd to other users home directories.

roy@localhost:/$ sudo -u www-data stat /home/roy/www
stat: cannot statx '/home/roy/www': Permission denied

The best solution in that case would be to add www-data to username group:

roy@localhost:/$ sudo gpasswd -a www-data roy
Adding user www-data to group roy
roy@localhost:/$ sudo -u www-data stat /home/roy/www
  File: /home/roy/www
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fc03h/64515d    Inode: 397083      Links: 6
Access: (0755/drwxr-xr-x)  Uid: (   33/www-data)   Gid: (   33/www-data)
Access: 2024-03-19 15:24:55.906448849 +0800
Modify: 2024-03-19 15:24:55.002369008 +0800
Change: 2024-03-19 15:24:55.002369008 +0800
 Birth: 2024-03-19 13:45:56.359413669 +0800

For your changes to work, restart nginx

roy@localhost:/$ sudo service nginx restart

Categories: nginx ubuntu

Tagged as:

RoyAkon