Li Wei

apache2 wordpress建站并设置https访问

一、域名访问 wordpress (http://www.domain.com)

wordpress放到 /var/wwww/html/wordpress 目录下面

    <Directory /var/www/html/wordpress/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>
    DocumentRoot /var/www/html/wordpress/

update wp_options set option_value='http://www.domain.com' where option_name="home";

update wp_options set option_value='http://www.domain.com' where option_name="siteurl";

配置完后直接用域名访问wordpress站点,不需要再加上目录名称。

二、apache2 wordpress开启https

1. apache2 配置

首先 find /. -name ‘mod_ssl.so’ 全局搜索有没有

    LoadModule ssl_module /usr/lib/apache2/modules/mod_ssl.so
    Listen 80 443
    ServerName www.domain.com
    DocumentRoot /var/www/html/wordpress/

    #证书公钥配置
    SSLCertificateFile /etc/apache2/cert/mypublic.pem
    #证书私钥配置
    SSLCertificateKeyFile /etc/apache2/cert/myprivate.key
    #证书链配置,如果该属性开头有 '#'字符,请删除掉
    SSLCertificateChainFile /etc/apache2/cert/mychain.pem
    # 修改加密套件如下
    SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM
    SSLHonorCipherOrder on
2. wordpress 配置

进入wordpress安装目录 /var/www/html/wordpress/

注意重定向配置的顺序

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

        RewriteEngine On
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
        #将 https 访问强制重定向至 http,代码如下:
        #RewriteCond %{SERVER_PORT} !^80$
        #RewriteRule ^.*$ http://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

注意 $_SERVER['HTTPS'] = 'on';配置的顺序。

    $_SERVER['HTTPS'] = 'on';

    /** Absolute path to the WordPress directory. */
    if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

    /** Sets up WordPress vars and included files. */
    require_once(ABSPATH . 'wp-settings.php');

    /* 强制后台和登录使用 SSL */
    define('FORCE_SSL_LOGIN', true);
    define('FORCE_SSL_ADMIN', true);

update wp_options set option_value='https://www.domain.com' where option_name="home";

update wp_options set option_value='https://www.domain.com' where option_name="siteurl";

update wp_posts set post_content = replace(post_content, 'http://www.domain.com','https://www.domain.com');

3. 重启apache2

apache2重启命令 /etc/init.d/apache2 restart 或者 service apache2 restart


Share this: