Initial commit with script to generate a new PHPStarter-based website

This commit is contained in:
Ramon Caballero 2026-04-11 15:01:16 +01:00
commit ca51f944ea
3 changed files with 307 additions and 0 deletions

64
README.md Normal file
View File

@ -0,0 +1,64 @@
# PHPStarter CLI
A lightweight commandline tool for generating new PHP websites based on the [PHPStarter](https://gitea.ramoncaballero.dev/mon/PHPStarter.git) template.
You get a fully working PHP project in seconds.
## Requirements
- Linux environment
- Apache2
- Git
- Bash
- PHP
- Composer
- MariaDB
## Installation
Clone this repository:
```bash
git clone https://gitea.ramoncaballero.dev/mon/phpstarter-cli.git
cd phpstarter-cli
```
Make the script executable:
```bash
chmod +x phpstarter.sh
```
<!-- (Optional) Install globally: -->
<!-- -->
<!-- ```bash -->
<!-- sudo cp phpstarter.sh /usr/local/bin/phpstarter -->
<!-- ``` -->
## Usage
Create a new PHP site:
```bash
sudo ./phpstarter.sh <mysite>
```
This will clone the [PHPStarter](https://gitea.ramoncaballero.dev/mon/PHPStarter.git) template, configure everything automatically, and set up a readytouse local development environment for your new site.
Your new site will be available at:
```bash
http://mysite.local
```
## Remove a Site
```bash
sudo rm -rf /var/www/sites/mysite
sudo a2dissite mysite.conf
sudo rm /etc/apache2/sites-available/mysite.conf
sudo systemctl reload apache2
sudo sed -i '/mysite\.local/d' /etc/hosts
```
You may also remove the database it manually if you no longer need it.

230
phpstarter.sh Normal file
View File

@ -0,0 +1,230 @@
#!/usr/bin/env bash
set -euo pipefail
# -------------------------------------
# CONFIGURATION
# -------------------------------------
TEMPLATE_REPO="https://gitea.ramoncaballero.dev/mon/PHPStarter.git"
SITES_DIR="/var/www/sites"
APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
APACHE_SERVICE="apache2"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VHOST_TEMPLATE="$SCRIPT_DIR/vhost-template.conf"
# -------------------------------------
# FUNCTIONS
# -------------------------------------
error() {
echo "ERROR: $*" >&2
exit 1
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || error "Required command '$1' not found"
}
# -------------------------------------
# PRECHECKS
# -------------------------------------
require_cmd git
require_cmd sed
require_cmd mariadb
require_cmd openssl
require_cmd a2ensite
require_cmd systemctl
require_cmd logname
require_cmd composer
[ -f "$VHOST_TEMPLATE" ] || error "Virtual host template not found at $VHOST_TEMPLATE"
[ -d "$SITES_DIR" ] || error "Sites directory not found at $SITES_DIR"
# -------------------------------------
# INPUT
# -------------------------------------
if [ $# -lt 1 ]; then
echo "Usage: sudo $0 <website-name>"
exit 1
fi
WEBSITE_NAME="$1"
WEBSITE_DIR="$SITES_DIR/$WEBSITE_NAME"
VHOST_CONF="$APACHE_SITES_AVAILABLE/$WEBSITE_NAME.conf"
if [ -d "$WEBSITE_DIR" ]; then
error "Directory $WEBSITE_DIR already exists"
fi
if [ -f "$VHOST_CONF" ]; then
error "VHost $VHOST_CONF already exists"
fi
REAL_USER="$(logname)"
# -------------------------------------
# CLONE TEMPLATE
# -------------------------------------
echo "Creating website directory at $WEBSITE_DIR..."
mkdir -p "$WEBSITE_DIR"
echo "Cloning PHPStarter template..."
git clone "$TEMPLATE_REPO" "$WEBSITE_DIR"
echo "Removing template Git history..."
rm -rf "$WEBSITE_DIR/.git"
# -------------------------------------
# COPY ENV EXAMPLE BEFORE PLACEHOLDER REPLACEMENT
# -------------------------------------
EXAMPLE_ENV_PHPSTARTER="$WEBSITE_DIR/config/PHPStarter.env.example"
EXAMPLE_ENV_WEBSITE="$WEBSITE_DIR/config/${WEBSITE_NAME}.env.example"
if [ -f "$EXAMPLE_ENV_WEBSITE" ]; then
EXAMPLE_ENV="$EXAMPLE_ENV_WEBSITE"
elif [ -f "$EXAMPLE_ENV_PHPSTARTER" ]; then
EXAMPLE_ENV="$EXAMPLE_ENV_PHPSTARTER"
else
EXAMPLE_ENV=""
fi
ENV_FILE="$WEBSITE_DIR/config/${WEBSITE_NAME}.env"
if [ -n "$EXAMPLE_ENV" ]; then
echo "Creating env file from example: $EXAMPLE_ENV -> $ENV_FILE"
cp "$EXAMPLE_ENV" "$ENV_FILE"
else
echo "No env example found, skipping env copy."
fi
# -------------------------------------
# REPLACE PLACEHOLDERS (CONTENT ONLY)
# -------------------------------------
echo "Replacing PHPStarter placeholders with $WEBSITE_NAME..."
grep -rIl "PHPStarter" "$WEBSITE_DIR" \
| grep -v "\.example$" \
| while read -r file; do
sed -i "s/PHPStarter/$WEBSITE_NAME/g" "$file"
done
# -------------------------------------
# RENAME CSS/JS FILES
# -------------------------------------
CSS_OLD="$WEBSITE_DIR/public/css/PHPStarter.css"
CSS_NEW="$WEBSITE_DIR/public/css/${WEBSITE_NAME}.css"
JS_OLD="$WEBSITE_DIR/public/js/PHPStarter.js"
JS_NEW="$WEBSITE_DIR/public/js/${WEBSITE_NAME}.js"
[ -f "$CSS_OLD" ] && mv "$CSS_OLD" "$CSS_NEW"
[ -f "$JS_OLD" ] && mv "$JS_OLD" "$JS_NEW"
# -------------------------------------
# PERMISSIONS BEFORE COMPOSER
# -------------------------------------
echo "Setting permissions so Composer can write vendor/..."
chown -R "$REAL_USER:www-data" "$WEBSITE_DIR"
find "$WEBSITE_DIR" -type d -exec chmod 775 {} \;
find "$WEBSITE_DIR" -type f -exec chmod 664 {} \;
# -------------------------------------
# COMPOSER INSTALL (RUN AS REAL USER)
# -------------------------------------
echo "Running composer install as $REAL_USER..."
sudo -u "$REAL_USER" composer install --no-dev --optimize-autoloader --working-dir="$WEBSITE_DIR"
# -------------------------------------
# DATABASE SETUP
# -------------------------------------
DB_USER="${WEBSITE_NAME}_user"
DB_NAME="$WEBSITE_NAME"
DB_PASS="$(openssl rand -base64 16)"
DB_SQL="$WEBSITE_DIR/config/database.sql"
if [ ! -f "$DB_SQL" ]; then
echo "No database.sql found, skipping DB creation."
else
echo "Injecting generated DB password into SQL file..."
sed -i "s/IDENTIFIED BY ''/IDENTIFIED BY '$DB_PASS'/" "$DB_SQL"
echo "Enter MariaDB root password to create database..."
read -rsp "MariaDB root password: " MYSQL_ROOT_PASSWORD
echo
echo "Running SQL file..."
if ! mariadb -u root -p"$MYSQL_ROOT_PASSWORD" < "$DB_SQL"; then
error "Failed to apply database.sql"
fi
fi
# -------------------------------------
# UPDATE ENV FILE
# -------------------------------------
if [ -f "$ENV_FILE" ]; then
echo "Updating environment file..."
sed -i "s/^DB_USER=.*/DB_USER=$DB_USER/" "$ENV_FILE" || true
sed -i "s/^DB_PASS=.*/DB_PASS=$DB_PASS/" "$ENV_FILE" || true
sed -i "s/^DB_NAME=.*/DB_NAME=$DB_NAME/" "$ENV_FILE" || true
fi
# -------------------------------------
# VIRTUAL HOST
# -------------------------------------
echo "Creating virtual host..."
cp "$VHOST_TEMPLATE" "$VHOST_CONF"
sed -i "s/PHPStarter/$WEBSITE_NAME/g" "$VHOST_CONF"
echo "Enabling site..."
a2ensite "$WEBSITE_NAME.conf"
echo "Reloading Apache..."
systemctl reload "$APACHE_SERVICE"
# -------------------------------------
# FINAL PERMISSIONS
# -------------------------------------
echo "Re-applying ownership and permissions..."
chown -R "$REAL_USER:www-data" "$WEBSITE_DIR"
find "$WEBSITE_DIR" -type d -exec chmod 775 {} \;
find "$WEBSITE_DIR" -type f -exec chmod 664 {} \;
# -------------------------------------
# HOSTS FILE ENTRY
# -------------------------------------
HOST_ENTRY="127.0.0.1 ${WEBSITE_NAME}.local"
echo "Adding hosts entry for ${WEBSITE_NAME}.local..."
if ! grep -q "${WEBSITE_NAME}.local" /etc/hosts; then
echo "$HOST_ENTRY" | sudo tee -a /etc/hosts >/dev/null
echo "Added hosts entry: $HOST_ENTRY"
else
echo "Hosts entry already exists, skipping."
fi
# -------------------------------------
# DONE
# -------------------------------------
echo
echo "Website created successfully!"
echo "Path: $WEBSITE_DIR"
echo "URL: http://${WEBSITE_NAME}.local"
echo "DB user: $DB_USER"
echo "DB name: $DB_NAME"
echo "DB pass: $DB_PASS"
echo

13
vhost-template.conf Normal file
View File

@ -0,0 +1,13 @@
<VirtualHost *:80>
ServerName PHPStarter.local
DocumentRoot /var/www/sites/PHPStarter/public
<Directory /var/www/sites/PHPStarter/public>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/PHPStarter-error.log
CustomLog ${APACHE_LOG_DIR}/PHPStarter-access.log combined
</VirtualHost>