ssl-guides 2026-12-02 ⏱ 4 min read

Configure SSL for PHP Applications

Configure SSL for PHP Applications

PHP does not serve HTTPS. The web server does: Apache with mod_php or PHP-FPM, or Nginx with PHP-FPM. Your PHP code only needs to know whether the request was secure, produce correct URLs, set cookies safely, and make verified HTTPS calls to other services. This guide covers the server setup and each of those code-level points.

Step 1: install the SSL certificate for PHP on the web server

Issue a certificate for your domain; a free Let's Encrypt certificate is a reasonable choice, and this Certbot guide shows how. For Apache, a minimal virtual host is:

<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/example

    SSLEngine on
    SSLCertificateFile    /etc/ssl/example/fullchain.pem
    SSLCertificateKeyFile /etc/ssl/example/privkey.pem
</VirtualHost>

For Nginx with PHP-FPM, the TLS lines are the same as for any site: listen 443 ssl;, ssl_certificate and ssl_certificate_key, plus a location ~ \.php$ block that passes to the FPM socket. If you have a hosting control panel, install the certificate there instead of editing files by hand.

Step 2: Redirect HTTP to HTTPS

Do this at the server level, not in PHP, so static files are covered too. In Apache .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

In Nginx, use a port 80 server block with return 301 https://$host$request_uri;. Our guide on redirecting HTTP to HTTPS without losing SEO covers the details.

Step 3: Detect HTTPS correctly in PHP

The classic check is !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'. Behind a load balancer or CDN, PHP sees plain HTTP and that value is empty. Many applications then read X-Forwarded-Proto. Only trust that header when the request came from your own proxy; otherwise anyone can send it. Frameworks such as Laravel and Symfony have a trusted proxy setting for exactly this reason.

Step 4: SSL settings for PHP sessions and cookies

php.ini settingValuePurpose
session.cookie_secure1Cookie only over HTTPS
session.cookie_httponly1Not readable by JavaScript
session.cookie_samesiteLaxLimits cross-site requests
session.use_strict_mode1Rejects unknown session IDs
session.use_only_cookies1No session IDs in URLs

You can also set these at runtime with session_set_cookie_params() before session_start(). Turning on cookie_secure while the site is still reachable over HTTP will stop sessions working there, so finish the redirect first.

Step 5: Verify outbound HTTPS calls

cURL in PHP verifies certificates by default. Leave CURLOPT_SSL_VERIFYPEER at true and CURLOPT_SSL_VERIFYHOST at 2. If a call fails with "unable to get local issuer certificate", the fix is to set curl.cainfo and openssl.cafile in php.ini to a current CA bundle, not to disable verification. Disabling it turns every outbound call into one that a network attacker can intercept.

Step 6: Fix mixed content

After moving to HTTPS, pages that load images, scripts or stylesheets over http:// are flagged by browsers. Search templates and database content for hard-coded http:// URLs and switch to relative or HTTPS URLs. The free mixed content checker can scan a page and list the offenders.

Verification

If you run a framework, apply its own HTTPS settings as well, such as trusted proxies and a forced HTTPS scheme, and compare your TLS versions with Best TLS Configuration for Nginx and Apache.

Shared hosting and control panels

On shared hosting you usually cannot edit the Apache or Nginx configuration. Install the certificate through the panel (cPanel, Plesk or the host's own tool), then use .htaccess for the redirect. Many hosts offer a built-in free certificate; check your host's current documentation for how it is enabled and renewed, because the process differs between providers.

WordPress and CMS sites

If the PHP application is WordPress or another CMS, you must also update the stored site URL to https:// in its settings or database. Otherwise the CMS emits http:// links and triggers a redirect on every request.

Related reading: free SSL for Laravel, fixing mixed content in WordPress and AutoSSL in WHM.

Frequently Asked Questions

Does PHP handle HTTPS itself?

No. The web server terminates TLS. PHP only needs to know whether the request was secure, and to build https URLs.

Why does my app think the request is not secure behind a load balancer?

Because the proxy talks plain HTTP to PHP. Trust the forwarded protocol header, but only from your own proxy, never from the open internet.

Should I disable cURL certificate checks to fix an SSL error?

No. Set curl.cainfo and openssl.cafile to a current CA bundle instead. Turning verification off exposes every outbound call.

Will secure cookies break the site during the move?

They will while HTTP is still reachable. Finish the redirect to HTTPS first, then turn on session.cookie_secure.

What about a CMS on top of PHP?

Set the CMS site address to https as well, or it keeps emitting http links and every page redirects.

🔒 Test Your Website SSL Certificate Now
Run Free SSL Check →

✅ Post-Installation Verification & SEO Checklist

After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:

  • Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
  • Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
  • Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
  • Confirm both root and www versions resolve cleanly to one canonical HTTPS URL.
  • Verify your HTTPS property and sitemap.xml inside Google Search Console and Bing Webmaster Tools.
Technically Reviewed by TryFreeSSL Security Team
Last updated: 2026-12-05
About Us →

Free SSL Certificates, Live SSL Checker & CSR Generator

Issue trusted Let's Encrypt SSL certificates in 60 seconds with zero server setup.

Related Articles

🤖 TryFreeSSL AI Assistant