Code snippets, tech tricks and other bits and bobs

Mass delete email (or any files) over SSH

I get a lot of automated mail that I do need for analysis, but it can be a pain to delete it all.

Admittedly, if I set filters better it would be easier, but even so, if you have thousands of emails in a folder and you need to delete them using webmail remotely, it is time consuming.

So let's do it with SSH.

WARNING. This shit is dangerous. If any of this doesn't make sense DON'T DO IT!

Login to your server via SSH

Change to your mail dir (this is the location on my CentOS 5 box)
Note the mail address format - @ is escaped with \, period are replaced with underscores:

$ cd /home/{domain username}/mail/.{user\@domin_tld}/{folder}/

If you are cleaning your inbox the folder is

cur

Test the message selection before you delete!
I'm deleting Cron messages from a server. The message source for these all contain 'Subject: Cron <user\@server\>' (I've anonymised the server address)
Note the search is regex, so special chars must be escaped

$ grep -l 'Subject\: Cron \<user\@server\>' * > grep.txt

Then I open the grep.txt file, copy some filenames and then open those files to double check I'm selecting the right messages.

Once I'm satisfied I'm not selecting anything I shouldn't:

$ grep -l 'Subject\: Cron' * | xargs rm -f

Shazzam. From >3000 emails to 150 in two minutes. I should probably check that account more often...

Filed under  //   *nix   CentOS 5   Regex   SSH  

cgi_wrapper: Per-domain php.ini under php-fastcgi (Plesk 9.5.2 + CentOS 5)

I recently discussed per-directory php directives utilising htscanner, allowing setting values with php_flag and php_value in .htaccess files.

While this is an enormous step forward in term of granular control for the webmaster, it doesn't allow complete control as some things don't work when set in .htaccess as I discovered when trying to set apc config values.

The problem stems from being unable to use vhosts.conf to specify php_admin_value as you would running PHP as an Apache module, and having to resort to other methods. I did attempt http://kb.parallels.com/9059, but it was a complete failure, because Paralells neglect to mention that you must be running Plesk 9.5.2+ in order to utilise cgi_wrapper as directed.

I had been avoiding updating Plesk, as last time I'd done so it had mangled quite a few things, but I bit the bullet, and this time the process seems to have been entirely successful.

From http://kb.parallels.com/9059

The hotfix is as follows:

It is possible to use custom php.ini for domains if php as FastCGI mode is used on it. 
It is necessary to apply hotfix. Fix is compatible with Parallels Plesk Panel versions 9.x.

Use the following instruction for applying hotfix.

1. Download hotfix from the attachment

# wget http://kb.parallels.com/Attachments/13985/Attachments/cgi_wrapper

2. Locate file cgi_wrapper

# cat /etc/psa/psa.conf |grep CGI_PHP CGI_PHP_BIN /var/www/cgi-bin/cgi_wrapper/cgi_wrapper

3. Create a backup of this file

# cp /var/www/cgi-bin/cgi_wrapper/cgi_wrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper_orig

4. Replace cgi_wrapper with the downloaded one

# cp cgi_wrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper

5. Copy php.ini to the configuration directory of domain

# cp php.ini /var/www/vhosts/domain.tld/conf/

6. Change permissions on the configuration directory of the domain

# chmod 0755 /var/www/vhosts/domain.tld/conf

7. Restart apache

# /etc/init.d/httpd restart
Should the download become unavailable, I have included the contents of the updated cgi_wrapper here for convenience:
#!/bin/sh

domain=`fgrep -m 1 "$UID" /etc/passwd| awk -F\: '{print $6}' |awk -F"/" '{print $5}'
PHPRC=/var/www/vhosts/$domain/conf/php.ini

[ -f ${PHPRC} ] || PHPRC="/etc/php.ini"

export PHPRC
exec /usr/bin/php-cgi -c "$PHPRC"

Filed under  //   CentOS 5   PHP   Plesk 9   Plesk 9.5.2+   cgi_wrapper   php-fastcgi   php.ini  

htscanner: Enabling php_admin directives in .htaccess under php-fastcgi (Plesk 9.x + CentOS 5)

[update 201107061858]
For per-domain php.ini under php-fastcgi see here
[/update]

Under Plesk running php-fastcgi  it is not possible to use php_admin_flag and php_admin_value directives in .htaccess files on a per-directory basis. Luckily this can be easily enabled with the htscanner extension.

All the information to get this working is out there, but as per usual, you have to piece it together from a variety of sources to get a complete picture. Here I shall provide a single complete set of instructions that have been proven to work on a CentOS5 box with Plesk 9 - although Plesk should be an irrelevance here, I mention it for completeness.

I am using PuTTY as my SSH client.

In order to install and use htscanner v1.0,0 , you must have the following things (aka dependancies):

PHP Version: PHP 5.1.0 or newer
PEAR Package: PEAR Installer 1.4.8 or newer

Odds of having >=PHP 5.1 without PEAR are slim to my knowledge, and installing it is beyond the scope of this post.

Got those? Ready to go!

To provide absolute clarity for inexperience bods, # is the SSH command prompt and shouldn't be included in typed commands! And if you REALLY need your hand held, hit [enter] after each line. Just so you don't sit there wondering why nothing is happening...

Download and extract htscanner

  1. SSH to your server with root access.
  2. Download the latest version of htaccess from http://pecl.php.net/package/htscanner currently 1.0.0
    #  wget http://pecl.php.net/get/htscanner-1.0.0.tgz
  3. Untar the file:
    # tar -xvsf htscanner-1.0.0.tgz

Compile and run installation

  1. Change to the newly created directory:
    # cd htscanner-1.0.0
  2. Run phpize:
    # phpize
  3. Run the configuration script with --enable-htscanner switch
    # ./configure --enable-htscanner
  4. Run make to build the installer:
    # make
  5. Run make install to actually install the extension
    # make install

Add htscanner settings

Either:

For systems that support it, you can simply copy htscanner.ini to your php conf.d directory. This is probably somwhere along the lines of /etc/php.d on CentOS 5 or perhaps for others /etc/httpd/conf.d but if you are having trouble finding it, run

# locate conf.d

You will find a bunch of them though, so make sure it's the right one. You can also try simply googling your *nix distribution name and conf.d - for example "centos 5 conf.d"

Or:

If you are running SuSE or another flavour of Linux which doesn't support this, you can add the contents of htscanner.ini to your php.ini file. To locate yours try the tips above for finding the php conf.d directory
The htscanner.ini file currently looks like this:

[htscanner]
extension="htscanner.so"

; The configuration file htscanner needs to scan for php_* directives
config_file=".htaccess"

; The fallback docroot when htscanner can't determine the current docroot
default_docroot="/"
default_ttl=300

; Stop when an error occured in RINIT (no document root, cannot get path_translated,...)
stop_on_error = 0

; Warn when an option cannot be set
verbose = 0

You should check the contents of your htscanner.ini file though, to be safe.
This is simple enough, but for the sake of completeness and for any poor n00bs out there, to do this, and then add to php.ini using 'vi' text editor: 

  1. Open htscanner.ini:
    # vi docs/htscanner.ini
  2. Highlight all text to copy to the clipboard
  3. Exit vi by simply typing
    :q

    and hitting [Enter]
  4. Open php.ini:
    # vi /etc/php.ini
  5. Scroll to the bottom of the file with [pgdn] key
  6. Enter editing mode by hitting [i] key
  7. Press [->] Left arrow key to ensure you are at the end of the last line
  8. Hit [Enter] to start a new line
  9. Right-click the mouse to paste the htscanner.ini contents
  10. Exit editing mode by hitting [Esc] key
  11. Save and quit vi:
    :wq
     
    and hit [Enter] 

Restart Apache

On CentOS 5 and similar distros:

#  service httpd restart

For other *nix versions, y'know, google it. Why not, sounds like fun to me?

And voila. You should now be up and running with per directory php_admin directives in your .htaccess files.

Filed under  //   *nix   .htaccess   CentOS 5   PHP   Plesk 9   php-fastcgi  

*nix - Locate files modified after specified datetime (CentOS 5)

This is basic stuff, but it took me a little while to find decent instructions and they still needed tweaking so for my own reference as much as anything:

Commands

touch

and
find

Syntax

$ touch -t mmddhhmm /path/to/tmp/file
 $ find /path/to/search/ -newer /path/to/tmp/file -print

[month][day][hour][minutes], so 10:25pm on Apr 19th is

04192225

Example

$ touch -t 04192225 /var/www/vhosts/{domain}/timesearch/findresult.txt
 $ find /var/www/vhosts/{domain}/httpdocs/components/com_community/ -newer /var/www/vhosts/{domain}/timesearch/findresult.txt -print

Output:

/var/www/vhosts/{domain}/httpdocs/components/com_community/controllers/frontpage.php
 /var/www/vhosts/{domain}/httpdocs/components/com_community/templates/agiles_social/activities.index.php
 /var/www/vhosts/{domain}/httpdocs/components/com_community/templates/agiles_social/params.ini

Filed under  //   *nix   CentOS 5  

*nix - Format and mount a drive - ext4

Great little guide here:

Skullbox.net | Adding a Hard Drive in Linux

But before you format a drive with the ext3 file system, be aware that ext4 is now available, and if it's good enough for Google...

Computerworld |  The best Linux file system of all?

Ext4 may not be available for mkfs on your system. I don't know about your system, but for CentOS 5, I did the following:

# yum provides "*/mkfs.ext4"

This asks yum to check which available packages provides ext4 support for mkfs:

e4fsprogs-1.41.9-3.el5.x86_64: Utilities for managing the fourth extended (ext4) filesystem
Repo: base
Matched from:
Filename: /sbin/mkfs.ext4

I'll have that then!

# yum install e4fsprogs-1.41.9-3.el5.x86_64

DO NOT rely on this being the best and most up to date package - check yum. If you are concerned about the validity of some repositories, check out the priorities plugin for yum

But now you can format your disk with ext4

mkfs -t ext4 /dev/{drive}

Bonza. Time for a beer...

Filed under  //   *nix   CentOS 5   Hardware   SysAdmin   ext4 file system  

After Plesk update Apache fails - can't get fastcgi file info: dynamic, errno: 2

It seems the Plesk updater when used from the interface can be a bit of a shitter.

Updating from 9.2.2 to 9.2.3 all my sites on the server went down. No biggie, I'll just restart Apache.

Starting http: Syntax error on line 59 of /var/www/vhosts/{domain}/conf/httpd.include:


can't get fastcgi file info: dynamic, errno: 2


[FAILED]

Oh awesome...

Luckily some research revealed that the problem lies in /etc/psa/psa.conf where the php-cgi location gets changed to 'dynamic'

So, to fix it edit /etc/psa/psa.conf and search for the line

CGI_PHP_BIN dynamic

and replace 'dynamic' with the location of php-cgi for your OS flavour.

Examples (CHECK before using!)
CentOS 5: /usr/bin/php-cgi
Suse 10.3: /usr/bin/php-cgi5
Debian: /usr/bin/php5-cgi

So, for my server which is CentOS 5:

CGI_PHP_BIN /usr/bin/php-cgi

then rebuild config and restart Apache:
# /usr/local/psa/admin/sbin/websrvmng -v -a
# service httpd restart
Starting httpd:                                       [ OK ]
#

Source: http://forum.parallels.com/showthread.php?t=89939

Filed under  //   *nix   Apache   CentOS 5   PHP   Plesk 9   SSH   SysAdmin  

mod_suphp kills your website on Plesk 9

This doesn't only happen in Plesk apparently as I have seen it reported with cPanel.

Essentially, after installing mod_suphp, if your website starts throwing error 404s when calling a file and shows the Plesk default page when visiting the site root, it's probably because the www prefix wasn't properly configured during domain setup. Essentially this is the same problem and solution as my last post, which was handy, cos I already had the solution once I worked out what the hell was going on, so see that post for the answer:

http://blog.absolutedisaster.co.uk/www-prefix-not-working-centos-5-plesk-922

Filed under  //   *nix   Apache   CentOS 5   Error 404   Plesk 9   SSH   SysAdmin   mod_suphp  

'www' prefix not working - CentOS 5 & Plesk 9.2.2

If you don't tick the 'www' checkbox when creating a domain in Plesk, the CNAME record for www.{domain}.{tld} is not created.

However, simply changing the setting or creating the CNAME record is not enough to get it working - Apache config needs rebuilding.

So to fix the situation, follow these steps:

STEP 1

EITHER:

Change domain settings:
Plesk > Domain > {domain}.{tld} > Domain Administrator Access
Domain name: Check the 'WWW' box
[OK]

OR:

Create CNAME record:

Plesk > Domain > {domain}.{tld} > DNS Settings > Add Record
Record Type: CNAME
Enter domain name: www
Enter IP address: {Main IP address for domain}

[OK] 

STEP 2

Rebuild Apache config:

Login to server via SSH as 'root'

# /usr/local/psa/admin/sbin/websrvmng -v -a

OR

# /usr/local/psa/admin/bin/websrvmng -v -a

DONE

If you had 'www' enabled or the CNAME record in place already the change will be reflected instantly, otherwise you will have to wait for the DNS changes to propogate.

Thanks to GeeksPal for pointing out the config rebuild!
http://www.geekspal.com/plesk/‘www’-prefix-not-working-centos-5-plesk-9-2/

Filed under  //   Apache   CNAME   CentOS 5   DNS   Plesk 9   SSH   SysAdmin  

Multiple FTP users in Plesk 9

By now, Plesk is supposed to have built in support for creating multiple FTP users through the web interface, but it doesn’t seem to have materialised. So for the mean time, SSH is still the way forward:

Create ftp user with their own home directory:

# /usr/sbin/useradd -d $HTTPD_VHOSTS_D/your_domain/some/location -s /bin/false USER_NAME

Set the ftp user’s password:

# passwd USER_NAME

Add user to group psacln:

# /usr/sbin/usermod -G psacln USER_NAME

Change folder permissions for home directory:

# chmod 755 $HTTPD_VHOSTS_D/your_domain/some/location

Change ownership to user and group:

# chown USER_NAME:psacln $HTTPD_VHOSTS_D/your_domain/some/location

Set execute permission on the httpdocs folder for the domain:

# chmod 751 $HTTPD_VHOSTS_D/your_domain/httpdocs

At this point you have a valid FTP user who can connect. But only to their own folder. Now I want users with FTP write access to httpdocs so I can give 3rd party support access on occasion and later revoke it without having to change my master password. Also, all their actions will be logged as their user, so we have a data trail in case of things going pear shaped. This is as far as Parallels' guide goes, so we have to do a couple more steps:

Set the user’s home directory to the webroot:

# /usr/sbin/usermod -d $HTTPD_VHOSTS_D/your_domain/httpdocs/ USER_NAME

Grant write permissions to the group on the webroot:

# chmod g+w $HTTPD_VHOSTS_D/your_domain/httpdocs/

And we are done!

Where $HTTPD_VHOSTS_D is your vhosts path (typically /var/www/vhosts on the CentOS 5 boxes I’ve used) and USER_NAME is your chosen… errr, wossitcalled…

‘/some/location’ should be a non public access directory, e.g. /var/www/vhosts/your_domain/web_users/USER_NAME

These intstructions are tested on CentOS 5 / Plesk 9. They should work for other *nix flavours, but as with everything on t'net, excercise caution, and on your own head be it.

http://kb.parallels.com/en/415

Filed under  //   *nix   CentOS 5   FTP   Plesk 9   SSH   SysAdmin