Have common sessions for multiple servers/subdomains with memcached

I’ll write a tutorial, with the steps to have your php sessions handled by memcached.

The idea is simple: instead of having the sessios written to a file, php will write them to memcache daemon, that will listen on a port on a server you desire. That means, many servers will be able to use the same sessions.

Most of what I will say, I read from this site, but I will describe, in addition to that, how to have more than one computer using the same memcache session (there is described for one computer only).

So, the simple steps are:

1. Install the memcached on the server that will host the sessions:

In Debian:

$ apt-get install memcached

2. Configure memcached:

$vim /etc/memcached.conf

other systems may save memcached.conf in different places. If you don’t find it, do:

$find /etc -name memcached

Pay attention at these elements:

-u root

this means memcached will run under root user. Change it to nobody.

-p 11211

this is the port memcached will listen on. Be sure to firewall it, and to allow acces to it just to those machines you trust.

-l 127.0.0.1

this is the address memcached will listen on. In this example (and on some systems, by default) there is just localhost. That means, other computers will not be able to connect to it. Change this to the ip of the server, or comment it, to listen on all interfaces.

Now restart the memcached:

$ /etc/init.d/memcached restart

Check if memcached listens to the desired port and interface:

$ netstat -vuptan | grep memcached

You should see a simmilar line:

tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN     1527/memcached

That means, memcached listens on port 11211, on all interfaces, and accepts connections from everywhere (unless you fix that with the firewall).

2. Install memcache client:

these operations should be done on all clients (servers that will use the memcache session handler).
$ apt-get install php5-memcache

$ pear install pecl/memcache

Answer yes, and enter, until it is installed.

if you have problems with the last step, try these (and repeat the command until success):

(if it tells that pear command is not recognized)

$ apt-get install pear

(if it tells something regarding the size):

$ apt-get install php5-dev

3. Configure php to use the memcache session handler:

Edit the /etc/php5/apache2/php.ini

$ vim /etc/php5/apache2/php.ini

At the end of the document add these:

extension=memcache.so
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"

In the last line, instead of 127.0.0.1 put the ip adress of the machine on which memcached runs.

Additionally, search for this lines:

session.save_handler = files

comment it with a semicolon („;”).

and also, any other „session.save_path” directive other than that inserted by you should be commented by a semicolon.

Now restart the apache webserver:

$ /etc/init.d/apache2 restart

4. Check whether the sessions get saved. Open a browser, and test if you can login, and if the login is persistent. If so, your server successfuly uses memcache.

Now, to have common sessions for two servers (for example: http://www.example.com and subdomain.example.com), you have to do one more thing. Edit again the /etc/php5/apache2/php.ini:

$ vim /etc/php5/apache2/php.ini

Find the directive „session.cookie_domain =”

Make it look like this:

session.cookie_domain = ".example.com"

(notice the dot in front of it). Instead of example.com use your own domain. Don’t do that if you have more domains pointing to the same servers. Instead, program your own session handler, in php.

Restart again the apache:

$ /etc/init.d/apache2 restart

And check once again.

If again no success, you can try the following things:

* analyze the cookies for the session (see if there is a dot „.” in front of the domain name). Try to edit the cookies, and see if they work.

* try to run memcache webserver , but not as a daemon, with verbose output:

– stop the memcached daemon:

$ /etc/init.d/memcached stop

– start memcache in debugging mode (use the proper port numbers):

$ memcached -u nobody -p 11211 -m 64 -vv

Now, login from different places, and look through the output to see how sessions are handled.

* Last, you can check one more time if you followed all these steps. If so, join the #memcached channel on freenode irc server, and ask there. As I found, the team is very friendly.

Anunțuri

Lasă un răspuns

Completează mai jos detaliile tale sau dă clic pe un icon pentru a te autentifica:

Logo WordPress.com

Comentezi folosind contul tău WordPress.com. Dezautentificare / Schimbă )

Poză Twitter

Comentezi folosind contul tău Twitter. Dezautentificare / Schimbă )

Fotografie Facebook

Comentezi folosind contul tău Facebook. Dezautentificare / Schimbă )

Fotografie Google+

Comentezi folosind contul tău Google+. Dezautentificare / Schimbă )

Conectare la %s

%d blogeri au apreciat asta: