================================================================================
          COMPLETE PRODUCTION UBUNTU SERVER SETUP COMMANDS (AWS / VPS)
   Dedicated VirtualHost for Domain: signalwire.apidevx.com
   LAMP Stack + PHP 8.3 + Redis + Supervisor (Horizon) + Let's Encrypt SSL
================================================================================

This guide contains all exact commands to configure a fresh Ubuntu 22.04 / 24.04
production server using Apache VirtualHost for https://signalwire.apidevx.com

--------------------------------------------------------------------------------
STEP 1: SYSTEM UPDATE & 2GB SWAP SETUP (CRITICAL FOR STABILITY)
--------------------------------------------------------------------------------
# 1.1 Update and upgrade system packages
sudo apt update && sudo apt upgrade -y

# 1.2 Install essential tools
sudo apt install -y curl wget git unzip zip htop software-properties-common nano

# 1.3 Create a 2GB Swap file (Prevents MySQL OOM crashes on 1GB/2GB RAM servers)
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo sysctl vm.swappiness=10
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf


--------------------------------------------------------------------------------
STEP 2: INSTALL APACHE WEB SERVER & ENABLE MODULES
--------------------------------------------------------------------------------
# 2.1 Install Apache
sudo apt install -y apache2

# 2.2 Enable essential Apache modules
sudo a2enmod rewrite ssl headers

# 2.3 Start and enable Apache
sudo systemctl enable --now apache2


--------------------------------------------------------------------------------
STEP 3: INSTALL PHP 8.3 & REQUIRED EXTENSIONS
--------------------------------------------------------------------------------
# 3.1 Add Ondrej PHP PPA (Ensures latest PHP 8.3 modules)
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

# 3.2 Install PHP 8.3 with all required modules
sudo apt install -y php8.3 libapache2-mod-php8.3 php8.3-cli php8.3-mysql php8.3-redis \
php8.3-curl php8.3-zip php8.3-mbstring php8.3-xml php8.3-gd php8.3-intl php8.3-bcmath

# 3.3 Verify PHP installation
php -v
php -m | grep -iE 'redis|pdo_mysql|curl|mbstring'


--------------------------------------------------------------------------------
STEP 4: INSTALL & OPTIMIZE MYSQL 8
--------------------------------------------------------------------------------
# 4.1 Install MySQL Server
sudo apt install -y mysql-server

# 4.2 Configure MySQL low-memory optimization (prevents crashes on low RAM)
sudo bash -c 'cat << "EOF" > /etc/mysql/conf.d/low-mem.cnf
[mysqld]
performance_schema = OFF
innodb_buffer_pool_size = 128M
innodb_log_buffer_size = 8M
max_connections = 50
EOF'

# 4.3 Start and enable MySQL
sudo systemctl enable --now mysql

# 4.4 Configure MySQL Root, Admin User, and Production Database
# (Change @admin!123@ to your desired strong production password)
sudo mysql << 'EOF'
-- Set root password
ALTER USER 'root'@'localhost' IDENTIFIED BY '@admin!123@';

-- Create dedicated admin user for localhost and 127.0.0.1
CREATE USER IF NOT EXISTS 'admin'@'localhost' IDENTIFIED BY '@admin!123@';
ALTER USER 'admin'@'localhost' IDENTIFIED BY '@admin!123@';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;

CREATE USER IF NOT EXISTS 'admin'@'127.0.0.1' IDENTIFIED BY '@admin!123@';
ALTER USER 'admin'@'127.0.0.1' IDENTIFIED BY '@admin!123@';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' WITH GRANT OPTION;

-- Create production database
CREATE DATABASE IF NOT EXISTS signalwire;

FLUSH PRIVILEGES;
EOF


--------------------------------------------------------------------------------
STEP 5: INSTALL & CONFIGURE REDIS SERVER
--------------------------------------------------------------------------------
# 5.1 Install Redis
sudo apt install -y redis-server php-redis

# 5.2 Start and enable Redis
sudo systemctl enable --now redis-server

# 5.3 Test Redis
redis-cli ping
# Expected output: PONG


--------------------------------------------------------------------------------
STEP 6: CREATE APACHE VIRTUAL HOST FOR signalwire.apidevx.com
--------------------------------------------------------------------------------
# 6.1 Create project directory
sudo mkdir -p /var/www/signalwire

# 6.2 Create Virtual Host configuration file:
sudo bash -c 'cat << "EOF" > /etc/apache2/sites-available/signalwire.apidevx.com.conf
<VirtualHost *:80>
    ServerName signalwire.apidevx.com
    ServerAdmin admin@apidevx.com
    DocumentRoot /var/www/signalwire/public

    <Directory /var/www/signalwire/public>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/signalwire_error.log
    CustomLog ${APACHE_LOG_DIR}/signalwire_access.log combined
</VirtualHost>
EOF'

# 6.3 Enable the Virtual Host site in Apache
sudo a2ensite signalwire.apidevx.com.conf

# 6.4 Reload Apache
sudo systemctl reload apache2


--------------------------------------------------------------------------------
STEP 7: UPLOAD CODE & CONFIGURE LARAVEL PROJECT
--------------------------------------------------------------------------------
# 7.1 Upload your project code into: /var/www/signalwire
# If uploading a zip file (e.g. signalwire-app.zip):
# cd /var/www/signalwire && unzip -o signalwire-app.zip

# 7.2 Set correct ownership and permissions:
sudo chown -R ubuntu:www-data /var/www/signalwire
sudo chmod -R 775 /var/www/signalwire
sudo chmod -R 775 /var/www/signalwire/storage /var/www/signalwire/bootstrap/cache
sudo usermod -aG www-data ubuntu

# 7.3 Navigate to project folder:
cd /var/www/signalwire

# 7.4 Configure /var/www/signalwire/.env file:
# APP_NAME="SignalWire GHL"
# APP_ENV=production
# APP_KEY= (generated in next step)
# APP_DEBUG=false
# APP_URL=https://signalwire.apidevx.com
#
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=signalwire
# DB_USERNAME=admin
# DB_PASSWORD=@admin!123@
#
# QUEUE_CONNECTION=redis
# CACHE_STORE=redis
# REDIS_CLIENT=phpredis
# REDIS_HOST=127.0.0.1
# REDIS_PORT=6379

# 7.5 Run Laravel setup & migrations:
php artisan key:generate
php artisan migrate --force
php artisan optimize:clear


--------------------------------------------------------------------------------
STEP 8: INSTALL SUPERVISOR & RUN LARAVEL HORIZON (QUEUES)
--------------------------------------------------------------------------------
# 8.1 Install Supervisor
sudo apt install -y supervisor

# 8.2 Create Horizon Supervisor configuration:
sudo bash -c 'cat << "EOF" > /etc/supervisor/conf.d/horizon.conf
[program:horizon]
process_name=%(program_name)s
command=php /var/www/signalwire/artisan horizon
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/var/www/signalwire/storage/logs/horizon.log
stopwaitsecs=3600
EOF'

# 8.3 Create and configure log file permissions:
sudo touch /var/www/signalwire/storage/logs/horizon.log
sudo chown www-data:www-data /var/www/signalwire/storage/logs/horizon.log
sudo chmod 664 /var/www/signalwire/storage/logs/horizon.log

# 8.4 Start Supervisor and Horizon worker:
sudo systemctl enable --now supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
# You will see: horizon   RUNNING


--------------------------------------------------------------------------------
STEP 9: DNS & LET'S ENCRYPT SSL CERTIFICATE
--------------------------------------------------------------------------------
# 9.1 DNS Setup in your DNS provider (cPanel / Cloudflare / GoDaddy):
# Add an A Record:
# Type: A
# Name: signalwire (or signalwire.apidevx.com)
# Value / Target IP: YOUR_NEW_PRODUCTION_SERVER_IP
#
# Ensure AWS Security Group has Port 80 (HTTP) and Port 443 (HTTPS) open.

# 9.2 Install Certbot:
sudo apt install -y certbot python3-certbot-apache

# 9.3 Request and install free SSL certificate with automatic redirect:
sudo certbot --apache -d signalwire.apidevx.com --redirect

# Certbot will automatically configure HTTPS on Apache and set up auto-renewal!


--------------------------------------------------------------------------------
STEP 10: VERIFICATION & LIVE ENDPOINTS
--------------------------------------------------------------------------------
Once setup is complete, your live URLs will be:

1. App Status:        https://signalwire.apidevx.com/
2. GHL Custom Page:   https://signalwire.apidevx.com/custom-page
3. OAuth Callback:    https://signalwire.apidevx.com/callback
4. Delivery Webhook:  https://signalwire.apidevx.com/api/delivery
5. Inbound Webhook:   https://signalwire.apidevx.com/api/inbound
6. Horizon Dashboard: https://signalwire.apidevx.com/horizon


--------------------------------------------------------------------------------
STEP 11: FUTURE DEPLOYMENTS / CODE UPDATE WORKFLOW
--------------------------------------------------------------------------------
Whenever you upload new code updates to production in the future, run:

cd /var/www/signalwire
php artisan migrate --force
php artisan optimize:clear
sudo supervisorctl restart horizon

================================================================================
                               END OF SETUP GUIDE
================================================================================
