首页 / 知识

使用PHP / Apache上传文件夹的适当权限是什么?

2023-04-12 04:57:00

使用PHP / Apache上传文件夹的适当权限是什么?

What are the proper permissions for an upload folder with PHP/Apache?

对不起这个基本问题-我是.NET开发人员,对LAMP设置没有太多经验。

我有一个PHP网站,该网站将允许上传到特定文件夹。 有人告诉我,该文件夹需要由网络服务器用户拥有,才能进行上载过程,因此我创建了该文件夹,然后将权限设置为:

1
2
chown apache:apache -R uploads/
chmod 755 -R uploads/

现在唯一的问题是FTP用户根本无法修改上传的文件。

是否有一个允许我仍然上传文件,然后再以除Web服务器用户之外的用户身份修改文件的权限设置?


您可以创建一个同时具有apache用户和FTP用户作为成员的新组,然后对上载文件夹775授予权限。这应该使apache和FTP用户都能够写入该文件夹中的文件,但保留其他所有人从修改它们。


如果您真的想这样做,我会同意Ryan的回答。

通常,在* nix环境中,您总是想放弃尽可能少的权限。

10的9倍是755的理想权限-因为唯一具有修改文件能力的用户将是Web服务器。如果您确实需要更改此设置,请与您的ftp用户一起更改为775。

由于您是自己首次接触php的人,因此这里有一个有用的链接,可提高您上传服务的安全性:
move_uploaded_file


或至少766。

  • 读取= 4
  • 写= 2
  • 执行= 1

7 =读取+写入+执行

6 =读+写

  • 第一个号码:所有者
  • 第二个数字:组
  • 第三个数字:其他用户

我会支持创建具有上传权限的ftp组的想法。但是,我认为没有必要给予775许可。 7代表读,写,执行。通常,您希望允许某些组进行读取和写入,但是根据情况,可能不需要执行。


重要的是apache用户和组应具有最小的read访问权限,在某些情况下应具有execute访问权限。其余的您可以授予0访问权限。

这是最安全的设置。


我要补充一点,如果您正在使用SELinux,则需要确保类型上下文为tmp_t。您可以使用chcon实用程序来完成此操作

chcon -t tmp_t uploads


根据@Ryan Ahearn的回答,以下是我在Ubuntu 16.04上所做的操作,以创建仅具有nginx的Web目录/var/www/html权限的用户front

脚步:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
* pre-steps:
    * basic prepare of server,
    * create user 'dev'
        which will be the owner of"/var/www/html",
    *
    * install nginx,
    *
*
* create user 'front'
    sudo useradd -d /home/front -s /bin/bash front
    sudo passwd front

    # create home folder, if not exists yet,
   sudo mkdir /home/front
    # set owner of new home folder,
   sudo chown -R front:front /home/front

    # switch to user,
   su - front

    # copy .bashrc, if not exists yet,
   cp /etc/skel/.bashrc ~front/
    cp /etc/skel/.profile ~front/

    # enable color,
   vi ~front/.bashrc
    # uncomment the line start with"force_color_prompt",

    # exit user
   exit
*
* add to group 'dev',
    sudo usermod -a -G dev front
* change owner of web dir,
    sudo chown -R dev:dev /var/www
* change permission of web dir,
    chmod 775 $(find /var/www/html -type d)
    chmod 664 $(find /var/www/html -type f)
*
* re-login as 'front'
    to make group take effect,
*
* test
*
* ok
*

还要记住CHOWNchgrp您的网站文件夹。尝试myusername# chown -R myusername:_www uploads


上传权限网站设置

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢