Setup New Ubuntu Server
April 26, 2026
82 views

Thông tin chung server
Server IP address: IP_ADDRESS
Server Domain: DOMAIN
Đăng ký Server SSL: https://dash.cloudflare.com
Server VM Template: TEMPLATE
Thông tin tài khoản root
Username: USER_NAME
Mật khẩu đăng nhập: PASSWORD
Cập nhật và nâng cấp các gói tài nguyên hệ thống
Thực thi các lệnh dưới đây:
sudo apt update && sudo apt upgrade -yKhởi động lại Server.
sudo rebootCài đặt các gói thư viện cần thiết
Thực thi lệnh dưới đây:
sudo apt install software-properties-common apt-transport-https wget -yThay đổi mật khẩu
Thực thi lệnh dưới đây để thay đổi mật khẩu:
passwdXem thông tin cấu hình dịch vụ ssh
Thực thi lệnh dưới đây để xem cấu hình SSH:
cat /etc/ssh/sshd_configXem thông tin một lệnh
Thực thi lệnh dưới đây để xem thông tin và cách sử dụng một lệnh:
man [command_name]Xem danh sách các gói tài nguyên hệ thống
Thực thi lệnh dưới đây để xem danh sách các gói tài nguyên trong hệ thống:
cat /etc/apt/sources.listCác lệnh cơ bản
Thực thi lệnh dưới đây để xem thông tin phần cứng Server:
lshwThực thi lệnh dưới đây để xem thông tin CPU:
lscpuXem thông tin hệ thống:
uname -aXem thông tin các tiến trình đang chạy:
htopXem thông tin bộ nhớ:
df -hDi chuyển trỏ chuột về đầu:
Control + ADi chuyển trỏ chuột về cuối:
Control + ECác lệnh về tài nguyên hệ thống
Cài đặt gói tài nguyên mới:
sudo apt -y install [package_name]Gỡ bỏ gói tài nguyên:
sudo apt remove [package_name]Các lệnh về quản lý User
Xem danh sách các user:
cut -d: -f1 /etc/passwdTạo mới user:
adduser [user_name]Thực thi lần lượt các lệnh dưới đây để thêm user vào Sudoers:
ls -l /etc/sudoers
chmod u+w /etc/sudoers
vi /etc/sudoers
"[username] ALL=(ALL:ALL) ALL" # Add this line at #User privilege specification
chmod u-w /etc/sudoersChuyển đổi giữa các tài khoản đăng nhập:
su [user_name]Xoá tài khoản và thư mục home:
sudo userdel [user_name]
sudo rm -rf /home/[user_name]Cài đặt Node
Thực thi lần lượt các lệnh dưới đây để cài đặt Node Version Manager (Xem thêm):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
source ~/.bashrcKiểm tra danh sách các Node version có thể cài đặt:
nvm list-remoteCài đặt Node version mong muốn:
nvm install [version]Kiểm tra danh sách các Node version đã cài đặt:
nvm listChuyển đổi giữa các Node version:
nvm use [version]Cài Node version mong muốn làm mặc định:
nvm alias default [version]Cài đặt Docker
Đầu tiên, cần cập nhật danh sách các gói tài nguyên (Xem thêm):
sudo apt updateNext, install a few prerequisite packages which let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-commonThen add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgAdd the Docker repository to APT sources:
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullUpdate your existing list of packages again for the addition to be recognized:
sudo apt updateMake sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ceFinally, install Docker:
sudo apt install docker-ceDocker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status dockerCài đặt Nginx Service
Thực thi lần lượt các lệnh dưới đây để cài đặt Nginx (Xem thêm):
sudo apt update
sudo apt install nginxList the application configurations that ufw knows how to work with by typing:
sudo ufw app listOutput:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSHYou can enable this by typing:
sudo ufw allow 'Nginx HTTP'You can verify the change by typing:
sudo ufw statusWe can check with the systemd init system to make sure the service is running by typing:
systemctl status nginxTo stop your web server, type:
sudo systemctl stop nginxTo start the web server when it is stopped, type:
sudo systemctl start nginxTo stop and then start the service again, type:
sudo systemctl restart nginxIf you are only making configuration changes, Nginx can often reload without dropping connections. To do this, type:
sudo systemctl reload nginxBy default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
sudo systemctl disable nginxTo re-enable the service to start up at boot, you can type:
sudo systemctl enable nginxCài đặt Portainer
First, create the volume that Portainer Server will use to store its database (Xem thêm):
docker volume create portainer_dataThen, download and install the Portainer Server container:
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:ltsNow that the installation is complete, you can log into your Portainer Server instance by opening a web browser and going to:
https://localhost:9443Cài đặt Nginx Proxy Manager
Create a docker-compose.yml file similar to this (Xem thêm):
services:
app:
image: 'docker.io/jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
- '80:80'
- '81:81'
- '443:443'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencryptBring up your stack by running:
docker-compose up -d
# If using docker-compose-plugin
docker compose up -dWhen your docker container is running, connect to it on port 81 for the admin interface. Sometimes this can take a little bit because of the entropy of keys. Below is default admin account:
Email: [email protected]
Password: changemeCài đặt Webmin
First, update your server’s package index if you’ve not done so recently (Xem thêm):
sudo apt updateThen we need to add the Webmin repository so that we can install and update Webmin using our package manager. We do this by adding the repository to the /etc/apt/sources.list file. Open the file in your preferred editor. Here, we’ll use nano:
sudo nano /etc/apt/sources.listThen add this line to the bottom of the file to add the new repository (/etc/apt/sources.list):
deb http://download.webmin.com/download/repository sarge contribNext, you’ll add the Webmin PGP key so that your system will trust the new repository. In order to do that, though, you must install the gnupg1 package, which is GNU’s tool for secure communication and data storage.
Following that, download the Webmin PGP key with wget and add it to your system’s list of keys:
wget -q -O- http://www.webmin.com/jcameron-key.asc | sudo apt-key addNext, update the list of packages again in order to include the now-trusted Webmin repository:
sudo apt updateThen install Webmin:
sudo apt install webminOnce the installation finishes, you’ll be presented with the following output:
. . .
Webmin install complete. You can now login to
https://your_server:10000 as root with your
root password, or as any user who can use sudo.Note: If you installed and enabled ufw during the prerequisite step, you will need to run the following command in order to allow Webmin through the firewall:
sudo ufw allow 10000Cài đặt "Internal redirect URL overrides" trong webmin configuration.
Cấu hình Trusted Referrers trong webmin configuration.
Cài đặt Wordpress
Xem thêm tại: https://vietnix.vn/cai-dat-wordpress-bang-docker-compose
Kiểm tra cấu hình Firewall
Xem trạng thái:
sudo ufw statusBật tắt tường lửa:
sudo ufw enable/disableKiểm tra tường lửa:
iptables -LThêm rules:
ufw allow [port]Xoá bỏ tất cả các Docker container
Thực thi lần lượt các lệnh sau để xoá bỏ tất cả các Docker container:
sudo docker ps -qa | xargs -n1 sudo docker stop
sudo docker ps -qa | xargs -n1 sudo docker rm
sudo docker images -qa | xargs -n1 sudo docker image rmKiểm tra các port đang chạy
Thực thi lệnh sau để kiểm tra các port đang chạy:
sudo lsof -i -P -n | grep LISTENXoá một port đang chạy:
kill [id]Factory Reset
Thực thi lần lượt các lệnh sau để factory reset:
sudo dpkg --configure -a
sudo apt-get update
sudo apt-get -f install
sudo apt-get full-upgrade
sudo apt-get install --reinstall ubuntu-desktop
sudo apt-get autoremove
sudo apt-get clean
sudo rebootCấu hình dịch vụ SSH
Cặp khoá xác thực được lưu trữ bên trong thư mục “/.ssh” ở thư mục home của mỗi user nhất định. Chỉnh sửa file cấu hình của ssh bằng lệnh dưới đây:
sudo vim /etc/ssh/sshd_configSau đó bật xác thực bằng public key và cấu hình thư mục chứa khoá xác thực. Khởi động lại dịch ssh:
sudo systemctl restart sshTạo cặp khoá xác thực tại thư mục home:
cd $HOME/.ssh && ssh-keygen -t rsaĐổi tên khoá xác thực public key:
sudo mv $HOME/.ssh/id_rsa.pub $HOME/.ssh/authorized_keysCopy khoá xác thực private key và lưu về máy client. Khoá xác thực tại máy client lưu ở “~/.ssh/id_rsa”. Ở máy client thực thi lệnh sau:
scp root@[ip]:.ssh/id_rsa ~/.ssh/id_rsaTạo file config ssh ở máy client:
touch ~/.ssh/configNội dung file cấu hình:
Host [ip_address]
User <username>
Port 22
PreferredAuthentications publickey
IdentityFile "~/.ssh/id_rsa"Phần quyền khoá xác thực ở server:
sudo chmod 600 $HOME/.ssh/authorized_keys
sudo chmod 700 $HOME/.ssh
sudo chmod 700 $HOME