Magento 1.9 Session Files clearing - php

Hi I currently have a magento 1.9 site which i use session storage as files as i find that using the database is very slow.
I currently have about 16 gig of session files which i want to delete.
If I run:
find . -name 'sess*' -mtime +7 -exec rm -f {} \;
The site grinds to a halt and then kills the database attached.
I then looked at garbage collection I changed:
session.gc_probability = 1
session.gc_divisor = 100
When I did this and restarted apache the site crashed too?
What would be the best way to remove these files?

You can delete the whole folder, from your Magento Root with the following
rm -rf var/session/*
Also, if you have enought RAM, you should consider using Redis to store session. Here is a nice tuto for Magento 1.

first go to the var/session folder of the magento root directory
find . -name "*" -print | xargs rm -rf
This command will delete all the data in the current folder without prompting you.

Related

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.

command to clean tmp directory on my web server

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.

php.log: what is the proper way to clear log file?

I'm troubleshooting a php log file that has expanded way too rapidly and used up a lot of disk space.
What is the best way to clear a log file?
is it a problem to simply delete the log file? And will that log file be recreated as php tries to write to it?
It is entirely safe to just delete the php.log file. It will be auto-created the next time it is needed.
On Linux you can do :
cat /dev/null > /var/logs/php.log
On Mac OS X 10.6 I deleted /var/log/apache2/error_log. Minor panic when it didn't re-appear upon refreshing my page. Just had to restart apache by going through the Sharing settings. Now she's back.
It IS safe to delete the log file by doing the following:
Delete the php.log file altogether.
If on Apache Restart the Server using "service apache2 restart" command
If on NGINX you do not have to restart the server
You can run this simple command from terminal(in WHM) or From SSH terminal.
find -name error_log -type f -exec rm -rf {} \;
This command will find all files with name "error_log" and remove it.
Enjoy.

Can I safely delete all content in /var/lib/php5?

I have over 5 million session files in /var/lib/php5
I would like to delete all files in this folder using rm *, however I'm not sure if there are other files other than the session files in that directory that should not be deleted.
Through SSH it took a few minutes to make the file count and I'm not sure I can navigate through there with all these random filenames.
The setup is ubuntu lucid linx, apache 2 and php5. In the most common of setups are there other folders / files in /var/lib/php5 that I should not delete?
Edit The reason I want to remove the files is because I moved session handling to a database and don't need any of the files anymore.
cd /var/lib/php5
find . -name "sess_*" -print | xargs rm -v
Let PHP's gc perform cleanup by itself. Find php.ini and change session.gc_probability to something bigger, save it and restart Apache (call any php script). It says here http://somethingemporium.com/2007/06/obscure-error-with-php5-on-debian-ubuntu-session-phpini-garbage
In Debian and Ubuntu, /var/lib/php5, where the session data is stored,
has permissions of drwx-wx-wt and should only be cleaned by a cron
script. So, the package maintainers disable automatic session garbage
collection.
Or you could try to put ini_set('session.gc_probability', 100); session_start(); (if your session.gc_divisor is equal to 100) in one of your scripts and call it. The best way is to put in empty php file, because it might perform cleanup for a very long period of time.
ps: I would also try to leave session.gc_probability 1 and set session.gc_divisor to 1. It should call gc at every run, but you need it just for a directory cleanup.
And check your cron /etc/cron.d/php5 - it should run every half an hour to purge session files in the /var/lib/php5/ directory.
pps: found interesting comment
This does not disable it (it is commented out). The default within the
engine is still used - phpinfo() shows the value to be 1. There is a
problem with garbage collection in Debian (and thus Ubuntu) but that's
due to PHP wanting to vacuum garbage that has already been removed by
the cron script. This causes an error that may be displayed on the
unlucky page.
in PHP7, this worked
cd /var/lib/php/sessions/
sudo find . -name "sess_*" -print |sudo xargs rm -v
If you're using CODEIGNITER, then try with following commands.
cd /var/lib/php/sessions/
sudo find . -name "ci_*" -print |sudo xargs rm -v
sudo find . -name "cises*" -print |sudo xargs rm -v
cd /var/lib/php5
sudo find . -name "sess_*" -print |sudo xargs rm -v
This has worked in my case .
On my ubuntu computer, at /var/lib/php5, there is a sess ID file:
sess_a7kjdaojmneuhcgslj
If I tried to remove this file using the command:
sudo rm sess_a7*
I got an error message saying there is no such a file. I had to use the full name as:
sudo rm sess_a7kjdaojmneuhcgslj
It worked. This is my two cents.

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.

Categories