我最近换了一台新电脑,并从旧电脑上复制了所有现有的用于 webdevelopment 项目的 git 存储库。在那台旧电脑上,我安装了 apache,以便将主文件夹子目录中的某些文件夹用作开发网站(静态或使用 PHP)。因此,例如,个人项目将位于 下/home/myusername/programming/personal/projectname/
。
现在在新电脑上,我尝试设置 nginx,以类似的方式使用它。
我跟着DigitalOcean 上的指南在运行 Linux Mint 18.1(Serena)的笔记本电脑上安装 nginx(以及 MySQL 和 PHP)。
我对 nginx 进行了如下配置:
/etc/nginx/sites-available/default
:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
此后,通过创建和测试/var/www/html/info.php
(如指南所示)成功完成。
我现在尝试为我的一个项目添加一个域。
/etc/nginx/sites-available/projectname.dev
:
server {
listen 80;
listen [::]:80;
server_name projectname.dev;
root /home/myusername/programming/personal/projectname/;
index index.php index.html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
(我还从 sites-enabled 添加了到此的符号链接:)ln -s /etc/nginx/sites-available/projectname.dev /etc/nginx/sites-enabled/projectname.dev
。
重新启动 nginx(sudo service nginx restart
)并添加127.0.0.1 projectname.dev
到我的/etc/hosts
文件后,我收到了 404 错误。
查看后/var/log/nginx/error.log
,我发现我的请求由于以下原因而失败:
2017/04/17 21:38:11 [crit] 30176#30176: *3 stat() "/home/myusername/programming/personal/projectname/" failed (13: Permission denied), client: 127.0.0.1, server: projectname.dev, request: "GET / HTTP/1.1", host: "projectname.dev"
我认为这与 linux/unix 中的文件/目录权限管理有关。我的主目录显然归我的用户名而 nginx 则由用户运行www-数据。但是,我不确定如何更改此文件夹的权限,以便www-数据/nginx 能够访问它(以及其中的文件)。
如何解决这个问题?为什么我在之前的电脑(也运行 Linux Mint)上配置 Apache 时没有遇到这个问题?
答案1
我曾经使用过nginx
,但是按照你的提示 - 这对我来说似乎是正确的 - 我会通过将组所有权更改为 www-data 并检查文件至少具有 0644 权限以及文件夹至少具有 0755 权限来更改相关文件/文件夹的权限。
如果您需要您的 Web 应用程序写入某些文件或目录,您可以将该文件/目录的所有者更改为 www-data 或添加组写入权限(文件为 0664,文件夹为 0775)。