将 Web 服务器根目录更改为所有者 root:www-775 权限的数据是否安全?

Is it safe to change web server root directory to owners root:www-data with 775 rights?

我遇到了 PHP fwrite 函数没有写任何东西的问题,我认为这是因为权利。

一个 apache 进程是 运行 由 root 和其他几个由 www-data:

$ ps -aux |grep apache
root 21239 0.0 0.3 222104 26524 ? Ss 02:31 0:00 /usr/sbin/apache2 -k start
www-data 21240 0.0 0.1 222316 13736 ? S 02:31 0:00 /usr/sbin/apache2 -k start
...

Web 根目录归 root:root 所有,组所有者 (755) 没有写入权限:

/var/www# ls -l
drwxr-xr-x 9 root root 4096 Feb 29 02:11 html

所以我将根目录的组所有者更改为 www-data 并授予写权限:

/var/www# chown root:www-data html
/var/www# chmod 775 html
/var/www# ls -l
drwxrwxr-x 9 root www-data 4096 Feb 29 02:11 html

现在成功了。我的问题是这是否是一个正确且最重要的安全设置。

这是因为 Apache 运行 在 Ubuntu 中的 www-data。但是,如果您下载源代码并编译它,它将作为用户 运行 daemon。 Apache 使用的文件夹应归用户 Apache 运行s as.

所有

因此,如果您需要更改 Apache 的默认用户和组 change/add 行:

User <your-username> # (Without angle-bracket)
Group <your-group> # (Without angle-bracket), this setting is usually the same as the user

您不能使用 PHP 编写,因为您正在使用 apache 中的 php 模块。如果您要使用 PHP-FPM,则必须更改 PHP-FPM 的 www.conf 文件中的默认用户和组。根据您提供的信息,您没有使用 PHP-FPM,因此,当您更改 Apache 的用户和组时,它也会应用于 PHP,因为 PHP 是 运行 在 Apache 中。如果您有很多访问者,并且您的网站需要更快,则通过执行以下操作启用 PHP-FPM:

1) 通过 运行ning 卸载 mod_php(版本号):

sudo a2dismod php(version-number)

(可选步骤)2) 您可能需要使用 mpm_event 并卸载 mpm_prefork。您可能会看到一条奇怪的消息,上面写着冲突和其他内容,但请忽略它。你可以通过 运行ning:

sudo a2dismod prefork && sudo a2enmod event

3) 安装PHP-FPM:

sudo apt install php(version-number)-fpm && sudo service php(version-number)-fpm start

4) 在 Apache 中启用 proxy_fcgi 模块:

sudo a2enmod proxy_fcgi

5) 将以下内容添加到 apache2.conf 文件:

<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler "proxy:fcgi://localhost:9000"
</FilesMatch>

6) 运行:

sudo service apache2 restart

大功告成!