Essential Ubuntu/Linux Commands for Static Blog Deployment
September 8, 2022
:150 :0
Ubuntu, Linux, and macOS provide exceptional development environments, though their desktop ecosystems differ from Windows. When working with cloud servers (like Tencent Cloud's lightweight instances), you'll primarily interact through a terminal interface. This guide covers essential commands for deploying static websites using Node.js and Nginx.
Note: All commands assume you have
sudo
privileges. Replace/path/to/www
with your actual directory path.
Package Management Fundamentals
Ubuntu/Debian (apt)
# Update package lists
sudo apt update && sudo apt upgrade -y
# Install Node.js (using NodeSource repository for latest versions)
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
# Install Nginx
sudo apt install nginx -y
macOS (Homebrew)
# Install Homebrew (if missing)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Node.js
brew install node@18
# Install Nginx
brew install nginx
Verify Installations
node -v # v18.x.x
npm -v # 9.x.x
nginx -v # nginx/1.x.x
Nginx Configuration
Basic Operations
# Start/Stop/Restart Nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
# Enable auto-start on boot
sudo systemctl enable nginx
# Check service status
sudo systemctl status nginx
Configure Virtual Host
- Create new config file:
sudo nano /etc/nginx/sites-available/myblog
- Add server block configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/myblog;
index index.html;
location / {
try_files $uri $uri/ =404;
}
# Enable compression
gzip on;
gzip_types text/plain text/css application/json application/javascript;
}
- Enable configuration:
sudo ln -s /etc/nginx/sites-available/myblog /etc/nginx/sites-enabled/
sudo nginx -t # Test configuration
sudo systemctl reload nginx
Permission Management
Understanding Linux Permissions
ls -l /var/www
# Output: drwxr-xr-x 2 root root 4096 Jan 1 12:34 myblog
- First character:
d
= directory,-
= file - Next 9 characters: Three triplets representing owner/group/others permissions
- Permissions:
r
= read,w
= write,x
= execute
Securing Web Directories
# Set owner to www-data (Nginx user)
sudo chown -R www-data:www-data /var/www/myblog
# Apply secure permissions (owner: rwx, group: rx, others: rx)
sudo chmod -R 755 /var/www/myblog
# Alternative (less secure) for write access:
sudo chmod -R 775 /var/www/myblog
Permission Troubleshooting
# Check Nginx error logs
sudo tail -f /var/log/nginx/error.log
# Common errors:
# 403 Forbidden: Incorrect permissions or missing index file
# 404 Not Found: Incorrect root directory path
Essential Linux Commands
Command | Description | Example |
---|---|---|
cp | Copy files/directories | cp -R source_dir/ dest_dir/ |
rm | Remove files/directories | rm -r directory/ |
mv | Move/rename files | mv old.txt new.txt |
grep | Search text patterns | grep "error" nginx.log |
find | Locate files | find /var/www -name "*.html" |
Deployment Workflow Example
- Transfer files to server:
# From local machine
scp -r ./build user@server_ip:/var/www/myblog
- Set permissions:
ssh user@server_ip
sudo chown -R www-data:www-data /var/www/myblog
sudo chmod -R 755 /var/www/myblog
- Reload Nginx:
sudo systemctl reload nginx
Security Best Practices
- Regular Updates:
sudo apt update && sudo apt upgrade -y
- Firewall Configuration:
sudo ufw allow 'Nginx Full' sudo ufw enable
- SSH Hardening:
- Disable root login
- Use SSH keys instead of passwords
- Automated Backups:
# Daily backup script 0 3 * * * tar -czf /backups/myblog-$(date +\%F).tar.gz /var/www/myblog
Pro Tip: Use
certbot
for free SSL certificates:
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
For extended learning, explore Nginx Official Documentation and Linux File Permissions Guide.