【问题标题】:Use php to write to create virtual host for apache使用php编写为apache创建虚拟主机
【发布时间】:2014-01-17 05:00:00
【问题描述】:

我正在尝试编写一个脚本来创建虚拟主机 vor apache(AWS 上的 CentOS 6.4) 我试过简单的写:

$result = file_put_contents( '/etc/httpd/conf/vhosts/test.conf', 'teststring'); 
var_dump($result);

这是我的许可和所有者信息:

-rw-r--r--. 1 apache apache 34445 Jan 16 11:08 httpd.conf
-rw-r--r--. 1 apache apache 13139 Aug 13 17:30 magic
drwxrwxrwx. 2 apache apache  4096 Jan 17 04:29 vhosts

但我仍然收到此消息:

Warning: file_put_contents(/etc/httpd/conf/vhosts/test.conf): failed to open stream: Permission denied in /var/www/server-control/src/test.php on line 8 bool(false) 

通过ps aux|grep httpd检查用户:

root      9804  0.0  0.0 246372  8712 ?        Ss   03:57   0:00 /usr/sbin/httpd
apache    9808  0.0  0.0 247024  7972 ?        S    03:57   0:00 /usr/sbin/httpd
apache    9809  0.0  0.0 248304  8644 ?        S    03:57   0:00 /usr/sbin/httpd
apache    9810  0.0  0.0 247536  7872 ?        S    03:57   0:00 /usr/sbin/httpd
apache    9811  0.0  0.0 247536  8020 ?        S    03:57   0:00 /usr/sbin/httpd
apache    9812  0.0  0.0 247536  7876 ?        S    03:57   0:00 /usr/sbin/httpd
apache    9813  0.0  0.0 247536  7948 ?        S    03:57   0:00 /usr/sbin/httpd

请帮忙!!有些服务器我可以通过将所有者更改为apache 来写入该文件夹,但这个不是。

【问题讨论】:

    标签: php apache permissions


    【解决方案1】:

    我已经解决了这个问题。运行ls -laZ,这就是区别:

    我的本​​地:

    [root@localhost conf]# ls -laZ
    drwxr-xr-x apache apache ?                                .
    drwxr-xr-x apache apache ?                                ..
    -rw-r--r-- apache apache ?                                httpd.conf
    -rw-r--r-- apache apache ?                                magic
    -rw-r--r-- apache apache ?                                ssl.crt
    -rw-r--r-- apache apache ?                                ssl.key
    drwxrwxrwx apache apache ?                                vhosts
    

    EC2 服务器:

    [root@ip-172-31-0-37 conf]# ls -laZ
    drwxr-xr-x. apache apache system_u:object_r:httpd_config_t:s0 .
    drwxr-xr-x. apache apache system_u:object_r:httpd_config_t:s0 ..
    -rw-r--r--. apache apache system_u:object_r:httpd_config_t:s0 httpd.conf
    -rw-r--r--. apache apache system_u:object_r:httpd_config_t:s0 magic
    drwxrwxrwx. apache apache unconfined_u:object_r:httpd_config_t:s0 vhosts
    

    这意味着 EC2 服务器在 SELinux permission context

    为了解决问题,我再次运行chcon -R --type=httpd_sys_content_t vhostsls -laZ

    [root@ip-172-31-0-37 conf]# ls -laZ
    drwxr-xr-x. apache apache system_u:object_r:httpd_config_t:s0 .
    drwxr-xr-x. apache apache system_u:object_r:httpd_config_t:s0 ..
    -rw-r--r--. apache apache system_u:object_r:httpd_config_t:s0 httpd.conf
    -rw-r--r--. apache apache system_u:object_r:httpd_config_t:s0 magic
    drwxrwxrwx. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 vhosts
    

    区别在于 vhosts 目录上的 httpd_sys_content_t 上下文

    现在apache可以写信给/etc/httpd/conf/vhosts

    如果我做错/理解错了,请纠正我。

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2022-09-27
      • 2012-04-26
      • 2014-01-27
      • 2013-04-04
      • 1970-01-01
      • 2012-01-06
      • 2011-09-10
      • 2016-06-15
      相关资源
      最近更新 更多