duanhuiqing9528 2015-03-06 22:45
浏览 53
已采纳

PHP和Apache - chown()使用Apache所有者:group?

I'm creating tests for an application that has 3 environments: dev, staging, and production.

I want to run a test which programmatically pulls the default apache owner and group (from httpd.conf), and checks to make sure the uploads directory is owned by this same owner/group combo.

Is there a PHP function that is capable of pulling this data?

  • 写回答

2条回答 默认 最新

  • dppi5167 2015-03-06 23:24
    关注

    Just run the PHP script below.

    Don't forget to change the input data for your setup:
    $apacheEnvVarsConfFile - the apache conf file that defines the user to be used; in this example it was /etc/apache2/envvars and the line defining it was export APACHE_RUN_USER=www-data
    $dirThatYouWantToTest - the directory for which you want to read the user/group permissions

    <?php
    
    ## define input data
    $apacheEnvVarsConfFile = '/etc/apache2/envvars';
    $dirThatYouWantToTest = '/var/www';
    
    ## compute the information that is needed
    $data = array();
    
    $data['apacheUser'] = str_replace('export APACHE_RUN_USER=', '', exec('cat '.$apacheEnvVarsConfFile.' | grep APACHE_RUN_USER'));
    
    $dirStat = stat($dirThatYouWantToTest);
    $data['dirTested'] = $dirThatYouWantToTest;
    $data['dirUser'] = posix_getpwuid($dirStat[4])['name'];
    $data['dirGroup'] = posix_getgrgid($dirStat[5])['name'];
    $data['dirPerms'] = substr(sprintf('%o', fileperms($dirThatYouWantToTest)), -4);
    
    echo '<pre>';print_r( $data );echo '</pre>';
    
    ## the code above will produce the following output (example): 
    /*
    Array
    (
        [apacheUser] => www-data
        [dirTested] => /var/www
        [dirUser] => john
        [dirGroup] => john
        [dirPerms] => 0755
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度