The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Start by running the same PhantomJS script with the same operating-system account and environment used by PHP, then capture the executable path, version, exit status, standard output, and standard error. That separates a PHP process-launch problem from a PhantomJS runtime failure, a page-loading or JavaScript problem, and a file-writing problem. There is no universal fix without the command, OS, PhantomJS version, error, and target page.
Diagnose the failure before changing settings
Use one small test page and one PhantomJS script, and record what each test does. First run it interactively; then run it as the web-server or PHP service account, ideally in the same container or service environment. A shell test under your personal account does not establish that PHP can access the same binary, libraries, script, environment variables, or output directory.
- Find the actual binary. In the environment where the command works, run
command -v phantomjsandphantomjs --version. Use the resulting absolute path in PHP, rather than relying on its interactive shell’sPATH. - Repeat as PHP’s service user. Use the appropriate account and service/container context for your deployment. Compare the resolved executable path, version, working directory, environment, and access to the script and destination directory.
- Separate process startup from page rendering. Run PhantomJS directly with a minimal script. If the process cannot start, investigate installation, permissions, libraries, or host security. If it starts but reports a page-load failure, focus on network and page behavior instead.
- Record evidence. Keep the exact command with secrets removed, exit code, stdout, stderr, and whether an output file was created, its size, and whether it opens. These details identify which branch to follow below.
The official PhantomJS troubleshooting guide warns that multiple installed versions can conflict over which binary is invoked. Do not assume the version in an interactive terminal is the one PHP runs.
Capture PHP’s child-process result
The title does not establish that your application uses PHP’s exec(); it may use another process API or a wrapper. Check the exact API and capture its return value and output. PHP’s official exec documentation describes the function’s arguments and return behavior.
Recommended Free Tools
#1 Best Overall
For a basic exec() diagnostic, use an absolute binary path and a script/output path PHP can access. This example stores stdout and the exit status; shell redirection also captures stderr into the same log. Adapt paths and the service user’s permissions to your deployment.
<?php
$binary = '/usr/local/bin/phantomjs';
$script = '/var/www/app/render.js';
$log = '/var/www/app/phantomjs.log';
$command = escapeshellarg($binary) . ' ' . escapeshellarg($script)
. ' 2>&1';
$output = [];
$exitCode = 0;
exec($command, $output, $exitCode);
file_put_contents($log, implode(PHP_EOL, $output) . PHP_EOL);
error_log('PhantomJS exit code: ' . $exitCode);
?>
Do not put API keys, cookies, authorization headers, or other secrets into logs. Avoid logging an unescaped command assembled from request data. If your application needs a generated target URL or filename, validate it and use the process API’s argument-escaping facilities or an argument-array interface where available. A process that returns no output is not proof of success: check the exit status, stderr, and expected file.
Instrument PhantomJS page loading and errors
Once the binary launches, determine whether the page opened and whether the render ran. The official PhantomJS quick start uses the page.open callback and explicitly exits the process. A minimal diagnostic script can follow that pattern:
var page = require('webpage').create();
var system = require('system');
var target = system.args[1];
var output = system.args[2];
page.onError = function (message, trace) {
console.error('Page error: ' + message);
trace.forEach(function (frame) {
console.error(' ' + frame.file + ':' + frame.line);
});
};
page.onConsoleMessage = function (message) {
console.log('Page console: ' + message);
};
page.onResourceRequested = function (request) {
console.log('Request: ' + request.url);
};
page.onResourceReceived = function (response) {
if (response.stage === 'end') {
console.log('Response: ' + response.status + ' ' + response.url);
}
};
page.open(target, function (status) {
console.log('page.open status: ' + status);
if (status === 'success') {
page.render(output);
console.log('Rendered: ' + output);
phantom.exit(0);
}
console.error('Page did not load successfully');
phantom.exit(1);
});
Save it as render.js, then invoke it with the target URL and a writable output path, for example /usr/local/bin/phantomjs /var/www/app/render.js https://example.com /var/www/app/out.png. Run that exact command under the PHP service identity as well as interactively. The callbacks make a page failure distinguishable from a PHP launch failure. PhantomJS does not forward page console messages by default, so page.onConsoleMessage is useful when the site’s own logs matter.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsRank #2
If the script performs asynchronous work beyond page.open, call phantom.exit() after that work finishes on every success and error path. PhantomJS will not necessarily terminate on its own; a missing exit can appear in PHP as a request that waits or times out.
Follow the symptom to the likely cause
“PhantomJS works in terminal but not in PHP”
Compare the service and interactive environments rather than changing the page script first. PHP may have a different PATH, working directory, user identity, environment variables, or filesystem permissions. Set the absolute binary path, ensure the service user can traverse parent directories and read the script, and confirm it can write to the output and log locations. Also verify that PHP’s process-execution function is available and not restricted by the host configuration.
“PhantomJS not working when called from PHP” or “command not found”
Inspect the captured error and exit status. If the executable is not found, use its absolute path and verify that the PHP service can access it. If there is no child output, check whether the chosen PHP API is permitted and whether the command is being quoted and constructed correctly. Compare the exact binary path and --version result between contexts; multiple installations can lead to PHP running a different release.
“PhantomJS permission denied from PHP”
Determine which object triggered the denial: the binary, a shared library, the script, a parent directory, or the output destination. Check access as the service account, not just as an administrator. On systems where SELinux is enabled, check its policy and audit messages: PhantomJS’s troubleshooting documentation notes SELinux can prevent it from working. Do not respond by broadly disabling host security; correct the relevant policy or deployment permissions.
“PHP exec PhantomJS returns blank image”
First establish whether an image was created and whether page.open returned success. If not, use stderr, resource events, and page errors to find why the page did not load. If it did load, check whether the page depends on JavaScript, delayed content, or resources that failed to arrive before rendering. A blank or incomplete output can also result from rendering before asynchronous content is ready; wait for a meaningful selector or page condition rather than adding an arbitrary delay without checking the page.
If the file exists and contains the page but its background is transparent, that can be expected: the PhantomJS render API notes that transparency may result when the page does not set a background color. Inspect the page’s CSS before treating that as a process failure.
HTTPS fails while HTTP works
Check the SSL libraries available to the actual PhantomJS process, particularly OpenSSL, and inspect resource and page-load errors. The project’s troubleshooting guidance identifies SSL libraries as a point to check when HTTPS fails. A successful plain-HTTP test does not verify TLS setup.
“PhantomJS cannot connect to X server”
Check the exact PhantomJS version before installing X11 or Xvfb. The official PhantomJS FAQ distinguishes releases: 1.4 and earlier needed an X server, while 1.5 and later were pure headless and did not require X11/Xvfb. Installing a display server is therefore not the general fix for newer versions; an X-server error on a newer release points to a version, packaging, or invocation mismatch to investigate.
Rank #4
Windows proxy delay or request failures
The PhantomJS troubleshooting guide documents a default-proxy latency issue on Windows and gives --proxy-type=none as a workaround for that situation. Use that switch only when the symptom and environment match the documented proxy issue. It may be inappropriate where a proxy is required to reach the target.
Check the output path and format
The render API documentation describes page.render(filename) and output formats. The filename extension selects the format; documented formats include PDF, PNG, JPEG, BMP, and PPM, while GIF support depends on the Qt build. Confirm that the extension matches the format you expect, and verify the output directory exists and is writable by PHP’s service account. A successful render to a different path can help distinguish a filesystem issue from a page issue.
- No file: check render timing, the destination path, directory traversal permissions, write access, and whether the script reached
page.render(). - Zero-length or unreadable file: capture stderr and exit status, confirm the render completed, and check format/build support.
- Valid image, wrong content: inspect load status, JavaScript errors, missing resource requests, and whether dynamic content was ready at capture time.
- Unexpected transparency: inspect the document background styling; transparency alone does not establish that rendering failed.
Use a repeatable troubleshooting checklist
- Record the OS, deployment type, PHP version, PhantomJS version, process API, exact sanitized command, target URL, and full stderr/stdout.
- Run the command with an absolute binary path as the PHP service user in the production-like environment.
- Verify executable and library access, script readability, output-directory write permission, working directory, and environment differences.
- Run the instrumented script and classify the result as launch failure, page-load failure, page-side error, or output failure.
- For network symptoms, inspect requested resources and response status; for HTTPS-only symptoms, investigate SSL/OpenSSL; for a Windows proxy symptom, evaluate the documented proxy workaround.
- For X-server errors, check the version before changing display dependencies. For hangs, verify every asynchronous branch exits.
- Retest after one change at a time, preserving the command and outputs that demonstrate whether the change addressed the observed failure.
Plan for PhantomJS’s legacy status
PhantomJS is a legacy renderer, not a maintained browser automation foundation. Its GitHub repository was archived on May 30, 2023, and the project wiki labels the 2.x branch deprecated and no longer maintained: PhantomJS project repository. If the rendering path is production-critical, evaluate a maintained browser renderer against the pages and deployment you actually support. Compare whether PHP can launch it under the service identity, browser and JavaScript behavior, OS/container and headless requirements, output formats and fidelity, and migration cost. Choosing a maintained alternative is planning advice, not a guaranteed fix for the specific PHP failure diagnosed above.
Or skip the browser setup
If you need a screenshot without maintaining a local PhantomJS runtime, ScreenshotNeo provides a website screenshot API and MCP server. One GET request with a URL returns a PNG, JPEG, WebP, or PDF. The following cURL example saves a WebP screenshot; see the ScreenshotNeo API documentation for options and response details.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
Python and Node.js versions of the same call:
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
- Cookie banners and consent overlays, newsletter popups, and chat widgets are removed before capture; each of those steps can be turned off.
- Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers report page verdict and billing status.
- An MCP server offers
take_screenshot,get_page_info, andcapture_pdftools for AI agents and MCP clients. - The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Every feature is on every plan.
Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month without a card.
Frequently Asked Questions
Do I need to install Xvfb to run PhantomJS from PHP?
Not for PhantomJS 1.5 and later according to the official FAQ; versions 1.4 and earlier needed an X server. Check the binary PHP actually launches before installing display dependencies.
Why does my PHP request hang even though PhantomJS starts?
A PhantomJS script may not exit after asynchronous work. Ensure each success and failure path calls phantom.exit() when its work is complete.
Does a transparent screenshot mean page.render failed?
Not necessarily. PhantomJS documents transparency as expected when the page has no background color set.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

