Obtain an SSL certificate from a reputable certificate authority (CA) such as Let's Encrypt or GlobalSign.
<?php // Generate a private key $privateKey = openssl_pkey_new(); // Generate a public key $publicKey = openssl_pkey_get_details($privateKey)['key']; // Generate a certificate signing request $csr = openssl_csr_new(); openssl_csr_add_field($csr, 'subject', 'CN=localhost'); openssl_csr_add_field($csr, 'organizationName', 'Your Organization'); openssl_csr_add_field($csr, 'organizationalUnitName', 'Your Organizational Unit'); openssl_csr_add_field($csr, 'commonName', 'localhost'); openssl_csr_sign($csr, $privateKey); // Generate a certificate $cert = openssl_csr_get_cert($csr); // Write the certificate to a file file_put_contents('cert.pem', $cert); // Write the private key to a file file_put_contents('privateKey.pem', $privateKey); ?>
Configure your web server to use the SSL certificate and private key.
<?php // Apache configurationServerName localhost DocumentRoot /var/www/html SSLEngine on SSLCertificateFile /path/to/cert.pem SSLCertificateKeyFile /path/to/privateKey.pem </VirtualHost> ?>
Update your website's URLs to use HTTPS instead of HTTP.
<?php // Update URLs in your HTML files <a href="https://www.example.com">Visit HTTPS site</a> ?>