# New Server Preparation Guide

## Server Information
- **Server**: vps2.scala4.com
- **User**: root
- **Password**: Sun-Morning-Wind-2026

## Quick Start

### Step 1: Connect to New Server
```bash
ssh root@vps2.scala4.com
# Password: Sun-Morning-Wind-2026
```

### Step 2: Run Preparation Script
```bash
# Copy the preparation script to the new server first, then:
chmod +x prepare-new-server.sh
./prepare-new-server.sh
```

**OR** if you want to run it directly from the old server:
```bash
# From old server, copy script to new server
scp /var/www/html/wordpress6/wordpress/EP/migration/prepare-new-server.sh root@vps2.scala4.com:/root/

# Then SSH to new server and run it
ssh root@vps2.scala4.com
chmod +x /root/prepare-new-server.sh
/root/prepare-new-server.sh
```

## What the Script Installs

### 1. System Updates
- Updates all system packages
- Installs essential tools (curl, wget, etc.)

### 2. Apache Web Server
- Apache 2.4
- Required modules: rewrite, ssl, headers, expires, deflate
- Configured and running

### 3. MariaDB Database Server
- MariaDB 10.x
- Secured installation
- Optimized configuration for web applications
- UTF8MB4 character set support

### 4. PHP Installation
- **PHP 8.1** (default for Apache)
- **PHP 7.4** (for compatibility with older apps)
- All required extensions:
  - MySQL/Mysqli
  - GD, Imagick (image processing)
  - XML, JSON, cURL
  - mbstring, zip, bcmath
  - intl, soap, opcache

### 5. ionCube Loader
- Required for WHMCS billing system
- Installed for both PHP 8.1 and PHP 7.4

### 6. SSL Certificates
- Certbot installed
- Ready for Let's Encrypt certificates

### 7. Security
- UFW firewall configured (ports 22, 80, 443)
- Fail2Ban installed for brute force protection

### 8. Development Tools
- Git (for pulling migration scripts)
- rsync (for efficient file transfers)
- Node.js 18.x (if needed for any Node.js apps)

### 9. Directory Structure
- `/var/www/html` - Web root (created and configured)
- `/backup/applications` - For application backups
- `/backup/databases` - For database backups

### 10. Timezone
- Set to Europe/Madrid

## Post-Installation Verification

After running the script, verify everything is working:

### 1. Check Apache
```bash
systemctl status apache2
curl http://localhost
```

### 2. Check PHP
```bash
php8.1 -v
php7.4 -v
php8.1 -m | grep ioncube  # Should show ioncube
```

### 3. Check MySQL
```bash
systemctl status mariadb
mysql -e "SELECT VERSION();"
```

### 4. Test PHP Info Page
Visit: `http://[SERVER_IP]/info.php`
- Should show PHP 8.1 information
- Should show ionCube Loader loaded
- **IMPORTANT**: Remove this file after testing!

### 5. Check Firewall
```bash
ufw status
```

## Manual Steps After Script Completion

### 1. Remove PHP Info Page
```bash
rm /var/www/html/info.php
```

### 2. Secure SSH Access (Recommended)
```bash
# Generate SSH key pair on your local machine, then:
# Copy public key to server
ssh-copy-id root@vps2.scala4.com

# Edit SSH config to disable password login (after testing key login)
nano /etc/ssh/sshd_config
# Set: PasswordAuthentication no
# Then: systemctl restart sshd
```

### 3. Create MySQL Admin User (if not done during script)
```bash
mysql
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'your-secure-password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
```

### 4. Copy Migration Scripts
From old server:
```bash
# Copy entire migration directory
rsync -avz /var/www/html/wordpress6/wordpress/EP/migration/ root@vps2.scala4.com:/root/migration/
```

## Next Steps After Preparation

1. **Run inventory scripts on OLD server** to document current state
2. **Start with Priority 1 applications** (WHMCS, Event Registration, Main WordPress)
3. **Test each application** thoroughly before DNS cutover
4. **Keep old server running** for 48-72 hours after migration

## Troubleshooting

### Apache not starting
```bash
systemctl status apache2
journalctl -xe
apache2ctl configtest
```

### PHP not working
```bash
# Check PHP module is enabled
apache2ctl -M | grep php

# Check PHP configuration
php8.1 -i | grep "Loaded Configuration File"
```

### ionCube not loading
```bash
# Check if file exists
ls -la /usr/lib/php/*/ioncube_loader_lin_*.so

# Check PHP modules
php8.1 -m | grep ioncube

# Check PHP error log
tail -f /var/log/apache2/error.log
```

### MySQL connection issues
```bash
# Check MySQL is running
systemctl status mariadb

# Test connection
mysql -u root -p

# Check MySQL error log
tail -f /var/log/mysql/error.log
```

## Important Security Notes

1. **Change root password** after first login
2. **Set up SSH keys** instead of password authentication
3. **Configure firewall** properly (script does basic setup)
4. **Remove info.php** after testing
5. **Keep system updated**: `apt-get update && apt-get upgrade`
6. **Monitor logs**: Check `/var/log/apache2/` and `/var/log/mysql/` regularly

## Support

If you encounter issues:
1. Check the script output for errors
2. Review system logs: `journalctl -xe`
3. Check application-specific logs
4. Verify all services are running: `systemctl status [service-name]`

