Kiến Thức Máy Tính

Cài đặt và cấu hình máy chủ webserver với hệ điều hành Centos 7 và Vesta

Môi trường cài đặt là Centos 7. Tại thời điểm thực hiện Centos 8 không thể cài được Vesta Control panel

Mở post (NAT) trên modem mạng

Hướng dẫn này làm ở Modem HG8045A2, các modem khác tìm thông tin trên mạng để thực hiện.

  • Đăng nhập modem, vào phần Adcanced Setup
  • Vào Forward Rules > DMZ Configuration nhấn New và nhập như ảnh. Thay IP bằng IP của máy cần mở port. Chức năng này sẽ mở hết các port về máy chủ.
  • Chức năng Port Mapping Configuration trên modem này tạm thời không dùng được với port 80, 443 (không biết tắt internal port ở đâu)

NAT Modem HG8045A2

Cài đặt máy chủ

Cài đặt centos 7 như bình thường. Lưu ý chọn Minimal Install để có một máy mới sạch, không có sẵn các thành phần thừa.
Nếu đĩa cài đặt là DVD ISO thì bạn cài đặt bình thường. Nếu là Net Install thì đến bước chọn Installation Source bạn nhập http://centos-hcm.viettelidc.com.vn/7/os/x86_64/ Hoặc lấy các Source khác nhanh hơn ở https://www.centos.org/download/mirrors/

Cài đặt Vesta Control panel

Đặt múi giờ và domain (nếu cần thiết).

Nếu trong quá trình cài đặt bạn cài múi giờ, hostname chuẩn rồi thì bỏ qua

timedatectl set-timezone Asia/Ho_Chi_Minh
hostnamectl set-hostname writeblabla.com
# check hostname
hostnamectl status

Cài đặt Vesta Control panel

# Download
curl -O http://vestacp.com/pub/vst-install.sh
# Cài đặt
bash vst-install.sh

Sau đó làm theo hệ thống yêu cầu. Nhập email, tên miền vào trong quá trình cài đặt. Tên miền nhập vào sẽ là tên miền mặc định khi trỏ IP vào. Nếu báo lỗi tồn tại httpd mặc dù đã gỡ có thể thêm --force để bỏ qua báo lỗi và cài đặt. Sau khi cài đặt màn hình sẽ hiển thị tài khoản admin và mật khẩu tối cao. Lưu giữ tài khoản này lại cẩn thận để đăng nhập.

Sửa dung lượng upload: Mở /etc/php.ini lên sửa sau đó chạy

systemctl restart nginx
systemctl restart httpd

Cài đặt Let’s Encrypt

Hiện nay SSL để chạy là không thể thiếu cho các website, do đó cần cài Let’s Encrypt để tạo SSL miễn phí.

# Cài certbot
yum install certbot

Tạo chứng chỉ cho site quản trị

service nginx stop
certbot certonly --standalone --agree-tos --renew-by-default --email youremail@gmail.com -d writeblabla.com

rm -f /usr/local/vesta/ssl/certificate.crt
rm -f /usr/local/vesta/ssl/certificate.key
cp /etc/letsencrypt/live/writeblabla.com/fullchain.pem /usr/local/vesta/ssl/certificate.crt
cp /etc/letsencrypt/live/writeblabla.com/privkey.pem /usr/local/vesta/ssl/certificate.key

chown root:mail /usr/local/vesta/ssl/certificate.crt
chown root:mail /usr/local/vesta/ssl/certificate.key
chmod 0660 /usr/local/vesta/ssl/certificate.crt
chmod 0660 /usr/local/vesta/ssl/certificate.key

service nginx start
service vesta restart
service httpd restart
service exim restart

Tạo chứng chỉ cho domain

certbot certonly -a webroot --webroot-path=/home/nukeviet/web/writeblabla.com/public_html --email myemail@gmail.com -d writeblabla.com -d www.writeblabla.com

Chứng chỉ SSL sẽ lưu ở: /etc/letsencrypt/live/writeblabla.com/ Đăng nhập quản trị Vesta của domain, sửa domain và đánh dấu vào mục SSL Support sau đó:

  • Copy nội dung file cert.pem bỏ vào ô SSL Certificate
  • Copy nội dung file privkey.pem bỏ vào ô SSL Key
  • Copy nội dung file fullchain.pem bỏ vào ô SSL Certificate Authority / Intermediate

Sửa file /home/admin/conf/web/writeblabla.com.nginx.conf thêm vào

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

Sau đó chạy systemctl restart nginx để bắt buộc chuyển http sang https
Tự động gia hạn chứng chỉ sau 85 ngày (90 ngày nó hết hạn nhưng gia hạn sớm 5 ngày cho chắc). Cron này chạy vào 3h sáng để tránh bị dừng website do khởi động lại nginx.

MAILTO=""

# Gia han SSL writeblabla.com
0 3 */85 * * sh /root/crontab/renew_ssl.writeblabla.com.sh

Nội dung file renew_ssl.writeblabla.com.sh:

#!/bin/sh

certbot certonly -a webroot --webroot-path=/home/nukeviet/web/writeblabla.com/public_html --agree-tos --renew-by-default --email myemail@gmail.com -d writeblabla.com -d www.writeblabla.com

rm -f /home/admin/conf/web/ssl.writeblabla.com.ca;
rm -f /home/admin/conf/web/ssl.writeblabla.com.crt;
rm -f /home/admin/conf/web/ssl.writeblabla.com.pem;
rm -f /home/admin/conf/web/ssl.writeblabla.com.key;

cp /etc/letsencrypt/live/writeblabla.com/chain.pem /home/admin/conf/web/ssl.writeblabla.com.ca;
cp /etc/letsencrypt/live/writeblabla.com/cert.pem /home/admin/conf/web/ssl.writeblabla.com.crt;
cp /etc/letsencrypt/live/writeblabla.com/fullchain.pem /home/admin/conf/web/ssl.writeblabla.com.pem;
cp /etc/letsencrypt/live/writeblabla.com/privkey.pem /home/admin/conf/web/ssl.writeblabla.com.key;

rm -f /usr/local/vesta/data/users/admin/ssl/writeblabla.com.ca;
rm -f /usr/local/vesta/data/users/admin/ssl/writeblabla.com.crt;
rm -f /usr/local/vesta/data/users/admin/ssl/writeblabla.com.pem;
rm -f /usr/local/vesta/data/users/admin/ssl/writeblabla.com.key;

cp /etc/letsencrypt/live/writeblabla.com/chain.pem /usr/local/vesta/data/users/admin/ssl/writeblabla.com.ca;
cp /etc/letsencrypt/live/writeblabla.com/cert.pem /usr/local/vesta/data/users/admin/ssl/writeblabla.com.crt;
cp /etc/letsencrypt/live/writeblabla.com/fullchain.pem /usr/local/vesta/data/users/admin/ssl/writeblabla.com.pem;
cp /etc/letsencrypt/live/writeblabla.com/privkey.pem /usr/local/vesta/data/users/admin/ssl/writeblabla.com.key;

systemctl restart nginx

Công việc tối ưu sau cài đặt

Cấu hình tường lửa

Máy chủ khi chạy sẽ có rất nhiều Bot tự dò pass SSH, pass FTP (thực tế thử cài 1 máy chủ hoàn toàn mới, sau 1 ngày thì có khoảng gần 10 địa chỉ IP khác nhau dò pass SSH cứ mỗi 3 phút một lần)... nên cần block các cổng này lại.
Sau khi cài đặt Vesta Control panel ta có sẵn iptables để làm tường lửa, ta sử dụng luôn khỏi cần cài cái khác.

- Kiểm tra trạng thái hoạt động

systemctl status iptables

- Hiển thị danh sách các quy tắc hiện tại

iptables -L

Mặc định sau khi cài xong các rules sẽ như sau:

Chain INPUT (policy DROP)
target          prot opt source               destination
fail2ban-VESTA  tcp  --  anywhere             anywhere             tcp dpt:us-srv
fail2ban-MAIL   tcp  --  anywhere             anywhere             multiport dports smtp,urd,submission,ms-v-worlds,pop3,pop3s,imap,imaps
fail2ban-FTP    tcp  --  anywhere             anywhere             tcp dpt:ftp
fail2ban-SSH    tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT          all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT          all  --  writeblabla.com      anywhere
ACCEPT          all  --  localhost            anywhere
ACCEPT          tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT          tcp  --  anywhere             anywhere             multiport dports http,https
ACCEPT          tcp  --  anywhere             anywhere             multiport dports ftp,entextxid:12100
ACCEPT          udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT          tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT          tcp  --  anywhere             anywhere             multiport dports smtp,urd,submission,ms-v-worlds
ACCEPT          tcp  --  anywhere             anywhere             multiport dports pop3,pop3s
ACCEPT          tcp  --  anywhere             anywhere             multiport dports imap,imaps
ACCEPT          tcp  --  anywhere             anywhere             multiport dports mysql,postgres
ACCEPT          tcp  --  anywhere             anywhere             tcp dpt:us-srv
ACCEPT          icmp --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target          prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target          prot opt source               destination

Chain fail2ban-FTP (1 references)
target          prot opt source               destination
RETURN          all  --  anywhere             anywhere

Chain fail2ban-MAIL (1 references)
target          prot opt source               destination
RETURN          all  --  anywhere             anywhere

Chain fail2ban-SSH (1 references)
target          prot opt source               destination
RETURN          all  --  anywhere             anywhere

Chain fail2ban-VESTA (1 references)
target          prot opt source               destination
RETURN          all  --  anywhere             anywhere

Chain vesta (0 references)
target          prot opt source               destination

Kịch bản quản lý sẽ là chặn toàn bộ kết nối vào SSH, FTP trừ mạng LAN và 1 IP cụ thể. Hành động cụ thể được biểu diễn

  • Cấm toàn bộ truy cập vào cổng 21 (FTP), 8083 (Vesta Control panel), 22 (SSH)
  • Cho phép truy cập các cổng trên từ dải IP local, IP cố định cụ thể

Thứ tự áp dụng của iptables được quét từ dưới lên do đó để không làm thay đổi gì đến các rule có sẵn, ta tiến hành thêm rule lên đầu tiên:

# Cho phép truy cập các cổng trên trong mạng nội bộ
iptables -I INPUT 1 -p tcp -s 192.168.1.1/24 -m multiport --dports 21,12000:12100,22,8083 -j ACCEPT

# Cho phép truy cập các cổng trên từ IP cụ thể
iptables -I INPUT 1 -p tcp -s 117.2.50.16 -m multiport --dports 21,12000:12100,22,8083 -j ACCEPT

# Chặn tất cả truy cập các cổng trên (thêm vào dòng thứ 3 sau)
iptables -I INPUT 3 -p tcp -m multiport --dports 21,12000:12100,22,8083 -j DROP

- Xóa rule trong Chain INPUT hàng số 19. Dùng iptables -L để xem các rules, sau đó thứ tự được tính sau dòng target prot opt source destination

iptables -D INPUT 19

- Lưu lại các quy tắc (sau bất kể thay đổi gì thì lưu lại nếu không khi khởi động lại server sẽ bị mất) và khởi động lại để áp dụng

service iptables save
systemctl restart iptables

Giới hạn IP truy cập phpmyadmin

Mở file /etc/httpd/conf.d/phpMyAdmin.conf tìm

<Directory /usr/share/phpMyAdmin/>
   Order Deny,Allow
   Deny from All
   Allow from All
</Directory>

Thay lại thành

<Directory /usr/share/phpMyAdmin/>
   Order Deny,Allow
   Deny from All
   Allow from 117.2.50.16
</Directory>

Trong đó 117.2.50.16 là địa chỉ IP của bạn. Sau đó chạy systemctl restart nginx, systemctl restart httpd để áp dụng.

Bình luận (0)