SIDEBAR
»
S
I
D
E
B
A
R
«
LNAMP 架构下配置 WordPress 登录和管理控制台使用 SSL
八 9th, 2011 by Neal Mi

 

服务器迁移之后,突然想起之前在 NameCheapNamecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting 买域名的时候顺便买了一年的 SSL,于是决定花些时间配置到 WordPress 里,因为 SSL的速度比较慢,所以只有在登录,注册和管理控制台才使用 SSL。下面记录下我配置的过程:

准备证书

因为Nginx的SSL模块使用的是 OpenSSL,所以要通过 OpenSSL 来生成公钥和私钥。

在终端(Terminal)里执行如下命令:

openssl req -new -newkey rsa:2048 -nodes -out www_nealmi_com.csr -keyout www_nealmi_com.key -subj "/C=cn/ST=Beijing/L=Beijing/O=SlientWalker Inc/OU=IT/CN=www.nealmi.com"

如果你觉得看着头大,可以使用在线生成工具来生成命令(https://www.digicert.com/easy-csr/openssl.htm

然后复制到终端(Terminal)里去执行。
执行之后会生成一个公钥(*.csr)和一个私钥(*.key)

接下来到 NameCheap 的网站激活 SSL,  依次选菜单 Manage Domains  ->  SSL Certificates(左侧)- >  Activate Now(每一个SSL产品后面) 就到了下面这个页面了。

打开公钥,复制里面的内容到上图的csr框里,Web Server 选择other (因为我使用 Nginx)。下一步之后,选择邮箱激活。之后你会收到一封授权邮件(基本上和网站的Email账户激活流程差不多),打开email里的链接,输入email里的激活码。之后不久你就会收到一封带有附件的邮件。里面有四个文件,其中一个是你域名的证书(www_xxx_com.crt), 另外三个是Root证书和中级证书。

证书已经准备完成,开始配置服务器。

Nginx SSL 配置

因为 Nginx 不像Apache那样可以单独指定你的证书和认证链证书(Root和中级),所以你要合并他们(*.crt)的内容到一个文件里(*.csr)。
文件内容必须为如下顺序:(你的证书-中级证书2-中级证书1-Root证书)
# Your SSL Certificate – www_xxx_com.crt
# Intermediate CA Certificate 2 – PositiveSSLCA.crt
# Intermediate CA Certificate 1 – UTNAddTrustSGCCA.crt
# Root CA Certificate – AddTrustExternalCARoot.crt

你可以直接使用文本编辑器合并,也可以使用cat命令

cat www_xxx_com.crt PositiveSSLCA.crt UTNAddTrustSGCCA.crt AddTrustExternalCARoot.crt > www_xxx_com_chain.csr

为了避免出错,建议你下载合并好的(https://support.comodo.com/index.php?_m=downloads&_a=viewdownload&downloaditemid=31&nav=0,1,4)认证链证书(Root和中级),然后简单和你自己的证书合并即可。

cat www_xxx_com.crt PositiveSSL.ca-bundle > www_xxx_com_chain.csr

 

终于到Nginx的SSL配置了,路途坎坷啊。

server {
listen 80;
listen 443 default_server ssl;#这个是 Nginx 0.7.14 之后的写法
# 路径根据你的实际情况修改
ssl_certificate /webserver/nginx/conf/www_nealmi_com_chain.csr;
ssl_certificate_key /webserver/nginx/conf/www_nealmi_com.key;
#下面注释参数根据你自己的需要设置,一般不设置也正常运行。
#keepalive_timeout 70;
#ssl_session_cache shared:SSL:10m;
#ssl_session_timeout 10m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
server_name www.nealmi.com;
index index.html index.htm index.php;
root /wwwroot/wordpress;
# 省略其他配置....
}

这不是结束,远远不是结束。

路漫漫其修远兮,吾将上下而求索。

WordPress相关的配置:

根据官方的介绍:http://codex.wordpress.org/Administration_Over_SSL,Wordpress本身需要修改 wp-setting.php:

require_once(ABSPATH . 'wp-settings.php');

之前,加入

define('FORCE_SSL_ADMIN', true);  // 管理控制台和登录都使用SSL,我使用这种。

或者

define('FORCE_SSL_LOGIN', true); // 仅登录使用SSL
另外,为了一些插件能正常识别https,需要将 WordPress 地址(URL)修改为 https://www.nealmi.com, 站点地址(URL)不变(还是:http://www.nealmi.com)

官方还介绍了一些Apache的配置方法,只是因为我使用的是 Nginx + Apache 事情就稍稍复杂了一些。

在之前的文章我介绍了如何让 Nginx 和 Apache 使用相同的端口 :迁移网站到阿里社区云全过程

还是沿用这个方案:

Nginx 监听公网IP的 80 和 443 端口

Apache 监听内网(本地)的 80 和 443 端口

修改后的Nginx配置

server {
listen 公网IP:80;
listen 公网IP:443 default_server ssl;#这个是 Nginx 0.7.14 之后的写法
# 路径根据你的实际情况修改
ssl_certificate /webserver/nginx/conf/www_nealmi_com_chain.csr;
ssl_certificate_key /webserver/nginx/conf/www_nealmi_com.key;
#下面注释参数根据你自己的需要设置,一般不设置也正常运行。
#keepalive_timeout 70;
#ssl_session_cache shared:SSL:10m;
#ssl_session_timeout 10m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
server_name www.nealmi.com;
index index.html index.htm index.php;
root /wwwroot/wordpress;
location / {
try_files $uri /index.php ;
}
#所有 php 请求由Apache处理
location ~ .*\\.(php)$ {
proxy_pass $scheme://127.0.0.1:$server_port; # 使用内置变量转发80请求到Apache的80,443到Apache的443
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#静态文件由Nginx处理
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
}

修改后Apache的配置:

Listen 127.0.0.1:80
Listen 127.0.0.1:443
#...省略无关内容
NameVirtualHost 127.0.0.1:80
NameVirtualHost 127.0.0.1:443
#...省略无关内容
ServerAdmin admin@admin.com
ServerName www.nealmi.com
DocumentRoot /wwwroot/main_site
SSLEngine on
SSLCertificateFile /server/nginx/conf/secure_nealmi_com.crt
SSLCertificateKeyFile /server/nginx/conf/secure_nealmi_com.key
SSLCertificateChainFile /server/nginx/conf/PositiveSSL.ca-bundle
ErrorDocument 404 /404.php
DirectoryIndex index.html index.php index.htm
Options +Includes
AllowOverride FileInfo
Order allow,deny
Allow from all
ServerAdmin admin@admin.com
DocumentRoot //wwwroot/main_site
ServerName www.nealmi.com
ErrorDocument 404 /404.php
DirectoryIndex index.html index.php index.htm
Options +Includes
AllowOverride FileInfo
Order allow,deny
Allow from all

目前为止,Nginx 和 Apache 已经配置好了,WordPress也已经修改完成。胜利在望啦!!

接下来要做的就是写RewriteRules,目的就是
1.当在 80 端口访问 admin 和 login 的时候,重写到 443 端口处理(http://www.nealmi.com/wp-admin -> https://www.nealmi.com/wp-admin)
2.当在 443 端口访问 非 admin 和 login 的时候, 重写到 80 端口处理 (https://www.nealmi.com/2011/08/ -> http://www.nealmi.com/2011/08/)

在wordpress根目录下的 .htaccess(没有就新建一个)里添加如下内容(网址根据你的实际情况修改):

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^https://www.nealmi.com$ http://www.nealmi.com [L]
RewriteRule ^https://nealmi.com$ http://www.nealmi.com [L]
# For a site running on port 443 or else (http over ssl)
# 供 443 端口上使用, 对应上文描述的 RewriteRules 目的 2
RewriteCond %{SERVER_PORT} !^80$
RewriteRule !^wp-(admin|login|register)(.*) - [C]
RewriteRule ^(.*)$ http://www.nealmi.com/$1 [L]
# For a site running on port 80 (http)
# 供 80 端口上使用,对应上文描述的 RewriteRules 目的 1
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^wp-(admin|login|register)(.*) https://www.nealmi.com/wp-$1$2 [L] #关键步骤
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

花了我近一天时间,反复的调试,修改,终于完成所有的配置。你可以试试我网站的登录。

迁移网站到阿里社区云全过程
八 5th, 2011 by Neal Mi

前两天收到了ServerPronto的服务通知,大致意思是说服务费要涨到90美元一个月(我当时是购买的超值促销的服务器25美元一个月)。涨价之后基本就没啥性价比了,只好准备换一个服务商。

经过一番考察,决定把迁移到阿里云的社区云服务(http://phpwind.aliyun.com/show/ecc)。最近开始促销,每月199人民币,1990一年,性价比非常不错。

购买不赘述,一路默认下一步即可。付款之后,约15分钟左右服务即可以使用。在管理控制台(http://phpwind.aliyun.com/product/vm_manage/)管理该服务,既可以看到关于服务器的详细信息(配置,IP,默认软件的安装位置等等),其实这就是一台Linux服务器(当然你也可以选择Windows系统)。

我默认安装的PHPwind,倒不因为我要建立一个论坛,我只是让他给我安装好默认的软件(Apache,PHP,Mysql),偷个小懒。

因为我之前使用的Nginx,所以还要手动安装一个Nginx。

安装Nginx:

SSH登录到服务器,下载最新版Nginx(http://nginx.org/en/download.html), 因为默认服务器没有安装PCRE,所以需要自己下载安装(ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/)。

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
tar xzvf pcre-8.12.tar.gz
wget http://nginx.org/download/nginx-1.1.0.tar.gz
tar xzvf /nginx-1.1.0.tar.gz
cd nginx-1.10
#注意参数 --with-pcre=../pcre-8.12
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-pcre=../pcre-8.12
make && make install

环境已经准备就绪,即所谓的(LNAMP架构)Linux + Nginx+ Apache + Mysql + PHP 。接下来开始迁移工作。

Nginx  作为前端的服务器,并且处理静态内容

Apache 处理动态内容(PHP)

Mysql 数据库

网站程序迁移:

这个就比较简单,直接把WordPress打包,拷贝到新的服务器。下述仅作描述流程所用,相关路径根据你的实际情况做修改。

#打包
tar czvf main-site.tar.gz main-site
#复制到新的服务器,这里建议用服务器直接对服务器,网速超快(主干网就是不一般啊!木哈哈),当然那你也可以下载-上传
scp main-site.tar.gz  username@host:~/wwwroot/
#ssh登录到新服务器,解压
cd wwwroot
tar xzvf  main-site.tar.gz

数据库迁移:

直接用mysqldump 导出数据库(http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html)。然后再导入到新服务器的数据库即可,建一个和原来服务器上相同的用户,最后密码也相同,这样省得去修改wordpress。

#导出数据库
mysqldump --opt -u root -p mainsite > exportDB.sql
#复制到新的服务器
scp  exportDB.sql  username@host:~/wwwroot/
#SSH登录到新服务器
cd wwwroot
#导入数据库
mysql -u root -p  main_site <  exportDB.sql

目前为止,环境,网站程序,数据库都齐了,可以开始最烦人的部分了,配置!!!

因为我们有两个webServer,都用80的端口,这个怎么办呢?端口冲突。仔细分析,Nginx对外提供服务,Apache仅对内提供服务,这个微妙的区别。解决办法:Nginx绑定到外网IP的80端口,Apache绑定内网(多台服务器)或本机(一台服务器)的80:端口。

配置Apache和Nginx:

Apache:

Listen 127.0.0.1:80
#...忽略默认内容
NameVirtualHost *:80
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" phpwind
CustomLog "|/webserver/apache2.2.15/bin/rotatelogs /logs/apache/%Y-%m-%d-9qc_log.log 100M" phpwind
ErrorLog "/logs/apache/apacheerror_log"
<VirtualHost *:80>
    ServerAdmin admin@admin.com
    DocumentRoot /wwwroot/main_site
    ServerName www.nealmi.com
    ServerAlias main
    ErrorDocument 404 /404.php
    DirectoryIndex index.html index.php index.htm
   <Directory "/wwwroot/main_site">
     Options +Includes
     AllowOverride FileInfo # Fancy Link 使用
     Order allow,deny
     Allow from all
   </Directory>
</VirtualHost>

 

Nginx:

#rewrite xx.com 成 www.xx.com
server {
listen       110.xx.xx.xx:80; # 外网IP
server_name  nealmi.com;
rewrite   ^  http://www.nealmi.com$request_uri? permanent;
}
server {
listen       110.xx.xx.xxx:80; # 外网IP
server_name  www.nealmi.com;
index index.html index.htm index.php;
root /wwwroot/main_site;
#Fancy Link必须设置这个
location / {
        try_files $uri /index.php;
}
#转发php请求到apache
location ~ .*\\.(php)$ {
    proxy_pass         http://127.0.0.1:80;
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    client_max_body_size       10m;
    client_body_buffer_size    128k;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}
location /status {
    stub_status on;
    access_log   off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
    expires      30d;
}
location ~ .*\.(js|css)?$ {
    expires      12h;
}
log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
access_log  /logs/nginx/access.log  access;
}

配置完成,接下来,将域名的记录指向到新服务的IP,大功告成!

这篇文章就是我在迁移到新服务器之后写的。

我使用的一些 WordPress 的插件
三 11th, 2011 by Neal Mi

WordPress 的众多第三方插件是它流行的原因之一,下面列出一些我使用的插件。

Add Logo to Admin - 一款可以在 WordPress 的管理界面 和 登录页面添加自定义 Logo的插件。

All in One SEO PackSEO(搜索引擎优化)插件。

Google XML Sitemaps自动生成 Google SiteMaps 的插件。

FancyBox for WordPress这个没啥了,就是和JQuery ColorBox一个东西。

Maintenance Mode可以把WordPress设置成维护模式的插件。

TPC! Memory Usage - 查看内存使用情况和系统信息。

WordPress Database Backup – 备份数据库。

WordPress连接微博 – 支持使用微博帐号登录 WordPress 博客,并且支持同步文章的标题和链接 到各大微博和社区。

WP Kit CN - 用来解决官方 WordPress 没有照顾到的中文相关问题。使用这个插件,你可以显示随机文章,最新留言(最新引用),留言最多文章,发表评论最多的网友,还有真正的文章摘要,等等,真正截断,没有乱码。此插件在桑葚基础上修改,支持最新评论显示表情。支持指定最新评论截断字数。使用了对中文兼容更好的截断算法,现在控制字数更精确了

WP Minify合并  js, css 文件,加快页面载入速度。

WP Smush.it自动优化图片大小,加快页面载入速度。

WP Super Cache - 缓存插件,加速页面打开速度。

WPtouch - 对于手机(Apple iPhoneiPod touchGoogle Android,Blackberry Storm and TorchPalm Pre等高端设备)用户,自动启用优化后的移动主题。

 

 

 

 

WordPress 时间问题
二 27th, 2011 by Neal Mi

昨天突然发现 WordPress 的时间不正确,在设置页面查看,时区设置没有错,对比自己电脑的 UTC 时间后,发现 UTC 时间不正确。估计是因为服务器时间不对。

使用 Putty 登录到服务器,在terminal里:

首先,执行如下,将输出当前服务器的 UTC 时间,

# date -u
Sun Feb 27 05:47:44 UTC 2011

我发现确实是因为服务器的 UTC 时间不正确,接下来就是和时间服务器同步了。
执行 rdate 命令与时间服务器同步,之后再检查 UTC 时间。

# rdate -s nist1.aol-va.truetime.com
# date -u
Sun Feb 27 15:47:44 UTC 2011

再次打开 WordPress 时区设置页面,查看时间,时间显示已经正确。

SIDEBAR
»
S
I
D
E
B
A
R
«
»  Substance:WordPress   »  Style:Ahren Ahimsa
© 沉默前行