Thiết lập hệ thống Web, Email và MySQL trên Debian
Hiện tại có 1 hạn chế nho nhỏ trong quá trình thiết lập MySQL cluster, và các MySQL daemon sẽ tự động khóa các vấn đề trong quá trình cài đặt ban đầu, ngay khi server thứ 2 khởi động. Các thành phần workaround được sử dụng tại đây để kích hoạt MySQL với myisam, và hệ thống cũng đã chỉ ra 1 vài báo cáo rằng MySQL server đã hoạt động thành công cùng với innodb trên GlusterFS.

 

Thiết lập 2 hệ thống cơ bản

Tại đây chúng ta sẽ sử dụng 1 server master (với giao diện điều khiển ISPConfig) và 1 server slave dùng để ánh xạ các dịch vụ web (apache), email (postfix và dovecot) và database (MySQL) của server master.

Trong bài viết này, chúng ta sẽ sử dụng hostnames và địa chỉ IP đối với 2 server:

Master Server
Hostname: server1.example.tld
IP address: 192.168.0.105
Slave server
Hostname: server2.example.tld
IP address: 192.168.0.106

Thiết lập và cài đặt 2 server

Những bước sau đây được thực hiện lần lượt trên server master và slave.

vi /etc/hosts

127.0.0.1 localhost
192.168.0.105 server1.example.tld
192.168.0.106 server2.example.tld

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Khởi tạo giá trị hostname của server:

echo server1.example.tld > /etc/hostname
/etc/init.d/hostname.sh start

Tại đây chúng ta sẽ sử dụng các tài khoản server1.example.tld trên server đầu tiên và server2.example.tld trên server thứ 2.

Chỉnh sửa file danh sách sources.list:

vi /etc/apt/sources.list

và hãy chắc chắn rằng nó có chứa 2 dòng sau:

deb http://volatile.debian.org/debian-volatile lenny/volatile main contrib non-free
deb http://www.backports.org/debian/ lenny-backports main contrib non-free

Dòng thứ nhất dành cho các repository của debian để nhận các gói cập nhật đối với ứng dụng antivirus ClamAV và SpamAssassin, dòng thứ 2 để cho các backports repository chứa các gói GlusterFS hiện thời.

Tiếp theo, chạy lệnh sau:

apt-get install debian-backports-keyring
apt-get update

để cài đặt các khóa key của backports repository và cập nhật cơ sở dữ liệu apt package. Gõ lệnh sau:

apt-get upgrade

để cài đặt những bản cập nhật mới nhất (nếu có).

Nếu muốn đồng bộ hóa thời gian trên hệ thống với server NTP (network time protocol) qua Internet thì gõ lệnh sau:

apt-get -y install ntp ntpdate

Muốn cài đặt postfix, dovecot và mysql thì các bạn chỉ cần 1 dòng lệnh duy nhất:

apt-get -y install postfix postfix-mysql postfix-doc mysql-client mysql-server openssl getmail4 rkhunter binutils dovecot-imapd dovecot-pop3d sudo

Điền mật khẩu mới cho mysql khi trình cài đặt yêu cầu và trả lời những câu hỏi tiếp theo:

Create directories for web-based administration ? <-- No
General type of configuration? <-- Internet site
Mail name? <-- server1.mydomain.tld
SSL certificate required <-- Ok

Trong bài thử nghiệm này, chúng ta muốn MySQL “lắng nghe” tất cả các tín hiệu, không chỉ từ localhost, do đó chúng ta điều chỉnh /etc/mysql/my.cnf và chú thích bên ngoài dòng bind-address = 127.0.0.1:

vi /etc/mysql/my.cnf

[...]
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address = 127.0.0.1
[...]

Sau đó khởi động lại MySQL:

/etc/init.d/mysql restart

Tiếp theo, để cài đặt amavisd-new, SpamAssassin, và ClamAV, chúng ta sử dụng lệnh sau:

apt-get -y install amavisd-new spamassassin clamav clamav-daemon zoo unzip bzip2 arj nomarch lzop cabextract apt-listchanges libnet-ldap-perl libauthen-sasl-perl clamav-docs daemon libio-string-perl libio-socket-ssl-perl libnet-ident-perl zip libnet-dns-perl

Sau đó tiếp tục với việc cài đặt Apache2, PHP5, phpMyAdmin, FCGI, suExec, Pear, và mcrypt:

apt-get -y install apache2 apache2.2-common apache2-doc apache2-mpm-prefork apache2-utils libexpat1 ssl-cert libapache2-mod-php5 php5 php5-common php5-gd php5-mysql php5-imap phpmyadmin php5-cli php5-cgi libapache2-mod-fcgid apache2-suexec php-pear php-auth php5-mcrypt mcrypt php5-imagick imagemagick libapache2-mod-suphp libopenssl-ruby libapache2-mod-ruby sudo

Bạn sẽ nhìn thấy câu hỏi sau:

Web server to reconfigure automatically: <-- apache2
Hãy gõ lệnh sau để kích hoạt các module của Apache như suexec, rewrite, ssl, actions:

a2enmod suexec rewrite ssl actions include ruby dav_fs dav auth_digest

Tiếp tục với việc cài đặt PureFTPd và quota:

apt-get -y install pure-ftpd-common pure-ftpd-mysql quota quotatool

Chỉnh sửa file /etc/fstab sao cho giống như sau:

vi /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc defaults 0 0
/dev/sda1 / ext3 errors=remount-ro,usrquota,grpquota 0 1
/dev/sda5 none swap sw 0 0 /dev/hda
/media/cdrom0 udf,iso9660 user,noauto 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto 0 0

Để kích hoạt chế độ quota, sử dụng những lệnh sau:

touch /quota.user /quota.group
chmod 600 /quota.*
mount -o remount /

quotacheck -avugm
quotaon -avug

Cài đặt BIND DNS Server:

apt-get -y install bind9 dnsutils

Với vlogger, webalizer và awstats:

apt-get -y install vlogger webalizer awstats

Tiếp theo với ứng dụng Jailkit, chỉ thực sự cần thiết nếu muốn chroot các tài khoản user của SSH (chú ý rằng Jailkit phải được cài đặt trước ISPConfig – nếu cài sau thì sẽ không hoạt động):

apt-get -y install build-essential autoconf automake1.9 libtool flex bison

cd /tmp
wget http://olivier.sessink.nl/jailkit/jailkit-2.11.tar.gz
tar xvfz jailkit-2.11.tar.gz
cd jailkit-2.11
./configure
make
make install
cd ..
rm -rf jailkit-2.11*

Cài đặt fail2ban:

apt-get install fail2ban

GlusterFS và Fuse:

apt-get -y --force-yes -t lenny-backports install fuse-utils
apt-get -y --force-yes install glusterfs-server glusterfs-client

Xóa bỏ file cấu hình tạm thời của GlusterFS:

rm -f /etc/glusterfs/*.vol

Tạo thư mục dữ liệu tương ứng của các volume GlusterFS:

mkdir /data/
mkdir /data/export-mysql
mkdir /data/export-mysql-ns
mkdir /data/export-vmail
mkdir /data/export-vmail-ns
mkdir /data/export-www
mkdir /data/export-www-ns

Tiếp đó, tạo file cấu hình GlusterFS server:

vi /etc/glusterfs/glusterfsd.vol

# Configuration for the mysql server volume
volume posix-mysql
type storage/posix
option directory /data/export-mysql
option background-unlink yes
end-volume

volume locks-mysql
type features/locks
option mandatory-locks on
subvolumes posix-mysql
end-volume

volume brick-mysql
type performance/io-threads
option thread-count 8
subvolumes locks-mysql
end-volume

# Configuration for the vmail server volume
volume posix-vmail
type storage/posix
option directory /data/export-vmail
end-volume

volume locks-vmail
type features/locks
subvolumes posix-vmail
end-volume

volume brick-vmail
type performance/io-threads
option thread-count 8
subvolumes locks-vmail
end-volume

# Configuration for the www server volume
volume posix-www
type storage/posix
option directory /data/export-www
end-volume

volume locks-www
type features/locks
subvolumes posix-www
end-volume

volume brick-www
type performance/io-threads
option thread-count 8
subvolumes locks-www
end-volume

# export all volumes
volume server
type protocol/server
option transport-type tcp
subvolumes brick-mysql brick-vmail brick-www

# authentication options for the mysql volume
option auth.addr.brick-mysql.allow 192.168.0.105,192.168.0.106
option auth.login.brick-mysql.allow user-mysql
option auth.login.user-mysql.password 7wQav7ExkFg6eW

# Authentification options for the vmail volume
option auth.addr.brick-vmail.allow 192.168.0.105,192.168.0.106
option auth.login.brick-vmail.allow user-vmail
option auth.login.user-vmail.password 7wQav7ExkFg6eW

# authentification options for www
option auth.addr.brick-www.allow 192.168.0.105,192.168.0.106
option auth.login.brick-www.allow user-www
option auth.login.user-www.password 7wQav7ExkFg6eW

end-volume

Thay thế địa chỉ IP với IPs từ server của bạn và mật khẩu 7wQav7ExkFg6eW với mật khẩu tùy chọn của người sử dụng. Khởi động GlusterFS server:

/etc/init.d/glusterfs-server start

Tiếp đó, tạo thêm 3 file client volume cần thiết để ghép file hệ thống của GlusterFS:

vi /etc/glusterfs/glusterfs-mysql.vol

volume remote1-mysql
type protocol/client
option transport-type tcp
option remote-host 192.168.0.105
option remote-subvolume brick-mysql
option username user-mysql
option password 7wQav7ExkFg6eW
end-volume

volume remote2-mysql
type protocol/client
option transport-type tcp
option remote-host 192.168.0.106
option remote-subvolume brick-mysql
option username user-mysql
option password 7wQav7ExkFg6eW
end-volume

volume replicate-mysql
type cluster/replicate
subvolumes remote1-mysql remote2-mysql
end-volume

volume cache-mysql
type performance/io-cache
option cache-size 25MB
subvolumes replicate-mysql
end-volume

vi /etc/glusterfs/glusterfs-vmail.vol

volume remote1-vmail
type protocol/client
option transport-type tcp
option remote-host 192.168.0.105
option remote-subvolume brick-vmail
option username user-vmail
option password 7wQav7ExkFg6eW
end-volume

volume remote2-vmail
type protocol/client
option transport-type tcp
option remote-host 192.168.0.106
option remote-subvolume brick-vmail
option username user-vmail
option password 7wQav7ExkFg6eW
end-volume

volume replicate-vmail
type cluster/replicate
subvolumes remote1-vmail remote2-vmail
end-volume

volume writebehind-vmail
type performance/write-behind
option window-size 1MB
subvolumes replicate-vmail
end-volume

volume cache-vmail
type performance/io-cache
option cache-size 256MB
subvolumes writebehind-vmail
end-volume

vi /etc/glusterfs/glusterfs-www.vol

volume remote1-www
type protocol/client
option transport-type tcp
option remote-host 192.168.0.105
option remote-subvolume brick-www
option username user-www
option password 7wQav7ExkFg6eW
end-volume

volume remote2-www
type protocol/client
option transport-type tcp
option remote-host 192.168.0.106
option remote-subvolume brick-www
option username user-www
option password 7wQav7ExkFg6eW
end-volume

volume replicate-www
type cluster/replicate
subvolumes remote1-www remote2-www
end-volume

volume writebehind-www
type performance/write-behind
option window-size 1MB
subvolumes replicate-www
end-volume

volume cache-www
type performance/io-cache
option cache-size 256MB
subvolumes writebehind-www
end-volume

Trước khi tiến hành ghép các volume vào các thư mục tương ứng, chúng ta cần dừng 1 số dịch vụ và sao lưu dữ liệu:

/etc/init.d/mysql stop
/etc/init.d/apache2 stop
/etc/init.d/postfix stop
/etc/init.d/dovecot stop

mv /var/lib/mysql /var/lib/mysql_bak
mv /var/www /var/www_bak

mkdir /var/lib/mysql
mkdir /var/www
mkdir /var/vmail

vi /etc/fstab

Sau đó thêm những dòng sau:

/etc/glusterfs/glusterfs-mysql.vol /var/lib/mysql glusterfs defaults 0 0
/etc/glusterfs/glusterfs-vmail.vol /var/vmail glusterfs defaults 0 0
/etc/glusterfs/glusterfs-www.vol /var/www glusterfs defaults 0 0

Gõ lệnh sau để bắt đầu quá trình ghép:

mount -a

Tiếp theo, Chown thư mục gốc mysql tới tài khoản user và nhóm mysql:

chown mysql:mysql /var/lib/mysql

Sau đó, copy ngược lại dữ liệu của mysql và www:

cp -prf /var/lib/mysql_bak/* /var/lib/mysql/
cp -prf /var/www_bak/* /var/www/

Lưu ý là chỉ thực hiện bước này trên server master, không làm trên server slave.

Tắt bỏ innodb bởi vì nó khóa mọi vấn đề liên quan khi MySQL khởi động:

vi /etc/mysql/my.cnf

Thay đổi dòng sau:

#skip-innodb

thành:

skip-innodb

Thêm dòng:

innodb_file_per_table

vào vị trí của section [mysqld] của file my.cnf. Khi tiến hành thiết lập những bước này trên server slave, hãy copy file /etc/mysql/debian.cnf từ server master trước khi khởi động lại MySQL!.

Bây giờ hãy khởi động lại những dịch vụ sau:

/etc/init.d/mysql start
/etc/init.d/apache2 start
/etc/init.d/postfix start
/etc/init.d/dovecot start

Thay đổi thứ tự boot GlusterFS để chắc chắn rằng GlusterFS server luôn luôn khởi động trước MySQL:

mv /etc/rc2.d/S20glusterfs-server /etc/rc2.d/S19glusterfs-server
mv /etc/rc3.d/S20glusterfs-server /etc/rc3.d/S16glusterfs-server
mv /etc/rc4.d/S20glusterfs-server /etc/rc4.d/S16glusterfs-server
mv /etc/rc5.d/S20glusterfs-server /etc/rc5.d/S16glusterfs-server

Cài đặt ISPConfig trên server master

Trước tiên, truy cập vào địa chỉ http://www.ispconfig.org/ispconfig-3/download/ và tải phiên bản ổn định mới nhất của ISPConfig 3. Tiếp theo, ghép 2 tài khoản user MySQL root vừa tạo trong cơ sở dữ liệu master để cho phép các truy cập root từ slave server hostname và IP address. Cách đơn giản nhất đề làm việc này là sử dụng các công cụ quản trị phpmyadmin dựa trên nền tảng web mà chúng ta đã cài đặt phía trên. Mở địa chỉ http://192.168.0.105/phpmyadmin bằng trình duyệt, đăng nhập vào tài khoản MySQL root user và thực thi những câu lệnh truy vấn MySQL sau:

CREATE USER 'root'@'192.168.0.106' IDENTIFIED BY 'myrootpassword';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'192.168.0.106' IDENTIFIED BY 'myrootpassword' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

CREATE USER 'root'@'server2.example.tld' IDENTIFIED BY 'myrootpassword';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'server2.example.tld' IDENTIFIED BY 'myrootpassword' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;

Với những câu lệnh sql trên, thay thế giá trị của 192.168.0.106 bằng địa chỉ IP của server thứ 2, đồng thời tiếp tục thay thế server2.example.tld với hostname của server thứ 2, và myrootpassword bằng mật khẩu root mong muốn. Khởi động lại MySQL và đóng bảng điều khiển phpmyadmin.

Quay trở lại bảng điều khiển chính của server1.example.tld và tải phiên bản ổn định mới nhất của ISPConfig 3:

cd /tmp
wget http://www.ispconfig.org/downloads/ISPConfig-3-stable.tar.gz
tar xfz ISPConfig-3-stable.tar.gz
cd ispconfig3_install/install/

Khởi động đoạn mã cài đặt:

php -q install.php

Select language (en,de) [en]: <-- en
Installation mode (standard,expert) [standard]: <-- standard
Full qualified hostname (FQDN) of the server, eg server1.domain.tld [server1.example.tld]: <-- server1.example.tld
MySQL server hostname [localhost]: <-- localhost
MySQL root username [root]: <-- root
MySQL root password []: <-- nhập mật khẩu mysql root password
MySQL database to create [dbispconfig]: <-- dbispconfig1
MySQL charset [utf8]: <-- utf8

Country Name (2 letter code) [AU]: <-- DE (mã ISO country nơi bạn sống)
State or Province Name (full name) [Some-State]: <-- Niedersachsen
Locality Name (eg, city) []: <-- Lueneburg (nhập tên thành phố)
Organization Name (eg, company) [Internet Widgits Pty Ltd]: <- ENTER
Organizational Unit Name (eg, section) []: <- ENTER
Common Name (eg, YOUR name) []: <- ENTER
Email Address []: <- ENTER

ISPConfig Port [8080]: <-- 8080

Sau đó xóa bỏ toàn bộ thư mục cài đặt như sau:

rm -rf /tmp/ispconfig3_install/install
rm -f /tmp/ISPConfig-3-stable.tar.gz

Cài đặt ISPConfig 3 trên server thứ 2

Đây là bước làm thứ 2 được tiến hành trên server slave – server2.example.tld. Lần này chúng ta sẽ sử dụng chế độ expert của trình cài đặt ISPConfig để thêm node vào ISPConfig server và database. Truy cập trang web của ISPConfig, và tải phiên bản ISPConfig 3 ổn định mới nhất:

cd /tmp
wget http://www.ispconfig.org/downloads/ISPConfig-3-stable.tar.gz
tar xfz ISPConfig-3-stable.tar.gz
cd ispconfig3_install/install/

Khởi động mã cài đặt:

php -q install.php

Select language (en,de) [en]: <-- en
Installation mode (standard,expert) [standard]: <-- expert
Full qualified hostname (FQDN) of the server, eg server2.domain.tld [server2.example.tld]: <-- server2.example.tld
MySQL server hostname [localhost]: <-- localhost
MySQL root username [root]: <-- root
MySQL root password []: <-- Enter your mysql root password here
MySQL database to create [dbispconfig]: <-- dbispconfig2 (the local ISPConfig database name of the master and slave must be different, as both servers share the same data directory)
MySQL charset [utf8]: <-- utf8
Shall this server join an existing ISPConfig multiserver setup (y,n) [n]: <-- y
MySQL master server hostname []: <-- server1.example.tld
MySQL master server root username [root]: <-- root
MySQL master server root password []: <-- Enter the root password of the master server here
MySQL master server database name [dbispconfig]: <-- dbispconfig1
Configure Mail (y,n) [y]: <-- y

Country Name (2 letter code) [AU]: <-- DE (Enter the ISO country code where you live here)
State or Province Name (full name) [Some-State]: <-- Niedersachsen (Enter the state where you live here)
Locality Name (eg, city) []: <-- Lueneburg (Enter the city here)
Organization Name (eg, company) [Internet Widgits Pty Ltd]: <- ENTER
Organizational Unit Name (eg, section) []: <- ENTER
Common Name (eg, YOUR name) []: <- ENTER
Email Address []: <- ENTER

Configure Jailkit (y,n) [y]: <-- y
Configure FTP Server (y,n) [y]: <-- y
Configure DNS Server (y,n) [y]: <-- y
Configure Apache Server (y,n) [y]: <-- y
Configure Firewall Server (y,n) [y]: <--y
Install ISPConfig Web-Interface (y,n) [y]: <--n

Chạy lệnh sau:

rm -f /var/www/ispconfig

để xóa bỏ đường dẫn ISPConfig interface trong thư mục /var/www. Xóa bỏ thư mục cài đặt như sau:

rm -rf /tmp/ispconfig3_install/install
rm -f /tmp/ISPConfig-3-stable.tar.gz

Cấu hình Replication trong ISPConfig

Đăng nhập vào ISPConfig trên server master bằng trình duyệt qua địa chỉ http://192.168.0.105:8080, sau đó chọn System > Server services > server2.example.tld:

Chọn đúng giá trị server1.example.tld trong dòng Is mirror of Server, sau đó nhấn Save.

Khi bạn muốn kích hoạt tính năng tưởng lửa trên server master hoặc slave, hãy chắc chắn rằng bạn đã mở cổng 6996 cho GlusterFS trên cả 2 server.

Trên đây là 1 số bước cơ bản trong quá trình cài đặt hệ thống web, email và cơ sở dữ liệu MySQL trên nền tảng Debian 5.0 với ISPConfig3 hỗ trợ. Chúc các bạn thành công!

(theo HowtoForge)
Bài vết cùng chủ đề