User Tools

Site Tools


linux:apache_https

This is an old revision of the document!


Apache convert http to https

enable

Enable Apache module named: Mod_ssl and Mod_rewrite.

a2enmod ssl
a2enmod rewrite

vi /etc/apache2/apache2.conf

<Directory /var/www/html>
AllowOverride All
</Directory>
# mkdir /etc/apache2/certificate
# cd /etc/apache2/certificate
# openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out apache-certificate.crt -keyout apache.key

<code> Generating a RSA private key …………++++ ……………………………………………….++++ writing new private key to 'apache.key'


You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank.


Country Name (2 letter code) [AU]:BR State or Province Name (full name) [Some-State]:Rio de Janeiro Locality Name (eg, city) []: Organization Name (eg, company) [Internet Widgits Pty Ltd]:TechExpert Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []:200.200.200.200 Email Address []: </cli>

# vi /etc/apache2/sites-enabled/000-default.conf

Here is the file, before our configuration.

<VirtualHost *:80>
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/html
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Here is the file, after our configuration.

<VirtualHost *:443>
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/html
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
      SSLEngine on
      SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
      SSLCertificateKeyFile /etc/apache2/certificate/apache.key
</VirtualHost>

Optionally, you may want to redirect HTTP users to the HTTPS version of your website.

<VirtualHost *:80>
      RewriteEngine On
      RewriteCond %{HTTPS} !=on
      RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</virtualhost>
<VirtualHost *:443>
      ServerAdmin webmaster@localhost
      DocumentRoot /var/www/html
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
      SSLEngine on
      SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
      SSLCertificateKeyFile /etc/apache2/certificate/apache.key
</VirtualHost>

Restart the Apache service

service apache2 restart
linux/apache_https.1663965465.txt.gz · Last modified: 2022/09/23 22:37 by manu