Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/setup_linux_nginx.sh
T
2026-01-20 21:56:29 +01:00

110 lines
3.2 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="${1:-/var/www/atomcms/app}"
SERVER_NAME="${2:-_}"
PHP_VERSION="8.5"
echo "Preparing system packages"
sudo apt-get update -y
sudo apt-get install -y software-properties-common curl gnupg2 ca-certificates lsb-release
echo "Adding PHP repository"
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update -y
echo "Installing PHP ${PHP_VERSION} and extensions"
sudo apt-get install -y "php${PHP_VERSION}-fpm" "php${PHP_VERSION}-cli" "php${PHP_VERSION}-curl" "php${PHP_VERSION}-mbstring" "php${PHP_VERSION}-gd" "php${PHP_VERSION}-intl" "php${PHP_VERSION}-mysql" "php${PHP_VERSION}-bcmath" "php${PHP_VERSION}-zip"
echo "Installing Nginx"
sudo apt-get install -y nginx
echo "Installing Composer and Yarn"
sudo apt-get install -y composer nodejs npm
sudo npm install -g yarn
echo "Ensuring application directory exists: ${ROOT_DIR}"
if [ ! -d "${ROOT_DIR}" ]; then
echo "Directory ${ROOT_DIR} does not exist. Clone the repository into this path and rerun."
exit 1
fi
cd "${ROOT_DIR}"
if [ ! -f ".env" ]; then
cp .env.example .env || true
fi
echo "Installing PHP dependencies"
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "Generating app key"
php artisan key:generate || true
echo "Installing JS dependencies"
yarn install --silent
echo "Building assets (Atom theme)"
yarn run build:atom
echo "Setting permissions"
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
NGINX_CONF="/etc/nginx/sites-available/atomcms.conf"
echo "Writing Nginx server block to ${NGINX_CONF}"
sudo tee "${NGINX_CONF}" >/dev/null <<EOF
server {
listen 80;
server_name ${SERVER_NAME};
root ${ROOT_DIR}/public;
index index.php index.html;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php${PHP_VERSION}-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
EOF
echo "Enabling Nginx site"
sudo ln -sf "${NGINX_CONF}" /etc/nginx/sites-enabled/atomcms.conf
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl enable --now "php${PHP_VERSION}-fpm"
echo "Setup complete. If using a domain, update DNS. Visit http://${SERVER_NAME}/"
# SSL Setup Prompt
echo ""
read -p "Do you want to setup SSL with Let's Encrypt? (y/n) " SETUP_SSL
if [[ "$SETUP_SSL" =~ ^[Yy]$ ]]; then
if [ "${SERVER_NAME}" = "_" ] || [ "${SERVER_NAME}" = "localhost" ]; then
echo "Error: Cannot setup SSL for server name '${SERVER_NAME}'. Please provide a valid domain name when running the script."
echo "Usage: ./setup_linux_nginx.sh <root_dir> <domain>"
else
echo "Installing Certbot..."
sudo apt-get install -y certbot python3-certbot-nginx
echo "Running Certbot for ${SERVER_NAME}..."
# Running non-interactively where possible, but certbot often requires email/tos interaction on first run.
# We assume the user is running this interactively.
sudo certbot --nginx -d "${SERVER_NAME}"
echo "SSL setup complete!"
fi
fi