You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DevOps工程课程作业解析与指导请求

Alright, let's break down your DevOps coursework assignment thoroughly—covering both the foundational labs you need to practice and the core WordPress + LAMP automation project. I'll give you actionable guidance, code snippets, and tips to nail every requirement, plus the optional challenges.


Foundational Lab Consolidation Tips

First, let's make sure you solidify the basics with the 10 required experiments. Here's how to approach each one:

  • Hello World Basics: Start with a simple #!/bin/bash script that echoes "Hello World". Practice making it executable with chmod +x script.sh and running it—get comfortable with the terminal workflow.
  • Variables & Arithmetic: Define variables like my_name="DevOps Learner", test arithmetic with $((5 + 3)) or expr 10 - 4, and interpolate variables in strings (e.g., echo "Hi, $my_name!").
  • User Input Interaction: Use read -p "Enter your favorite OS: " os to capture input. Try the -s flag for hidden input (perfect for password practice later).
  • Command-Line Arguments: Access args with $1, $2, etc. Build a script that takes two numbers as args and prints their sum, or a greeting using $1 as the user's name.
  • Arrays: Define arrays like tools=("Git" "Docker" "Ansible"), loop through them with for tool in "${tools[@]}"; do echo "$tool is essential!"; done, and access individual elements with ${tools[0]}.
  • Conditional Statements: Build scripts with if-elif-else blocks. Check if a file exists (if [ -f myfile.txt ]; then echo "File exists!"; fi), compare numbers, or validate string inputs.
  • Loops & Menus: Create an infinite while true loop with a case statement to build a simple menu (e.g., 1. Show Disk Usage, 2. Generate Password, 3. Exit).
  • Redirection & Debugging: Redirect output to logs with echo "Installing Apache..." >> setup.log, use set -x to debug line-by-line execution, and set -e to exit on errors.
  • Interactive System Tool: Combine all the above into one script with a menu that includes:
    • A personalized greeting (uses user input)
    • System info (runs uname -a, df -h, uptime and displays results)
    • Backup function (tars a directory with a timestamp like backup_$(date +%Y%m%d).tar.gz)
    • Password generator (uses openssl rand -base64 12 to create secure passwords)

Core Project: WordPress + LAMP Automation Script

Let's tackle the main assignment with modular, commented code and step-by-step explanations.

1. Capture & Validate User Input

First, build a function to get required details with validation to ensure no empty inputs:

#!/bin/bash

# Capture user input with validation
get_user_input() {
  # Prompt for domain
  read -p "Enter your domain name (e.g., myblog.com): " DOMAIN
  while [ -z "$DOMAIN" ]; do
    echo "Error: Domain can't be empty!"
    read -p "Enter your domain name: " DOMAIN
  done

  # Prompt for database name
  read -p "Enter database name: " DB_NAME
  while [ -z "$DB_NAME" ]; do
    echo "Error: Database name can't be empty!"
    read -p "Enter database name: " DB_NAME
  done

  # Prompt for database username
  read -p "Enter database username: " DB_USER
  while [ -z "$DB_USER" ]; do
    echo "Error: Database username can't be empty!"
    read -p "Enter database username: " DB_USER
  done

  # Prompt for database password (hidden input)
  read -s -p "Enter database password: " DB_PASS
  echo
  while [ -z "$DB_PASS" ]; do
    echo "Error: Database password can't be empty!"
    read -s -p "Enter database password: " DB_PASS
    echo
  done
}

2. Install LAMP Stack with Firewall Rules

Create a function to update packages, install Apache, MariaDB, PHP, and secure the firewall:

install_lamp_stack() {
  echo "=== Updating system packages ==="
  apt update && apt upgrade -y

  echo "=== Installing Apache ==="
  apt install apache2 -y

  echo "=== Configuring UFW Firewall ==="
  ufw allow 'Apache Full'
  # Auto-confirm UFW enable
  ufw enable <<EOF
y
EOF

  echo "=== Installing MariaDB ==="
  apt install mariadb-server mariadb-client -y

  # Secure MariaDB (auto-respond to prompts)
  mysql_secure_installation <<EOF
y
$DB_PASS
$DB_PASS
y
y
y
y
EOF

  echo "=== Installing PHP & Required Extensions ==="
  apt install php libapache2-mod-php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

  # Restart Apache to apply PHP changes
  systemctl restart apache2
}

3. Configure Virtual Host & Self-Signed SSL

Build a function to set up the virtual host and generate a self-signed SSL certificate:

configure_virtual_host() {
  echo "=== Creating Virtual Host for $DOMAIN ==="
  # Create HTTP virtual host config
  cat > /etc/apache2/sites-available/$DOMAIN.conf <<EOF
<VirtualHost *:80>
    ServerAdmin webmaster@$DOMAIN
    ServerName $DOMAIN
    ServerAlias www.$DOMAIN
    DocumentRoot /var/www/$DOMAIN

    ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-error.log
    CustomLog \${APACHE_LOG_DIR}/$DOMAIN-access.log combined

    <Directory /var/www/$DOMAIN>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF

  # Enable the site and rewrite module
  a2ensite $DOMAIN.conf
  a2enmod rewrite

  echo "=== Generating Self-Signed SSL Certificate ==="
  openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/$DOMAIN.key -out /etc/ssl/certs/$DOMAIN.crt <<EOF
US
California
San Francisco
MyDevOpsClass
CourseProject
$DOMAIN
admin@$DOMAIN
EOF

  # Create SSL virtual host config
  cat > /etc/apache2/sites-available/$DOMAIN-ssl.conf <<EOF
<VirtualHost *:443>
    ServerAdmin webmaster@$DOMAIN
    ServerName $DOMAIN
    ServerAlias www.$DOMAIN
    DocumentRoot /var/www/$DOMAIN

    ErrorLog \${APACHE_LOG_DIR}/$DOMAIN-ssl-error.log
    CustomLog \${APACHE_LOG_DIR}/$DOMAIN-ssl-access.log combined

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/$DOMAIN.crt
    SSLCertificateKeyFile /etc/ssl/private/$DOMAIN.key

    <Directory /var/www/$DOMAIN>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>
EOF

  # Enable SSL site and modules
  a2ensite $DOMAIN-ssl.conf
  a2enmod ssl
  systemctl restart apache2
}

4. Set Up WordPress Database

Create a function to build the database, user, and grant permissions:

setup_database() {
  echo "=== Creating Database & User ==="
  mysql -u root -p$DB_PASS <<EOF
CREATE DATABASE $DB_NAME;
CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASS';
GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';
FLUSH PRIVILEGES;
EOF
}

5. Download & Configure WordPress

Build a function to fetch WordPress, set up wp-config.php, and fix permissions:

setup_wordpress() {
  echo "=== Downloading WordPress ==="
  cd /var/www
  wget https://wordpress.org/latest.tar.gz
  tar -xzf latest.tar.gz
  mv wordpress $DOMAIN
  rm latest.tar.gz

  echo "=== Configuring wp-config.php ==="
  cp /var/www/$DOMAIN/wp-config-sample.php /var/www/$DOMAIN/wp-config.php
  # Replace placeholder values
  sed -i "s/database_name_here/$DB_NAME/" /var/www/$DOMAIN/wp-config.php
  sed -i "s/username_here/$DB_USER/" /var/www/$DOMAIN/wp-config.php
  sed -i "s/password_here/$DB_PASS/" /var/www/$DOMAIN/wp-config.php

  # Add unique WordPress keys & salts (auto-fetched from WordPress API)
  wget -qO- https://api.wordpress.org/secret-key/1.1/salt/ >> /var/www/$DOMAIN/wp-config.php

  echo "=== Setting File Permissions ==="
  chown -R www-data:www-data /var/www/$DOMAIN
  chmod -R 755 /var/www/$DOMAIN
}

6. Modular Main Execution

Wrap everything in a main function to run steps in order, with root user validation:

main() {
  # Check if script is run as root
  if [ "$(id -u)" -ne 0 ]; then
    echo "Error: This script must be run with sudo/root privileges!"
    exit 1
  fi

  # Run all functions in sequence
  get_user_input
  install_lamp_stack
  configure_virtual_host
  setup_database
  setup_wordpress

  echo "=== Installation Complete! ==="
  echo "Visit https://$DOMAIN in your browser to finish setting up WordPress."
}

# Execute the main function
main

7. Test on Fresh Ubuntu VM

  • Spin up a fresh Ubuntu 22.04/20.04 VM (use VirtualBox, VMware, or a cloud instance)
  • Copy the script to the VM (use scp or paste it into a file with nano wp-lamp-automation.sh)
  • Make it executable: chmod +x wp-lamp-automation.sh
  • Run it with sudo: sudo ./wp-lamp-automation.sh
  • Capture screenshots of:
    • The script running (input prompts, installation steps)
    • The final WordPress setup page (after navigating to your domain)

Optional Challenge Tasks

Here's how to tackle the extra requirements:

  • Add set -euo pipefail: At the very top of the script (right after #!/bin/bash), add set -euo pipefail—this makes the script exit on errors, treat unset variables as errors, and propagate pipe failures.
  • Log File Recording: Add this at the start of the main function to log all output to a file:
    LOG_FILE="/var/log/wp-lamp-install.log"
    # Redirect stdout/stderr to log file and terminal
    exec > >(tee -a $LOG_FILE) 2>&1
    
  • Basic Error Checking: Add checks after critical commands, e.g.:
    # After installing Apache
    if ! systemctl is-active --quiet apache2; then
      echo "Error: Apache failed to start!"
      exit 1
    fi
    

Post-Assignment Next Steps
  • Jot down any issues you encountered (e.g., permission errors, SSL warnings, database connection failures) to discuss in your next class.
  • Test edge cases: What happens if the user enters an invalid domain? What if the database user already exists? Add error handling to address these scenarios.

内容的提问来源于stack exchange,提问作者Cristian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 12:27:47