File size: 4,952 Bytes
bf47729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
set -e

echo "Starting WordPress for Hugging Face Spaces..."

# Create necessary directories
mkdir -p /var/www/html/wp-content/database
mkdir -p /var/www/html/wp-content/uploads
mkdir -p /var/www/html/wp-content/cache

# Set proper permissions
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
chmod 644 /var/www/html/.htaccess

# Initialize SQLite database if it doesn't exist
if [ ! -f "/var/www/html/wp-content/database/wordpress.db" ]; then
    echo "Initializing WordPress database..."
    
    # Create empty SQLite database
    touch /var/www/html/wp-content/database/wordpress.db
    chown www-data:www-data /var/www/html/wp-content/database/wordpress.db
    chmod 644 /var/www/html/wp-content/database/wordpress.db
    
    # Initialize WordPress tables using WP-CLI if available, or let WordPress handle it
    echo "Database will be initialized on first visit"
fi

# Create a simple index.html for health check
cat > /var/www/html/health.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>WordPress on Hugging Face Spaces</title>
    <meta charset="utf-8">
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; background: #f1f1f1; }
        .container { background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        .status { color: #46b450; font-weight: bold; }
        .info { background: #e7f3ff; padding: 15px; border-radius: 4px; margin: 20px 0; }
    </style>
</head>
<body>
    <div class="container">
        <h1>πŸš€ WordPress on Hugging Face Spaces</h1>
        <p class="status">βœ… Service is running</p>
        <div class="info">
            <h3>πŸ“‹ Setup Information:</h3>
            <ul>
                <li><strong>WordPress Version:</strong> 6.8.1</li>
                <li><strong>PHP Version:</strong> 8.3</li>
                <li><strong>Database:</strong> SQLite</li>
                <li><strong>Web Server:</strong> Apache</li>
            </ul>
        </div>
        <p><a href="/" style="background: #0073aa; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">🏠 Go to WordPress</a></p>
        <p><a href="/wp-admin/" style="background: #46b450; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">βš™οΈ Admin Panel</a></p>
    </div>
</body>
</html>
EOF

# Create demo content script
cat > /var/www/html/wp-content/demo-setup.php << 'EOF'
<?php
/**
 * Demo content setup for WordPress on HF Spaces
 */
if (!defined('ABSPATH')) {
    define('ABSPATH', dirname(__FILE__) . '/../');
}

require_once ABSPATH . 'wp-config.php';
require_once ABSPATH . 'wp-includes/wp-db.php';
require_once ABSPATH . 'wp-admin/includes/upgrade.php';

// Create demo user if not exists
if (!username_exists('demo') && !email_exists('[email protected]')) {
    $user_id = wp_create_user('demo', 'demo123', '[email protected]');
    if (!is_wp_error($user_id)) {
        $user = new WP_User($user_id);
        $user->set_role('administrator');
        echo "Demo user created: demo/demo123\n";
    }
}

// Create demo post
$demo_post = array(
    'post_title'    => 'Welcome to WordPress on Hugging Face Spaces!',
    'post_content'  => '<h2>πŸŽ‰ Congratulations!</h2><p>You have successfully deployed WordPress on Hugging Face Spaces. This is a demo installation running on:</p><ul><li><strong>WordPress 6.8.1</strong></li><li><strong>PHP 8.3</strong></li><li><strong>SQLite Database</strong></li><li><strong>Apache Web Server</strong></li></ul><h3>πŸš€ What you can do:</h3><ul><li>Create and edit posts</li><li>Install themes and plugins</li><li>Customize your site</li><li>Explore WordPress features</li></ul><p><em>Note: This is a demo environment. Data may not persist between deployments.</em></p>',
    'post_status'   => 'publish',
    'post_author'   => 1,
    'post_type'     => 'post'
);

if (!get_page_by_title('Welcome to WordPress on Hugging Face Spaces!')) {
    wp_insert_post($demo_post);
    echo "Demo post created\n";
}

echo "Demo setup completed\n";
?>
EOF

# Set up WordPress if not already configured
if [ ! -f "/var/www/html/wp-config.php" ]; then
    echo "WordPress configuration not found, copying HF Spaces config..."
    cp /var/www/html/wp-config-hf.php /var/www/html/wp-config.php
fi

# Start Apache in background
echo "Starting Apache web server on port 7860..."
apache2-foreground &
APACHE_PID=$!

# Wait for Apache to start
sleep 5

# Check if WordPress is accessible
echo "Checking WordPress accessibility..."
if curl -f http://localhost:7860/health.html > /dev/null 2>&1; then
    echo "βœ… WordPress is accessible at http://localhost:7860"
else
    echo "⚠️ WordPress may not be fully ready yet"
fi

# Keep the container running
echo "WordPress is ready! Access it at https://your-space-name.hf.space"
echo "Admin panel: https://your-space-name.hf.space/wp-admin/"
echo "Demo credentials: demo/demo123"

# Wait for Apache process
wait $APACHE_PID