Trên Directadmin có hệ thống webmail chạy cũng khá ổn định. Tuy nhiên link login thường là https://ten-mien.com/roundcube. Hơi khó nhớ!
Hôm nay mình chia sẽ cách cấu hình webmail.ten-mien.com thành link mặc định cho tất cả tên miền trên server directadmin của mình.
Đối tượng áp dụng:
- Webserver: Apache
- Let’s encrypt enable
- Đã cài nano
Bước 1: Cấu hình Apache
Đầu tiên ta cấu hình cho port 80 (non ssl), ta chạy lệnh
nano /usr/local/directadmin/data/templates/custom/virtual_host2.conf.CUSTOM.4.post
Sau đó ta thêm vào file nội dung sau:
</VirtualHost> <VirtualHost |IP|:|PORT_80| |MULTI_IP|> ServerName webmail.|DOMAIN| ServerAdmin |ADMIN| DocumentRoot /var/www/html/roundcube CustomLog /var/log/httpd/domains/|DOMAIN|.bytes bytes CustomLog /var/log/httpd/domains/|DOMAIN|.log combined ErrorLog /var/log/httpd/domains/|DOMAIN|.error.log <IfModule !mod_ruid2.c> SuexecUserGroup webapps webapps </IfModule>
Tiếp theo là cấu hình port 443 (ssl), ta chạy lệnh
nano /usr/local/directadmin/data/templates/custom/virtual_host2_secure.conf.CUSTOM.4.post
sau đó thêm vào nội dung sau:
</VirtualHost> <VirtualHost |IP|:|PORT_443| |MULTI_IP|> ServerName webmail.|DOMAIN| ServerAdmin |ADMIN| DocumentRoot /var/www/html/roundcube SSLEngine on SSLCertificateFile |CERT| SSLCertificateKeyFile |KEY| |CAROOT| CustomLog /var/log/httpd/domains/|DOMAIN|.bytes bytes CustomLog /var/log/httpd/domains/|DOMAIN|.log combined ErrorLog /var/log/httpd/domains/|DOMAIN|.error.log <IfModule !mod_ruid2.c> SuexecUserGroup webapps webapps </IfModule>
Cuối cùng ta chạy các lệnh sau để cập nhật lại cấu hình apache
echo "action=rewrite&value=httpd" >> /usr/local/directadmin/data/task.queue /usr/local/directadmin/dataskq d cd /usr/local/directadmin/custombuild ./build rewrite_confs
Bước 2: Thêm trường webmail vào mặc định DNS
Chỉ đơn giản là chạy các lệnh sau:
cd /usr/local/directadmin/data/templates cp dns_a.conf custom cd custom perl -pi -e "s/^smtp=\\|IP\\|\n/smtp=\\|IP\\|\nwebmail=\\|IP\\|\n/" dns_a.conf
Bước 3: Cấu hình cài đặt ssl cho webmail bằng let’s encrypt
Để cài ssl bằng let’s encrypt thì dĩ nhiên là phải cấu hình cho let’s encrypt nó nhận được link webmail, vì mặc định webmail chưa được thêm vào trong host dns. Để làm được điều này ta chạy lệnh sau:
nano /usr/local/directadmin/conf/directadmin.conf
và thêm 2 dòng sau vào cuối file
letsencrypt_list=www:mail:ftp:pop:smtp:webmail letsencrypt_list_selected=www:webmail
Restart dịch vụ directadmin: service directadmin restart
Sau đó chúng ta tiến hành cài SSL cho webmail như cài ssl thông thường (Lưu ý là phải trỏ sub domain webmail về IP của server nhé)
Kết quả demo: https://webmail.legiang.blog/
Note: Nếu các bạn muốn link webmail này chỉ chạy trên https, hãy thêm các dòng sau vào file cấu hình port 80 bên trên
Chúng ta chạy lệnh
nano /usr/local/directadmin/data/templates/custom/virtual_host2.conf.CUSTOM.4.post
Rồi thêm vào
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
Sau khi thêm xong chúng ta chạy lệnh sau để cập nhật lại cấu hình cho các tên miền
cd /usr/local/directadmin/custombuild /build rewrite_confs
Cấu hình cho Nginx Proxy Apache
Đây là dạng cấu hình mà nhiều bạn hay dùng nhất. Nên không thể bỏ qua việc cấu hình webmail trên nó
- Cấu hình port 80 (non ssl)
Chúng ta chạy các lệnh sau:
cd /usr/local/directadmin/data/templates cp nginx_server.conf custom cd custom nano nginx_server.conf
Sau đó thêm vào cuối file nội dung sau:
server { listen |IP|:|PORT_80|; |MULTI_IP| server_name webmail.|DOMAIN|; root /var/www/html/roundcube; index index.php index.html index.htm; access_log /var/log/nginx/domains/|DOMAIN|.log; access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes; error_log /var/log/nginx/domains/|DOMAIN|.error.log; |*if HAVE_PHP1_FPM="1"| # use fastcgi for all php files location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/nginx_limits.conf; if (-f $request_filename) { fastcgi_pass unix:/usr/local/php|PHP1_RELEASE|/sockets/webapps.sock; } } |*endif| |*if HAVE_NGINX_PROXY="1"| location / { # access_log off; proxy_pass http://127.0.0.1:|PORT_8080|; proxy_set_header X-Client-IP $remote_addr; proxy_set_header X-Accel-Internal /nginx_static_files; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /nginx_static_files/ { # access_log /var/log/nginx/access_log_proxy; alias /var/www/html/roundcube/; internal; } |*endif| # deny access to apache .htaccess files location ~ /\.ht { deny all; } }
2. Cấu hình port 443 (ssl)
Chúng ta sẽ chạy các lệnh sau đây:
cd /usr/local/directadmin/data/templates/ cp nginx_server_secure.conf custom cd custom nano nginx_server_secure.conf
Ta cũng thêm vào cuối file đoạn code sau:
server { listen |IP|:|PORT_443| ssl|SPACE_HTTP2|; |MULTI_IP| server_name webmail.|DOMAIN|; root /var/www/html/roundcube; index index.php index.html index.htm; ssl_certificate |CERT|; ssl_certificate_key |KEY|; |FORCE_SSL_REDIRECT| access_log /var/log/nginx/domains/|DOMAIN|.log; access_log /var/log/nginx/domains/|DOMAIN|.bytes bytes; error_log /var/log/nginx/domains/|DOMAIN|.error.log; |*if HAVE_PHP1_FPM="1"| # use fastcgi for all php files location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/nginx_limits.conf; if (-f $request_filename) { fastcgi_pass unix:/usr/local/php|PHP1_RELEASE|/sockets/webapps.sock; } } |*endif| |*if HAVE_NGINX_PROXY="1"| location / { # access_log off; proxy_pass https://127.0.0.1:|PORT_8081|; proxy_set_header X-Client-IP $remote_addr; proxy_set_header X-Accel-Internal /nginx_static_files; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /nginx_static_files/ { # access_log /var/log/nginx/access_log_proxy; alias /var/www/html/roundcube/; internal; } |*endif| # deny access to apache .htaccess files location ~ /\.ht { deny all; } }
Sau cùng là chạy các lệnh sau để cập nhật cấu hình
cd /usr/local/directadmin/custombuild ./build rewrite_confs
Chúc các bạn thành công!