Quản Trị Mạng - SquirrelMail là giao diện web giúp người dùng có thể thao tác gửi nhận mail bằng trình duyệt (webmail). Các gói phần mềm SquirrelMail từ kho Debian/Ubuntu được đi kèm một tập tin cấu hình cho Apache, nhưng không dành cho nginx. Trong bài viết trước chúng tôi đã giới thiệu tới các bạn lợi ích của máy chủ nginx và cách chạy phpMyAdmin trên đó. Hôm nay chúng tôi sẽ hướng dẫn các bạn sử dụng SquirrelMail trong một nginx vhost trên hệ điều hành Debian Squeeze/Ubuntu 11.04.
Lưu ý
Ở đây chúng tôi sẽ sử dụng SquirrelMail trong một vhost có tên www.example.com/example.com với thư mục root /var/www/www.example.com/web. Bạn cần có một cài đặt LEMP đang hoạt động.
>> Xem thêm: Hướng dẫn cài đặt Nginx với PHP5, PHP–FPM và MySQL trên CentOS 6.0
Ngoài ra, chúng ta cần chạy toàn bộ các bước hướng dẫn sau đây với quyền root. Do dó bạn cần thêm vào tất cả các dòng lệnh trong bài chuỗi sudo, hoặc trở thành root với lệnh:
sudo su
Cài đặt APC
APC là một PHP opcode cacher hoàn toàn miễn phí và mở cho bộ nhớ đệm giúp tối ưu hóa code php trung gian. Nó tương tự như các PHP opcode cacher khác, như eAccelerator và XCache. Chúng tôi khuyến cáo bạn nên cài đặt APC để tăng tốc độ tải trang PHP của mình.
Sử dụng dòng lệnh sau để tiến hành cài đặt APC:
apt-get install php-apc
Nếu bạn sử dụng PHP-FPM như FastCGI daemon của mình, hãy khởi động lại nó bằng lệnh sau:
/etc/init.d/php5-fpm restart
Nếu bạn sử dụng Lighttpd spawn-fcgi (chạy trên cổng 9000) như FastCGI daemon, cần đóng lại hoàn toàn và tạo một cái mới như sau:
netstat -tap
Một danh sách hiện ra như dưới đây, bạn chỉ cần chú ý tới PID của quá trình spawn-fcgi:
[email protected]:~# netstat -tap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 *:sunrpc *:* LISTEN 734/portmap
tcp 0 0 *:www *:* LISTEN 2987/nginx
tcp 0 0 *:ssh *:* LISTEN 1531/sshd
tcp 0 0 *:57174 *:* LISTEN 748/rpc.statd
tcp 0 0 localhost.localdom:smtp *:* LISTEN 1507/exim4
tcp 0 0 localhost.localdom:9000 *:* LISTEN 1542/php5-cgi
tcp 0 0 localhost.localdo:mysql *:* LISTEN 1168/mysqld
tcp 0 52 server1.example.com:ssh 192.168.0.198:2462 ESTABLISHED 1557/0
tcp6 0 0 [::]:www [::]:* LISTEN 2987/nginx
tcp6 0 0 [::]:ssh [::]:* LISTEN 1531/sshd
tcp6 0 0 ip6-localhost:smtp [::]:* LISTEN 1507/exim4
[email protected]:~#
Ở ví dụ này số PID là 1542, vì thế chúng tôi sẽ đóng nó lại như sau:
kill -9 1542
Tiếp theo tạo ra một quá trình spawn-fcgi mới:
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid
Cấu hình Vhost
Nếu bạn không được thiết lập sẵn nginx vhost, có thể làm theo cách dưới đây.
Tài liệu root của chúng tôi trên website www.example.com là /var/www/www.example.com/web. Nếu nó không tồn tại, có thể tạo ra bằng lệnh:
mkdir -p /var/www/www.example.com/web
Tiếp theo tạo một cấu hình nginx vhost cơ bản cho www.example.com trong đường dẫn /etc/nginx/sites-available/:
vi /etc/nginx/sites-available/www.example.com.vhost
server { listen 80; server_name www.example.com example.com; root /var/www/www.example.com/web; if ($http_host != "www.example.com") { rewrite ^ http://www.example.com$request_uri permanent; } index index.php index.html; location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { allow all; log_not_found off; access_log off; } # Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS! location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_ { deny all; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). location ~ /\. { deny all; access_log off; log_not_found off; } location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ { expires max; log_not_found off; } location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
Để kích hoạt vhost, chúng ta tạo một symlink (liên kết tượng trưng) cho nó từ đường dẫn /etc/nginx/sites-enabled/:
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/www.example.com.vhost www.example.com.vhost
Cuối cùng nạp lại nginx để những thay đổi này có hiệu lực.
/etc/init.d/nginx reload
Cài đặt và cấu hình SquirrelMail + SquirrelMail Vhost
Để cài đặt SquirrelMail bạn chạy lệnh sau:
apt-get install squirrelmail
Tiếp theo chúng ta tiến hành cấu hình cho SquirrelMail và cho nó biết daemon POP3-IMAP mà bạn sử dụng. Ở ví dụ này chúng tôi chọn Courier, bạn có thể điều chỉnh theo nhu cầu của mình:
squirrelmail-configure
Báo cho SquirrelMail biết rằng cần sử dụng Courier-IMAP/-POP3:
SquirrelMail Configuration : Read: config.php (1.4.0) --------------------------------------------------------- Main Menu -- 1. Organization Preferences 2. Server Settings 3. Folder Defaults 4. General Options 5. Themes 6. Address Books 7. Message of the Day (MOTD) 8. Plugins 9. Database 10. Languages D. Set pre-defined settings for specific IMAP servers C Turn color on S Save data Q Quit Command >> <-- D SquirrelMail Configuration : Read: config.php --------------------------------------------------------- While we have been building SquirrelMail, we have discovered some preferences that work better with some servers that don't work so well with others. If you select your IMAP server, this option will set some pre-defined settings for that server. Please note that you will still need to go through and make sure everything is correct. This does not change everything. There are only a few settings that this will change. Please select your IMAP server: bincimap = Binc IMAP server courier = Courier IMAP server cyrus = Cyrus IMAP server dovecot = Dovecot Secure IMAP server exchange = Microsoft Exchange IMAP server hmailserver = hMailServer macosx = Mac OS X Mailserver mercury32 = Mercury/32 uw = University of Washington's IMAP server gmail = IMAP access to Google mail (Gmail) accounts quit = Do not change anything Command >> <-- courier SquirrelMail Configuration : Read: config.php --------------------------------------------------------- While we have been building SquirrelMail, we have discovered some preferences that work better with some servers that don't work so well with others. If you select your IMAP server, this option will set some pre-defined settings for that server. Please note that you will still need to go through and make sure everything is correct. This does not change everything. There are only a few settings that this will change. Please select your IMAP server: bincimap = Binc IMAP server courier = Courier IMAP server cyrus = Cyrus IMAP server dovecot = Dovecot Secure IMAP server exchange = Microsoft Exchange IMAP server hmailserver = hMailServer macosx = Mac OS X Mailserver mercury32 = Mercury/32 uw = University of Washington's IMAP server quit = Do not change anything Command >> courier imap_server_type = courier default_folder_prefix = INBOX. trash_folder = Trash sent_folder = Sent draft_folder = Drafts show_prefix_option = false default_sub_of_inbox = false show_contain_subfolders_option = false optional_delimiter = . delete_folder = true Press any key to continue... <-- ENTER SquirrelMail Configuration : Read: config.php (1.4.0) --------------------------------------------------------- Main Menu -- 1. Organization Preferences 2. Server Settings 3. Folder Defaults 4. General Options 5. Themes 6. Address Books 7. Message of the Day (MOTD) 8. Plugins 9. Database 10. Languages D. Set pre-defined settings for specific IMAP servers C Turn color on S Save data Q Quit Command >> <-- S SquirrelMail Configuration : Read: config.php (1.4.0) --------------------------------------------------------- Main Menu -- 1. Organization Preferences 2. Server Settings 3. Folder Defaults 4. General Options 5. Themes 6. Address Books 7. Message of the Day (MOTD) 8. Plugins 9. Database 10. Languages D. Set pre-defined settings for specific IMAP servers C Turn color on S Save data Q Quit Command >> S Data saved in config.php Press enter to continue... <-- ENTER SquirrelMail Configuration : Read: config.php (1.4.0) --------------------------------------------------------- Main Menu -- 1. Organization Preferences 2. Server Settings 3. Folder Defaults 4. General Options 5. Themes 6. Address Books 7. Message of the Day (MOTD) 8. Plugins 9. Database 10. Languages D. Set pre-defined settings for specific IMAP servers C Turn color on S Save data Q Quit Command >> <-- Q
Bây giờ bạn có thể tìm thấy SquirrelMail trong đường dẫn /usr/share/squirrelmail/. Việc cần làm lúc này là cấu hình vhost để nginx có thể tìm thấy SquirrelMail trong thư mục trên.
Mở /etc/nginx/sites-available/www.example.com.vhost:
vi /etc/nginx/sites-available/www.example.com.vhost
Và thêm vào phần nội dung sau trong server {}:
server { [...] location /squirrelmail { root /usr/share/; index index.php index.html index.htm; location ~ ^/squirrelmail/(.+\.php)$ { try_files $uri =404; root /usr/share/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include /etc/nginx/fastcgi_params; } location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /usr/share/; } } location /webmail { rewrite ^/* /squirrelmail last; } [...] }
Khởi động lại nginx:
/etc/init.d/nginx reload
Sau khi hoàn thành, mở trình duyệt của bạn lên và nhập vào địa chỉ http://www.example.com/squirrelmail hoặc http://www.example.com/webmail. Màn hình sẽ hiển thị như sau:
Tiến hành đăng nhập vào SquirrelMail:
Trường hợp bạn sử dụng https thay cho http trên vhost, chỉ cần thêm dòng fastcgi_param HTTPS on; cho phần cấu hình SquirrelMail như dưới đây:
vi /etc/nginx/sites-available/www.example.com.vhost
server { [...] location /squirrelmail { root /usr/share/; index index.php index.html index.htm; location ~ ^/squirrelmail/(.+\.php)$ { try_files $uri =404; root /usr/share/; fastcgi_pass 127.0.0.1:9000; fastcgi_param HTTPS on; # <-- Dong moi duoc them vao fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include /etc/nginx/fastcgi_params; } location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /usr/share/; } } location /webmail { rewrite ^/* /squirrelmail last; } [...] }
Nếu muốn sử dụng đồng thời cả http lẫn https cho vhost, bạn thêm phần http {} trong /etc/nginx/nginx.conf và sử dụng biến $fastcgi_https để xác định việc dùng https (vhost ở đây là www.example.com):
vi /etc/nginx/nginx.conf
[...] http { [...] ## Detect when HTTPS is used map $scheme $fastcgi_https {
default off;
https on;
} ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } [...]
Sau đó mở file cấu hình vhost của bạn, thay dòng fastcgi_param HTTPS on; bằng fastcgi_param HTTPS $fastcgi_https; (với $fastcgi_https là biến vừa khởi tạo):
vi /etc/nginx/sites-available/www.example.com.vhost
server { [...] location /squirrelmail { root /usr/share/; index index.php index.html index.htm; location ~ ^/squirrelmail/(.+\.php)$ { try_files $uri =404; root /usr/share/; fastcgi_pass 127.0.0.1:9000; fastcgi_param HTTPS $fastcgi_https; # <-- Dong moi duoc them vao fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include /etc/nginx/fastcgi_params; } location ~* ^/squirrelmail/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /usr/share/; } } location /webmail { rewrite ^/* /squirrelmail last; } [...] }
Cuối cùng khởi động lại nginx:
/etc/init.d/nginx reload
Các link tham khảo:
SquirrelMail: http://squirrelmail.org/
nginx: http://nginx.org/
nginx Wiki: http://wiki.nginx.org/
Debian: http://www.debian.org/
Ubuntu: http://www.ubuntu.com/