Raspberry Pi B+ LEMP服务器PHP更新SQL后空白页问题求助
Hey Marc, let's break down why you're seeing a blank page after syncing 1900 transactions (way more than your expected 100) on your 512MB Raspberry Pi Model B+. That tiny amount of RAM is almost certainly the main culprit here, but let's walk through step-by-step fixes and checks to get things working again.
1. First, Reveal the Hidden Error (Blank Pages = Hidden PHP Issues)
Blank pages in PHP usually mean errors are being suppressed. Let's uncover them:
- Temporary error display in your script: Add these lines at the very top of your PHP sync/summary file to force errors to show up:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); - Check server logs (more reliable):
- View Nginx's error log in real-time:
sudo tail -f /var/log/nginx/error.log - View PHP-FPM's log (adjust the version number to match your setup, e.g.,
php7.4-fpm):sudo tail -f /var/log/php7.4-fpm.log
- View Nginx's error log in real-time:
2. Diagnose Memory Pressure on Your Pi
512MB is barely enough for a basic LEMP stack, let alone processing 1900 transactions. Let's check how your memory is being used:
- Run this command to see current memory/swap usage:
Iffree -husedmemory is near 512MB orswapis being heavily utilized, your system is starved for RAM. - Temporary swap boost (for testing only): Pi SD cards don't handle frequent swap writes well, but you can add a temporary swap file to rule out memory issues:
Remember to disable this later if you don't need it withsudo fallocate -l 512M /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfilesudo swapoff /swapfile.
3. Tweak PHP Configuration (But Don't Overdo It)
Your PHP settings might be too restrictive for the task:
- Edit your PHP-FPM config file (e.g.,
/etc/php/7.4/fpm/php.ini):- Raise
memory_limitto a reasonable value (don't go over 256MB on a 512MB Pi):memory_limit = 256M - Increase
max_execution_timeif syncing is timing out:max_execution_time = 60
- Raise
- Restart PHP-FPM to apply changes:
sudo systemctl restart php7.4-fpm
4. Optimize Your Code (The Long-Term Fix)
The real solution is to stop forcing your Pi to handle 1900 transactions at once. Here's how:
- Batch process transactions: Instead of pulling all 1900 records from the API in one go, fetch and sync them in small chunks (e.g., 100 at a time):
$offset = 0; $batchSize = 100; do { // Fetch a single batch from the API $transactionBatch = get_transactions_from_api($offset, $batchSize); if (empty($transactionBatch)) break; // Sync this batch to your database sync_batch_to_db($transactionBatch); $offset += $batchSize; } while (true); - Let the database do the heavy lifting: For your summary page, don't load all transaction data into PHP to calculate totals. Use SQL aggregate functions like
SUM()orCOUNT()instead—databases are optimized for this:SELECT SUM(amount) AS total_sales, COUNT(*) AS total_transactions FROM transactions; - Add database indexes: If your summary query is slow, add indexes to columns you're filtering/sorting by (e.g., transaction date):
CREATE INDEX idx_transaction_date ON transactions(transaction_date);
5. Check if the OOM Killer Killed Your Process
If your Pi runs out of memory entirely, the Linux Out-of-Memory (OOM) Killer will terminate processes to save the system. Check if this happened:
dmesg | grep -i oom
If you see a line like Out of memory: Killed process ... (php-fpm), that confirms memory was the issue—batch processing is non-negotiable here.
内容的提问来源于stack exchange,提问作者Marc

