Installation of Memcache in PHP on Ubuntu/Debian

Happy new year all! Just a quick post about how to install Memcache in PHP on an Ubuntu or Debian server (I think it will apply to Redhat based servers, just substitute the apt-get for yum).

Log on to your server as root (either directly or using sudo su), then install the Memcache daemon through apt:

apt-get install memcached

Once installed you need to install the Memcache module into PHP. This is done through PECL which in turn is installed through PEAR. When they are both installed, you need to run the command:

pecl install memcache

This will install the memcache extension, and once done will tell you to add the the line “extension=memcache.so” into your php.ini file. I prefer keeping these in separate files (so that the php.ini file can be updated). The easy way to do this is:

echo "extension=memcache.so" > /etc/php5/conf.d/memcache.ini

And finally, reload Apache

/etc/init.d/apache2 reload

If successful, Memcache will now appear in your phpinfo().

Virtual Hosts for Development with Apache on Ubuntu

I do a lot of development on Ubuntu, as I often have multiple projects on the go which are nothing to do with each other, it’s often easier to create separate virtual hosts on my local development machine. This means that when they are ready for the “real world”, they are already set up as isolated sites at the root of their domain (rather than in a subdirectory of an existing site).

In order to do this, you need to create a new virtual host in your Apache config. Create a new file in the directory /etc/apache2/sites-available and open it in your favourite editor. It doesn’t matter what the file is called, but it’s best to keep it descriptive. We’ll call this project “mysite”, so the file can be called “mysite”. In the file we need to configure the Apache virtual host.

<VirtualHost 127.0.0.1>
ServerName mysite.localhost
DocumentRoot /var/www/mysite/public/
</VirtualHost>

In the VirtualHost tag, you put the IP, seeing as I only want this for local loopback (for development) I have just put 127.0.0.1. The ServerName is the URL that you use to connect to the site and the DocumentRoot is where the public documents are stored. This is a very basic set up, so there are many more options you can add.

To make the site enabled, you create a symbolic link to the file from the sites-enabled directory.

cd /etc/apache2/sites-enabled
ln -s ../sites-available/mysite mysite

You now need to add the subdomain (mysite.localhost) to the list of hosts, so open /etc/hosts in your favourite editor and append the line:

127.0.0.1 mysite.localhost

And then restart Apache:

sudo /etc/init.d/apache2 restart

Now you should be able to visit http://mysite.localhost on the local machine (assuming the directory does actually exist).

This should also be similar on MacOS and other linux Distros, but the file locations (particularly for Apache) will vary.

suPHP + Userdir on Ubuntu

Recently I’ve had the need to combine suPHP with the userdir mod for Apache on Ubuntu. By default they don’t play nice together. So here is a quick guide on how to get it working.

If you have installed a standard LAMP server (there are many guides on how to do this), you now need to install the suphp package for apache, this is called libapache2-mod-suphp:

$ sudo apt-get install libapache2-mod-suphp

Once that has been installed open the file /etc/suphp/suphp.conf in your favourite text editor. Find the line that has the docroot on, and change the docroot so that it is just “/”. This means that the suphp engine will parse anywhere in the file system, and not just in the standard html directory, thus allowing users to have their own.

docroot=/

You may also want to change the security options as appropriate, just change the “false” to “true” of the applicable ones the enable them. This is worth experimenting with. Further down you will want to set the “check_vhost_docroot” is set to false, this again is to do with the fact that userdirs are not in the vhost’s document root.

check_vhost_docroot=false

Finally you have the min_uid and min_gid properties. These are worth altering if you still want to be able to have a website running as www-data (such as the default website). If this is the case, change them both to the uid and gid of www-data (33 by default). It is not recommended to allow suphp to run as root, so do not set it to 0.

min_uid=33
min_gid=33

Finally, you need to enable the mods suphp and userdir, and disable the mod php5, this is done with two commands, and then restart apache2:

sudo a2dismod php5
sudo a2enmod userdir suphp
sudo /etc/init.d/apache2 restart

This should then allow you to run php scripts as the user who created them. To test this, create a new php file that contains:

<?php
system(id);

This will give you information of the user that the php process is running as. I recommend changing the ownership and retrying it, just make sure sure suphp is running as it should be.

Happy New Year and Updates

With it being the festive season, I have not done a huge amount of interest recently. I’m hoping to have a few things for you soon. Currently I have been experimenting with Drupal, having never used it (properly) before, this is a real learning curve.

Also I am working on getting suPHP to work with userdir, to allow users of a server to have their own webspace in a secure fashion. I will be blogging about this when it is done.

However, I have found a few things that may be of interest:
Things you probably didn’t know about PHP – Some really interesting and neat things you can do with PHP.

PHP Object Generator – Generates class structures and objects, so you don’t have to