Laravel 5, Ngnix web server www-data permissions issue - php

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

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?

Wordpress (wp-cli): Unable to import file. Reason: The uploaded file could not be moved to wp-content/uploads. Permissions error?

I am running Wordpress on a Debian Based (Raspbian Buster) LEMP server. For the most part, everything is working fine except some problems with uploading photos using the command line interface. I have many pictures in my servers ~/Pictures folder that I am trying to upload to wordpress using the command line interface.
I navigate to the root directory of my wordpress installation and run the following command:
wp media import ~/Pictures/*.jpg
To which I get the following errors:
Warning: Unable to import file '/home/Danran/Pictures/img_0754.jpg'. Reason: The uploaded file could not be moved to wp-content/uploads/2020/03.
for each corresponding file. I am fairly certain that this is some sort of permissions error, but because I am a noob, I don't want to risk tinkering around with permissions in my wordpress directory without some sort of guidance by a pro.
How can I get rid of this error and successfully import my photos to wordpressing using the wp-cli?
could you please verify that you have write permissions in the uploads directory?
if u are using apache, normally what I do is set the www-data group as the owner of that directory
chown -R www-data:www-data /var/www/wordpress/wp-content/uploads
but, if what you want is to know if it is a permissions problem, simply set the permissions to 777 of that directory, and try to upload the files again... if you succeed, then it is a problem permissions, and setting them to 777 grants the cli access to that folder.
chmod 777 /var/www/wordpress/wp-content/uploads
Once you upload all of your photos with the CLI, you can change the permissions back to their original with chmod -R 755 on the same uploads folder you originally modified.
However, I usually set the permissions as follows ..
Directories: 644
Files: 755
find /var/www/wordpress/ -type f -exec chmod 644 {} \;
find /var/www/wordpress/ -type d -exec chmod 755 {} \;
EDIT1
Compare it with yours:
/var/www/wordpress/
root#www:/var/www# ls -l
total 212
-rw-r--r-- 1 www-data nogroup 420 Nov 30 2017 index.php
-rw-r--r-- 1 www-data nogroup 19935 Jan 1 2019 license.txt
-rw-r--r-- 1 www-data nogroup 7368 Sep 2 2019 readme.html
-rw-r--r-- 1 www-data nogroup 6939 Sep 2 2019 wp-activate.php
drwxr-xr-x 9 www-data nogroup 4096 Dec 18 17:16 wp-admin
-rw-r--r-- 1 www-data nogroup 369 Nov 30 2017 wp-blog-header.php
-rw-r--r-- 1 www-data nogroup 2283 Jan 20 2019 wp-comments-post.php
-rw-r--r-- 1 www-data nogroup 2898 Jan 7 2019 wp-config-sample.php
-rw-r--r-- 1 www-data root 3310 Jan 7 11:54 wp-config.php
drwxr-xr-x 8 www-data nogroup 4096 Jan 7 15:17 wp-content
-rw-r--r-- 1 www-data nogroup 3955 Oct 10 18:52 wp-cron.php
drwxr-xr-x 20 www-data nogroup 12288 Dec 18 17:16 wp-includes
-rw-r--r-- 1 www-data nogroup 2504 Sep 2 2019 wp-links-opml.php
-rw-r--r-- 1 www-data nogroup 3326 Sep 2 2019 wp-load.php
-rw-r--r-- 1 www-data nogroup 47597 Dec 9 08:30 wp-login.php
-rw-r--r-- 1 www-data nogroup 8483 Sep 2 2019 wp-mail.php
-rw-r--r-- 1 www-data nogroup 19120 Oct 15 11:37 wp-settings.php
-rw-r--r-- 1 www-data nogroup 31112 Sep 2 2019 wp-signup.php
-rw-r--r-- 1 www-data nogroup 4764 Nov 30 2017 wp-trackback.php
-rw-r--r-- 1 www-data nogroup 3150 Jul 1 2019 xmlrpc.php
/var/www/wordpress/wp-content
root#www:/var/www/wp-content# ls -l
total 28
drwxr-xr-x 3 www-data www-data 4096 Jan 7 14:52 cache
-rw-r--r-- 1 www-data nogroup 28 Jan 8 2012 index.php
drwxr-xr-x 2 www-data www-data 4096 Jan 7 11:55 languages
drwxr-xr-x 11 www-data nogroup 4096 Jan 8 16:24 plugins
drwxr-xr-x 4 www-data nogroup 4096 Jan 7 13:51 themes
drwxr-xr-x 2 www-data www-data 4096 Jan 7 14:52 upgrade
drwxr-xr-x 6 www-data www-data 4096 Jan 7 14:50 uploads

PDFTK Cannot Create User Directory

I am attempting to run pdftk as user www-data with the following command (as root):
sudo -u www-data /snap/bin/pdftk
But I am getting this error:
cannot create user data directory: /var/www/snap/pdftk/9: Read-only
file system
(Ubuntu 18.04, PDFTK installed with SNAP).
The /var/www/snap/pdftk and /var/www/snap/pdftk/9 directories have rw permissions for the www-data user:
drwxr-xr-x 3 www-data www-data 4096 Jan 23 15:44 ./
drwxrwxrwx 4 root root 4096 Jan 23 15:44 ../
drwxrwxr-x 4 www-data www-data 4096 Jan 23 15:44 pdftk/
drwxrwxr-x 4 www-data www-data 4096 Jan 23 15:44 ./
drwxr-xr-x 3 www-data www-data 4096 Jan 23 15:44 ../
drwxrwxr-x 2 www-data www-data 4096 Jan 23 15:44 9/
drwxr-xr-x 2 www-data www-data 4096 Jan 23 15:44 common/
lrwxrwxrwx 1 www-data www-data 1 Jan 23 15:44 current -> 9/
I need to be able to run PDFTK as www-data user because I want to call PDFTK from a PHP script served by apache2, as in:
<?php
`/snap/bin/pdftk file1.pdf file2.pdf cat output file3.pdf`;
NOTE: the pdftk wrapper for PHP, mikehaertl/php-pdftk, doesn't seem to want to save files either. I tried the ->saveAs() function but no file is generated, even when attempting to write to /tmp, which should have full write permissions. No errors in apache2 error log, so I'm betting it's the same issue.
I encountered exactly the same problem! In particular, I didn't face this problem when using pdftk in Ubuntu 18.10 with pdftk already included back, but faced this problem on a server with Ubuntu 18.04.
My workaround is NOT to install pdftk via snap. I installed pdftk with debian packages, following the instruction at https://www.webdesign101.net/install-pdftk-on-ubuntu-18-04-bionic/.
Everything then just works fine.

webhook php file work from terminal but not from bitbucket

I'm using bitbucket to host my git repository, the repo holds a test website at the moment, I have created a bitbucket webhook, so when I push to the bitbucket repo, the changes show up as live on the digitalOcean VPS, in other words.. when bitbucket receives a push, it calls the webhook php file, and that php file has a shell script that pulls from github..
the hook file
Hook path : /var/www/html/hook.php
the site folder
Site path : /var/www/html/webhooks/
the hook.php file looks like so
<?php
echo "________PHP_AUTO_PULL________";
$output = shell_exec('git -C ./webhooks/ pull https://userName:password#bitbucket.org/userName/repo.git master');
echo "<pre>$output</pre>";
?>
when I do this in terminal
php hook.php
it does the job normally, and it pulls..
but the problem is, webhooks only shows this reply
________PHP_AUTO_PULL________
indicating that it does no pull, yes I have checked, no pull occured, how to make the hook execute the file normally?
permissions and owners are provided in these listings
listing for /var/www/html/
drwxrwxr-x 3 www-data www-data 4096 Mar 28 09:21 ./
drwxrwxr-x 3 www-data www-data 4096 Mar 3 16:49 ../
-rwxrwxrwx 1 www-data root 200 Mar 28 09:05 hook.php*
-rw-rw-r-- 1 www-data www-data 20 Mar 3 16:49 info.php
drwxr-xr-x 3 root root 4096 Mar 28 09:03 webhooks/
listing for /var/www/html/webhooks/
drwxr-xr-x 3 root root 4096 Mar 28 09:03 ./
drwxrwxr-x 3 www-data www-data 4096 Mar 28 09:21 ../
-rw-r--r-- 1 root root 295 Mar 27 15:13 content.html
drwxr-xr-x 8 root root 4096 Mar 28 09:03 .git/
-rw-r--r-- 1 root root 444 Mar 27 15:13 index.html
-rw-r--r-- 1 root root 963 Mar 27 15:13 menu_1.html
-rw-r--r-- 1 root root 13 Mar 28 09:03 number.txt
my webserver is nginx
any idea why it works from terminal, but bitbucket can't have it to work?
I have managed to solve it, using :
echo shell_exec("/usr/bin/git pull https://userName:password#bitbucket.org/userName/repo.git master 2>&1");
the 2>&1 part was helping me to see errors about permissions of folders, I used
chown -R www-data .git/
and it's working fine.

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?

Categories