(draft ...)

Contents

Migrate sites from a Linux dedicated server to a new webserver

In this tutorial some practical examples for who have to transfer data and webserver configuration from a dedicated Linux server to a new server. The information contained in this article are to be intended "as is" and without any warranty, however I believe that this brief history may be useful to someone (even to myself, when I will try to remember how I made it) .

The new server, get the features

After a long wait, here is new Linux dedicated server, I connect with ssh: ssh root @ server_ip_address. And now, how to get the features of the new server? These the commands at first login:

uname -a
cat /etc/issue
fdisk -l
free

so I get:

sistema a 64bit
CentOS release 6.3 (Final)
2HD da 2TB in RAID1
16G RAM

great machine!!! ...

Install basics services, install apache

The server is installed right at the bare minimum, "service httpd start" shows that apache is not installed; mcedit, my favorite text editor, there is no (ok, there would be vim, that s ...) ... I proceed with apache installation ... and Midnight Commander, of course:

yum install httpd
yum install mc
mcedit /etc/httpd/conf/httpd.conf
chkconfig --levels 235 httpd on
service httpd start
with "chkconfig ..." I made ​​permanent starting httpd
in /etc/httpd/conf/httpd.conf I uncommented the line "NameVirtualHost *:80"

... but http://server_IP in my browser still not will respond, in CentOS we should also enable the firewall to access to port 80:

echo "-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT" >> /etc/sysconfig/iptables
/etc/init.d/iptables restart

finalmente vedo la pagina web di default di apache sul mio browser ... ok, apache è installato! ... decido subito di eliminare l'indexes per sicurezza:

# diff /etc/httpd/conf/01-httpd.conf /etc/httpd/conf/httpd.conf
---
331c331
<     Options Indexes FollowSymLinks
---
>     Options FollowSymLinks
554c554
<     Options Indexes MultiViews FollowSymLinks
---
>     Options MultiViews FollowSymLinks

Installare e configurare mysql, installare php

anche mysql non è installato, procedo con l'installazione:

yum install mysql mysql-devel mysql-server
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start

e php e librerie per mysql, visto che anche queste mancano:

yum install php
yum install mysqli
yum install php-mysql
yum install php-mysqli

La configurazione di php è in /etc/php.ini ... ecco le modifiche di php.ini e la configurazione di mysql /etc/my.cnf:

# diff /etc/01-php.ini /etc/php.ini
729c729
< post_max_size = 8M
---
> post_max_size = 24M
878c878
< upload_max_filesize = 2M
---
> upload_max_filesize = 48M
#/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf8
default-collation=utf8_unicode_ci
character-set-server=utf8
collation-server=utf8_unicode_ci
 
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
 
[client]
default-character-set=utf8

Installare phpmyadmin

Decido anche di installare phpmyadmin per la migrazione dei database ... ma non mi servirà per il momento, usero degli script, molto più efficienti per il dump ed il caricamento dei db ...

Con wget scarico l'ultima versione di phpmyadmin da SF: http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/

poi scompatto il tar.gz in /var/www/html/ e rinomino la cartella con un nome di fantasia ("phpmyadmin" è il primo nome che gli scanner di rete cercano):

cd /var/www/html/
wget ...
tar xzf phpMyAdmin-x.y.z-all-languages.tar.gz
mv phpMyAdmin-x.y.z-all-languages abc123

phpmyadmin sarà ora accessibile da http://server_IP/abc123 ... ma prima bisogna modificare la linea di config.inc.php da:

$cfg['Servers'][$i]['auth_type'] = 'cookies';

a:

$cfg['Servers'][$i]['auth_type'] = 'http';

Migrare i siti web ed i database da un server all'altro

Per trasferire il codice dei siti web da un server all'altro ho preferito fare un tar.gz della cartella html, trasferendola poi con scp ... esempio:

sul veccho server:

ssh root@old_server_IP
cd /var/www/html
tar -czplf /sites.tar.gz *
exit

sul nuovo server:

ssh root@new_server_IP
cd /var/www/html
scp root@old_server_IP:/sites.tar.gz .
tar -xzplf /sites.tar.gz

per i database ho usato invece alcuni script per il dump ed il restore ... ho creato un file di testo con nome_db, utente e password:

db_list.txt, file di testo con le credenziali di accesso ai database

name_db1|user_db1|password_db1
name_db2|user_db2|password_db2
name_db3|user_db3|password_db3
...

dumpdbs.sh, dump dei database presenti sul vecchio server:

#!/bin/bash
# dumpdbs.sh - dump several dbs as listed in db_list.txt
ROOT_PW="old_rootpw_mysql"
 
IFS="|"
cat db_list.txt |
while read DB_NAME DB_USER DB_PASS
do
 
 echo "Dump DB $DB_NAME ..."
 mysqldump --user root --password="$ROOT_PW" $DB_NAME | gzip > $DB_NAME.sql.gz
 
done

restoredbs.sh, caricamento dei database sul nuovo server

#!/bin/bash
# restoredbs.sh - restore several dbs as listed in db_list.txt
ROOT_PW="new_pwroot_db"
 
create_db() {
 
EXPECTED_ARGS=3
E_BADARGS=65
MYSQL=`which mysql`
 
Q1="CREATE DATABASE IF NOT EXISTS $1;"
Q2="GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE, LOCK TABLES ON $1.* TO '$2'@'localhost' IDENTIFIED BY '$3';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: $0 dbname dbuser dbpass"
  exit $E_BADARGS
fi
 
$MYSQL --user root --password="$ROOT_PW" -e "$SQL"
 
}
 
echo "DB in list are:"
echo "-"
 
IFS="|"
cat db_list.txt |
while read DB_NAME DB_USER DB_PASS
do
 echo -n "$DB_NAME "
done
 
echo
echo "-"
 
echo -n "Name of database you wish restore [all=restore all] : "
 
read DBX
 
if [ "a$DBX" == "a" ] ; then
 exit
fi
 
export BDX_RESTORE="$DBX"
 
IFS="|"
cat db_list.txt |
while read DB_NAME DB_USER DB_PASS
do
 
 if [ "a$BDX_RESTORE" == "a$DB_NAME" ] ; then
 
  echo "Restore $DB_NAME ..."
  create_db $DB_NAME $DB_USER $DB_PASS
  zcat $DB_NAME.sql.gz | mysql --user root --password="$ROOT_PW" $DB_NAME
 
 fi
 
 if [ "a$BDX_RESTORE" == "aall" ] ; then
 
  echo "Restore $DB_NAME ..."
  create_db $DB_NAME $DB_USER $DB_PASS
  zcat $DB_NAME.sql.gz | mysql --user root --password="$ROOT_PW" $DB_NAME
 fi
 
done

Convertire i database mysql in utf8

Alcuni cms trasferiti,in particolare moodle, segnalano che il database non è utf8 ... ecco uno script che risolve: scarica il database .sql, lo converte in utf8 e lo ricarica in mysql:

migratedb-utf8.sh

#!/bin/bash
 
if [ $# -ne 3 ]
then
  echo "Usage: $0 dbusername password dbname"
  exit
fi
 
#  $1-dbusername $2-password $3-dbname
mysqldump -u$1 -p$2 -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B $3 > dump.sql
sed 's/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/' <dump.sql \
| sed 's/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/' >dump-fixed.sql
mysql -u$1 -p$2 < dump-fixed.sql

Soluzione di alcuni problemi con i cms installati

Ecco alcuni dei problemi riscontrati in runtime e relative soluzioni

phpmyadmin

error:

service mysqli not found in config.php.ini

solution:

yum install php-mysqli

phpbb

error:

function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead
[phpBB Debug] PHP Notice: in file /viewtopic.php on line 981: getdate(): It is not safe to rely on the system's timezone settings.
You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and
you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead

solution: add to /etc/php.ini

date.timezone = "Europe/Berlin"

moodle

Some errors in admin panel, solved installing these packages

yum install php-mbstring
yum install php-xmlrpc
yum install php-gd

php sendmail not send mail

In CentOS selinux block sendmail via php by default, to solve:

setsebool -P httpd_can_sendmail 1

command line "mail", mail rejected

Sendmail invia le mail da linea di comando ma queste vengono respinte. E' spesso un problema di nome di dominio, per risolvere setta un nome di dominio valido collegato all'IP del server (come da pannello DNS):

hostname my_valid_domain.dom

Riferimenti

CentOS - install apache server
How to install MySQL
Retrieved from "http://www.zoros.org/wiki/index.php?title=Migrating_from_one_dedicated_Linux_server_to_another,_how_I_made_it,_a_simple_howto"