Archive for the ‘Web Server’ Category

Moving a live site with minimal downtime

Sunday, March 30th, 2008

Abstract

This article will explain how to move a live and running site from one server to another with minimal downtime. Please note, that in order to follow you will need ssh permissions on both servers and high-level permissions (root) on the server to which you are transferring.

Overview of the procedure

When transferring a live site from one server (a shared hosting account for example) to another server (a VPS or dedicated) a major concern is downtime of the live website. In order for the website to get transferred you would need to copy the webite over to the new server and change the DNS pointers to the new server for the domain your website uses. For a static website this is as easy as that, but what if your website is dynamic and users can change database records and upload images to the website. A simple copy of the website will not work, because some images and database records will be lost.

To avoid this problem we are going to mount all the directories, that users are able to update using NFS and will allow network access to the mysql database from the old server.

For the example I will be using the debian operating system. You will be able to follow easily using other setups as well.

Preparing the new server

Ok, so you received a new server and now you need to prepare it for the website. I will not discuss the process f configuring Apache/PHP/MySQL or any other technology your website uses, as this is very user specific.

First you need to install the NFS tools. On Debian the packages you will need are nfs-server and nfs-client. You need to install them. After they are installed you will see, that an /etc/exports file appeared. This is where you will be adding mountable directories.

Let’s say I want to transfer a domain name example.com from the old server with IP of 62.62.62.62 and on the new server (63.63.63.63) I plan to use it at /home/admin/domains/example.com/public_html . Basically all the web-accessible files will be stored in the public_html directory.

I would recommend, for testing purposes to try and make this directory mountable and mount it on the old server. To do this add the following line to /etc/exports file

/home/admin/domains/example.com/public_html 62.62.62.62(rw,no_root_squash)

then type

/etc/init.d/nfs-kernel-server restart

Note, that  your nfs restart script could be different.

This will allow the old server with IP 62.62.62.62 to mount the public_html directory.

Now, log in to the old server and mount this directory somewhere. I would like to mount it into /tmp/example.com

mkdir /tmp/example/com

mount 63.63.63.63:/home/admin/domains/example.com/public_html /tmp/example.com

At this point the folder will be mounted and ready. Now, you should put a maintenance notice on the website, so that users do not update it anymore while you are copying the website and the database to avoid any corruption of the system.

Start copying your website to the new server (copy the website to the /tmp/example.com directory. Since this is an NFS share it could take a while to copy, so use cp with -v parameter, so you could see the files being copied.

Database Copy

While the website is being copied you need to create a database dump on the old server. To do this simply login through the shell and type

mysqldump –user USERNAME –password DATABASE > database_dump.sql

This will dump all your data into the database_dump.sql file.

Copy this file to the new server using any method you prefer. Then on the new server type:

mysql –user USERNAME –password DATABASE < database_dump.sql

 This will restore the database on the new server. It may take a long time to restore depending on your MySQL settings and the size of the database itself.

Enabling remote access to the new database server

By default MySQL does not allow remote connections and all users are created to be able to login only from the local server. To fix this you will need to grant all permissions to the database you use to the user, who could connect from the old server (62.62.62.62 in our case). To do this from the command line use:

grant all on DATABASE.* to USER@’62.62.62.62′ identified by ‘PASSWORD’

Now your old server can access the database at the new server.

Also, please check the mysql configuration file /etc/my.cnf or /etc/mysql/my.cnf and comment the line which says

skip-networking

otherwise you will not be able to use the networking at all with mysql.

Mounting updatable directories 

Ok, so your website is now copied to the new server and you are ready to configure mountable directories.

Let’s assume, that in our case we have 1 updatable directory, which is images and it is in the root of the website.

You need to update the /etc/exports file with the following:

 /home/admin/domains/example.com/public_html/images 62.62.62.62(rw,no_root_squash)

And commend out the other line we added previously to stop mounting the public_html folder and restart the nfs server.

Then go to the 62.62.62.62 server. I will assume, that the website was in /var/www/htdocs/example.com

You will need to type the following in order to mount the images directory:

mv /var/www/htdocs/example.com/images /var/www/htdocs/example.com/img_bkp

mkdir /var/www/htdocs/example.com/images

mount 63.63.63.63:/home/admin/domains/example.com/public_html/images /var/www/htdocs/example.com/images

Assign your permissions to the directory (depending on your setup you might need to change the owner of the directory as well).

Restarting the website and waiting

Now, you need to reconfigure the old website at 62.62.62.62 to use the new database server at 63.63.63.63. Configure the website respectively and fill in the username and password of the new mysql server and then remove the maintenance mode from the webite. It should continue working as normal, but the database and user updatable directories are now at the new server.

After this you can point the dns to the new server and once it propagates your website will be running off the new server. This usually takes 12-48 hours.

Virtual Hosting With Lighttpd

Sunday, December 23rd, 2007

Abstract

This article describes setup of lighttpd web server for virtual hosting support using evhost module. The presentation operating system is openSuSE 10.3, however it does work in a similar way on other linux distributions.

Checking module status

There are several ways lighttpd could support virtual hosting. Lighttpdeven supports MySQL based virtual hosts, we will explain how to set it up using the evhost module. This module comes standard in openSuSE, however it is disabled by default. Go to /etc/lighttpd/modules.conf and find the line for evhost module. Please read the comments posted in this file, which say, that only 1 virtual hosting module could be loaded at a time. If simple vhost module is included, comment it out with a # sign and remove the # sign from the include line for evhost. In the end it should look like this:

evhost module included

Configuration of the evhost module

Now, that the module is loaded it needs to be configured. At this point you need to decide where do you want to store all your site files (of course if you want sites to link to user public_html directories you could use a default path and then create symlinks to public_html directories). We are not dealing with public shared hosting and I personally don’t like to store website files on the root file system (because sites can grow unpredictably and use up all space causing issues), so I changed the server root to /home/www and assigned correct permissions there (lighttpd:lighttpd). The vhosts dirparameter was left un-touched as it is fine to append vhosts to server root. The end result should look similar to this:

Modified lighttpd.conf file to change server root

At this point we need to look at the evhost.conf file in the conf.d directory. It configures the patters, which tell lighttpd how to process the domain name and where to get the files from. Please note, that this is just a routing file and you don’t need to put domain-specific configuration inside of it (like mod_rewrite or custom log, etc). The comments inside this file are very self-explanatory and you can configure the service how you want to. For me the result is show below:

evhost path pattern displayed

What this config does, it it appends the domain name + tld to the vhosts dir, which is /home/www/vhosts and also appends the htdocs dir where the end files will be located. The htdocs put here is used for backups and to prevent a mess. I backup only the htdocs directory and put the tgz file in the respective domain + tld directory. If you do not need this, you can skip this directory.  So if we take an example to domain www.torqzone.com, then the path to it would be /home/www/vhosts/torqzone.com/htdocs .

If I wanted to create a torqzone.net domain I could simply symlink the torqzone.net to torqzone.com and it would work instantly.

Custom vhost configuration

As I mentioned previously you will need some specific settings for every domain (log files, etc). I like to put these settings into vhosts.d dir (although you are not obliged to to id, but it keeps things clean).

An example of such a configuration for torqzone.com is shown below:

Torqzone vhost sample configuration

The reason why the $HTTP["host"] is not == to the domain is because I want subdomains to point to the same place as the main domain is pointing. So if I type www.torqzone.com it works the same as torqzone.com.

The log filename links to the log for this domain. This is necessary to provide access statistics through any log analizer application like webalizer or awstats. Please note, that in the log_root (which is /var/log/lighttpd) I have created a directory torqzone.com, please do not forget to assign appropriate permissions to it.

This is it

After these steps you should be able to server multiple domains names on the server.

PHP setup with Lighttpd on openSuSE

Saturday, December 22nd, 2007

Abstract

This article will explain in detail how to install PHP and Lighttpd on suseLinux 10.3 and how to make them work together.

Start with Lighttpd and PHP installation

First off you need to login to your box as root and start up yast (yast2). Then you search for lighttpd and you should see a screen like this (lighttpd checked for install already):

Lighttpd found in YaST software management

After this accept everything, but do not exit YaST and choose the option to install more software.

Search for php now. At this point you do not choose apache2_mod_php5, as you will not be using Apache. Lighttpd runs PHP as a fastcgi module (there are other methods to run PHP, but I personally prefer running as fast cgi). So what is obligatory for selection here is the php5 itself and the php5-fastcgi. You might want to select other modules depending on what you require for your web application to run. In the end you should see screen similar to this (I installed a bunch of other modules as well, but you don’t have to, the only obligatory ones are php5 and php5-fastcgi).

PHP Packages selected

After selection let YaST install any dependencies.

Configuration

Ok, so now PHP and Lighttpd are installed, they are not, however configured to work with each other just yet. In SuSE there is a sample configuration file, which has everything defined for PHP, but it is commented out. So you need to perform the following steps:

Open /etc/lighttpd/conf.d/fastcgi.conf file and uncomment the server.modules += ( “mod_fastcgi”)

The un-comment all the PHP lines for the PHP as shown on the screenshot below.

fastcgi config file

Note the comment above the lines you just un-commented. It says, that this default config is for servers with large amount of RAM and that are more or less designated to running PHP sites. If this is not the case for you please change the values specified there. You can also use the values from the screenshot above.

Permissions

After this you need to assign permissions to the /var/lib/php5 directory. Sessions will be save here and for some reason it has wrong permissions by default. What you need to do is change ownership to lighttpd:lighttpd (chown -R lighttpd:lighttpd /var/lib/php5 - this assumes you did not change the user under which lighttpd is started).

This is it

After this you should be able to start / restart lighttpd (/etc/init.d/lighttpd restart) and it will load everything and you will be ready to run PHP scripts on your server. The next step now is to change the options in php.ini (since we use fastcgi it is located in /etc/php5/fastcgi/php.ini)