Skip to content

Instantly share code, notes, and snippets.

@eypsilon
Last active October 13, 2021 07:37
Show Gist options
  • Select an option

  • Save eypsilon/92c06c7e2582cb0ffc7aeb428c717c4a to your computer and use it in GitHub Desktop.

Select an option

Save eypsilon/92c06c7e2582cb0ffc7aeb428c717c4a to your computer and use it in GitHub Desktop.
https for local env on Ubuntu 20.04 using apache2

https for local env on Ubuntu 20.04 using apache2

Additional to: kifarunix.com /
Create Locally Trusted SSL Certificates with mkcert on Ubuntu 20.04

Setup with the DocumentRoot on a separate SSD Partition called Server # /media/user_many/Server/www

; OS drive
/Filesystem
    /home
        /user_many
            /ssl-certs
; second drive: /media/user_many/Server
/Server 
    /www
        /example.loc
            /public

Create Project dir

eg: /media/user_many/Server/www/example.loc/public

cd /media/user_many/Server/www
mkdir example.loc
cd example.loc
mkdir public
cd public
echo '<h1>Hakuna Matata!</h1>' > index.html

Set Hostname

sudo nano /etc/hosts
# add
127.0.0.1  example.loc www.example.loc

Apache Config

# copy 'default.conf' for the new host 
cd /etc/apache2/sites-available/
sudo cp 000-default.conf example.loc.conf
sudo nano example.loc.conf

set Defines (variables), copy all & paste to example.loc.conf

Define DEFAULT_HOSTNAME     "example.loc"
Define DEFAULT_WWW_HOSTNAME "www.example.loc"
Define DEFAULT_CERT_ROOT    "/home/user_many"
Define DEFAULT_DOC_ROOT     "/media/user_many/Server/www/${DEFAULT_HOSTNAME}/public"
Define DEFAULT_CERT_APPIX   "+1"

<VirtualHost *:80>
    ServerName ${DEFAULT_WWW_HOSTNAME}
    ServerAlias ${DEFAULT_HOSTNAME}
    DocumentRoot ${DEFAULT_DOC_ROOT}
</VirtualHost>

<VirtualHost *:443>
    ServerName  ${DEFAULT_WWW_HOSTNAME}
    ServerAlias ${DEFAULT_HOSTNAME}
    SSLEngine On
    SSLCertificateFile    ${DEFAULT_CERT_ROOT}/${DEFAULT_HOSTNAME}${DEFAULT_CERT_APPIX}.pem
    SSLCertificateKeyFile ${DEFAULT_CERT_ROOT}/${DEFAULT_HOSTNAME}${DEFAULT_CERT_APPIX}-key.pem
    DocumentRoot          ${DEFAULT_DOC_ROOT}
</VirtualHost>

Create Certificate & Key for the Hostnames

cd /home/user_many/ssl-certs
mkcert example.loc '*.example.loc' localhost 127.0.0.1 ::1

Some basic .htaccess settings

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
  # Header set X-Frame-Options "allow-from https://www.google.com/"
  # https extras
  Header set Referrer-Policy "same-origin"
  Header set Feature-Policy "geolocation 'self'"
  Header set Content-Security-Policy "frame-ancestors 'self'"
  Header set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
  Header always set Strict-Transport-Security "max-age=31536000" early
  Header unset Strict-Transport-Security env=!HTTPS
  Header set X-XSS-Protection "1; mode=block" "expr=%{CONTENT_TYPE} =~ m#text/html#i"
  Header set X-Content-Type-Options "nosniff"
  Header set X-Frame-Options "SAMEORIGIN"
</IfModule>
<IfModule mod_setenvif.c>
  # SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
  <IfModule mod_headers.c>
      <FilesMatch "\.(bmp|cur|gif|ico|jpe?g|png|svgz?|webp)$">
          SetEnvIf Origin ":" IS_CORS
          Header set Access-Control-Allow-Origin "*" env=IS_CORS
      </FilesMatch>
  </IfModule>
</IfModule>

Finish

# enable page 
sudo a2ensite example.loc.conf

# restart server
sudo systemctl restart apache2

# have fun
// always needed and always forgotten
_italic_  **bold**  `monospace`  ~~strikethrough~~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment