How to create SSL localhost on Ubuntu 22.04 and 20.04 LTS Linux running Nginx or Apache helps you transfer a domain from http to https just same on the server.
How to create SSL localhost on Ubuntu 22.04 and 20.04 Linux
Step 1. Generate SSL certificate and configure hosts file
For SSL (https) to work on localhost, you first need to have a domain. On localhost, you can manually create any domain and configure it in /etc/hosts. Open the file with nano and add 127.0.0.1 domain.com to the end of the file then save. For example, here itsmeit will generate SSL certificate with domain itsmeit.co.
sudo nano /etc/hosts
When you assign the domain itsmeit.co to the localhost ip 127.0.0.1 then you will run the website directly with the domain itsmeit.co instead of http://localhost/project or http://127.0.0.1/project.
Next, run the following command on the terminal to generate 2 ssl certificate files with the endings “.key” and “.crt“, remember to change itsmeit.co to your domain name.
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/ itsmeit.co.key -out /etc/ssl/certs/ itsmeit.co.crt
Step 2. Configure SSL certificate localhost with Apache2
To run your domain with HTTPS (SSL), you need to add port 443 in your apache domain config file. For example, the apache2 itsmeit config WordPress file for the domain is /etc/apache2/sites-available/ itsmeit.co.conf.
<VirtualHost *:80> ServerName itsmeit.co ServerAlias www. itsmeit.co Redirect permanent / https://itsmeit.co/ </VirtualHost> <VirtualHost *:443> ServerName itsmeit.co ServerAlias www. itsmeit.co DocumentRoot /var/www/vhosts/ itsmeit.co/httpdocs Protocols h2 http:/1.1 <If "%{HTTP_HOST} == 'www. itsmeit.co'"> Redirect permanent / https://itsmeit.co/ </If> ErrorLog ${APACHE_LOG_DIR}/ itsmeit.co-error.log CustomLog ${APACHE_LOG_DIR}/ itsmeit.co-access.log combined SSLEngine On SSLCertificateFile /etc/ssl/certs/ itsmeit.co.crt SSLCertificateKeyFile /etc/ssl/private/ itsmeit.co.key SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384 SSLProtocol All -TLSv1.2 -TLSv1.3 SSLCompression off #SSLUseStapling on Header always set Strict-Transport-Security "max-age=63072000" <Directory /var/www/vhosts/ itsmeit.co/httpdocs/> Options FollowSymlinks AllowOverride All Require all granted </Directory> </VirtualHost>
Edit the domain name, the path to the source code, and the SSL path, and save the file. Note, when copying the above code, the apostrophe character (“) may be wrong, check the characters (“).
Continue, for the above configuration to work you need to enable the SSL module.
sudo a2enmod ssl
The next step after creating SSL on localhost Ubuntu 22.04 | 20.04 is the Global ServerName
configuration:
Open the apache2.conf file and add ServerName 127.0.0.1 to the end of the file.
sudo nano /etc/apache2/apache2.conf
. . . # Include the virtual host configurations: IncludeOptional sites-enabled/*.conf # vim: syntax=apache ts=4 sw=4 sts=4 sr noet ServerName 127.0.0.1
Finally, check the configuration file syntax again, if OK then restart apache2 is okay. If there is an error message, run the command sudo apachectl configtest
to determine the error.
sudo service apache2 restart
Step 3. Configure SSL certificate localhost with Nginx
Similar to apache, after you config /etc/hosts and create SSL localhost on Ubuntu with 2 files ending in “.key” and “.crt” you just need to add config for port 443 to run HTTPS. Example for WordPress file config :
server { listen 80; listen [::]:80; listen 443 ssl http2; root /var/www/vhosts/ itsmeit.co/httpdocs; index index.html index.htm; server_name itsmeit.co www. itsmeit.co; access_log /var/log/nginx/ itsmeit.co.vhost.log; error_log /var/log/nginx/ itsmeit.co.vhost.err; ssl_certificate /etc/ssl/certs/ itsmeit.co.crt; ssl_certificate_key /etc/ssl/private/ itsmeit.co.key; ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384"; ssl_ecdh_curve secp384r1; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; resolver 8.8.8.8 8.8.4.4 valid=300s; resolver_timeout 5s; location / { try_files $uri $uri/ =404; } }
Explain
- root is the path to the directory running the website
- server_name is the website domain
- listen 80 is the default port (*:80) when installing Nginx
- listen 443 SSL http2 is the port configuration to run SSL (https)
- ssl_certificate and ssl_certificate_key are the key for the authentication file
Re-check the configuration and save the file, reset Nginx if there are no errors. If there is an error then run the command sudo nginx -t
to identify and fix the error.
Setup SSL localhost through the above instructions will run the domain with HTTPS, but localhost SSL is not authenticated, so you need to confirm it manually as shown below. Create SSL localhost on Ubuntu 22.04 | 20.04 serves to test plugins that need to connect to APIs such as facebook, google…
Above are 2 ways to create SSL localhost on Ubuntu 22.04 | 20.04 LTS running Nginx or Apache helps you transfer from http to https just the same on the server. Hope this article helps you!
– Advertising –