Drupal install

AI suggested full automated install of Drupal.

I'd appreciate any comments.
Code:
#!/usr/bin/env sh

#!/bin/sh
# ============================================================
# FreeBSD Automatic Drupal Installer
# Apache + PostgreSQL + PHP + Composer
# ============================================================

set -e

# ------------------ CONFIGURATION (EDIT THESE) ------------------
DOMAIN="example.com"                          # Your domain or server IP
DOCROOT="/usr/local/www/${DOMAIN}"            # Document root
SITE_NAME="My Drupal Site"
ADMIN_USER="admin"
ADMIN_PASS="ChangeMeStrongPassword123!"       # Change this!
ADMIN_EMAIL="admin@${DOMAIN}"

DB_NAME="drupal"
DB_USER="drupaluser"
DB_PASS="AnotherStrongDBPassword456!"         # Change this!
DB_HOST="localhost"

PHP_VERSION="83"                              # 83 recommended (or 84)
DRUPAL_VERSION="10"                           # 10 or 11
# ---------------------------------------------------------------

echo "==> Updating system packages..."
pkg update -f
pkg upgrade -y

echo "==> Installing required packages..."
pkg install -y \
  apache24 \
  postgresql16-server postgresql16-client \
  php${PHP_VERSION} \
  php${PHP_VERSION}-pgsql \
  php${PHP_VERSION}-gd \
  php${PHP_VERSION}-curl \
  php${PHP_VERSION}-zip \
  php${PHP_VERSION}-mbstring \
  php${PHP_VERSION}-xml \
  php${PHP_VERSION}-tokenizer \
  php${PHP_VERSION}-zlib \
  php${PHP_VERSION}-session \
  php${PHP_VERSION}-filter \
  php${PHP_VERSION}-fileinfo \
  php${PHP_VERSION}-dom \
  php${PHP_VERSION}-simplexml \
  php${PHP_VERSION}-xmlreader \
  php${PHP_VERSION}-xmlwriter \
  php${PHP_VERSION}-opcache \
  php${PHP_VERSION}-intl \
  php${PHP_VERSION}-bcmath \
  mod_php${PHP_VERSION} \
  composer \
  curl \
  git

echo "==> Enabling services..."
sysrc apache24_enable="YES"
sysrc postgresql_enable="YES"

echo "==> Initializing and starting PostgreSQL..."
if [ ! -d /var/db/postgres/data16 ]; then
  service postgresql initdb
fi
service postgresql start
sleep 3

echo "==> Creating PostgreSQL database and user..."
su - postgres -c "psql -c \"CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASS}';\"" || true
su - postgres -c "psql -c \"CREATE DATABASE ${DB_NAME} OWNER ${DB_USER};\"" || true
su - postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};\"" || true

# Grant schema privileges (important for Drupal)
su - postgres -c "psql -d ${DB_NAME} -c \"GRANT ALL ON SCHEMA public TO ${DB_USER};\""
su - postgres -c "psql -d ${DB_NAME} -c \"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ${DB_USER};\""

echo "==> Creating document root..."
mkdir -p "${DOCROOT}"
cd /usr/local/www

echo "==> Downloading Drupal ${DRUPAL_VERSION} with Composer..."
if [ ! -d "${DOCROOT}/web" ]; then
  composer create-project drupal/recommended-project:${DRUPAL_VERSION} "${DOMAIN}" --no-interaction
fi

cd "${DOCROOT}"

echo "==> Installing Drupal dependencies..."
composer install --no-interaction

echo "==> Installing Drush (Drupal CLI)..."
composer require drush/drush --no-interaction

echo "==> Setting correct permissions..."
chown -R www:www "${DOCROOT}"
find "${DOCROOT}" -type d -exec chmod 755 {} \;
find "${DOCROOT}" -type f -exec chmod 644 {} \;

# Make files directory writable
mkdir -p web/sites/default/files
chmod 775 web/sites/default/files
chown -R www:www web/sites/default/files

echo "==> Configuring Apache..."

# Enable required Apache modules
sed -i '' 's/#LoadModule rewrite_module/LoadModule rewrite_module/' /usr/local/etc/apache24/httpd.conf
sed -i '' 's/#LoadModule proxy_module/LoadModule proxy_module/' /usr/local/etc/apache24/httpd.conf
sed -i '' 's/#LoadModule proxy_fcgi_module/LoadModule proxy_fcgi_module/' /usr/local/etc/apache24/httpd.conf

# Create VirtualHost
cat > /usr/local/etc/apache24/Includes/${DOMAIN}.conf <<EOF
<VirtualHost *:80>
    ServerName ${DOMAIN}
    ServerAlias www.${DOMAIN}
    DocumentRoot "${DOCROOT}/web"

    <Directory "${DOCROOT}/web">
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

    ErrorLog "/var/log/${DOMAIN}-error.log"
    CustomLog "/var/log/${DOMAIN}-access.log" combined
</VirtualHost>
EOF

# Ensure DirectoryIndex includes index.php
if ! grep -q "DirectoryIndex index.php" /usr/local/etc/apache24/httpd.conf; then
  sed -i '' 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' /usr/local/etc/apache24/httpd.conf
fi

echo "==> Testing Apache configuration..."
apachectl configtest

echo "==> Restarting services..."
service apache24 restart
service postgresql restart

echo "==> Installing Drupal via Drush (fully automatic)..."
cd "${DOCROOT}"

# Create settings.php if it doesn't exist
if [ ! -f web/sites/default/settings.php ]; then
  cp web/sites/default/default.settings.php web/sites/default/settings.php
  chown www:www web/sites/default/settings.php
  chmod 666 web/sites/default/settings.php
fi

# Run site install with Drush
./vendor/bin/drush site:install standard \
  --db-url="pgsql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}" \
  --site-name="${SITE_NAME}" \
  --account-name="${ADMIN_USER}" \
  --account-pass="${ADMIN_PASS}" \
  --account-mail="${ADMIN_EMAIL}" \
  --yes

# Fix permissions after install
chmod 644 web/sites/default/settings.php
chown -R www:www "${DOCROOT}"

echo ""
echo "============================================================"
echo " Drupal installed successfully!"
echo "============================================================"
echo " URL:            http://${DOMAIN}"
echo " Admin user:     ${ADMIN_USER}"
echo " Admin password: ${ADMIN_PASS}"
echo " Document root:  ${DOCROOT}/web"
echo " Database:       PostgreSQL → ${DB_NAME} / ${DB_USER}"
echo ""
echo " Useful commands:"
echo "   cd ${DOCROOT}"
echo "   ./vendor/bin/drush status"
echo "   ./vendor/bin/drush cr          # clear cache"
echo "============================================================"
 
Code:
echo "==> Setting correct permissions..."
chown -R www:www "${DOCROOT}"
find "${DOCROOT}" -type d -exec chmod 755 {} \;
find "${DOCROOT}" -type f -exec chmod 644 {} \;
 
Back
Top