How to enable GD in PHP5 on Debian / Apache
Debian comes with a package called php5-gd.
Type the following to install this package.
apt-get install php5-gd
Restart Apache.
Done.
Try running "apt-get update" if you get the following error:
Err http://security.debian.org lenny/updates/main php5-gd 5.2.6.dfsg.1-1+lenny4
404 Not Found [IP: 130.89.149.225 80]
Failed to fetch http://security.debian.org/pool/updates/main/p/php5/php5-gd_5.2.6.dfsg.1-1+lenny4_i386.deb 404 Not Found [IP: 130.89.149.225 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
Setting up a user with restricted SFTP access
If you are running a Debian Linux webserver you may want to create users that can upload and download files from specific directories via SFTP. This post will explain how to set this up. First you need to create a user. If you want to create a user with the name 'webdev' then login as root and use the following commands at the prompt.
useradd webdev
Now you want to set the password for the new user 'webdev'
passwd webdev
Now we can create a home directory with the following commands.
mkdir /home/webdev chown webdev:users /home/webdev
Now we only want this user to be able to login to the server via SFTP. We can do this by changing the users shell to sftp-server.
usermod -s /usr/lib/sftp-server webdev
Next we need to make the sftp-server a valid shell by adding the line "/usr/lib/stfp-server" at the end of the file shells in the etc directory.
By default, a new user will be assigned to a new group with the same name as the new user. So in our case the user 'webdev' will belong to the group 'webdev'. In order to make our new user the owner of any other directory including sub-directories (such as the website document root) we can execute the following command.
chown -R webdev:webdev /var/www
So now the new user webdev will be able to login va SFTP and have owner permissions to their home directory as well the webserver document root directory.
References: The following page has more useful information about managing groups and users in Linux.
Further Restrictions
In the example above we made the directory that holds the website belong to the group "webdev". If you have other users on the system that you do not want to have access to the website data then it could be wise to assign the www directory to the group "www-data" and remove read access for other system users to the directory www.
This is in order to limit access from users except the web server that runs under the user www-data.
For example:
usermod -a -G www-data webdev usermod -g www-data webdev chown -R :www-data /var/www chmod -R o-r /var/www
By doing the above, we have added the user webdev to the group www-data (the same user that Apache normally runs as). Then we make www-data the default group for the user webdev, whereafter we make the www directory belong to the www-data group and set the permissions such that the files in that directory are group-readable but not world-readable.
How to enable mod_rewrite in Apache 2 on Debian Linux
Simple! Just run the command
a2enmod rewrite
Restart Apache 2 by running the command
/etc/init.d/apache2 restart
Done!
...if you are still having problems getting mod_rewrite to work, check that the config file for the site (located in the directory /etc/apache/sites-available/) does not have "AllowOverride None". If it does then change it to AllowOverride All instead in order to allow mod_rewrite commands to be set in the .htaccess file.
Setting up a Linux VPS
Rimuhosting offer good VPS packages and have datacenters in various locations around the globe. The main reasons for staying with Rimuhosting have been ease of setting up the service, server reliability and the level of support they provide. The support saff at Rimuhosting are allways very helpful and go the extra mile to help you out.
Here I am going to show how to set up a Linux VPS from Rimuhosting for web and email hosting. The VPS will be running Debian Linux 5.0 32-bit with Apache, MySQL, PHP. The server configuration is 512MB RAM, 4GB Disk space, 100GB data transfer limit per month, with a Webmin control panel. On a London based server this costs £27 per month (or £324 per annum).
After ordering your VPS package you will receive an order confirmation and later a VPS Setup email specifying the IP address of the server. In this example it took 2 minutes between the order confirmation email and the VPS Setup email.
Apache comes pre-configured and running on these servers so all you need to do now is to put the new server's IP address from the VPS Setup email in the address bar of your browser and you'll get a web page saying "It works!".
Now we need to configure the server to work the way we want it to...
The first time you log in to Webmin at https://<your IP address>:10000/ you will get a Security Failed message if you are using Firefox (presumably the same or similar will happen in other browsers too). This is normal and happens only because the browser can't validate the security certificate with a certificate authority. Just add exception for this certificate by clicking the relevant buttons in the browser window.
Setting up the Apache 2 Webserver using Webmin
In Webmin (we are using Webmin 1.490) you can install the relevant servers under the "Servers" option in the main menu on the left. Since Apache is active by default we don't need to do anything under "Apache Webserver".
Setting up the MySQL Database Server using Webmin
Click on the "MySQL Database Server" option and click the button that says "Start MySQL Server". We now want to set up our database that will be used by our website. On the following page and under "MySQL Databases", click on "Create a new database".
Specify a new database with the default character set and with the name "webdata" or whichever other name you prefer. Click on the "Create" button.
Now that we have created the database we need to specify users that will have access rights to the database. Clicking on "User Permissions" will reveal that there is already a database user called "root" set up and this user will have the same password as your VPS root password.
We are going to set up two users - one that has administrator rights and that can connect to the database from any host (We'll use this user later to connect from the MySQL Administrator tool), and another user with limited rights that will be used by our webiste.
Click on "Create new user" and specify the username "adminuser", set a strong password, set host to "any", details select all permissions by holding down shift while selecting permissions from the list. The click the "Create" button.
Next do the same for the "webuser", except only specify the following permissions: select, insert, update, delete, drop, alter, create tables, create temp tables and lock tables. Under host, specify "localhost" instead of "any" for the webuser.
We now need to configure the MySQL server to accept connections from any host so that we can use the "MySQL Administrator" tool (that runs on your local computer) to connect to the database. Go to the "MySQL Server Configuration" page and select "Any" under "MySQL server listening address" instead of the default 127.0.0.1. Click on Save.
Now restart the MySQL database server by clicking the "Stop MySQL Server" button in Webmin and then click on "Start MySQL Server".
With, for example, the tool MySQL Administrator, you should now be able to remotely login to your MySQL database server as the user "adminuser".
Finally, check that the server is configured to start the MySQL server at boot. In Webmin, go to the "Bootup and Shutdown" section under the System menu. Check that the MySQL deamon is configured to start at boot, and if it isn't change the setting accordingly.
Remote Server Admin and File Upload
We use SSH instead of telnet and SCP instead of FTP as SSH and SCP are more secure. With SSH and SCP passwords and data are encrypted before being sent over the Internet, whereas telnet and FTP don't encrypt.
SSH provides a method for securely logging into your VPS in order to administrate your server. Nothing is required to set up SSH on your VPS as SSH is configured to run by default. In order to login via SSH you need a client and for this we reccommend PuTTY. It is a free tool that can be downloaded from the Internet.
If your development computer is a Windows PC, then we suggest using the tool WinSCP for uploading files to your server. SCP is also pre-configured on your server so nothing is required to set it up there.
Other Webserver Settings
You also might want to consider how to enable mod_rewrite.
Thumbnail rollover image gallery with captions
This image gallery is implemented in PHP and uses javascript and CSS to display the image under the mouse pointer. The benefit of this rollover effect is that the site visitor can vey quickly browse the images in the gallery. The downside is that all images need to be loaded first. You can see a variant of this gallery in action at http://www.orcabags.co.uk/bags.php
Pleae feel free to use this code in any way you want. If you use it on your website then please let me know. Feedback is always welcome.
The images to display in the gallery are specified in a simple text file that defines the image filename and the caption to use for each image. In this example this file is called gallery1.txt and contains the following lines.
img1.jpg;Image caption 1 img2.jpg;Image caption 2 img3.jpg;Image caption 3 img4.jpg;Image caption 4
The gallery script assumes that you have two folders 'images' and 'thumbs' in the same place as the gallery1.txt file where all the images are stored. The thumbnails should have the same filenames as their full-size equivalents that are stored in the 'images' folder.
In order to implement this gallery on your page you need to insert the javascript block that you get from calling gallery_script() from gallery.php (gallery.php is listed further down on this page). The resulting string should be placed inside the html header.
<?php
// This is done in the page header...
echo gallery_script('galleries/', 'galleries/gallery1.txt');
?>;
Then, in the body of the page, you need to call the function gallery() at the place where you want the gallery to be displayed.
<?php
gallery('galleries/', 'galleries/gallery1.txt');
?>
The gallery is implemented in the gallery.php file and needs to be included at the top of your page.
<?php
require_once('galleries/gallery.php');
?>
The following code is the 'gallery.php' script.
<?php
define('GALLERY_TNWIDTH', '80');
define('GALLERY_TNHEIGHT', '80');
define('GALLERY_NUMCOLS', '2');
define('GALLERY_TNBORDER', '1');
function gallery_script($folder, $gallery, $stickyimage=true) {
$str = "<script language=\"JavaScript\" type=\"text/JavaScript\">\n";
$str .= "<!--\n";
$str .= "var preloadedimages = new Array();\n";
$str .= "var images = new Array();\n";
$str .= "var captions = new Array();\n\n";
$str .= "var blankimage;\n";
$str .= "blankimage = new Image();\n";
$str .= "blankimage.src = \"/img/blank.gif\";\n";
$num = 0;
if (($handle = @fopen($gallery, "r")) !== false) {
// Parse the "images.txt" file and display gallery...
while (!feof($handle)) {
$line = fgets($handle);
$params = explode(";", $line);
$str .= "images[$num] = \"".trim($params[0])."\";\n";
$str .= "captions[$num] = \"".trim($params[1])."\";\n";
$str .= "preloadedimages[$num] = new Image();\n";
$str .= "preloadedimages[$num].src = \"".$folder."images/".trim($params[0])."\";\n";
$num++;
}
fclose($handle);
}
$str .= "\nfunction showimage(id) { \n";
$str .= " var str = \"".$folder."images/\"+images[id];\n";
$str .= " document.getElementById(\"galleryimage\").src = str;\n";
$str .= " document.getElementById(\"caption\").childNodes[0].nodeValue = captions[id];\n";
$str .= "}\n";
$str .= "\nfunction hideimage(id) { \n";
if (!$stickyimage) {
$str .= " var str = \"".$folder."images/\"+images[id];\n";
$str .= " document.getElementById(\"galleryimage\").src = \"/img/blank.gif\";\n"; //str;\n";
$str .= " document.getElementById(\"caption\").childNodes[0].nodeValue = \"\";\n"; //captions[id];\n";
}
$str .= "}\n";
$str .= "-->\n";
$str .= "</script>\n";
return $str;
}
function gallery($folder, $gallery, $stickyimage=true) {
echo '<div style="border:0px dashed blue;padding:0px;margin-top:10px;height:1000px;position:relative;">';
$num = 0; $rows = 0;
if (($handle = @fopen($gallery, "r")) !== false) {
// Parse the "images.txt" file and display gallery...
while (!feof($handle)) {
$line = fgets($handle);
$params = explode(";", $line);
if ($num%GALLERY_NUMCOLS == 0) { /*echo "<br>";*/ $rows++; }
// draw thumbnail...
echo "<a style=\"padding:4px\" href=\"javascript:void(0);\" onmouseover=\"showimage($num);\" onmouseout=\"hideimage($num);\"><img src=\"".$folder."thumbs/".$params[0]."\" width=\"".GALLERY_TNWIDTH."\" height=\"".GALLERY_TNHEIGHT."\" style=\"padding:0px;border:solid ".GALLERY_TNBORDER."px black\"></a>";
if ($num == 0) {
$firstimage = $params[0];
$firstcaption = $params[1];
}
$num++;
}
fclose($handle);
}
$top = 95;
$left = 4;
if (!$stickyimage) {
echo '<br><br><br><br><div style="position:absolute;top:'.$top.'px;left:'.$left.'px;"><img id="galleryimage" border="0" src="/img/blank.gif">';
echo '<br><span id="caption"></span></div>';
} else {
echo '<br><br><br><br><div style="position:absolute;top:'.$top.'px;left:'.$left.'px;"><img id="galleryimage" border="0" src="'.$folder.'images/'.$firstimage.'">';
echo '<br><span id="caption">'.$firstcaption.'</span></div>';
}
echo '</div>';
}
?>
Disabling new user email notifications in Wordpress
Sometimes there is a need to disable the email that is automatically sent to newly registered users. One example might be if you are setting up new users in your blog and don't want these new users to be notified until the data-entry has been completed.
This can be done by modifying the file /wp-includes/pluggable.php. Locate the function wp_new_user_notification(). This starts on line 1144 in Wordpress version 2.8.5.
function wp_new_user_notification($user_id, $plaintext_pass = '') {
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
$message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
@wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
if ( empty($plaintext_pass) )
return;
$message = sprintf(__('Username: %s'), $user_login) . "\r\n";
$message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
$message .= wp_login_url() . "\r\n";
wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
}
Simply comment out the last line to disable the email..
// wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
TinyMCE not working in IE6 – solved
If you got TinyMCE working in most browsers except IE6 then first check how you reference the tinymce javascript file.
We had the following
<!-- TinyMCE -->
<script type="text/javascript" src="../js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas",
theme : "advanced"
});
</script>
<!-- /TinyMCE -->
...which is wrong. The correct code is
<!-- TinyMCE -->
<script type="text/javascript" src="/js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas",
theme : "advanced"
});
</script>
<!-- /TinyMCE -->
Note the src="/js/tiny_mce/tiny_mce.js" The problem was reported by a client that were using IE6 and could not upgrade to anything else for internal IT-policy reasons. We easily found the error by looking at the apache error logs on their virtual private server which showed.
[Thu Nov 19 13:20:13 2009] [error] [client xx.xx.xx.xx] Invalid URI in request GET /../js/tiny_mce/themes/advanced/editor_template.js HTTP/1.1, referer: http://www.example.com/admin/
It seems the client (the web-client, i.e. IE6) had introduced an extra slash '/' before the javascript filename. Hope this helps someone.