Fix: CURL ERROR: Recv failure: Connection reset by peer – PHP Curl

cURL is a helpful command line utility for transferring data with various network protocols. Many times, when you are trying to cURL any specific URL, you might face an error, CURL ERROR: Recv failure: Connection reset by peer – PHP Curl or cURL: TCP Connection reset by Peer; there are a few reasons for that; let’s understand those, and it’s fixed.

[somesh@lab1 ~]# curl someshzade.com
curl: (56) Recv failure: Connection reset by peer

Allow IP Address or Port

First of all make sure the HTTP, HTTPS-related TCP ports and the IP address are whitelisted in the firewall. If you are having IPTABLES installed on the server then use the following commands to whitelist the IP and Ports,

To Allow IP Address,

[somesh@lab1 ~]# iptables -I INPUT -s <IP ADDRESS> -j ALLOW

To Allow Port,

[somesh@lab1 ~]# iptables -I INPUT -p tcp --dport <PORT NO> -j ALLOW

If in case you are using a software firewall such as CSF, then follow the below steps,

To Allow IP Address,

Navigate to CSF Dashboard → csf – Quick Actions → Enter the IP Address in QUICK ALLOW → Hit Enter.

To Allow Port,

Navigate to CSF Dashboard → Firewall Configuration → IPv4 Port Settings.

Update cURL and PHP

On many occasions, I found that this is an error related to the un-updated cURL PHP module and the PHP. In this case, you can update the cURL PHP module and the PHP version.

How to Install or Upgrade PHP 8.1 on Ubuntu 20.04

How to upgrade the PHP version on CentOS 7

Install/Upgrade PHP 8.1 on AlmaLinux 8

Service Level Block

If you are using any ANTI-DDoS service or a third-party vendor DNS service, you must check if they are blocking the connection with the destination or source server IP address.

In a shared hosting environment, we noticed LiteSpeed DDoS protection blocks the local host (127.0.0.1) and server’s assigned IP addresses. In that case, you must check and verify /tmp/lshttpd/.rtreport for any banned entry and make sure you whitelist the IP addresses through LiteSpeed’s Trusted IPs.

Check IP Address & TCP

Many times the issues belong to the host side. If your web hosting provider or a server provider blocks the IP address or a specific TCP port then that time you might end up in this situation.

How can I resolve this?

In this case, contact your web hosting or server provider providing the error and the steps to replicate the issue.

I hope the above solutions helped you with the resolution. If you still have any queries, shoot me a DM.