Subversion Stuff

To establish a subversion repository anew:

svnadmin create /var/svn/myrepos
svn mkdir -m “creating the trunk dir” file:///var/svn/myrepos/repo1
svn import -m “Adding first directory” /var/www/vhosts/mydomain.com/wordpress file:///var/svn/myrepos/repo1
**Note that if you don’t specify a directory to copy, subversion is happy to add the one you are in to the repo.
svnadmin verify /var/svn/myrepos/

Subversion is not so forthcoming with the meaning of errors. If there is corruption in one of the revisions, it will just output the error when you do a

svnadmin verify repos/
[…]
* Verified revision 907.
* Verified revision 908.
* Verified revision 909.
svnadmin: Corrupt representation ‘907 21815 45 30922 158d3e72732f45bf6f02919b22fc899a’
svnadmin: Malformed representation header

This means that Revision 909 is good but Revision 910 is corrupted. The next step is to dump all the repos from 911 through the end of the repo which is in this case revision 947.

$ svnadmin dump –incremental -r 911:947 master/ > repo_name.svn_dump
* Dumped revision 911.
* Dumped revision 912.
* Dumped revision 913.
[…]
* Dumped revision 947.

svnadmin create /path/to/newrepo
svnadmin load /path/to/newrepoy < repo_name.svn_dump svnadmin verify repos/ * Verified revision 0. * Verified revision 1. * Verified revision 2. [...] * Verified revision 945. * Verified revision 946. * Verified revision 947.

Posted in Linux | Leave a comment

Grepping (and egrepping) with Style

Grep is a powerful tool. It can do many things well. It can also be used in conjunction with other tools. If you are new to grep, it is a supercharged search tool. The basic format of the command is:

grep [options] PATTERN [FILE…]

Many options are available and described in the man page, so I won’t go into them here. I will just list some of my favorite grep commands. (who am I kidding? I just want a place to copy them from when I am not at my own computer)

#This command searches for a string and includes the preceding 5 lines and the 5 lines that follow it and shows the line numbers as well.

grep -n -B5 -A5 searchstring /tmp/filename

#This command uses grep to traverse the output from a find command.
find . -exec grep searchstring {} \; -print

#This command allows you to output a sorted list of connections from the month of Mar 2013
grep Mar/2013 /var/log/apache2/access.log |  awk ‘{ print $1 }’ | sort -n | uniq -c | sort -rn | head

#Finding warnings and errors in your log
egrep -w ‘warning|error|critical’ /var/log/messages

Posted in Linux | Leave a comment

zend_mm_heap corrupted messages in apache logs

zend_mm_heap corrupted
if php5.2. google for patch
if php5.3 increase apc memory size in apc.ini

Posted in Linux | Leave a comment

Access Control Lists

Add ACL for apache user:

First see what ACLs exist already:

root@server1 mysite.org]# getfacl /var/www/vhosts/mysite.org/uploads
getfacl: Removing leading ‘/’ from absolute path names
# file: var/www/vhosts/mysite.org/uploads
# owner: user1
# group: user1
user::rwx
group::rwx
mask::rwx
other::r-x
default:user::rwx
default:group::rwx
default:mask::rwx
default:other::r-x

If apache user isn’t listed, do this:

setfacl -R -m u:apache:rwx /var/www/vhosts/mysite.org
setfacl -R -m d:u:apache:rwx /var/www/vhosts/mysite.org

Once you have them working as you want, back them up for safe keeping:
getfacl -R –absolute-names /foo/bar > /home/user/faclbackup

Posted in Linux | Leave a comment

Tune MySQL

wget http://www.day32.com/MySQL/tuning-primer.sh

sh tuning-primer.sh

Posted in Linux | Leave a comment

Common rewrite rules

The easiest way to redirect one site to another is by adding the following to the Virtualhost:

Redirect 301 / http://newsite.com/

Note: When AllowOverride is set to allow the use of .htaccess files, Apache will look in every directory for .htaccess files. Permitting .htaccess files causes a performance hit, whether or not you actually even use them! The .htaccess file is loaded every time a document is requested.

Redirect https traffic to http

By redirecting everything that comes in on port 443:

RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteCond %{HTTP_HOST} ^(www.)mydomain\.com$ [NC,OR]
RewriteRule ^/(.*)http://www\.mydomain\.com/$1 [R,L]

or by checking the x-forwarded-proto header:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://www.domain.com$1 [R=301,L]

Redirect http traffic to https

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Redirect non-www to www:
(If you see an Internal Server error, check the logs for APC errors)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

or

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect www to non-www:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Redirect to maintenance page that displays image .jpg

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/maintenance.html$
RewriteCond %{REQUEST_URI} !/image.jpg$
RewriteRule $ /maintenance.html [R=302,L]

 

Redirect for purpose of changing displayed URL
RewriteCond %{SERVER_NAME}  ^fubar.com$
RewriteRule ^/$ http://fubar.net [L]

#To make all letters in the url lowercase
RewriteEngine on
RewriteBase /
RewriteMap insensitive tolower:
RewriteRule ^[\/]*(.*)$ /${insensitive:$1} [R,L]

 

#For phpMyAdmin
RewriteCond %{HTTPS} off$
RewriteRule ^/phpmyadmin https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]$

RedirectMatch 301 ^(/phpmyadmin[^/]*/.*)$ https://hostname$1

Posted in Linux | Leave a comment

Number of processors/number of cores

cat /proc/cpuinfo | grep processor | wc -l

Counts the number of processor lines

Posted in Linux | Leave a comment

compression on your site

Ensure that the browser sends the Accept-encoding: gzip, deflate header

Add the following to your .htaccess file:

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/js
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or, compress certain file types by extension:


SetOutputFilter DEFLATE

Test it with this:

curl -s -I -H “Accept-Encoding: gzip” http://www.vickistan.com

[root@web01 wherever.com]# curl -s -I -H “Accept-Encoding: gzip” http://www.vickistan.com
HTTP/1.1 200 OK
Server:
X-Powered-By: PHP
X-Pingback: http://vickistan.com/xmlrpc.php
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Content-Type: text/html; charset=UTF-8
Accept-Ranges: bytes
Date: Mon, 30 Jul 2012 14:55:44 GMT
X-Varnish: 1170541803
Age: 0
Via: 1.1 varnish
Connection: keep-alive

or use this site:

http://www.gidnetwork.com/tools/gzip-test.php

Posted in Linux | Leave a comment

PHP email script to test email

Sometimes you gotta test outgoing email. If you have php installed, it is as easy as adding the following into a file called something like emailtest.php and executing with php emailtest.php. Check the email logs to ensure it went out.

< ?php
$to = "someone@somewhere.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@somewhere-else.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
Posted in Linux | Leave a comment

Cool MySQL command list

# to see the full CREATE TABLE statement and show table engine
show create table table_name\G

#To see full CREATE TABLE statement on all tables in a database
export database=”dbname”;for table in `mysql -s –column-name=false -e “SHOW TABLES” $database`;do mysql -e “SHOW CREATE TABLE $table” $database;done

#convert a table from MyISAM to InnoDB
ALTER TABLE table_name ENGINE=InnoDB;

#watch the processlist fly by (from the command line/not a MySQL command)
watch “mysql -e ‘show processlist;'”

#List mysql users from the command line.
mysql -u root -B -N -p -e “SELECT user, host FROM user” mysql

#Show grants for particular myaql user instead of the one you are logged in as
SHOW GRANTS [FOR user]

#Add a new user with only SELECT and UPDATE on a specific database
grant SELECT,UPDATE on database.* to ‘user’@’%’ IDENTIFIED BY ‘PASSWORD’;

#Add a new user with all privileges on a specific database
grant all on database.* to user@’IP_ADDRESS’ IDENTIFIED BY ‘PASSWORD’;

#List all tables in all databases
USE information_schema;
SELECT TABLE_SCHEMA, TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA NOT IN (‘mysql’, ‘information_schema’, ‘performance_schema’) ORDER BY TABLE_SCHEMA, TABLE_NAME;

#Dump all databases to a file
mysqldump –all-databases > /tmp/all.sql

#To import a database
mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

#To turn on slow query logging from the mysql prompt

#Set the long query time to whatever is appropriate, the default is 10 seconds
mysql> SET GLOBAL long_query_time=1;
#Set the location that the slow query log should go to, make sure the folder path exists and that the mysql user has write permissions to it
mysql> SET GLOBAL slow_query_log_file=’/var/lib/mysqllogs/slow-log’;
#And finally set a variables to enable logging
mysql> SET GLOBAL slow_query_log=1;

#To Repair and Optimize all MySql tables:
mysqlcheck –u root –p –auto-repair –check –optimize –all-databases

#To get the sizes of your databases
SELECT table_schema “Data Base Name”, sum( data_length + index_length ) / 1024 / 1024 “Data Base Size in MB” FROM information_schema.TABLES GROUP BY table_schema;
#For extra points, tune your Innodb Buffer Pool accordingly.

#To dump tables t1, t2, t3, t7 from the database test:
mysqldump test t1 t3 t7 > tabledump.sql

#Determine whether a table is indexed:
show index from [table name]

Posted in Linux | Leave a comment