Chỉnh DNS khi không truy cập được mạng
sudo nano /etc/resolv.conf
nameserver 8.8.8.8
BƯỚC 1 CÀI SSH
sudo apt-get update
sudo apt-get upgrade
sudo apt install openssh-server openssh-client
sudo systemctl status ssh
ip a (xem ip)
BƯỚC 2 CÀI IP TĨNH
50-cloud-init.yaml (ubuntu server)
01-netcfg.yaml (ubuntu desktop)
=> tạo 1 file cấu hình và lưu lại như tên ở trên
sudo nano /etc/netplan/50-cloud-init.yaml
network:
version: 2
renderer: networkd
ethernets:
enp7s0:
dhcp4: no
dhcp6: no
addresses:
- 192.168.14.165/24
routes:
- to: 0.0.0.0/0
via: 192.168.14.9
nameservers:
addresses: [192.168.14.9]
sudo netplan apply
networkctl status (kiểm tra mạng)
sudo reboot
BƯỚC 3 CÀI APACHE
sudo apt update
sudo apt install apache2
apache2 -v (kiểm tra)
systemctl status apache2 (kiểm tra Apache)
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
systemctl reload apache2 (Khởi động lại dịch vụ)
systemctl enable apache2 (Bật khởi chạy cùng hệ thống)
sudo nano /etc/apache2/ports.conf
BƯỚC 4 CÀI MYSLQ
sudo apt update
sudo apt upgrade
sudo apt install mariadb-server
sudo systemctl status mysql (kiểm tra)
systemctl start mariadb (chạy)
BƯỚC 5 CÀI PHP
sudo apt-get install php
systemctl reload apache2 (khởi động lại Apache)
BƯỚC 6 CÀI PHPMYADMIN
sudo apt install phpmyadmin
=> (tên đăng nhập phpmyadmin + mật khẩu bạn chọn lúc cài)
sudo service mysql start (mở)
sudo /etc/init.d/mysql stop (tắt)
sudo /etc/init.d/mysql restart (khởi động lại)
=> Để truy cập phpMyAdmin các bạn gõ trên thanh địa chỉ ip_của máy/phpmyadmin .
BƯỚC 7 TẠO TÀI KHOẢNG MYSQL
sudo mysql -u root -p (đăng nhập)
create user 'username'@'localhost' identified by 'password'; (Tao ten nguoi dung myphp)
set password for ''username'@'localhost' = password('password'); (đổi password)
GRANT ALL PRIVILEGES ON * . * TO 'new_user'@'localhost'; (toàn quyền)
FLUSH PRIVILEGES; (thực hiện ngay)
=> tăng dung lượng upload file PHP
vào thư mục /etc/php/7.4/apache2/php.ini (chỉnh file php.ini)
upload_max_filesize = 20M
post_max_size = 20M
max_execution_time=300
max_input_time=300
memory_limit=128M
export database
mysqldump -u ubuntu -pubuntu trungdungmedia.com > trungdungmedia_com.sql
import database
mysql -u ubuntu -pquenmatkhau trungdungmedia.com < trungdungmedia_com.sql
BƯỚC 8 TẠO VIRTUAL HOST CHỨA TRANG WEB
Tìm /etc/apache2/sites-available
tạo file tên miền.com.conf
=================================================================================
<VirtualHost *:80>
ServerName tenmien
ServerAlias www. tenmien
ServerAdmin webmaster@tenmien
DocumentRoot /var/www/thu muc chua web
<Directory /var/www/thu muc chua web>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/domain1.com-error.log
CustomLog ${APACHE_LOG_DIR}/domain1.com-access.log combined
</VirtualHost>
=================================================================================
apache2ctl -t
sudo a2ensite tên.conf (thực thi)
sudo systemctl restart apache2
BƯỚC 9 ĐĂNG KÝ CHỨNG CHỈ SSL MIỄN PHÍ
=> (trang chủ của certbot) https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-20-04
sudo apt install certbot python3-certbot-apache
sudo certbot --apache (cấp chứng chỉ)
sudo certbot certificates (kiểm tra chứng chỉ sắp hết hạng)
sudo certbot renew (gia hạn chứng chỉ)
KIỂM TRA CHỈNH NGÀY GIỜ HỆ THỐNG
sudo timedatectl set-timezone
sudo timedatectl set-timezone Asia/Ho_Chi_Minh
sudo timedatectl set-ntp on
timedatectl (xem múi giờ)
THÊM NGƯỜI DÙNG
sudo adduser "đặt tên"
sudo usermod -aG sudo "đặt tên"
ĐỔI CỔNG SSH UBUNTU
sudo nano /etc/ssh/sshd_config
Tìm dòng chứa thông số cổng SSH hiện tại. Mặc định là #Port 22.
Xóa ký tự # ở đầu dòng và sửa đổi số cổng thành cổng mới mà bạn muốn sử dụng. Ví dụ: Port 2222.
sudo systemctl restart ssh
ĐỔI port apache2
sudo nano /etc/apache2/ports.conf
sudo nano /etc/apache2/sites-available/000-default.conf
sudo apache2ctl configtest
sudo systemctl restart apache2
TÌM FILE
grep -r "chữ cần tìm" .
sudo find . -name "file cần tìm"
gỡ ứng dụng đã cài đặt
Gõ dpkg --list
Gõ sudo apt-get --purge remove tên ứng dụng cần gỡ bỏ
tải file
wget http://example.com/file.zip
THÊM Ổ HDD VÀO SERVER
lsblk (Kt)
sudo mkfs.ext4 /dev/tên ổ (định dạng ext4)
sudo fdisk /dev/tên ổ (format ổ đĩa)
lsblk -o NAME,UUID,SIZE (lấy UUID)
sudo mount UUID=5C8C05AA8C05802E /hdd1 (mount ổ cứng vào)
sudo mount UUID=D88A61A88A6183BA /hdd2 (mount ổ cứng vào)
KIỂM TRA SỨC KHỎE Ổ ĐĨA
sudo smartctl -H /dev/sda
Cài proxy ngược cho apache2
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
chỉ chạy proxy
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod ssl
sudo systemctl restart apache2
-------------------------------------------
<VirtualHost *:80>
ServerName xemlive.trungdungmedia.com
# Cấu hình Reverse Proxy
ProxyPreserveHost On
ProxyPass / http://192.168.14.165:2001/
ProxyPassReverse / http://192.168.14.165:2001/
# Nhật ký lỗi và truy cập
ErrorLog ${APACHE_LOG_DIR}/xemlive.trungdungmedia.com_error.log
CustomLog ${APACHE_LOG_DIR}/xemlive.trungdungmedia.com_access.log combined
</VirtualHost>
-------------------------------------------
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName xemlive.trungdungmedia.com
# Cấu hình Reverse Proxy
ProxyPreserveHost On
ProxyPass / http://192.168.14.165:2001/
ProxyPassReverse / http://192.168.14.165:2001/
# SSL
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/xemlive.trungdungmedia.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/xemlive.trungdungmedia.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
# Fix lỗi WebSocket (nếu dùng WebSocket hoặc Streamlit không hiển thị đúng)
RewriteEngine on
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://192.168.14.165:2001/$1 [P,L]
ProxyPassReverse / http://192.168.14.165:2001/
</VirtualHost>
</IfModule>
-------------------------------------------
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName daitruyenhinhcanhan.trungdungmedia.com
# SSL Configuration
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/daitruyenhinhcanhan.trungdungmedia.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/daitruyenhinhcanhan.trungdungmedia.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
# WebSocket Support - QUAN TRỌNG!
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://192.168.14.165:5005/$1 [P,L]
# WebSocket proxy cho /ws endpoint
ProxyPass /ws ws://192.168.14.165:5006/ws
ProxyPassReverse /ws ws://192.168.14.165:5005/ws
# Standard HTTP proxy
ProxyPreserveHost On
ProxyPass / http://192.168.14.165:5005/
ProxyPassReverse / http://192.168.14.165:5005/
# Proxy timeout (cho WebSocket)
ProxyTimeout 86400
# Headers
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
# Logging
ErrorLog ${APACHE_LOG_DIR}/photoqr-error.log
CustomLog ${APACHE_LOG_DIR}/photoqr-access.log combined
</VirtualHost>
</IfModule>
kiểm tra cấu hình
✔️ cat /proc/cpuinfo
✔️ CPU: lscpu
✔️ RAM: free -h
✔️ Ổ cứng: df -h, lsblk
✔️ Hệ điều hành: lsb_release -a, uname -a
✔️ Mạng: ip a, ifconfig -a
✔️ Tiến trình: top, htop
Tạo truy cập ssh không cần mật khẩu
* trên win tạo chạy cmd mkdir %USERPROFILE%\.ssh
ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_ssh_tunnel
---------------------------------------------
Sao chép khóa SSH tự động
cat ~/.ssh/id_rsa_ssh_tunnel.pub | ssh -p 2552 sv02@27.74.246.165 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
---------------------------------------------
trên win tạo chạy cmd
type %USERPROFILE%\.ssh\id_rsa_ssh_tunnel.pub | ssh -p 2552 sv02@27.74.246.165 "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
---------------------------------------------
kết nối trên win
ssh -p 22 -i %USERPROFILE%\.ssh\id_rsa_ssh_tunnel sv04@27.74.246.165
---------------------------------------------
tạo log kiểm tra lỗi
@reboot /bin/bash /home/sv01/chia_se_file_pro.sh > /home/sv01/cronlog.txt 2>&1
10 tiến trình ngốn CPU nhiều
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 15
thư mục cha không cho ghi
sudo chown -R www-data:www-data /var/www/trungdungmedia.com/cautruyenhinh
sudo chmod -R 775 /var/www/trungdungmedia.com/cautruyenhinh
kiểm tra xem có bao nhiêu ứng dụng đang chạy
ps aux | grep nginx
ps aux | grep ffmpeg
đổi mật khẩu
passwd
TẮT TƯỜNG LỮA UFW
sudo ufw status - kiểm tra
sudo ufw disable - tắt
sudo ufw enable - mở
TẮT TƯỜNG LỮA HỆ THỐNG
sudo systemctl stop firewalld
sudo systemctl disable firewalld
sudo systemctl mask firewalld
systemctl status firewalld