BASH
Set Up New Web Project Directory with Correct Permissions
Automate the creation of a new web project directory, ensuring proper ownership and read/write permissions for web server access and secure development workflows.
#!/bin/bash
PROJECT_NAME="$1"
WEB_USER="www-data" # Common web server user (e.g., apache, nginx)
WEB_GROUP="www-data" # Common web server group
if [ -z "$PROJECT_NAME" ]; then
echo "Usage: $0 <project_name>" >&2
exit 1
fi
PROJECT_PATH="/var/www/$PROJECT_NAME"
if [ -d "$PROJECT_PATH" ]; then
echo "Error: Directory '$PROJECT_PATH' already exists." >&2
exit 1
fi
mkdir -p "$PROJECT_PATH"
# Set ownership to web server user/group
chown -R "$WEB_USER":"$WEB_GROUP" "$PROJECT_PATH"
# Set directory permissions: owner rwx, group rwx, others rx
find "$PROJECT_PATH" -type d -exec chmod 775 {} +
# Set file permissions: owner rw, group rw, others r
find "$PROJECT_PATH" -type f -exec chmod 664 {} +
echo "Project directory '$PROJECT_PATH' created with appropriate permissions for web server."
How it works: This script creates a new directory for a web project, setting its ownership to the web server user and group (e.g., `www-data` on Debian/Ubuntu). It then applies standard permissions: 775 for directories (read, write, execute for owner/group; read, execute for others) and 664 for files (read, write for owner/group; read for others). This ensures the web server can read and write necessary files, while maintaining a secure environment and allowing other developers in the web group to collaborate effectively.