How to Rearrange Your Package

Posted by Nessa | Posted in uncategorized | Posted on 20-12-2007

8

For some odd reason, Ubuntu and other Debian-based distros don’t like the standard x86 RPMs that most vendors package their software in. If you want to install a third-party RPM, you have to use alien to repackage the RPM into a .deb file before you can install it:

sudo apt-get install alien
alien -k your-rpm-file.rpm

This will convert the RPM to a .deb file. From here you can use dpkg to install it:

sudo dpkg -i your-rpm-file.deb

Whitespace is Evil

Posted by Nessa | Posted in uncategorized | Posted on 19-12-2007

2

I have this recurring nightmare of PHP apps that don’t trim whitespace when entering info. As a habitual copy and paster, it would be nice if some programmers could tend to my laziness and reluctance to type. Yes, Tony, I’m talking about you. Ever hear of the trim() function? Yea, it’s the Brazilian wax of PHP you inconsiderate bastard.

$text = "some text with extra spaces ";
$trimmed = trim($text);
echo $trimmed;

More of Using PHP for Server Info

Posted by Nessa | Posted in uncategorized | Posted on 17-12-2007

1

I’ll eventually get the whole thing up here, but I’ve been working on a simple server info script to help me and the other members of the system admin team keep up with the gazillion servers we have and all their different configurations. One of the reasons it’s taking so long (aside from my recent alcohol binges) is that it has to be portable to every server without the need for specific modifications, regardless of their setup. This eventually calls for using a simple if statement and empty() function to decide what info to output. Really, it’s so easy that I don’t even know why I’m posting it, but it kinda supplements this and this.

I’ll take the Ruby example I used earlier to find out what version of Ruby is installed:

$rubyver = exec("ruby -v |awk {'print $2'}"); ?>

Most of our servers don’t have Ruby installed, so I instead of getting an ugly ass error or nothing at all, I’d rather the script gracefully output its absence:

if (empty($rubyver)) {
echo "<font color='red'>Ruby is not installed on this server</font>";
} else {
echo "Ruby Version $rubyver";
}

This code fragment will check the output of the $rubyver variable, so if Ruby is not installed then the variable will return no value. Since the variable is then considered empty, the first echo statement is executing telling the viewer that Ruby is not installed on the server. If Ruby is installed on the server, then the second echo statement will run.

Importing CSV Into MySQL

Posted by Nessa | Posted in uncategorized | Posted on 08-12-2007

11

The newer versions of phpMyAdmin for some reason no longer include the option to import CSV files. If you are trying to import an Excel or other delimited file, first make sure that it’s in CSV format (with Excel you can export it as CSV). Then log into phpMyAdmin or your MySQL prompt and run this command:

load data local infile ‘/path/to/file.csv‘ into table yourtablename
fields terminated by ‘,’
enclosed by ‘”‘
lines terminated by ‘\n’;

With phpMyAdmin this is done within the ‘SQL’ section, indicated by the ‘SQL’ tab.

Common PHP Errors

Posted by Nessa | Posted in uncategorized | Posted on 07-12-2007

9

I’m going back to the basics here, you know, when you wrote your first PHP script and saw an ugly-ass error message pop up on your screen? Error messages are the best tool a programmer has.

Set up Error Reporting

Most PHP errors are straight forward, but there are times where you don’t see any which makes it very difficult to tell what the problem is.

The first step of PHP troubleshooting is to turn error reporting on. For security reasons you’ll want error reporting off by default, but if something goes wrong you’ll need the information for debugging. You can usually enable error reporting by adding this line to the problem script:

<?php error_reporting(E_ALL) ?>

Or you can add these lines to the root .htaccess:

php_flag display_errors on
php_value error_reporting 6143

This will usually display an error useful for troubleshooting, that is, if the software and your server configuration allows it.

Parse Errors

Parse error: parse error, unexpected T_STRING in……

This is a syntax error. Perhaps you forgot a semi-colon at the end of a line, or you forgot a double quote (“) or an end bracket (}) after you started one. For quote and semicolon issues, the problem is usually the line above the one reported in the error. For brackets, it may be at the end of the script.

Parse error: syntax error, unexpected $end in

You’re most likely missing a } somewhere. Make sure that each { you have is also closed with a }.

Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in..

There may be double quotes within double quotes. They either need to be escaped or brought to single quotes. It’s also possible that a new PHP statement was started before the previous was finished.

Header Errors

Warning: Cannot add header information – headers already sent by (output started at /home/vnessa5/www/errors.php:9) in….

Warning: Cannot send session cache limiter – headers already sent in somefile.php on line 222

Naturally, HTML will parse before PHP. The script is trying to send header information after you’ve already sent output to the browser. HTTP headers are required to be sent before any output from your script, which means that a header function must be placed before any html or even a white space. There are two solutions for this. Either (1) Set the header tags the top of the document, or (2) insert a header redirect by adding this to the very top of the page to force the output buffer:

<?php ob_start();

Then this at the very end of the page (not usually required)

ob_end_flush(); ?>

mySQL Result Source Errors

Warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in…

You need to take a look at the $result variable used to define the loop. More than likely there is a syntax error on the reported line before the $result field, or the value of $result does not exist.

Supplied argument is not a valid stream resource…

This is usually caused when your code is looking for a table or other resource in the database that does not exist.

Sessions are not being created or maintained

This can apply to any of the scenarios below:

(1) The program isn’t remembering your login
(2) Your shopping cart won’t hold items
(3) Your php script is redirecting like crazy
(4) “Call undefined function session_start” error
(5) PHP isn’t processing pages called by something like index.php?page=home&id=7

Your site is most likely dependent on register_globals. You can enable them by putting this line in your .htaccess (or just enabling in your php.ini if you have access):

php_flag register_globals On

Stream Errors

Warning: failed to open stream…

Warning: main(/index.php): failed to open stream: No such file or directory in…

This is usually because either the specified file is missing, or a file declared in a require() or include() function is missing. The easiest way to fix this is by re-installing the PHP program from a freshly-uploaded copy, or restoring the original config.php and just changing the db information. The include path may also be incorrect, but either way your script is looking for a file that isn’t there, or it is looking in the wrong place.

Warning: fopen(…): failed to open stream: Permission denied in…

This is a permissions and/or ownership issue. Try first setting the permissions to 777 just to see if the script will run. If so, you should narrow down the permissions to 775. If not, set the user/group to user:nobody.

Warning: <…> is not a valid stream resource…

Warning: fread(): supplied argument is not a valid stream resource in…

This is an error seen when trying to use functions like fopen(), fread(), feof(), etc. and are usually caused by an invalid or unavailable resource that is being called in the line specified. For instance, if the fread() function is returning this error, it could be that the file it is trying to access does not have the correct permissions or does not exist.

Warning: Failed opening….

Warning: Failed opening ‘…’ for inclusion (include_path=’.:/usr/local/lib/php’) in Unknown on line 0

Make the sure that the file mentioned (and its holding directorie) has read + execute permissions, and that the path to the file is correct. If not, you’ll need to add the path into the PHP code: (or .htaccess)

include(“/path/to/files”);
Blank PHP Pages

You go to a .php page, but it’s blank.

The scope of what can cause blank pages is very broad, but there are a few things to look at:

-Is error reporting turned off anywhere in the script or in the .htaccess? If so, turn it on to see what is happening (php_flag display_errors on), or add the lines at the top of this page into the script.

-Is the PHP script even generating any output (usually you can tell my finding the print function?

-Check the database connection, i.e, username, dbname, user added to db, etc.

-Try using the full <?PHP ?> tags, rather than the shorter versions <? ?>

Also, if the software is prebundled (like phpBB or Gallery), then the index or one of the include pages could be corrupted. Usually you can just replace the problem page with a working version from another installation.
Max Execution Time Error

You receive some variant of a “Max_execution_time” error when loading a page.

This is caused when a PHP script takes longer to execute than the server allows, but can be adjusted by adding a PHP directive to your .htaccess: (in seconds, 0 = unlimited) or modifying the value in php.ini.

php_value max_execution_time 0

Open_basedir Errors

Warning: Unknown(): open_basedir restriction in effect.

This is a protective feature of Apache that restricts PHP from accessing files/folders outside the user’s home directory. Most of the time this is due to an incorrect include path in one or more of the config files (which are usually mentioned). Look for something like this:

/includes/somefile.php
/admin/files/anotherfile.php

The heading / tells the filesystem that these folders are on the server root, and thus prevents PHP from accessing them. You can usually fix this by changing the path to these files to be absolute to their location:

/home/username/public_html/includes/somefile.php

or

./includes/somefile.php

Convert Database to UTF-8

Posted by Nessa | Posted in uncategorized | Posted on 06-12-2007

15

We seriously see a ton of customers coming in with the type of databases that are a nightmare to move over. When you’re dealing with special characters in a database, you have to make sure that the charset and collation are dumped *with* the database, so that when you move it to another server the tables and data create properly. The biggest annoyance so far is converting tables back to UTF-8, as when this is done through the MySQL shell or phpmyadmin is had to be done table-by-table. So, I wrote this simple PHP script to do it all at once:

<?php
// Database info

$dbhost = 'localhost';
$dbuser = 'db_user';
$dbpass = 'password';
$dbname = 'db_name';

//---------------

header('Content-type: text/plain');

$dbconn = mysql_connect($dbhost, $dbuser, $dbpass) or die( mysql_error() );
$db = mysql_select_db($dbname) or die( mysql_error() );


$sql = 'SHOW TABLES';
$result = mysql_query($sql) or die( mysql_error() );

while ( $row = mysql_fetch_row($result) )
{
$table = mysql_real_escape_string($row[0]);
$sql = "ALTER TABLE $table DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql_query($sql) or die( mysql_error() );
print "$table changed to UTF-8.\n";
}


mysql_close($dbconn);
?>

If course, you can adjust the ALTER TABLE statement to any character set and collation that you need.

Using an .htaccess with PHP Compiled as CGI

Posted by Nessa | Posted in uncategorized | Posted on 06-12-2007

3

We recently started installing suPHP on a few of our servers, which is an implementation of phpsuexec, only a ton faster and with less overhead. Yesterday I posted a tutorial on how to install suPHP on a PHP5/Apache server outside of EasyApache, but today I shall go over how to enable PHP directives in your .htaccess instead of using the php.ini.

First of all, if you’d rather use the .htaccess than the php.ini capabilities of a phpsuexec environment, then shame on you. But, we have some customers who are terrified of php.ini and would rather use the .htaccess. So what? Ok, well there is a workaround. Mr. Joye released a newer version of htscanner that now works with suPHP, and allows you to use php directives in your .htaccess just like you did when PHP was installed as and Apache module. All you have to do is download the tarball and install like any other PHP module:

wget http://pecl.php.net/get/htscanner-0.8.1.tgz

tar -xvzf htscanner-0.8.1.tgz

cd .htscanner-0.8.1

./configure && make && make install

Then add a loader for the resulting binary to your php.ini:

extension=”/htscanner.so”

From here, you can add your php_value and php_flag statements to your .htaccess as usual within <ifmodule> tags. I should note that the reason why you normally would not be able to overwrite php.ini values in your .htaccess is because .htaccess is an Apache file. Under phpsuexec/suPHP, PHP runs as a CGI module so Apache does not understand what those mean, so it will either ignore them all together or return a nasty 500 error.

Interactive RoR Tutorial

Posted by Nessa | Posted in uncategorized | Posted on 05-12-2007

2

So it’s official, I’ve started my Ruby programming classes in school and I’ll finally be able to see what it can do.  We started offering Ruby on some of our servers here and we’re all learning a little bit about it before we start offering it on our mainstream hosting…because you have to have at least one person who knows the feature well enough to help other people.  From what I’ve played with so far it seems to be the same logic as PHP and perl, so let’s just see how it goes.  For those of you wanting to try out some Ruby, there’s a nifty little Ruby emulator and mini-tutorial at http://tryruby.hobix.com/

SHOUTcast is Soooo Easy to Install — Are You Kidding Me?

Posted by Nessa | Posted in uncategorized | Posted on 04-12-2007

3

It surprises me how often people get nervous over installing things just because it’s new to them. The other day I had a guy harass our technical support for 20 minutes on the phone because he was wanted to install SHOUTcast. Honestly, before then I had never installed SC simply because I don’t use it, but all he had to do was RTFM (which is what I did)…but we all know that customers don’t like to read documentation and would rather you do everything for them.  Don’t get me wrong, I mean, part of my job is consulting with people on things like this and I enjoy doing it, but there’s a line to draw when the answer is right in front of you and you just don’t want to look.

So, here it is…a dumbed down and simplified tutorial for SHOUTcast. Yes, you can do this without the help of your hosting company, only you might need to the open your SC port in the firewall (don’t expect them to do this if you’re on a shared server)

Create a folder in the outside public_html, like sc or shoutcast, then download the Shoutcast server files from http://www.shoutcast.com/download/files.phtml:

mkdir sc
cd sc
wget http://www.shoutcast.com/downloads/sc1-9-8/sc_serv_1.9.8_Linux.tar.gz
tar -xvzf sc_serv_1.9.8_Linux.tar.gz
chmod 755 sc_serv
cd ..
chown -R user:user sc

This will put three files on the server:

README.TXT
sc_serv
sc_serv.con

From here, the configure the Shoutcast server and setup via the sc_serv.conf. However, take note of what port that Shoutcast server will be running on so it can be enabled in the server firewall. This is set in the sc_serv.conf file as well:

PortBase=8000

Once the configuration is complete the SC server needs to be started. As long as you have SSH access you can start it yourself. To start the server, type:

./sc_serv &

To stop the server, just do a killall -9 sc_serv. Note the ‘&’ after the command, which is one of the few techniques used to keep a process running even after your SSH session closes.

Upgrade to Subversion 1.4.x on CentOS

Posted by Nessa | Posted in uncategorized | Posted on 04-12-2007

4

I don’t know if it’s just me, but it seems that CentOS is kinda slow with their software updates. It’s not too big of a deal since CentOS is based off of RHEL so you can usually use RHEL 4-5 RPM’s, but those usually bombard me with failed dependencies. I find that the generic x86 RPMs do the trick, so for any of you trying to update your Subversion installations to a more recent version, here’s what you have to do on CentOS:

Optionally remove the old version by running yum remove subversion (assuming you installed with Yum)

wget http://the.earth.li/pub/subversion/summersoft.fay.ar.us/pub/subversion/latest/rhel-4/i386/subversion-1.4.5-1.i386.rpm
wget http://www.eu.apache.org/dist/apr/binaries/rpm/i386/apr-1.2.8-1.i386.rpm

wget http://www.eu.apache.org/dist/apr/binaries/rpm/i386/apr-util-1.2.8-1.i386.rpm

rpm -i apr-util-1.2.8-1.i386.rpm
rpm -i apr-1.2.8-1.i386.rpm
rpm -i subversion-1.4.5-1.i386.rpm

Some people may have to do a ‘yum install postgresql-libs’ as for some reason APR wants that stuff.