PHP - mkdir permission denied on digital ocean - php

I have deployed my Laravel application on DigitalOcean (without any third party packge usage). The application is functioning fine, except for uploading of files by the users.
I have installed LEMP stack by following the guide of DevMarketer (YouTube playlist).
Scenario:
I want to upload the file and store in my public directory. But, doing so, I get the error as Permission Denied.
Here's the code of directory that will get created it if not exist:
$path = public_path() . '/email-attachments/';
if (! file_exists($path)) {
mkdir($path, 0775, true);
}
On SSHing on to the server as root, I ran the following command to check the permission of the files and folders:
root#host-pc:~# ls -la /var/www/laravel
total 456
drwxrwsr-x 12 www-data www-data 4096 Aug 21 09:45 .
drwxrwsr-x 4 www-data www-data 4096 Aug 18 07:13 ..
-rwxrwxr-x 1 www-data www-data 665 Aug 21 08:23 .env
-rw-rwSr-- 1 www-data www-data 521 Aug 21 09:45 .env.example
-rw-rwSr-- 1 www-data www-data 111 Aug 21 09:45 .gitattributes
-rw-rwSr-- 1 www-data www-data 157 Aug 21 09:45 .gitignore
drwxrwsr-x 8 www-data www-data 4096 Aug 21 09:45 app
-rw-rwSr-- 1 www-data www-data 1646 Aug 21 09:45 artisan
drwxrwsr-x 3 www-data www-data 4096 Aug 21 09:45 bootstrap
-rw-rwSr-- 1 www-data www-data 1496 Aug 21 09:45 composer.json
-rw-rwSr-- 1 www-data www-data 150371 Aug 21 09:45 composer.lock
drwxrwsr-x 2 www-data www-data 4096 Aug 21 09:45 config
drwxrwsr-x 5 www-data www-data 4096 Aug 21 09:45 database
-rw-rwSr-- 1 www-data www-data 1063 Aug 21 09:45 package.json
-rw-rwSr-- 1 www-data www-data 1100 Aug 21 09:45 phpunit.xml
drwxrwsr-x 10 www-data www-data 4096 Aug 21 09:45 public
-rwxrwxr-x 1 www-data www-data 440 Aug 21 06:27 queue_worker.log
drwxrwsr-x 5 www-data www-data 4096 Aug 18 07:56 resources
drwxrwsr-x 2 www-data www-data 4096 Aug 21 09:45 routes
-rw-rwSr-- 1 www-data www-data 563 Aug 21 09:45 server.php
drwxrwsr-x 6 www-data www-data 4096 Aug 18 07:56 storage
drwxrwsr-x 4 www-data www-data 4096 Aug 21 09:45 tests
drwxrwsr-x 39 www-data www-data 4096 Aug 21 05:58 vendor
-rw-rwSr-- 1 www-data www-data 1439 Aug 21 09:45 webpack.mix.js
-rw-rwSr-- 1 www-data www-data 212974 Aug 21 09:45 yarn.lock
Following is the result of ps -ef | grep nginx
root#host-pc:~# ps -ef | grep nginx
root 14862 1 0 11:45 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 14863 14862 0 11:45 ? 00:00:00 nginx: worker process
root 15489 12952 0 12:19 pts/0 00:00:00 grep --color=auto nginx
Following is the result of ps -ef | grep php
root#host-pc:~# ps -ef | grep php
root 10173 9901 0 05:05 ? 00:00:16 php /var/www/laravel/artisan queue:work --tries=5
root 10174 9901 0 05:05 ? 00:00:15 php /var/www/laravel/artisan queue:work --tries=5
root 10175 9901 0 05:05 ? 00:00:16 php /var/www/laravel/artisan queue:work --tries=5
root 10176 9901 0 05:05 ? 00:00:16 php /var/www/laravel/artisan queue:work --tries=5
root 10177 9901 0 05:05 ? 00:00:15 php /var/www/laravel/artisan queue:work --tries=5
root 10178 9901 0 05:05 ? 00:00:15 php /var/www/laravel/artisan queue:work --tries=5
root 10179 9901 0 05:05 ? 00:00:15 php /var/www/laravel/artisan queue:work --tries=5
root 10180 9901 0 05:05 ? 00:00:16 php /var/www/laravel/artisan queue:work --tries=5
root 15251 1 0 11:50 ? 00:00:00 php-fpm: master process (/etc/php/5.6/fpm/php-fpm.conf)
www-data 15255 15251 0 11:50 ? 00:00:02 php-fpm: pool www
www-data 15256 15251 0 11:50 ? 00:00:02 php-fpm: pool www
root 15495 12952 0 12:21 pts/0 00:00:00 grep --color=auto php
I don't know what is the mistake that I am doing (I know it must be a silly one), but I couldn't find the solution yet.
Please help me out with this. Thanks.

If you change this
$path = public_path() . '/email-attachments/';
To this
$path = public_path('email-attachments/');

Hopefully, This will help you.
$path = base_path() . '/public/email-attachments';
mkdir($path, 0755, true);

Related

The stream or file "/var/www/storage/logs/laravel.log" could not be opened - Docker & Laravel

I am trying to set the permissions of my test webpage to run injunction with my nginx container. When I execute:
$ docker exec test_site_app php artisan storage:link
I get this permission error:
The stream or file "/var/www/storage/logs/laravel.log" could not be opened in append mode: failed to open stream: Permission denied
When I execute a $ docker exec test_site_app ls -la command on my app, I see that the permissions are:
drwxr-xr-x 15 www-data www-data 4096 Jan 20 00:33 .
drwxr-xr-x 1 root root 4096 Dec 11 07:16 ..
-rw-r--r-- 1 www-data www-data 220 Jan 20 00:33 .editorconfig
-rw-r--r-- 1 www-data www-data 829 Jan 20 00:33 .env
-rw-r--r-- 1 www-data www-data 811 Jan 20 00:33 .env.example
drwxr-xr-x 8 www-data www-data 4096 Jan 20 00:33 .git
-rw-r--r-- 1 www-data www-data 111 Jan 20 00:33 .gitattributes
-rw-r--r-- 1 www-data www-data 191 Jan 20 00:33 .gitignore
-rw-r--r-- 1 www-data www-data 181 Jan 20 00:33 .styleci.yml
-rw-r--r-- 1 www-data www-data 7133 Jan 20 00:33 CHANGELOG.md
-rw-r--r-- 1 www-data www-data 3780 Jan 20 00:33 README.md
drwxr-xr-x 7 www-data www-data 4096 Jan 20 00:33 app
-rwxr-xr-x 1 www-data www-data 1686 Jan 20 00:33 artisan
drwxr-xr-x 3 www-data www-data 4096 Jan 20 00:33 bootstrap
-rw-r--r-- 1 www-data www-data 1646 Jan 20 00:33 composer.json
-rw-r--r-- 1 www-data www-data 267526 Jan 20 00:33 composer.lock
drwxr-xr-x 2 www-data www-data 4096 Jan 20 00:33 config
drwxr-xr-x 5 www-data www-data 4096 Jan 20 00:33 database
drwxr-xr-x 2 www-data www-data 4096 Jan 20 00:33 mysql
-rw-r--r-- 1 www-data www-data 473 Jan 20 00:33 package.json
drwxr-xr-x 2 www-data www-data 4096 Jan 20 00:33 php
-rw-r--r-- 1 www-data www-data 1202 Jan 20 00:33 phpunit.xml
drwxr-xr-x 2 www-data www-data 4096 Jan 20 00:33 public
drwxr-xr-x 6 www-data www-data 4096 Jan 20 00:33 resources
drwxr-xr-x 2 www-data www-data 4096 Jan 20 00:33 routes
-rw-r--r-- 1 www-data www-data 563 Jan 20 00:33 server.php
drwxr-xr-x 5 www-data www-data 4096 Jan 20 00:33 storage
drwxr-xr-x 4 www-data www-data 4096 Jan 20 00:33 tests
drwxr-xr-x 44 www-data www-data 4096 Jan 20 00:33 vendor
-rw-r--r-- 1 www-data www-data 559 Jan 20 00:33 webpack.mix.js
However, when I run whoami on each container (app and nginx) I see this:
$ docker exec test_site_app whoami
www
$ docker exec test_site_nginx whoami
root
I specifically created the user www in my app within my Dockerfile:
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
COPY . /var/www
COPY --chown=www:www . /var/www
USER www
However, my Nginx uses nobody within the nginx.conf because if I try to use www-data I get this error:
getpwnam("www-data") failed in /etc/nginx/nginx.conf:1 nginx: [emerg]
getpwnam("www-data") failed in /etc/nginx/nginx.conf:1
So already here I can see the permission issues but I really am not sure how to fix this. All of my Laravel files are chown -R www-data:www-data before I run $ docker-compose up --build -d.
To create my environment, here are my configurations hosted via Gist.
How can I fix this permission issue?

Laravel 5, Ngnix web server www-data permissions issue

I'm running a Laravel 5.7 app in the Ngnix server. All my files and directory permissions are set to www-data:www-data user:group. The problem is when I tried to run following PHP artisan command from the code using PHP exec() function, it is giving permission denied issue to create file under storage/log/ folder, which is already 777 and the queue was never executed.
$file_name = "queue-log";
exec('php artisan queue:work --tries=3 --stop-when-empty > storage/logs/'.$file_name.'.log &',$array);
When I simply try to do it by CLI, it works normally. My php.ini is not in safe mode and exec() is not under disabled functions list and exec("whoami") returns www-data when debugging from the browser.
Any clues what is going on?
The output of ls -la storage is
drwxrwxrwx 6 www-data www-data 4096 Jun 13 11:30 .
drwxrwxr-x 14 www-data www-data 4096 Jul 3 10:39 ..
drwxrwxrwx 5 www-data www-data 4096 Mar 22 14:58 app
drwxrwxrwx 2 www-data www-data 4096 Jul 19 07:04 debugbar
drwxrwxrwx 6 www-data www-data 4096 Jun 12 23:13 framework
drwxrwxrwx 2 www-data www-data 4096 Jul 23 04:40 logs
and the output of ls -la storage/logs
drwxrwxrwx 2 www-data www-data 4096 Jul 23 04:40 .
drwxrwxrwx 6 www-data www-data 4096 Jun 13 11:30 ..
-rwxrwxrwx 1 www-data www-data 96950 Jun 13 05:07 laravel-2019-06-13.log

file_put_contents on PHP won't work due to permission denial on www-data user

I'm very new to linux and I wanted to run a certain php test file on amazon linux VM image for the first time, but due to permission error, I cannot proceed running my test tool.
Here's our installer.php code to check if we have write permission. All files are stored in /var/www/html/ folder.
if(file_put_contents("check.txt", " ")===FALSE) {
echo json_encode(array("error"=>1, "message"=>"Provide WRITE permission to www-data."));
exit(0);
}
else {
unlink("check.txt");
}
And here's our /var/www/ permission:
[ec2-user#ip-1xx-xx-2x-xx www]$ ls -l
total 16
drwxrwsr-x+ 2 www-data www-data 4096 Jul 19 00:12 cgi-bin
drwxrwsr-x+ 3 www-data www-data 4096 Dec 2 10:41 error
drwxrwsr-x+ 11 www-data www-data 4096 Dec 3 12:34 html
drwxrwsr-x+ 3 www-data www-data 4096 Dec 2 11:08 icons
/var/www/html/ permission:
-rwxrwxr-x+ 1 www-data www-data 3942 Oct 12 12:40 action.php
drwxrwsr-x+ 5 www-data www-data 4096 Oct 29 19:44 backend
-rwxrwxr-x+ 1 www-data www-data 212 Dec 2 17:11 countdown.js
drwxrwsr-x+ 3 www-data www-data 4096 Oct 29 19:44 css
drwxrwsr-x+ 2 www-data www-data 4096 Oct 29 19:44 img
-rwxrwxr-x+ 1 www-data www-data 42 Oct 5 22:26 index.php
drwxrwsr-x+ 2 www-data www-data 4096 Oct 13 04:22 install
-rwxrwxr-x+ 1 www-data www-data 2087 Dec 2 22:26 installer.php
-rwxrwxr-x+ 1 www-data www-data 4890 Dec 2 18:26 install.php
-rwxrwxr-x+ 1 www-data www-data 6499 Oct 13 04:15 settings.php
-rwxrwxr-x+ 1 www-data www-data 3753 Oct 12 04:02 tables.sql
-rwxrwxr-x+ 1 www-data www-data 19965 Oct 20 02:20 inventory.php
drwxrwsr-x+ 2 www-data www-data 4096 Oct 29 19:44 js
-rwxrwxr-x+ 1 www-data www-data 41 Dec 3 12:32 key.txt
-rwxrwxr-x+ 1 www-data www-data 8643 Oct 12 12:40 keywords.php
-rwxrwxr-x+ 1 www-data www-data 181043 Sep 29 14:15 loader-wizard.php
-rwxrwxr-x+ 1 www-data www-data 4174 Oct 26 00:10 login.php
-rwxrwxr-x+ 1 www-data www-data 480 Oct 12 12:40 logout.php
-rwxrwxr-x+ 1 www-data www-data 47 Oct 13 03:55 runTime.php
-rwxrwxr-x+ 1 www-data www-data 44391 Sep 29 14:15 loader-wizard.zip

Joomla 3.4 folder permission issue

I have migrated my site to new server using Akeeba Backup.
The environment of new server is Ubuntu 16.04 LTS + nginx 1.10.0 + Joomla 3.4.8 + PHP 5.6
I can see my site running well and I can also login to admin tool, which means nginx, php-fpm, and mysql are running well.
However all files/folders in System Information -> Folder Permission are all Unwritable.
I have tried any possible solutions I can figure out, can anyone help?
php-fpm and nginx is run by www-data.
$ ps aux|grep fpm
root 1293 0.0 3.4 249700 20592 ? Ss 06:27 0:00 php-fpm: master process (/etc/php/5.6/fpm/php-fpm.conf)
www-data 3397 0.2 7.5 259964 45384 ? S 06:57 0:04 php-fpm: pool www
www-data 3403 0.2 7.5 259952 45244 ? S 06:57 0:04 php-fpm: pool www
www-data 3405 0.2 9.0 269948 54716 ? S 06:57 0:03 php-fpm: pool www
$ ps aux|grep nginx
root 1348 0.0 0.2 125192 1500 ? Ss 06:27 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 1349 0.0 1.1 125584 6840 ? S 06:27 0:00 nginx: worker process
All files are 644 and folder are 775 under /usr/share/nginx/html
-rw-r--r-- 1 www-data www-data 15 May 22 15:18 404.html
-rw-r--r-- 1 www-data www-data 15 May 22 15:18 50x.html
drwxrwxr-x 10 www-data www-data 4096 May 22 15:18 administrator/
drwxrwxr-x 2 www-data www-data 4096 May 22 15:18 bin/
drwxrwxr-x 4 www-data www-data 4096 May 22 15:18 cache/
drwxrwxr-x 2 www-data www-data 4096 May 22 15:18 cli/
drwxrwxr-x 33 www-data www-data 4096 May 22 15:18 components/
-rw-rw-r-- 1 www-data www-data 3240 May 24 07:18 configuration.php
-rw-r--r-- 1 www-data www-data 586 May 24 06:59 dir.php
-rw-r--r-- 1 www-data www-data 2915 May 22 15:18 htaccess.txt
drwxrwxr-x 6 www-data www-data 4096 May 22 15:18 images/
drwxrwxr-x 2 www-data www-data 4096 May 22 15:18 includes/
-rw-r--r-- 1 www-data www-data 1211 May 22 15:18 index.php
-rw-r--r-- 1 www-data www-data 20 May 22 15:18 info.php
-rw-r--r-- 1 www-data www-data 1873 May 22 15:18 joomla.xml
drwxrwxr-x 6 www-data www-data 4096 May 22 15:18 language/
drwxrwxr-x 5 www-data www-data 4096 May 22 15:18 layouts/
drwxrwxr-x 15 www-data www-data 4096 May 22 15:18 libraries/
-rw-r--r-- 1 www-data www-data 18092 May 22 15:18 LICENSE.txt
drwxrwxrwt 2 www-data www-data 4096 May 23 14:46 logs/
drwxrwxr-x 32 www-data www-data 4096 May 22 15:18 media/
drwxrwxr-x 36 www-data www-data 4096 May 22 15:18 modules/
drwxrwxr-x 29 www-data www-data 4096 May 22 15:18 plugins/
-rw-r--r-- 1 www-data www-data 53 May 22 15:18 README.md
-rw-r--r-- 1 www-data www-data 4213 May 22 15:18 README.txt
-rw-r--r-- 1 www-data www-data 296 May 22 15:18 robots.txt
-rw-r--r-- 1 www-data www-data 842 May 22 15:18 robots.txt.dist
drwxrwxr-x 9 www-data www-data 4096 May 22 15:18 templates/
drwxrwxrwt 2 www-data www-data 4096 May 22 15:18 tmp/
-rw-r--r-- 1 www-data www-data 1690 May 22 15:18 web.config.txt
I also write PHP script to check the permission and it shows files under /usr/share/nginx/html/ are still not writable.
<?php
$root = getcwd();
echo exec('whoami')." ";
echo "$root ";
echo is_readable("/usr/share/nginx/html") ? 'yes ' : 'no ';
echo is_writable("/usr/share/nginx/html") ? 'yes ' : 'no ';
echo is_writable("/tmp") ? 'yes ' : 'no ';
?>
The result shows "www-data /usr/share/nginx/html yes no yes" even the owner is www-data:www-data and mode is 755 on /usr/share/nginx/html
If you create a test php file in your root directory with just
<?php echo exec('whoami'); ?>
and then access it with a browser, do you see www-data or is it something else?

centos 6 permissions issue

I have a dedicated server with Centos6 and cPanel, all I want is to install Magento 2 on this server. My problem is with files and folders permissions. in the Magento 2 documentation I need to set this permissions find . -type d -exec chmod 770 {} \; && find . -type f -exec chmod 660 {} \; && chmod u+x bin/magento . when I set this permision to my home/username/public_html I get this error in browser.
Forbidden
You don't have permission to access / on this server.
Server unable to read htaccess file, denying access to be safe
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
is anyone who can point me in one direction to solve this?
thank you
**Output**
**ls -al /path/to/magento**
drwxr-x---. 13 nobody nobody 4096 Jan 29 18:30 ./
drwx--x--x. 10 quick quick 4096 Jan 29 18:29 ../
drwxr-xr-x. 4 nobody root 4096 Jan 29 18:30 app/
drwxr-xr-x. 2 nobody root 4096 Jan 29 18:30 bin/
drwxr-xr-x. 2 nobody quick 4096 Jan 29 18:28 cgi-bin/
-rw-r--r--. 1 nobody root 437404 Jan 28 04:12 CHANGELOG.md
-rw-r--r--. 1 nobody root 1851 Jan 28 04:10 composer.json
-rw-r--r--. 1 nobody root 315984 Jan 28 04:12 composer.lock
-rw-r--r--. 1 nobody root 3425 Jan 28 04:12 CONTRIBUTING.md
-rw-r--r--. 1 nobody root 10011 Jan 28 04:12 CONTRIBUTOR_LICENSE_AGREEMENT.html
-rw-r--r--. 1 nobody root 631 Jan 28 04:12 COPYING.txt
drwxr-xr-x. 4 nobody root 4096 Jan 29 18:30 dev/
-rw-r--r--. 1 nobody root 1032 Jan 28 04:10 .gitignore
-rw-r--r--. 1 nobody root 2918 Jan 28 04:12 Gruntfile.js
-rw-r--r--. 1 nobody root 7592 Jan 28 04:12 .htaccess
-rw-r--r--. 1 nobody root 6419 Jan 28 04:12 .htaccess.sample
-rw-r--r--. 1 nobody root 1358 Jan 28 04:12 index.php
drwxr-xr-x. 4 nobody root 4096 Jan 29 18:30 lib/
-rw-r--r--. 1 nobody root 10374 Jan 28 04:12 LICENSE_AFL.txt
-rw-r--r--. 1 nobody root 10364 Jan 28 04:12 LICENSE.txt
-rw-r--r--. 1 nobody quick 35749362 Jan 29 18:29 Magento-CE-2.0.2-2016-01-28-02-26-45.tar.bz2
-rw-r--r--. 1 nobody root 4108 Jan 28 04:12 nginx.conf.sample
-rw-r--r--. 1 nobody root 1427 Jan 28 04:12 package.json
-rw-r--r--. 1 nobody root 1659 Jan 28 04:12 .php_cs
-rw-r--r--. 1 nobody root 804 Jan 28 04:12 php.ini.sample
drwxr-xr-x. 2 nobody root 4096 Jan 29 18:30 phpserver/
drwxr-xr-x. 6 nobody root 4096 Jan 29 18:30 pub/
-rw-r--r--. 1 nobody root 4388 Jan 28 04:10 README.md
drwxr-xr-x. 7 nobody root 4096 Jan 29 18:30 setup/
-rw-r--r--. 1 nobody root 3731 Jan 28 04:12 .travis.yml
drwxr-xr-x. 8 nobody root 4096 Jan 29 18:30 update/
drwxr-xr-x. 2 nobody root 4096 Jan 29 18:30 var/
drwxr-xr-x. 27 nobody root 4096 Jan 29 18:30 vendor/
**ps -ef | grep httpd**
root 14165 1 0 11:43 ? 00:00:01 /usr/local/apache/bin/httpd -k start
nobody 18801 14165 0 18:35 ? 00:00:00 /usr/local/apache/bin/httpd -k start
nobody 18802 14165 0 18:35 ? 00:00:00 /usr/local/apache/bin/httpd -k start
nobody 18803 14165 0 18:35 ? 00:00:00 /usr/local/apache/bin/httpd -k start
nobody 18804 14165 0 18:35 ? 00:00:00 /usr/local/apache/bin/httpd -k start
nobody 18805 14165 0 18:35 ? 00:00:00 /usr/local/apache/bin/httpd -k start
root 18975 20796 0 18:43 pts/0 00:00:00 grep httpd
You also probably need to chwon user:group bin/magento on the files you have installed (the magento files). You need to figure out which user Apache is running as and change user:group above to that, something like apache:apache or whatever is in your httpd.conf file.
Post some more info (ls -al /path/to/magento and ps -ef | grep httpd for apache), but I'll bet that's your problem.
Edit
Based on your edited output, you need to chown the files to be owned by nobody, which is the user apache is running as. This can be done via chown -R nobody /path/to/magento
Note that you only show the .bz2 archive, but you need to chown the extracted files.
Also, you said you had to use the user that cpanel created when you installed it, so there you go.

Categories