专注于WEB前端开发, 追求更好的用户体验, 更好的开发体验 [长沙前端QQ群:234746733]

2019年4月

  • Mac自签名ssl证书, 绑定域名启用https

    / 分类: 工具,实践 / No Comments

    生成root CA(仅需一次)

    自签名证书, IOS 13/macOS 10.15有了新规定, 所以需要按如下方式生成.

    # 多个域名都可以用root CA签名
    mkdir ~/.ssl; cd ~/.ssl;
    openssl genrsa -out rootca.key 2048;
    openssl req -new -key rootca.key -out roo
    
    
    ----------
    
    
    tca.csr -sha256 -subj "/CN=Dev Root CA"
    echo "basicConstraints=CA:true" > rootca.cnf;
    openssl x509 -req -in rootca.csr -signkey rootca.key -out rootca.crt -extfile rootca.cnf -sha256 -days 825;
    open rootca.crt; # Mac钥匙串 打开(或, 双击文件名)
    # 钥匙串: 搜索 Dev Root CA, 双击证书, 设置始终信任 (Trust - When using this certificate: Always Trust).
    # for Android:
    # openssl x509 -inform PEM -outform DER -in rootca.crt -out rootca.der.crt

    创建本地ssl证书(使用lvh.me域名)

    openssl req -new -newkey rsa:2048 -nodes -keyout lvh.me.key -out lvh.me.csr -sha256 -subj "/CN=lvh.me"
    cat > domain.cnf <<-EOF
    authorityKeyIdentifier=keyid,issuer
    basicConstraints=CA:false
    keyUsage=digitalSignature,keyEncipherment,dataEncipherment
    extendedKeyUsage=serverAuth,clientAuth
    subjectAltName=@alt_names
    [alt_names]
    DNS.1=lvh.me
    DNS.2=*.lvh.me
    EOF
    # 使用root CA签名
    openssl x509 -req -in lvh.me.csr -out lvh.me.crt -extfile domain.cnf -CA rootca.crt -CAkey rootca.key -CAcreateserial -sha256 -days 825

    lvh.me 是一个指向127.0.0.1的泛域名服务, 优点: 不需要改DNS/hosts, 各项目使用不同的域名方便隔离(比如cookies/密码管理器). 类似的还有xip.io, nip.io, localtest.me, fuf.me, vcap.me等.

    使用Apache或Nginx配置域名和虚拟机

    本地域名绑定 apache

    # 1. 编辑 `/etc/apache2/httpd.conf`, 添加行:
    ServerName localhost
    # 取消这些行的注释:
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    LoadModule proxy_module libexec/apache2/mod_proxy.so
    LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
    LoadModule macro_module libexec/apache2/mod_macro.so
    LoadModule ssl_module libexec/apache2/mod_ssl.so
    Include /private/etc/apache2/extra/httpd-ssl.conf
    LoadModule socache_shmcb_module libexec/apache2/mod_socache_shmcb.so
    LoadModule http2_module libexec/apache2/mod_http2.so
    
    # 2. 编辑 `/private/etc/apache2/extra/httpd-vhosts.conf`, 增加:
    <Macro SSLLvh>
        SSLEngine on
        SSLCertificateFile /Users/leon/.ssl/lvh.me.crt
        SSLCertificateKeyFile /Users/leon/.ssl/lvh.me.key
        Protocols h2 http/1.1
    </Macro>
    <Macro Site>
        ServerName leon.lvh.me
        ProxyRequests off
        <Location />
            ProxyPass http://localhost:3000/
            ProxyPassReverse http://localhost:3000/
        </Location>
    </Macro>
    <VirtualHost *:80>
        Use Site
    </VirtualHost>
    <VirtualHost *:443>
       Use SSLLvh
       Use Site
    </VirtualHost>
    # 3. 启动Apache
    # sudo apachectl configtest # test
    sudo apachectl -k restart # 启动
    # sudo apachectl -e debug # print errors

    查看全文 »