I want to generate a file by batch.php, which is linked to the button in this page:
<?php
echo "Hello world!";
'cat 123 > uploads/cat.txt';
echo "Bye world!";
?>
Clicking on the button does launch batch.php, however, cat.txt is NOT generated on the server side.
Does anyone know how to fix this? Maybe I need to setup some permission for the folder?
Edit 1:
ps aux|grep -E 'apache|www-data|http|php' returns:
root 12269 0.0 2.1 278256 10796 ? Ss Aug02 3:38 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
www-data 12301 0.0 0.8 97728 4044 ? S Aug02 3:27 nginx: worker process
www-data 12302 0.0 0.8 98104 4296 ? S Aug02 3:54 nginx: worker process
www-data 12303 0.0 0.9 97940 4520 ? S Aug02 3:46 nginx: worker process
www-data 12304 0.0 0.9 98132 4544 ? S Aug02 3:47 nginx: worker process
timur 14888 0.0 0.1 11712 932 pts/0 S+ 15:04 0:00 grep --color=auto -E apache|www-data|http|php
www-data 18748 0.0 8.7 298196 43668 ? S Sep07 0:12 php-fpm: pool www
www-data 18749 0.0 6.4 286660 32432 ? S Sep07 0:11 php-fpm: pool www
www-data 21100 0.0 8.3 296204 42048 ? S Sep05 1:02 php-fpm: pool www
ls -l returns:
total 20
-rw-r--r-- 1 root root 84 Sep 12 08:33 batch.php
-rw-r--r-- 1 timur timur 5120 Sep 12 09:00 generateFile.exe
-rw-rw-r-- 1 timur timur 138 Sep 12 08:28 index.html
drwxrwxrwx 2 timur timur 4096 Sep 12 15:03 uploads
I granted 777 to uploads, and I tried all the possible owner names and owner groups (ie, timur:timur, www-data:www-data and root:root), but none of them works...
The key is to write exec('cat 123 > uploads/cat.txt') or shell_exec('cat 123 > uploads/cat.txt') instead of 'cat 123 > uploads/cat.txt'.
Then, although drwxrwxrwx 2 timur timur 4096 Sep 12 15:03 uploads works, assigning 755 and www-data:www-data (reserved to PhP) to uploads is more correct.
Related
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);
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?
I am having some permission issues with Nginx and Php fastcgi when trying to get to the php file. I am using 5.5.15 and Nginx 1.6.0 in Redhat 7 .
My php file is very simple for now.
<?php
echo "\nscript owner : ".get_current_user()."\n";
$myFile = '/usr/share/nginx/html/test.log';
$fh = fopen($myFile, 'a') or die("can''t open file");
?>
Get current user will result in : "myuser"
The error that I am getting is the following:
2014/08/26 22:47:14 [error] 6424#0: *16 FastCGI sent in stderr: "PHP message: PHP Warning: fopen(/usr/share/nginx/html/test.log): failed to open stream: Permission denied in /usr/share/nginx/html/test.php on line 19" while reading response header from upstream, client: XXXXXX, server: XXXXXXX, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "XXXXXXX"
Here is the permissions for the directory /usr/share/nginx( all of the parent directories have x permissions):
drwxrwsrwx. 4 myuser myuser 4096 Aug 26 22:32 html
Running the following commands:
$ ps aux | grep "nginx: worker process"
myuser 6423 0.0 0.3 111228 3880 ? S 22:36 0:00 nginx: worker process
myuser 6424 0.0 0.5 111228 5428 ? S 22:36 0:00 nginx: worker process
myuser 6480 0.0 0.0 112640 980 pts/0 R+ 22:41 0:00 grep --color=auto nginx: worker process
$ ps aux | grep "php"
myuser 5930 0.0 0.1 128616 1860 pts/0 T 21:09 0:00 vi /etc/php-fpm.conf
myuser 5931 0.0 0.2 128628 2052 pts/0 T 21:09 0:00 vi /etc/php.ini
myuser 5933 0.0 0.1 128616 1864 pts/0 T 21:13 0:00 vi /etc/php-fpm.conf
myuser 5934 0.0 0.1 128616 1860 pts/0 T 21:14 0:00 vi /etc/php-fpm.d/www.conf
myuser 5935 0.0 0.1 128616 1864 pts/0 T 21:15 0:00 vi /etc/php-fpm.conf
root 6313 0.0 2.4 544732 25208 ? Ss 22:25 0:00 php-fpm: master process (/etc/php-fpm.conf)
myuser 6314 0.0 0.8 544732 8356 ? S 22:25 0:00 php-fpm: pool www
myuser 6315 0.0 0.8 544732 8328 ? S 22:25 0:00 php-fpm: pool www
myuser 6316 0.0 0.9 545076 9892 ? S 22:25 0:00 php-fpm: pool www
myuser 6317 0.0 0.9 544860 9452 ? S 22:25 0:00 php-fpm: pool www
myuser 6318 0.0 0.9 544860 9212 ? S 22:25 0:00 php-fpm: pool www
myuser 6483 0.0 0.0 112640 976 pts/0 R+ 22:47 0:00 grep --color=auto php
My Server looks like the following:
server {
listen 80;
root /usr/share/nginx/html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
And in the nginx.config I am using the same user: "user ec2-user;"
I have also changed the /etc/php-fpm.d/www.conf file to have the same user and group.
user = myuser
group = myuser
So, both Nginx and PHP are running on the same user "myuser".
All the directories up to where the log file and the php file are located(/usr/share/nginx/html) have x access and that user has 777 access to that html directory.
Not sure what I am missing. I have been searching online for 2 days now but no luck.
I suppose rights are correct, but you have 3 line of code in test.php, but due to error this is not true :) Check what file you run.
"in /usr/share/nginx/html/test.php on line 19"
Today earlier my nginx server was 100% CPU usage, the process using all CPU was php-cgi.
I login and kill all php-cgi with this command.
kill -s 9 PID
Now after restarting my server is not working, I see the message "No input file specified.". I google this message, but nothing works, I suppose I have just to start php-cgi again, but can't find how start it.
UPDATE
If I run top command, I can see php-cgi running
1049 root 20 0 336m 20m 10m S 0.0 0.3 0:00.37 httpd
1051 apache 20 0 219m 5472 608 S 0.0 0.1 0:00.55 httpd
1080 root 20 0 20888 1180 592 S 0.0 0.0 0:00.02 crond
1182 root 20 0 19256 976 384 S 0.0 0.0 0:00.00 nginx
1183 nginx 20 0 19856 3176 1364 S 0.0 0.1 0:05.65 nginx
2326 apache 20 0 337m 13m 2512 S 0.0 0.2 0:02.07 httpd
2331 apache 20 0 337m 13m 2564 S 0.0 0.2 0:02.10 httpd
2696 root 20 0 96656 3820 2944 S 0.0 0.1 0:00.18 sshd
2701 root 20 0 12084 1696 1336 S 0.0 0.0 0:00.03 bash
2808 apache 20 0 337m 12m 1988 S 0.0 0.2 0:00.22 httpd
2864 root 20 0 12632 1228 948 R 0.0 0.0 0:00.29 top
2908 ulisses 20 0 183m 11m 6704 S 0.0 0.2 0:00.07 php-cgi
Run ps aux command, also show cgi-php
root 1049 0.0 0.3 344532 20700 ? Ss 14:39 0:00 /usr/sbin/httpd
apache 1051 0.0 0.0 224920 5472 ? S 14:39 0:00 /usr/sbin/httpd
root 1080 0.0 0.0 20888 1180 ? Ss 14:39 0:00 crond
root 1182 0.0 0.0 19256 976 ? Ss 14:43 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1183 0.0 0.0 19856 3176 ? S 14:43 0:05 nginx: worker process
apache 2326 0.0 0.2 345492 13900 ? S 16:56 0:02 /usr/sbin/httpd
apache 2331 0.0 0.2 345480 13944 ? S 16:57 0:02 /usr/sbin/httpd
root 2696 0.0 0.0 96656 3820 ? Ss 17:41 0:00 sshd: root#pts/0
root 2701 0.0 0.0 12084 1696 pts/0 Ss 17:42 0:00 -bash
apache 2808 0.0 0.2 345164 12848 ? S 17:52 0:00 /usr/sbin/httpd
ulisses 2929 0.8 0.1 187732 11976 ? S 18:06 0:00 /usr/bin/php-cgi -c /var/www/vhosts/teclasap.com.br/etc/php.ini
root 2932 0.0 0.0 10480 932 pts/0 R+ 18:06 0:00 ps aux
You can kill all php-cgi by
sudo killall -9 php-cgi
Environment
PHP -V output: PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May 2 2011 23:00:17) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
cat /etc/issue output: Ubuntu 11.04
Apache2 -V ouput: Server version: Apache/2.2.17 (Ubuntu) Server built: Sep 1 2011 09:31:14
Browser About output: Firefox 6.0.2
PS -AUX Ouput
root 2943 0.0 0.3 206420 12428 ? Ss Sep19 0:20 /usr/sbin/apache2 -k start
www-data 18658 0.0 0.2 208552 11096 ? S Sep25 0:00 /usr/sbin/apache2 -k start
www-data 18659 0.0 0.3 208976 12036 ? S Sep25 0:00 /usr/sbin/apache2 -k start
www-data 18660 0.0 0.3 210532 12476 ? S Sep25 0:00 /usr/sbin/apache2 -k start
www-data 18661 0.0 0.3 210276 11820 ? S Sep25 0:00 /usr/sbin/apache2 -k start
www-data 18662 0.0 0.2 206948 10236 ? S Sep25 0:00 /usr/sbin/apache2 -k start
www-data 20037 0.0 0.3 208976 12128 ? S 08:22 0:00 /usr/sbin/apache2 -k start
www-data 20039 0.0 0.3 209132 11748 ? S 08:23 0:00 /usr/sbin/apache2 -k start
www-data 20120 0.0 0.3 209004 12000 ? S 09:04 0:00 /usr/sbin/apache2 -k start
File Permissions
drwxr-xr-x 2 www-data www-data 4096 2011-09-26 15:24 .
drwxr-xr-x 4 www-data www-data 4096 2011-08-26 11:31 ..
-rw-r--r-- 1 root root 161976 2011-08-26 16:26 market.txt
-rw-r--r-- 1 root root 0 2011-09-26 14:55 test1.txt
-rw-r--r-- 1 root root 0 2011-09-26 14:55 test2.txt
-rw-r--r-- 1 root root 0 2011-09-26 14:55 test3.txt
-rw-r--r-- 1 root root 0 2011-09-26 14:55 test4.txt
-rw-r--r-- 1 root root 0 2011-09-26 15:02 test5.txt
Code
rename($file, "$dest/$file");
Question
When I run the above code on the files listed in the File Permissions section above, it properly moves the file from its current location to a new location and removes the original. How is this possible when apache2 is running as www-data and the files are owned by root and only have read access for non-root users? On the PHP documentation it says:
Warnings may be generated if the destination filesystem doesn't permit chown() or chmod() system calls to be made on files — for example, if the destination filesystem is a FAT filesystem.
Does rename() call either of those system functions during the process? If so, why? Not that it matters anyway as www-data should not be able to chown/chmod a file owned by root anyway.
Can anyone explain to me how this is occuring?
Additional Information
I have tried this with the PHP script owned by root and by www-data and it works.
I tried to provide as much pertinent info as possible but let me know if you need anything else.
Moves don't "remove" originals, unless the move takes place across filesystem boundaries. Within a single filesystem, a move simply rewrites the relevant directory entries so it APPEARS that you've copied/deleted the file, but all you've done is a bit of housekeeping. Since www-data owns the directories in question, it can rewrite the directory entries representing those files all it wants, and never touch the actual files.
When moving files you are not editing the files themselves, but rather the directory they are part of. In your case that directory is owned by www-data (the apache process)