CentOS下Apache的SELinux權限(上傳圖片后寫入指定文件夾)

今天本來在寫一個接口,上傳圖片,然后保存,數據庫中記住路徑,這樣的一個任務

搗鼓了半天,上傳圖片代碼寫好了

- (void)uploadFile {
    NSString *path = @"/Users/apple/Desktop/acc.png";

    NSString *urlStr = @"http://www.cry.com/uploadFile.php";
    
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSError *error;
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:path] name:@"file" fileName:@"acc.png" mimeType:@"image/png" error:&error];
        NSLog(@"%@",error);
        
    } error:nil];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionUploadTask *task = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
        
        NSLog(@"%@",uploadProgress);
    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        
        NSLog(@"response: %@",response);
        NSLog(@"responseobj: %@",responseObject);
        NSLog(@"%@",responseObject[@"status"]);
        NSLog(@"error: %@",error);

    }];
    [task resume];

}

虛擬機服務器的php代碼也寫了(簡陋的代碼),只是想試試傳圖片的流程

<?php
header("Content-Type:application/json");
/** * Created by PhpStorm. * User: apple * Date: 16/11/10 * Time: 上午9:45 */
$tmpfilename = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];
$path = "images/$name";
$json_arr ['newPath'] = $path;
if (is_uploaded_file($tmpfilename)) {               
$json_arr["isULfile"] = "是上傳文件";
} else {    
$json_arr["isULfile"] = "不是上傳文件";
}
$isSucc = move_uploaded_file($tmpfilename,$path);
if ($isSucc) {    $json_arr["status"] = "上傳成功";
} else {   
 $json_arr["status"] = "上傳失敗";  
  $json_arr["desc"] = $isSucc;
}
$json_arr += $_FILES;
echo json_encode($json_arr,JSON_PRETTY_PRINT);
?>

然而,后臺服務器接受到了圖片,臨時文件已經有了

每次給我輸出的都是

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}

responseobj: {
    file =     {
       error = 0;
       name = "acc.png";
       size = 707;
        "tmp_name" = "/tmp/phpw095q1";
        type = "image/png";
    };
    isULfile = "是上傳文件";(php里的is_uploaded_file()函數)
    newPath = "/var/www/cry.com/images/acc.png";(寫入的路徑)
    status = "\U4e0a\U4f20\U6210\U529f";
}
2016-11-10 19:41:10.289 OC32[89155:5771018] 上傳失敗

status里的那串就是"上傳失敗"

然后我登入虛擬機,查看日志/var/log/httpd/error_log,因為PHP就是以模塊的形式集成在Apache上運行的,日志在httpd里(apache就是httpd)

Thu Nov 10 18:17:16.452810 2016] [:error] [pid 6019] [client 192.168.1.219:61588] PHP Warning:  move_uploaded_file(/var/www/cry.com/images/acc.png): failed to open stream: Permission denied in /var/www/cry.com/uploadFile.php on line 21
Thu Nov 10 18:17:16.498295 2016] [:error] [pid 6019] [client 192.168.1.219:61588] PHP Warning:  move_uploaded_file(): Unable to move '/tmp/phpj5KtFB' to '/var/www/cry.com/images/acc.png' in /var/www/cry.com/uploadFile.php on line 21

???Permission denied???,這是沒權限的意思嗎

查看了很多,什么chown修改,chmod 777修改,各種權限,都還是上傳失敗.

最后發現是SELinux在搞鬼

SELinux 更能遵從最小權限的理念。在缺省的 enforcing 情況下,一切均被拒絕,接著有一系列例外的政策來允許系統的每個元素(服務、程序、用戶)運作時所需的訪問權。當一項服務、程序或用戶嘗試訪問或修改一個它不須用的文件或資源時,它的請求會遭拒絕,而這個行動會被記錄下來

所以,我們可以在/etc/selinux/config修改SELinux配置,關掉它

但是,肯定還有別的方法

看到有段文字說所有進程及文件都擁有一個 SELinux 的安全性脈絡,可以用ls -Z查看

-rwxr-xr-x. apache apache system_u:object_r:httpd_sys_content_t:s0 uploadFile.php
-rw-r--r--. apache apache unconfined_u:object_r:httpd_sys_content_t:s0 images

httpd_sys_content_t 這是apache的權限角色

在這篇文章里https://blog.lysender.com/2015/07/centos-7-selinux-php-apache-cannot-writeaccess-file-no-matter-what/ 看到

雖然英文一般,但是看到了關鍵字wirete to those path

http://newscentral.exsees.com/item/5b6cccab6ed05ce053bc09f70dc9386e-99c0b81ebd43e7b6848a00939f39b855 里面博主說

 sudo  chcon -R -t httpd_sys_rw_content_t  /var/www/cry.com/images

把我準備寫入的文件夾的權限角色從httpd_sys_content_t
改成httpd_sys_rw_content_t

run~

就這樣,看起來好像解決了

參考:
http://newscentral.exsees.com/item/5b6cccab6ed05ce053bc09f70dc9386e-99c0b81ebd43e7b6848a00939f39b855

https://blog.lysender.com/2015/07/centos-7-selinux-php-apache-cannot-writeaccess-file-no-matter-what/

有更好的方法,可以留言~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容