Repository navigation
Document HTTP proxy configuration for WP-CLI and WordPress core #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dilipom13
wants to merge
5
commits into
wp-cli:main
Choose a base branch
from
dilipom13:fix/601-http-proxy-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
253b3be
Document HTTP proxy configuration for WP-CLI and WordPress core
dilipom13 125b9fe
Address CodeRabbit review feedback on HTTP proxy guide
dilipom13 6806b75
Merge branch 'main' into fix/601-http-proxy-docs
dilipom13 d2d2122
Address package maintainer review on the HTTP proxy guide
dilipom13 e433772
changes fixed
dilipom13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,198 @@ | ||||||||||||||||||
| # HTTP Proxy Configuration | ||||||||||||||||||
|
|
||||||||||||||||||
| When running WP-CLI in restricted network environments or behind corporate firewalls, you may need to route outbound HTTP/HTTPS requests through an HTTP proxy. | ||||||||||||||||||
|
|
||||||||||||||||||
| WP-CLI and WordPress core use the Requests HTTP library for performing remote HTTP requests. WP-CLI has no HTTP proxy setting of its own, but when PHP's cURL extension sends a request, cURL reads some standard proxy environment variables itself: HTTPS requests go through a proxy set in `https_proxy` or `HTTPS_PROXY`, and hosts listed in `no_proxy` or `NO_PROXY` connect directly. | ||||||||||||||||||
|
|
||||||||||||||||||
| To configure an HTTP proxy for WP-CLI, you can use the community package [`ekamran/wp-cli-http-proxy-command`](https://github.com/ekamran/wp-cli-http-proxy-command) (recommended for WordPress core requests and WP-CLI's own HTTP requests), or manually configure WordPress core proxy constants in a bootstrap file. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Using the HTTP Proxy Package (Recommended) | ||||||||||||||||||
|
|
||||||||||||||||||
| The [`ekamran/wp-cli-http-proxy-command`](https://github.com/ekamran/wp-cli-http-proxy-command) community package configures the proxy for both WordPress core HTTP requests and WP-CLI's own HTTP requests made through `WP_CLI\Utils\http_request()`, such as `wp core verify-checksums`. | ||||||||||||||||||
|
|
||||||||||||||||||
| > Note: The package requires WP-CLI 2.12.0 or newer. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Installation | ||||||||||||||||||
|
|
||||||||||||||||||
| Install the package using the `wp package install` command: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| wp package install ekamran/wp-cli-http-proxy-command:@stable | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Behind a firewall that allowed only the proxy, this install failed without
Suggested change
|
||||||||||||||||||
|
|
||||||||||||||||||
| If the machine can reach the internet only through the proxy, set `https_proxy` when you run this command and any other `wp package` command (see Limitations below): | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| https_proxy=http://proxy.example.com:8080 wp package install ekamran/wp-cli-http-proxy-command:@stable | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Configuration | ||||||||||||||||||
|
|
||||||||||||||||||
| Add an `http-proxy` configuration block to your project's `wp-cli.yml` or your global `~/.wp-cli/config.yml` file: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| url: http://proxy.example.com:8080 | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Scheme-less proxy URLs are also supported: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| url: proxy.example.com:8080 | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Or configure the host and port separately: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| host: proxy.example.com | ||||||||||||||||||
| port: 8080 | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Authentication and Bypass Hosts | ||||||||||||||||||
|
|
||||||||||||||||||
| If your proxy requires authentication or if specific hosts should bypass the proxy, specify them in the configuration: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| url: http://proxy.example.com:8080 | ||||||||||||||||||
| username: proxy-user | ||||||||||||||||||
| password: proxy-password | ||||||||||||||||||
| bypass-hosts: | ||||||||||||||||||
| - localhost | ||||||||||||||||||
| - 127.0.0.1 | ||||||||||||||||||
| - "*.local" | ||||||||||||||||||
| - "*.example.com" | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| > Warning: Do not commit proxy credentials to source control. Use an untracked configuration file (for example `wp-cli.local.yml`), restrict file permissions, or inject credentials through your deployment environment. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Using Environment Variables | ||||||||||||||||||
|
|
||||||||||||||||||
| By default, proxy environment variables are not read automatically. To opt into reading proxy settings from your environment variables, set `env: true`: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| env: true | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| When `env: true` is set, the package selects the **first non-empty** environment variable from this list and applies **one global proxy** for all requests: | ||||||||||||||||||
|
|
||||||||||||||||||
| 1. `HTTPS_PROXY` | ||||||||||||||||||
| 2. `https_proxy` | ||||||||||||||||||
| 3. `HTTP_PROXY` | ||||||||||||||||||
| 4. `http_proxy` | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| This differs from libcurl, which can use separate proxies per scheme (for example `http_proxy` for HTTP and `https_proxy` for HTTPS). The package does not read `NO_PROXY` or `no_proxy`, but cURL still does, so a host listed there skips the proxy configured by this package as well. To keep bypass rules in your WP-CLI configuration, list those hosts in `bypass-hosts`. | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Diagnostic Commands | ||||||||||||||||||
|
|
||||||||||||||||||
| The package includes diagnostic commands to help verify your proxy configuration. | ||||||||||||||||||
|
|
||||||||||||||||||
| Check the current proxy configuration status: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| wp http-proxy status | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| Perform a test HTTP request through WP-CLI's HTTP helper to verify connectivity: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| wp http-proxy check https://wordpress.org | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Behavior and Disabling | ||||||||||||||||||
|
|
||||||||||||||||||
| When active, the package: | ||||||||||||||||||
|
|
||||||||||||||||||
| - Automatically defines missing WordPress proxy constants (`WP_PROXY_HOST`, `WP_PROXY_PORT`, `WP_PROXY_USERNAME`, `WP_PROXY_PASSWORD`, `WP_PROXY_BYPASS_HOSTS`) after `wp-config.php` loads. | ||||||||||||||||||
| - Configures WP-CLI's internal HTTP requests by filtering `http_request_options`. | ||||||||||||||||||
| - Normalizes proxy URLs to Requests' `host:port` format. | ||||||||||||||||||
| - Applies `bypass-hosts` to both WordPress and WP-CLI HTTP requests. | ||||||||||||||||||
|
|
||||||||||||||||||
| To temporarily disable the proxy without removing the package, set `enabled: false` in your YAML configuration: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| http-proxy: | ||||||||||||||||||
| enabled: false | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| You can also bypass all installed packages for a single command invocation by passing the `--skip-packages` global flag: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| wp --skip-packages core update | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| ### Limitations | ||||||||||||||||||
|
|
||||||||||||||||||
| Composer-backed `wp package` commands, including `browse`, `list`, `install`, and `update`, use a separate HTTP client. Their network requests are not configured by this package. | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Manual Configuration (WordPress Core Only) | ||||||||||||||||||
|
|
||||||||||||||||||
| If you cannot install community packages or only need to configure proxy support for WordPress core HTTP requests, you can define the core proxy constants in a custom PHP file loaded via `wp-cli.yml`. | ||||||||||||||||||
|
|
||||||||||||||||||
| 1. Create a `proxy.php` file (e.g. in your project or home directory): | ||||||||||||||||||
|
|
||||||||||||||||||
| ```php | ||||||||||||||||||
| <?php | ||||||||||||||||||
| $proxy_host = getenv( 'HTTPS_PROXY' ) ?: getenv( 'https_proxy' ) ?: getenv( 'HTTP_PROXY' ) ?: getenv( 'http_proxy' ); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( ! $proxy_host ) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| $proxy_url = parse_url( $proxy_host ); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( ! is_array( $proxy_url ) || empty( $proxy_url['host'] ) ) { | ||||||||||||||||||
| return; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| $proxy_port = ! empty( $proxy_url['port'] ) ? (int) $proxy_url['port'] : null; | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( null === $proxy_port ) { | ||||||||||||||||||
| $scheme = ! empty( $proxy_url['scheme'] ) ? $proxy_url['scheme'] : 'http'; | ||||||||||||||||||
| $proxy_port = ( 'https' === $scheme ) ? 443 : 80; | ||||||||||||||||||
| } | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| define( 'WP_PROXY_HOST', $proxy_url['host'] ); | ||||||||||||||||||
| define( 'WP_PROXY_PORT', $proxy_port ); | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( ! empty( $proxy_url['user'] ) ) { | ||||||||||||||||||
| define( 'WP_PROXY_USERNAME', rawurldecode( $proxy_url['user'] ) ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( ! empty( $proxy_url['pass'] ) ) { | ||||||||||||||||||
| define( 'WP_PROXY_PASSWORD', rawurldecode( $proxy_url['pass'] ) ); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| if ( ! defined( 'WP_PROXY_BYPASS_HOSTS' ) ) { | ||||||||||||||||||
| define( 'WP_PROXY_BYPASS_HOSTS', 'localhost, 127.0.0.1' ); | ||||||||||||||||||
| } | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| 2. Require this file in your `wp-cli.yml` or `~/.wp-cli/config.yml`: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```yaml | ||||||||||||||||||
| require: | ||||||||||||||||||
| - proxy.php | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| Alternatively, when creating a new WordPress install, you can embed proxy constants in `wp-config.php` using `wp config create --extra-php`: | ||||||||||||||||||
|
|
||||||||||||||||||
| ```bash | ||||||||||||||||||
| wp config create --dbname=example --dbuser=root --dbpass=secret --extra-php <<'PHP' | ||||||||||||||||||
| define( 'WP_PROXY_HOST', 'proxy.example.com' ); | ||||||||||||||||||
| define( 'WP_PROXY_PORT', 8080 ); | ||||||||||||||||||
| PHP | ||||||||||||||||||
| ``` | ||||||||||||||||||
|
|
||||||||||||||||||
| This `extra-php` approach only applies to the generated `wp-config.php` at install time. It does not configure WP-CLI's own HTTP requests and is not suitable for existing installs unless you add the constants manually. | ||||||||||||||||||
|
|
||||||||||||||||||
| > Note: This manual approach only defines the constants that WordPress core uses for its own HTTP requests. WP-CLI's own HTTP requests do not use them. To route those through the proxy as well, use the [`ekamran/wp-cli-http-proxy-command`](https://github.com/ekamran/wp-cli-http-proxy-command) package. Neither approach covers `wp package` commands (see Limitations above). | ||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||
|
|
||||||||||||||||||
| ## Related Documentation | ||||||||||||||||||
|
|
||||||||||||||||||
| * [Global parameters and configuration files](https://make.wordpress.org/cli/handbook/references/config/) — how `wp-cli.yml` and the `require` option work. | ||||||||||||||||||
| * [Installing WP-CLI Packages](https://make.wordpress.org/cli/handbook/guides/installing-packages/) — package installation and version constraints. | ||||||||||||||||||
| * [Troubleshooting Guide](https://make.wordpress.org/cli/handbook/guides/troubleshooting/) — debugging network and configuration issues. | ||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document proxy setup for the installation.
If direct outbound access is blocked, this command can fail before the proxy package is installed. Composer does not use the package's
http-proxyconfiguration. Explain how to sethttps_proxyfor installation, and provide a local-install alternative where the initial WP-CLI lookups cannot use that proxy. (github.com)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, a suggestion for
https_proxyis in my review.A local-install fallback is not needed. Behind a firewall that allowed only the proxy, a first install with only
https_proxyset succeeded on WP-CLI 2.12.0, and every lookup (package index, Packagist, GitHub) went through the proxy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ekamran Thanks for testing the first install. I withdraw the request for a local-install fallback.
Your
https_proxysuggestion addresses the remaining documentation concern once it is applied.You are interacting with an AI system.