command to clean tmp directory on my web server - php

I was getting write failed: No space left on device (28) in my websites.
So I checked my tmp size using ssh and it was 100% full.
What command can I use through ssh to free up space in the tmp directory?

You just need to remove the files
rm -rf /path/to/tmp/*
You need to adjust /path/to/tmp with the path to your directory containing the temp files.
Warning: Please keep in mind, that all removed files are truly removed (= lost). So check all parameters first, before using this command.

cd /tmp
rm -fr *
With PHP I don't know if you have permission to delete the files:
$files = glob('/tmp/*');
foreach($files as $file){
if(is_file($file)){
unlink($file);
}
}

You can remove the file inside the tmp directory just go to the tmp directory
cd tmp/
and run the following command
rm -rf ./
It will delete all the directories inside that directory.
and
rm -rf *.*
It will delete all the files inside that directory.

rm -rf tmp/
Recursively deletes the directory tmp, and all files in it, including subdirectories. And better be careful with this command!!

To overcome your problem do what one of the posters has suggested.
But to avoid it in the future set up a cron job to tidy up periodically.
Look into using find system command to find old files that can be safely deleted (i.e. temporary files that will not be in use).

Test this command. It shows the files?
(replace /tmp/ if this is not the folder you wanted to clean up)
find /tmp/ -mtime +6 -exec ls {} \;
Edit your cron table:
$> crontab -e
Add a line:
* 6 * * * find /tmp/ -mtime +6 -exec rm -r {} \;
To permanently delete all files (daily) in /tmp, that are a handfull of days old.

Related

Magento cron changing folder permissions

We have a Magento website hosted on EC2 and for some reason var folder permissions keep on changing after some time. I think its happening because of magento default cron which runs every couple of hours and it seems it flushes the cache and at that time folder permission gets changed. Because of this website stops working.
Here is the screenshot of the error which we get once permissions are changed:
I am solving this by running chmod command through terminal but this is happening every couple of hours, so we need some permanent solution so that this problem is not repeated.
The probable answer is that the cron job executes under your user - and the directory is owned by apache (or www-data or nobody or whatever user your webserver runs as).
To get it to work, you could set up the cron job to run as the webserver user. Something like this:
su -l www-data -c 'crontab -e'
Alternatively, you could change the permissions to 775 (read-write-execute for the owner and group, and read-execute for others) and set the group ownership of the folder to the user running the cron job.
I think that you have some permission restriction in your folder, try these commands:
Magento File permissions:
chmod -R 644 ./*
find . -type d -exec chmod 755 {} ;
find . -type f -exec chmod 644 {} ;
chmod 550 ./mage

How to set Laravel to generate files with required permissions

I have a deploy BASH script which runs as Jenkins CI job. It runs under the jenkins user. Deploy needs to delete old directory and replace it with new one. But there is a problem. Laravel generates the files like session or cache with chmod 644 as www-data user. It means although Jenkins is in the www-data group he can not delete the generated files cause group has only read permission.
I found something about permissions in Laravel documentation, but it does not work cause it is only for storage/app folder.
The question is is there a way to force Laravel or PHP demon to generate files with required permissions e.g. 664? Hope it is. Thanks for any help.
My straight forward solution is to run endless background script as root which find and deletes required directories every 10 seconds. It runs as nohup so it is still running although I close the terminal.
while true
do
find /var/www -maxdepth 1 -type d -name 'deploy-old-*' -exec rm -rf {} \;
sleep 10
done
The final solution is to set up ACL privileges for parent directory via setfacl command.
setfacl -R -dm "g:www-data:rw" /path/to/dir
This ensures the generated files will inherit the ACL privileges from parent dir.

Enabling write permissions Ubuntu Server in var/www/image directory

I'm trying to get my php test upload script to work and was wondering what the command would be to allow files to be uploaded in ubuntu server in the var/www/image directory
What username will be uploading the files? Usually on Ubuntu the Apache web server username is www-data. You can check for sure by finding the web server process in a process list command and seeing which username under which it is running.
ps aux | grep apache or ps aux | grep httpd should give you that answer.
Then you will usually want to make that Apache username the owner of the directory and all files and directories within it:
cd /var/www/image
# recursively (all subdirs & files) set owner to www-data for current directory
chown -R www-data .
Ordinarily, the above should be enough, but if for some reason the directory, files or subdirectories do not have write permission for the owner username, that's easily fixed by changing the permissions to add that write access, like this:
cd /var/www/image
# recursively add "w"rite permissions for the "u"ser (owner) to current directory
chmod -R u+w .
cd /var/www/image
For file like image you don't need execution permission :
sudo chmod 664 *
If you have directories inside image and you want to apply permission :
sudo find . -type d -exec chmod 755 "{}" \;
This will recursively search your directory and chmod 755 all directories only.
Similarly, the following will chmod all files only (and ignore the directories):
sudo find . -type f -exec chmod 644 "{}" \;
File name with space case (thanks to Nicklas B)
find . -type f -print0 | xargs -0 chmod 644
Change edit group to www-data
chown -R (owner):www-data (folder)
And folder permission to 775
cd /var/www
sudo chmod 775 image
You'll be prompted to enter the admin password - do so, and hit [return].
Change edit group may solve most of the problems of permissions.
Changing do www-data and set permission to 775.

shell_exec and git pull

I was hoping someone could help, I have a PHP page which uses shell_exec to zip up a directory and run git pull to bring down recent repository changes.
$op = shell_exec("cd /home/user/git/$repo/$dir/; zip -r /home/user/archives/$dir.$datestamp.zip $dir; cd /home/user/git/$repo/$dir/; git pull");
The zip works fine. If I change git pull to for example git log or git status - within my shell_exec, this works also, and I can see the log file.
Just doesn't seem to like git pull.
I saw another similar post to this, but wasn't sure how it was achieved >> Shell_exec with git pull?
From your description in the comments it seems that the problem is that your apache user cannot write to the repository, which is clearly required when you use git pull. You have two courses of action:
Setup up Apache to run the script as another user (e.g. using suEXEC either on a VirtualHost or via userdir)
Change the permissions on your repository so the apache user can write to it
You should think carefully about the security implications of either choice, but the second option is probably easiest. If you don't already have such a group, you can create it with:
addgroup gitwriters
... and then add yourself and the Apache user to this group:
adduser [yourusername] gitwriters
adduser apache gitwriters
Then you can follow the instructions in another question to change the permissions on the repository. To reiterate those with some slight variations:
# Recursively, set the group ownership of every file and directory of your repository:
chgrp -R gitwriters /path/to/your/repo
# Recursively, make every file and directory of your repository readable and writable
# by the group:
chmod -R g+rw /path/to/your/repo
# Recursively, set the setgid of every directory in the repository. The setgid bit
# on directories means that files created in the directory will have the same group
# ownership as the directory.
find /path/to/your/repo -type d -print0 | xargs -0 chmod g+s
Then hopefully your git pull should work.

Making executable all PHP files (recursively)

I just downloaded MediaWiki software on my server for installation. After decompressing it, I noticed that PHP files were not executable.
I ran chmod +x *.php* (there are also .php5 files) but it didn't work in subdirectories.
How can I add the executable flag to all PHP scripts inside the MediaWiki folder recursively scanning the subfolders?
Thank you in advance.
Use bash in the MediaWiki directory
find . -iname "*.php" | xargs chmod +x
It does not work in subdirectories, because *.php* does not match any directories and hence does not include it.
Therefore you should use something like find ./ -iname "*.php*" -exec chmod 755 {} \; with the respective bits to set.

Categories