LinMinquan's Blog

Experience technology to change life

apache2 ssl configulation subdomain

Environment: Ubuntu 16.04, Apache 2.4.18,

主域名已经配置好了 ssl ,想给一个二级域名配置 ssl ,证书从 sslforfree.com 那配置下载后,上传到 /etc/apache2/ssl ,在 /etc/apache2/sites-available 里复制主域名已经配置好的 conf 进行修改,见下图。

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin account@gl-inet.com
        ServerName docs.gl-inet.com 
        ServerAlias docs.gl-inet.com
        DocumentRoot /var/www/newdocs

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on

        SSLCertificateFile  /etc/apache2/ssl/docs_gl-inet_com_certificate.crt
        SSLCertificateKeyFile /etc/apache2/ssl/docs_gl-inet_com_private.key
        SSLCertificateChainFile /etc/apache2/ssl/docs_gl-inet_com_ca_bundle.crt
        
        DirectoryIndex index.php index.html
            <Directory />
                    Options FollowSymLinks
                    AllowOverride None
            </Directory>
            <Directory /var/www/gl-inet.com/public_html/docs/>
                    Options -ExecCGI
                    AllowOverride None
                    RemoveHandler .cgi .pl .py .php4 .pcgi4 .php .php3 .phtml .pcgi .php5 .pcgi5
                    RemoveType .cgi .pl .py .php4 .pcgi4 .php .php3 .phtml .pcgi .php5 .pcgi5
                    <Files *>
                            SetHandler none
                            SetHandler default-handler
                            Options -ExecCGI
                            RemoveHandler .cgi .pl .py .php4 .pcgi4 .php .php3 .phtml .pcgi .php5 .pcgi5
                    </Files>
            </Directory>

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

    </VirtualHost>
</IfModule>

我不太确定上面的三个 是否需要删除。

然后需要敲下面一个命令,它会在 /etc/apache2/sites-enabled 生成一个软链接(应该是软链接吧)

sudo a2ensite 002-ssl-docs.conf

然后重启一下 apache

sudo service apache2 reload

 

Force http to https

这个配置需要在另一个 conf 里配置,即对应二级域名的 80 的 conf 里配置,加多下面三行。

RewriteEngine on
RewriteCond  %{HTTPS} !=on
RewriteRule  ^(.*) https://%{SERVER_NAME}$1 [L,R]

完事的 config 见下面

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    ServerName docs.gl-inet.com
    DocumentRoot /var/www/newdocs
    DirectoryIndex index.html

    RewriteEngine on
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*) https://%{SERVER_NAME}$1 [L,R]

    <Directory /var/www/newdocs/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
        SetHandler default-handler
        Options -ExecCGI
        php_flag engine off
        RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
        <Files *>
            AllowOverride None
            SetHandler none
            SetHandler default-handler
            Options -ExecCGI
            php_flag engine off
            RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
        </Files>
#               Order allow,deny
#               allow from all
       </Directory>
  
  
       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn
  
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

 

 

 

 

 


Share