Changing port number in Trellis

I am trying to make api requests between two local dev sites. I noticed that I can’t connect to one site from another via the WP Rest API (it simply times out and says it cannot connect) but my API request when pasting the url into the browser successfully returns the data.

Then I noticed that both sites are running on port 80. I found a few people asking how to change the port to a custom one however the posts are a few years old and include broken links to documentation.

Here’s what my hosts/staging file looks like:

[development]
192.168.56.6 ansible_port=90

[web]
192.168.56.6 ansible_port=90

I also tried this:

[development]
192.168.56.6:90

[web]
192.168.56.6:90

However the site is still running on port 80 after restarting the local server. I also reprovisioned to see if that worked, but it did not.

Can anyone provide insight on how to change ports in Trellis?

Setting ansible_port in the hosts file is for using a non-standard port to connect to the server over SSH and is unrelated to anything with the webserver.

Modifying port 80 for the webserver isn’t possible out of the box, it is hardcoded in the configs (see the references to 80 in the wordpress-site template).

Based on your post, I’m not seeing why you would need to be modifying the webserver ports. If you provide more details/show some code, then maybe someone can chime in with suggestions on how to fix your REST API connection issues.

1 Like

What’s strange is that request goes through when made to the remote staging server of website A from the dev site of website B.

Also, when I purposely input the wrong url for the REST API, I am able to properly handle the exception.

It’s only when I make the request to the dev site of Website A that it times out and provides no chance to handle the exception.

Here is the error it produces:

# cURL error 7: Failed to connect mysite.test port 80 after 65058 ms: Connection refused (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for http://mysite.test/wp-json/wp/v2/data_publications?include=17793

And when I copy the url mentioned in the error and simply paste it into my browser or curl it, it returns a proper response.

So that’s what led me to think that having both sites running on the same port and making requests to each other could be jamming things up, but I’ll admit I don’t really understand what happens within a port.

Here is my truncated with() function from my composer (note I am using Guzzle for the http request):

public function with(){
         
        $content = [
            'error' => false
        ];
        $post_type = isset($_GET['post_type']) ? $_GET['post_type'] : null;
        $post_id = isset($_GET['post_id']) ? $_GET['post_id'] : null;
        
        $ccc_api_root = env('CCC_API_ROOT');
        

        if(!$post_type || !$post_id):
            $content['error'] = true;
            return ['content' => $content];
        endif;

        $client = new Client([
            'base_uri' => "{$ccc_api_root}/wp-json/wp/v2/",
            'request.options' => [
                'exceptions' => false,
                'timeout' => 3.00,
                'debug' => true
            ]
        ]);

        try {
            $response = $client->request('GET', "{$post_type}?include={$post_id}" );
            
            $body_json = $response->getbody()->getContents();
            $body = json_decode($body_json);
            //do stuff with data
        
            $content += [
                    //content data I am returning
            ];

            return ['content' => $content];
        }
        
        catch(ClientException $e){
            
            $content['error']= true;
            return ['content' => $content];
        }

    }