在 FreeBSD 上使用 nginx 和 php 为多个用户提供适当的权限

在 FreeBSD 上使用 nginx 和 php 为多个用户提供适当的权限

我正在尝试使用 nginx 设置多用户 FreeBSD 服务器。

以下是我迄今为止的步骤:

在我的/usr/local/etc/nginx/nginx.conf文件中,我设置user www www;nginx 充当 www 用户,属于 www 组。我还在user = wwwgroup = www的 中设置了/usr/local/etc/php-fpm.conf

我想要实现的是,我(作为管理员)可以将用户添加到我的系统中并为他们创建一个文件夹(当然还有 nginx.conf 中的相应服务器条目)供/usr/local/www他们使用,有点像共享托管环境(无需任何自动设置)。

安装 nginx 和 php 后,我创建了第一个测试用户anon,并为他创建了一个文件夹/usr/local/www/anonsite

然后我chown anon:www anonsite让他成为所有者,并将组设置为 www,文件夹的权限如下所示:drwxr-xr-x 3 anon www 4 Apr 11 22:00 anonsite

在此文件夹中创建一个 info.php 并将anon浏览器指向它现在可以正常工作。然后我测试了下载和提取重力,但它只会显示一个空白页(我猜是因为我的权限设置错误)。如果我更改 php-fpm.conf user=anon,它会按预期工作,或者chmod -R g+w /usr/local/www/anonsite在提取下载的 grav 文件夹后使用 ,它也会正常工作。

这就是我目前陷入困境、无法理解的地方。在我看来,这两种“修复”似乎都是错误的或不好的做法。如果我将我的设置与我使用的共享主机提供商进行比较,那里的 webroot 文件夹只有drwxr-x--- 5 username apache 4096 Apr 2 05:00 username权限,提取重力测试设置后,它会立即工作(这可能是因为 Apache 的工作方式?)。

有人可以向我解释为什么会出现这种情况并指导我完成正确设置的步骤,或者我做错了什么?

我尝试的方法总体上是否被认为是不好的做法?

答案1

nginx 可能能够根据需要读取文件,只需将文件的组所有者明确设置为 nginx 用户
,而对于 php-fpm,nginx 用户可以简单地设置为侦听器
php-fpm.d/php-fpm-user1.conf

[grav]
user = user1
group = use1

listen = /var/run/php-fpm-user1.sock

listen.owner = www
listen.group = www

pm = dynamic 
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

php.ini
cgi.fix_pathinfo=0

nginx.conf

user www;
worker_processes auto;
worker_rlimit_nofile 8192; # should be bigger than worker_connections
pid /run/nginx.pid;

events {
    use kqueue; # No epoll on FreeBSD
    worker_connections 8000;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    keepalive_timeout 30; # longer values are better for each ssl client, but take up a worker connection longer
    types_hash_max_size 2048;
    server_tokens off;

    # maximum file upload size
    # update 'upload_max_filesize' & 'post_max_size' in /etc/php5/fpm/php.ini accordingly
    client_max_body_size 32m;
    # client_body_timeout 60s; # increase for very long file uploads

    # set default index file (can be overwritten for each site individually)
    index index.html;

    # load MIME types
    include mime.types; # get this file from https://github.com/h5bp/server-configs-nginx
    default_type application/octet-stream; # set default MIME type

    # logging
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # turn on gzip compression
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 5;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_types
        application/atom+xml
        application/javascript
        application/json
        application/ld+json
        application/manifest+json
        application/rss+xml
        application/vnd.geo+json
        application/vnd.ms-fontobject
        application/x-font-ttf
        application/x-web-app-manifest+json
        application/xhtml+xml
        application/xml
        font/opentype
        image/bmp
        image/svg+xml
        image/x-icon
        text/cache-manifest
        text/css
        text/plain
        text/vcard
        text/vnd.rim.location.xloc
        text/vtt
        text/x-component
        text/x-cross-domain-policy;

    # disable content type sniffing for more security
    add_header "X-Content-Type-Options" "nosniff";

    # force the latest IE version
    add_header "X-UA-Compatible" "IE=Edge";

    # enable anti-cross-site scripting filter built into IE 8+
    add_header "X-XSS-Protection" "1; mode=block";

}
server {
    #listen 80;
    index index.html index.php;

    ## Begin - Server Info
    root /home/user1/www/html;
    server_name localhost;
    ## End - Server Info

    ## Begin - Index
    # for subfolders, simply adjust:
    # `location /subfolder {`
    # and the rewrite to use `/subfolder/index.php`
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    ## End - Index

    ## Begin - Security
    # deny all direct access for these folders
    location ~* /(\.git|cache|bin|logs|backup|tests)/.*$ { return 403; }
    # deny running scripts inside core system folders
    location ~* /(system|vendor)/.*\.(txt|xml|md|html|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny running scripts inside user folder
    location ~* /user/.*\.(txt|md|yaml|yml|php|pl|py|cgi|twig|sh|bat)$ { return 403; }
    # deny access to specific files in the root folder
    location ~ /(LICENSE\.txt|composer\.lock|composer\.json|nginx\.conf|web\.config|htaccess\.txt|\.htaccess) { return 403; }
    ## End - Security

    ## Begin - PHP
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php-fpm-user1.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
    ## End - PHP
}`

相关内容