linux-server-expert
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLinux Server Expert
Linux服务器专家
Initial Server Setup
初始服务器配置
bash
undefinedbash
undefinedUpdate system
Update system
apt update && apt upgrade -y
apt update && apt upgrade -y
Create user with sudo
Create user with sudo
adduser deploy
usermod -aG sudo deploy
adduser deploy
usermod -aG sudo deploy
SSH key auth
SSH key auth
mkdir -p /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
mkdir -p /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
Add public key to authorized_keys
Add public key to authorized_keys
Disable root login & password auth
Disable root login & password auth
vim /etc/ssh/sshd_config
vim /etc/ssh/sshd_config
PermitRootLogin no
PermitRootLogin no
PasswordAuthentication no
PasswordAuthentication no
systemctl restart sshd
undefinedsystemctl restart sshd
undefinedFirewall (UFW)
防火墙(UFW)
bash
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw statusbash
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw statusNginx Configuration
Nginx配置
nginx
undefinednginx
undefined/etc/nginx/sites-available/myapp
/etc/nginx/sites-available/myapp
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}}
```bashserver {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}}
```bashEnable site
Enable site
ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
undefinedln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
undefinedSSL with Let's Encrypt
使用Let's Encrypt配置SSL
bash
apt install certbot python3-certbot-nginx -y
certbot --nginx -d example.com -d www.example.combash
apt install certbot python3-certbot-nginx -y
certbot --nginx -d example.com -d www.example.comAuto-renewal is set up automatically
Auto-renewal is set up automatically
certbot renew --dry-run
undefinedcertbot renew --dry-run
undefinedSystemd Service
Systemd服务
ini
undefinedini
undefined/etc/systemd/system/myapp.service
/etc/systemd/system/myapp.service
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapp
ExecStart=/usr/bin/node dist/main.js
Restart=on-failure
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
```bash
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp
systemctl status myapp[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/home/deploy/myapp
ExecStart=/usr/bin/node dist/main.js
Restart=on-failure
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
```bash
systemctl daemon-reload
systemctl enable myapp
systemctl start myapp
systemctl status myappQuick Commands
常用命令
bash
undefinedbash
undefinedLogs
Logs
journalctl -u myapp -f # Service logs
tail -f /var/log/nginx/error.log
journalctl -u myapp -f # Service logs
tail -f /var/log/nginx/error.log
Disk
Disk
df -h # Disk usage
du -sh /var/* # Directory sizes
df -h # Disk usage
du -sh /var/* # Directory sizes
Process
Process
htop # Process monitor
lsof -i :3000 # What uses port
undefinedhtop # Process monitor
lsof -i :3000 # What uses port
undefined