Huỳnh Công Đức

Cố gắng làm những điều bình thường trở thành phi thường

Thiết lập máy chủ Ubuntu 11.10 và cài đặt ISPConfig 3 (Phần2)

ISPconfig là một phần mềm quản lý hosting mã nguồn mở, cho phép bạn cấu hình các dịch vụ thông qua một trình duyệt web: Apache web server, Postfix mail server, MySQL, BIND hoặc MyDNS nameserver, PureFTPd, SpamAssassin, ClamAV… Trong bài viết sau chúng tôi sẽ hướng dẫn các bạn cách tạo một máy chủ Ubuntu 11.10 (Oneiric Ocelot) cho việc cài đặt ISPConfig 3, sau đó tiến hành cài đặt ISPConfig 3.

4. Lấy quyền root

Sau khi reboot, bạn có thể đăng nhập với tài khoản vừa tạo ra (administrator). Bởi vì bạn cần chạy những dòng lệnh tiếp theo với quyền root nên cần thêm từ sudo sau mỗi lệnh, hoặc trở thành root ngay bây giờ:

sudo su

Bạn cũng có thể kích hoạt đăng nhập root bằng cách chạy lệnh:

sudo passwd root

Và tạo ra một mật khẩu root. Sau đó bạn có thể đăng nhập trực tiếp với quyền root. Tuy nhiên điều này không được khuyến cáo từ các nhà phát triển và cộng đồng Ubuntu bởi nhiều lý do.

5. Cài đặt SSH Server (tùy chọn không bắt buộc)

Nếu bạn không cài đặt OpenSSH server trong quá trình cài đặt, bạn có thể cài bằng lệnh:

apt-get install ssh openssh-server

Từ đây bạn có thể dùng chương trình SSH Client như PuTTY để kết nối từ máy trạm tới Ubuntu 11.10 Server và tiếp tục các bước tiếp theo.

6. Cài đặt vim-nox (không bắt buộc)

Do chúng tôi sử dụng vi là chương trình text editor. Nhưng theo mặc định, vi tạo một số hành động lạ với Ubuntu và Debian, để sửa lỗi này ta cần cài vim-nox:

pt-get install vim-nox

(Bạn không cần cài vim-nox nếu bạn sử dụng joe hay nano).

7. Cấu hình network:

Bởi vì trình cài đặt Ubuntu đã cấu hình hệ thống để có các thiết lập mạng thông qua DHCP, mà ta cần một địa chỉ IP tĩnh nên sẽ cấu hình lại /etc/network/interfaces và điều chỉnh nếu cần. Ví dụ chúng ta có địa chỉ IP 192.168.0.100:

vi /etc/network/interfaces

# This file describes the network interfaces available on your system 
# and how to activate them. For more information, see interfaces(5). 
 # The loopback network interface 

auto lo 
iface lo inet loopback  
# The primary network interface 
auto eth0 
iface eth0 inet static         
address 192.168.0.100         
netmask 255.255.255.0         
network 192.168.0.0        
broadcast 192.168.0.255         
gateway 192.168.0.1

Khởi động lại mạng:

/etc/init.d/networking restart

Chỉnh sửa /etc/hosts như sau:

vi /etc/hosts

 

127.0.0.1       localhost.localdomain   localhost 
192.168.0.100   server1.example.com     server1  
# The following lines are desirable for IPv6 capable hosts 
::1     ip6-localhost ip6-loopback 
fe00::0 ip6-localnet 
ff00::0 ip6-mcastprefix 
ff02::1 ip6-allnodes 
ff02::2 ip6-allrouters

Bây giờ chạy:

echo server1.example.com > /etc/hostname
/etc/init.d/hostname restart

Sau đó chạy tiếp:

hostname
hostname -f

Cả hai sẽ hiển thị server1.example.com.

8. Chỉnh sửa /etc/apt/sources.list và cập nhật Linux

Sửa lại /etc/apt/sources.list. Cho đĩa cài đặt vào, chắc chắn rằng universe và multiverse repositories được bật:

vi /etc/apt/sources.list

#

# deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ dists/oneiric/main/binary-i386/
# deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ dists/oneiric/restricted/binary-i386/
# deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ oneiric main restricted

#deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ dists/oneiric/main/binary-i386/
#deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ dists/oneiric/restricted/binary-i386/
#deb cdrom:[Ubuntu-Server 11.10 _Oneiric Ocelot_ - Release amd64 (20111011)]/ oneiric main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://de.archive.ubuntu.com/ubuntu/ oneiric main restricted
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://de.archive.ubuntu.com/ubuntu/ oneiric-updates main restricted
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://de.archive.ubuntu.com/ubuntu/ oneiric universe
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric universe
deb http://de.archive.ubuntu.com/ubuntu/ oneiric-updates universe
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://de.archive.ubuntu.com/ubuntu/ oneiric multiverse
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric multiverse
deb http://de.archive.ubuntu.com/ubuntu/ oneiric-updates multiverse
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://de.archive.ubuntu.com/ubuntu/ oneiric-backports main restricted universe multiverse
deb-src http://de.archive.ubuntu.com/ubuntu/ oneiric-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu oneiric-security main restricted
deb-src http://security.ubuntu.com/ubuntu oneiric-security main restricted
deb http://security.ubuntu.com/ubuntu oneiric-security universe
deb-src http://security.ubuntu.com/ubuntu oneiric-security universe
deb http://security.ubuntu.com/ubuntu oneiric-security multiverse
deb-src http://security.ubuntu.com/ubuntu oneiric-security multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu oneiric partner
# deb-src http://archive.canonical.com/ubuntu oneiric partner

## Uncomment the following two lines to add software from Ubuntu's
## 'extras' repository.
## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
# deb http://extras.ubuntu.com/ubuntu oneiric main
# deb-src http://extras.ubuntu.com/ubuntu oneiric main

Chạy lệnh:

apt-get update

Cập nhật cơ sở dữ liệu của apt package:

apt-get upgrade

Cuối cùng khởi động lại hệ thống:

reboot

9. Thay đổi Shell mặc định

/bin/sh là một symlink tới /bin/dash, tuy nhiên chúng ta cần /bin/bash chứ không phải /bin/dash. Vì vậy thay đổi nó bằng lệnh:

dpkg-reconfigure dash

Use dash as the default system shell (/bin/sh)? <– chọn No

Nếu không làm điều này, cài đặt ISPConfig sẽ thất bại.

10. Vô hiệu hóa AppArmor

AppArmor là một phần mở rộng bảo mật (tương tự SELinux). Mặc dù nó mang lại nhiều khả năng bảo mật nhưng cũng gây nhiều phiến phức. Vì vậy bạn có thể vô hiệu hóa nó (sẽ là cần thiết để cài đặt ISPConfig sau này):

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove
apt-get remove apparmor apparmor-utils

11. Đồng bộ hóa đồng hồ hề thống

Đây là một ý tưởng tốt để đồng bộ hệ thống thời gian với NTP (network time protocol). Chỉ cần chạy:

apt-get install ntp ntpdate

12. Cài đặt Postfix, Courier, Saslauthd, MySQL, rkhunter, binutils

Chúng ta có thể cài đặt toàn bộ chương trình này bằng dòng lệnh:

apt-get install postfix postfix-mysql postfix-doc mysql-client mysql-server courier-authdaemon courier-authlib-mysql courier-pop courier-pop-ssl courier-imap courier-imap-ssl libsasl2-2 libsasl2-modules libsasl2-modules-sql sasl2-bin libpam-mysql openssl getmail4 rkhunter binutils maildrop

Bạn sẽ nhận được những câu hỏi sau:

New password for the MySQL “root” user: <– yourrootsqlpassword
Repeat password for the MySQL “root” user: <– yourrootsqlpassword
Create directories for web-based administration? <– No
General type of mail configuration: <– Internet Site
System mail name: <– server1.example.com
SSL certificate required <– Ok

Sửa lại file cấu hình của mysql để MySQL có thể giao tiếp trên tất cả các giao thức, không chỉ mình localhost, bằng cách sửa file /etc/mysql/my.cnf và tắt 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 đó restart MySQL:

/etc/init.d/mysql restart

Bây giờ kiểm tra kết nối mạng có được kích hoạt:

netstat -tap | grep mysql

Đầu ra sẽ trông như thế này;

root@server1:~# netstat -tap | grep mysql
tcp        0      0 *:mysql                 *:*                     LISTEN      22355/mysqld
root@server1:~#

Trong quá trình cài đặt, chứng chỉ SSL cho IMAP-SSL và POP3-SSL sẽ được tạo với hostname localhost. Để thay đổi tên cho đúng hostname Server1.example.com, chỉ cần xóa chứng thư này:

cd /etc/courier
rm -f /etc/courier/imapd.pem
rm -f /etc/courier/pop3d.pem

Và sửa đổi hai tập tin sau đây, thay thế CN=localhost bằng CN=server1.example.com (bạn cũng có thể đổi thành giá trị khác nếu cần thiết).

vi /etc/courier/imapd.cnf

[…]
CN=server1.example.com
[…]

vi /etc/courier/pop3d.cnf

[…]
CN=server1.example.com
[…]

Sau đó tạo lại chứng thư:

mkimapdcert
mkpop3dcert

Khởi động lại Courier-IMAP-SSL và Courier-POP3-SSL:

/etc/init.d/courier-imap-ssl restart
/etc/init.d/courier-pop-ssl restart
13. Cài đặt Amavisd-new, SpamAssassin, và Clamav

Bạn sử dụng dòng lệnh sau để cài đặt Amavisd-new, SpamAssassin, và Clamav:

apt-get 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

ISPConfig 3 sử dụng Amavisd để tải bộ lọc SpamAssassin, chúng ta cần cho dừng quá trình này để giải phóng RAM:

/etc/init.d/spamassassin stop
update-rc.d -f spamassassin remove

14. Cài đặt Apache2, PHP5, phpMyAdmin, FCGI, suExec, Pear, và mcrypt

Apache2, PHP5, phpMyAdmin, FCGI, suExec, Pear, và mcrypt có thể cài đặt dễ dàng bằng lệnh sau:

apt-get 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 libruby libapache2-mod-ruby

Bạn sẽ thấy hai câu hỏi:

Web server to reconfigure automatically: <– nhập apache2
Configure database for phpmyadmin with dbconfig-common? <– nhập No

Tiếp theo chạy những lệnh sau để kích hoạt các module của Apache: suexec, rewrite, ssl, actions, và include (cộng với dav, dav_fs, và auth_digest nếu bạn muốn dùng WebDAV):

a2enmod suexec rewrite ssl actions include
a2enmod dav_fs dav auth_digest

Restart lại Apache:

/etc/init.d/apache2 restart

Nếu bạn muốn lưu trữ các tập tin Ruby với phần mở rộng .rb trên các trang web của mình thông qua ISPConfig, bạn cần chú thích dòng application/x-ruby rb trong /etc/mime.types:

vi /etc/mime.types

[…]
#application/x-ruby                             rb
[…]

(Điều này chỉ cần thiết đối với những file .rb).

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

/etc/init.d/apache2 restart

15. Cài đặt PureFTPd và Quota

PureFTPd và Quota có thể cài đặt bằng lệnh:

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

Sửa lại file /etc/default/pure-ftpd-common:

vi /etc/default/pure-ftpd-common

Và đảm bảo chắc chắn start mode được set là standalone và VIRTUALCHROOT=true:

[…]
STANDALONE_OR_INETD=standalone
[…]
VIRTUALCHROOT=true
[…]

Bây giờ chúng ta cấu hình PureFTPd để cho phép FTP và TLS. Bằng cách kết hợp phương pháp mã hóa TLS sẽ làm cho giao thức FTP được an toàn hơn rất nhiều.

echo 1 > /etc/pure-ftpd/conf/TLS

Để sử dụng TLS, chúng ta cần tạo chứng chỉ SSL. Ở đây chúng tôi sẽ tạo trong /etc/ssl/private/, do đó cần tạo thư mục đó trước:

mkdir -p /etc/ssl/private/

Tiếp theo tạo chứng chỉ SSL:

openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout /etc/ssl/private/pure-ftpd.pem -out /etc/ssl/private/pure-ftpd.pem

Country Name (2 letter code) [AU]: <– Enter your Country Name (e.g., “DE”).
State or Province Name (full name) [Some-State]: <– Enter your State or Province Name.
Locality Name (eg, city) []: <– Enter your City.
Organization Name (eg, company) [Internet Widgits Pty Ltd]: <– Enter your Organization Name (e.g., the name of your company).
Organizational Unit Name (eg, section) []: <– Enter your Organizational Unit Name (e.g. “IT Department”).
Common Name (eg, YOUR name) []: <– Enter the Fully Qualified Domain Name of the system (e.g. “server1.example.com”).
Email Address []: <– Enter your Email Address.

Thay đổi các điều khoản của chứng chỉ SSL:

chmod 600 /etc/ssl/private/pure-ftpd.pem

Restart PureFTPd:

/etc/init.d/pure-ftpd-mysql restart

Sửa lại /etc/fstab. Thêm vào dòng ,usrjquota=quota.user,grpjquota=quota.group,jqfmt=vfsv0 tới partition với mount point /:

vi /etc/fstab

# /etc/fstab: static file system information.
#
# Use ‘blkid’ to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
proc            /proc           proc    nodev,noexec,nosuid 0       0
/dev/mapper/server1-root /               ext4    errors=remount-ro,usrjquota=quota.user,grpjquota=quota.group,jqfmt=vfsv0 0       1
# /boot was on /dev/sda1 during installation
UUID=6fbce377-c3d6-4eb3-8299-88797d4ad18d /boot           ext2    defaults        0       2
/dev/mapper/server1-swap_1 none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0

Kích hoạt quota, chạy lệnh sau:

mount -o remount /

quotacheck -avugm
quotaon -avug

16. Cài đặt BIND DNS Server

Bạn chạy lệnh sau để cài đặt BIND:

apt-get install bind9 dnsutils

17. Cài đặt logger, Webalizer, và AWstats

apt-get install vlogger webalizer awstats

Sau đó mở /etc/cron.d/awstats:

vi /etc/cron.d/awstats

Và tạo chú thích về hai việc định kỳ trong đó:

#*/10 * * * * www-data [ -x /usr/share/awstats/tools/update.sh ] && /usr/share/awstats/tools/update.sh  # Generate static reports: #10 03 * * * www-data [ -x /usr/share/awstats/tools/buildstatic.sh ] && /usr/share/awstats/tools/buildstatic.sh

18. Cài đặt Jailkit

Jailkit chỉ cần thiết nếu bạn muốn Chroot user SSH, cần lưu ý rằng Jailkit phải được cài đặt trước ISPConfig. Tuyệt đối không làm ngược lại.

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

cd /tmp
wget http://olivier.sessink.nl/jailkit/jailkit-2.14.tar.gz
tar xvfz jailkit-2.14.tar.gz
cd jailkit-2.14
./debian/rules binary

Bạn sẽ nhận được thông báo:

x86_64-linux-gnu-gcc -lpthread -o jk_socketd jk_socketd.o jk_lib.o utils.o iniparser.o
jk_socketd.o: In function `main’:
/tmp/jailkit-2.14/src/jk_socketd.c:474: undefined reference to `pthread_create’
collect2: ld returned 1 exit status
make[2]: *** [jk_socketd] Error 1
make[2]: Leaving directory `/tmp/jailkit-2.14/src’
make[1]: *** [all] Error 2
make[1]: Leaving directory `/tmp/jailkit-2.14′
make: *** [build-arch-stamp] Error 2
root@server1:/tmp/jailkit-2.14#

Hãy cài đặt gcc-4.4:

apt-get install gcc-4.4

Và xem /usr/bin/gcc:

ls -l /usr/bin/gcc*
/usr/bin/gcc hiện hành phải là một symlink tới /usr/bin/gcc-4.6:
root@server1:/tmp/jailkit-2.14# ls -l /usr/bin/gcc*
lrwxrwxrwx 1 root root      7 2011-08-14 09:16 /usr/bin/gcc -> gcc-4.6
-rwxr-xr-x 1 root root 259232 2011-10-05 23:56 /usr/bin/gcc-4.4
-rwxr-xr-x 1 root root 349120 2011-09-16 16:31 /usr/bin/gcc-4.6
root@server1:/tmp/jailkit-2.14#

Chúng ta sẽ thay đổi điều này bằng cách liên kết /usr/bin/gcc tới /usr/bin/gcc-4.4:

ln -sf /usr/bin/gcc-4.4 /usr/bin/gcc

Và xây dựng Jailkit:

make clean
./configure
make
make clean
./debian/rules binary

Nếu lệnh ./debian/rules binary không đưa ra lỗi nào, bạn có thể cài đặt gói .deb như sau:

cd ..
dpkg -i jailkit_2.14-1_*.deb
rm -rf jailkit-2.14*

Cuối cùng chúng ta thay đổi symlink /usr/bin/gcc để nó trỏ tới /usr/bin/gcc-4.6 lần nữa.

ln -sf /usr/bin/gcc-4.6 /usr/bin/gcc

Nguồn:  Đ.Hải – Quantrimang 

Bình luận về bài viết này