feat(blog): 添加 1Panel 自动化部署笔记,提供轻量级部署方案和 SSH 免密登录配置
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
# 1Panel Automated Deployment Notes: Say Goodbye to Manual Uploads, One-Command Takeoff from Local
|
||||
---
|
||||
layout: "@/layouts/BlogPostLayout.astro"
|
||||
title: "1Panel Automated Deployment Notes: Say Goodbye to Manual Uploads, One-Command Takeoff from Local"
|
||||
description: "Learn how to build a lightweight deployment pipeline using 1Panel with SSH key authentication and ACL permissions. Complete guide for solo developers to automate server deployments."
|
||||
date: "2024-06-08"
|
||||
image: "https://images.unsplash.com/photo-1558494949-ef010cbdcc31?q=80&w=1470&auto=format&fit=crop"
|
||||
tags: ["1Panel", "DevOps", "Automation", "SSH", "Deployment"]
|
||||
tagId: ["1panel", "devops", "automation", "ssh", "deployment"]
|
||||
category: "DevOps"
|
||||
categoryId: "devops"
|
||||
readTime: "5 min read"
|
||||
---
|
||||
|
||||
Updating code used to make me feel like a "human FTP client":
|
||||
|
||||
@@ -15,11 +26,13 @@ Running scripts as root? That's like performing surgery with a chainsaw. Let's c
|
||||
|
||||
```bash
|
||||
# Create the deployment account
|
||||
sudo adduser deploy_zgy
|
||||
sudo adduser deploy_user
|
||||
|
||||
# Critical: disable password login, SSH keys only
|
||||
# Even if someone guesses the password, they can't get in
|
||||
sudo usermod -s /usr/sbin/nologin deploy_zgy
|
||||
# -l locks the password account (禁用密码登录)
|
||||
# -u unlocks the password account (如需恢复密码登录)
|
||||
sudo passwd -l deploy_user
|
||||
```
|
||||
|
||||
## 2. The Permission Puzzle: ACL to the Rescue
|
||||
@@ -34,10 +47,10 @@ sudo apt install acl -y
|
||||
|
||||
# Grant access to sites directory (make sure this is your web root)
|
||||
# -R means recursive, but be careful if directory has other sites
|
||||
sudo setfacl -R -m u:deploy_zgy:rwx /opt/1panel/www/sites
|
||||
sudo setfacl -R -m u:deploy_user:rwx /opt/1panel/www/sites
|
||||
|
||||
# Set default inheritance: new sites get access automatically
|
||||
sudo setfacl -R -d -m u:deploy_zgy:rwx /opt/1panel/www/sites
|
||||
sudo setfacl -R -d -m u:deploy_user:rwx /opt/1panel/www/sites
|
||||
```
|
||||
|
||||
## 3. SSH Key Access: No More Password Typing
|
||||
@@ -46,31 +59,31 @@ Copy your local SSH public key to the server, and never type a password again.
|
||||
|
||||
```bash
|
||||
# Create SSH directory for deployment user
|
||||
sudo -u deploy_zgy mkdir -p /home/deploy_zgy/.ssh
|
||||
sudo -u deploy_user mkdir -p /home/deploy_user/.ssh
|
||||
|
||||
# Append your public key (using >> not > to avoid overwriting)
|
||||
echo "your-public-key-here" >> /home/deploy_zgy/.ssh/authorized_keys
|
||||
echo "your-ssh-public-key-content" >> /home/deploy_user/.ssh/authorized_keys
|
||||
|
||||
# Permissions must be exact, or SSH will refuse
|
||||
sudo chown -R deploy_zgy:deploy_zgy /home/deploy_zgy/.ssh
|
||||
sudo chmod 700 /home/deploy_zgy/.ssh
|
||||
sudo chmod 600 /home/deploy_zgy/.ssh/authorized_keys
|
||||
sudo chown -R deploy_user:deploy_user /home/deploy_user/.ssh
|
||||
sudo chmod 700 /home/deploy_user/.ssh
|
||||
sudo chmod 600 /home/deploy_user/.ssh/authorized_keys
|
||||
```
|
||||
|
||||
**Even lazier method**: If you have `ssh-copy-id` locally, just run `ssh-copy-id deploy_zgy@your-server-ip`.
|
||||
**Even lazier method**: If you have `ssh-copy-id` locally, just run `ssh-copy-id deploy_user@your-server-ip`.
|
||||
|
||||
## 4. SSH Aliases (For the Truly Lazy)
|
||||
|
||||
Memorize IP addresses? Not in this decade. Add this to your local `~/.ssh/config`:
|
||||
|
||||
```text
|
||||
Host ny-web # Nickname for your server
|
||||
HostName 192.xxx.xxx.xxx # Your server IP
|
||||
User deploy_zgy # Login user
|
||||
Host your-server-alias # Nickname for your server
|
||||
HostName your-server-ip-address # Your server IP
|
||||
User deploy_user # Login user
|
||||
IdentityFile ~/.ssh/id_rsa # Private key path
|
||||
```
|
||||
|
||||
Now just type `ssh ny-web` and you're in. Magic.
|
||||
Now just type `ssh your-server-alias` and you're in. Magic.
|
||||
|
||||
## 5. The Grand Finale: One-Command Deployment
|
||||
|
||||
@@ -90,7 +103,7 @@ echo "🚀 Syncing to production..."
|
||||
# -z: compress during transfer
|
||||
# --delete: ⚠️ Warning: removes files on target that don't exist locally!
|
||||
# Remove this flag for first sync to be safe
|
||||
rsync -avz --delete --progress ./dist/ ny-web:/opt/1panel/www/sites/my-project-folder/
|
||||
rsync -avz --delete --progress ./dist/ your-server-alias:/opt/1panel/www/sites/your-project-directory/
|
||||
|
||||
echo "✅ Deployment complete!"
|
||||
echo "⏱️ Next time just run: ./deploy.sh"
|
||||
Reference in New Issue
Block a user