nginx使用ssl模块配置HTTPS支持

默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中。通常这个文件名类似libssl-dev。

生成证书

可以通过以下步骤生成一个简单的证书:
首先,进入你想创建证书和私钥的目录,例如:

  1. $ cd /usr/local/nginx/conf

创建服务器私钥,命令会让你输入一个口令:

  1. $ openssl genrsa -des3 -out server.key 1024

创建签名请求的证书(CSR):

  1. $ openssl req -new -key server.key -out server.csr

在加载SSL支持的Nginx并使用上述私钥时除去必须的口令:

  1. $ cp server.key server.key.org
  2. $ openssl rsa -in server.key.org -out server.key

配置nginx

最后标记证书使用上述私钥和CSR:

  1. $ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

修改Nginx配置文件,让其包含新标记的证书和私钥:

  1. server {
  2.     server_name YOUR_DOMAINNAME_HERE;
  3.     listen 443;
  4.     ssl on;
  5.     ssl_certificate /usr/local/nginx/conf/server.crt;
  6.     ssl_certificate_key /usr/local/nginx/conf/server.key;
  7. }

重启nginx。
这样就可以通过以下方式访问:
https://YOUR_DOMAINNAME_HERE
另外还可以加入如下代码实现80端口重定向到443

  1. server {
  2. listen 80;
  3. server_name ww.webres.wang;
  4. rewrite ^(.*) https://$server_name$1 permanent;
  5. }

 

Apache tomcat JRE安装配置

tomcat处理静态不如Apache,所以我们使用jk_mod模块达到apache处理静态内容,tomcat处理jsp文件的目的。

安装JRE

http://www.oracle.com下载JRE软件。

  1. wget http://download.oracle.com/otn-pub/java/jdk/7u2-b13/jre-7u2-linux-i586.tar.gz
  2. tar xzf jre-7u2-linux-i586.tar.gz
  3. mv jre1.7.0_02/ /usr/local/jre

tomcat安装

到http://tomcat.apache.org/下载tomcat

  1. wget http://labs.renren.com/apache-mirror/tomcat/tomcat-7/v7.0.23/bin/apache-tomcat-7.0.23.tar.gz
  2. tar xzf apache-tomcat-7.0.23.tar.gz
  3. mv apache-tomcat-7.0.23 /usr/local/tomcat

设置环境变量

  1. vi /etc/profile

在文件末尾增加:

  1. export JRE_HOME=/usr/local/jre

执行source命令使生效

  1. source /etc/profile

tomcat根目录设置

1.打开tomcat安装目录下面conf目录下的server.xml文件。
2.找到如下文字:

  1. <Host name="localhost" appBase="webapps"
  2.        unpackWARs="true" autoDeploy="true">

3.在上面找到的文字后面添加如下内容

  1. <Context path="" docBase="/usr/local/apache/htadocs" debug="0" reloadable="true" />

/usr/local/apache/htadocs即为根目录地址。

安装apache

  1. wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.2.22.tar.gz
  2. tar xzf httpd-2.2.22.tar.gz
  3. cd httpd-2.2.22
  4. ./configure –prefix=/usr/local/apache
  5. make && make install

安装jk_mod模块

  1. wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.33-src.tar.gz
  2. tar xzf tomcat-connectors-1.2.33-src.tar.gz
  3. cd tomcat-connectors-1.2.33-src/native
  4. ./configure –with-apxs=/usr/local/apache/bin/apxs
  5. make && make install

apache与tomcat连接配置

配置与tomcat连接主要有三个文件:
httpd.conf,workers.properties,uriworkermap.properties
1、httpd.conf

  1. # 加载 mod_jk 模块
  2. LoadModule jk_module modules/mod_jk.so
  3. #
  4. # 配置mod_jk模块
  5. #
  6. <IfModule jk_module>
  7. JkWorkersFile conf/workers.properties
  8. JkMountFile conf/uriworkermap.properties
  9. JkLogFile logs/mod_jk.log
  10. JkLogLevel warn
  11. </IfModule>

2、workers.properties

  1. #定义两个可用的worker wlb和jkstatus
  2. worker.list=wlb,jkstatus
  3.  
  4. #定义一个物理为ajp13w,类型ajp13(可以定义多个用作负载均衡)
  5. worker.ajp13w.type=ajp13
  6. worker.ajp13w.host=localhost
  7. worker.ajp13w.port=8009
  8.  
  9. #定义一个逻辑worker wlb,类型为lb,用来管理物理worker ajp13w
  10. worker.wlb.type=lb
  11. worker.wlb.balance_workers=ajp13w
  12.  
  13. #定义一个类型为status的worker jkstatus,用来监控 JK 本身
  14. worker.jkstatus.type=status

3、uriworkermap.properties

  1. #这文件作用是指定哪些url由worker wlb处理,哪些文件不允许wlb处理。
  2. /*=wlb
  3. /status=jkstatus
  4. /examples/*=wlb
  5. !/*.jpg=wlb
  6. !/*.png=wlb
  7. !/*.css=wlb
  8. !/*.js=wlb
  9. !/*.htm=wlb
  10. !/*.html=wlb
  11.  
  12. #/jkstatus由worker status处理,静态的由apache处理,剩下的由wlb处理。

启动程序

  1. /usr/local/apache/bin/apachectl -k start
  2. /usr/local/tomcat/bin/startup.sh

CentOS安装iRedMail web邮件服务器

中文名为“艾瑞得邮件”,由 rhms 项目 更名而来。是针对 Red Hat(R) Enterprise Linux 和 CentOS 设计的邮件服务器解决方案,是在操作系统安装好后使用的一套 shell 脚本,用于快速部署一套功能完善的邮件服务器解决方案。iredmail谷歌开源地址:http://code.google.com/p/iredmail/

iredmail系统要求

iredmail安装需要干净的系统环境,意思是服务器不存在与iredmail相关的组件,如apache,mysql,OpenLDAP,postfix,Dovecot,Amavisd等。否则会覆盖原来的配置文件,甚至导致iredmail安装失败。
为了能安装iredmail,你需要满足以下两个条件:
1、一个干净的系统,如RHEL,CentOS,Scientific Linux 系统。
2、不低于512MB的内存。

安装前的准备

1、设置一个完全合格的域名(FQDN)主机名
执行命令hostname -f以查看当前主机名:

  1. $ hostname -f
  2. demo.iredmail.org

在RHEL/CentOS/Scientific Linux系统,设置hostname通常是改以下两个文件
/etc/sysconfig/network 主机名设置。
/etc/hosts 主机名解析,需要放在第一位。
2、激活yum软件库以安装新软件包
CentOS系统软件库配置文件在/etc/yum.repos.d/目录下,后缀是.repo,打开这些文件,如果包含”enabled=1″,表示已经激活。
3、下载最新发布的iRedMail
到这里下载:http://code.google.com/p/iredmail/wiki/Download?tm=2
解压软件包:

  1. # cd /root/
  2. # tar xjf iRedMail-x.y.z.tar.bz2

开始安装iRedMail

如果服务器在中国,由于域名被墙,需要打开文件/root/iRedMail-x.y.z/pkgs/get_all.sh,查找iredmail.org替换为173.254.22.21。

  1. # cd /root/iRedMail-x.y.z/
  2. # bash iRedMail.sh

下面是安装截图:
1)欢迎安装界面.
Postfix
2)指定存储邮箱的路径,默认是/var/vmail/。
Postfix
3)选择存储帐号的后端,选择你熟悉的。之后你可以iRedAdmin管理帐号。
Postfix
4)如果选择的后端是OpenLDAP,接下来会问两个问题。
4.1)LDAP后缀
Postfix
4.2)LDAP密码
Postfix
5)设置mysql root密码
Postfix
6)设置你第一个邮箱域名
Postfix
7)设置邮箱管理地址
Postfix
8)设置邮箱后台管理密码
Postfix
9)设置邮箱用户密码
Postfix
10)选择可选组件安装
Postfix
11)设置默认语言
Postfix
回答完这些问题之后,安装程序问是否开始安装,输入y即可。

安装完成之后需要知道的重要事情

1、阅读/root/iRedMail-x.y.z/iRedMail.tips,它包含一些与iredmail相关的信息
2、为SPF设置dns记录http://code.google.com/p/iredmail/wiki/DNS_SPF
3、为DKIM设置DNS记录http://code.google.com/p/iredmail/wiki/DNS_DKIM

webmail和其它一些程序的入口

Webmail http://your_server/mail/ (or /webmail, /roundcube)
iRedAdmin (admin panel) httpS://your_server/iredadmin/
phpMyAdmin httpS://your_server/phpmyadmin/
phpLDAPadmin httpS://your_server/phpldapadmin/
Awstats httpS://your_server/awstats/awstats.pl?config=web (or ?config=smtp)

配置apache以fastcgi运行php

apache默认是用自带的mod_php模块运行php,现在我们介绍使用fastcgi来执行php脚本。先说下fastcgi的优点:
Fastcgi的优点:

  • 从稳定性上看, fastcgi是以独立的进程池运行来cgi,单独一个进程死掉,系统可以很轻易的丢弃,然后重新分 配新的进程来运行逻辑.
  • · 从安全性上看,Fastcgi支持分布式运算. fastcgi和宿主的server完全独立, fastcgi怎么down也不会把server搞垮.
  • · 从性能上看, fastcgi把动态逻辑的处理从server中分离出来, 大负荷的IO处理还是留给宿主server, 这样宿主server可以一心一意作IO,对于一个普通的动态网页来说, 逻辑处理可能只有一小部分, 大量的图片等静态
  • IO处理完全不需要逻辑程序的参与.
  • · 从扩展性上讲, fastcgi是一个中立的技术标准, 完全可以支持任何语言写的处理程序 (php,java,perl,ruby,c++,python…)
  • · 适用操作系统,可在任何平台上http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz使用
  • 安装apache

    1. wget http://apache.ziply.com//httpd/httpd-2.2.21.tar.gz
    2. tar xzf httpd-2.2.21.tar.gz
    3. cd httpd-2.2.21
    4. ./configure –prefix=/usr/local/apache
    5. make && make install

    安装fastcgi

    1. wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
    2. tar xzf mod_fastcgi-2.4.6.tar.gz
    3. cd mod_fastcgi-2.4.6
    4. cp Makefile.AP2 Makefile
    5. make top_dir=/usr/local/apache
    6. make top_dir=/usr/local/apache install

    完成之后编辑httpd.conf配置文件,加入fastcgi模块装载代码:

    1. LoadModule fastcgi_module modules/mod_fastcgi.so

    安装php5.2

    1. wget http://us2.php.net/get/php-5.2.17.tar.gz/from/am.php.net/mirror
    2. tar xzf php-5.2.17.tar.gz
    3. cd php-5.2.17
    4. ./configure –prefix=/usr/local/php –enable-fastcgi –disable-cli
    5. make && make install

    配置apache支持php

    编辑httpd.conf文件,加入如下代码:

    1. ### fastcgi ###
    2. ScriptAlias /fcgi-bin/ "/usr/local/php/bin/"
    3. AddHandler php-fastcgi .php
    4. Action php-fastcgi /fcgi-bin/php-cgi
    5. AddType application/x-httpd-php .php
    6.  
    7. <IfModule mod_fcgid.c>
    8.     AddHandler fcgid-script. .php .fcgi   ### 暂时只配置支持.php
    9.     IdleTimeout 300
    10.     ProcessLifeTime 1800
    11.     MaxProcessCount 100
    12.     DefaultMinClassProcessCount 3
    13.     DefaultMaxClassProcessCount 8
    14.     IPCConnectTimeout 15
    15.     IPCCommTimeout 300
    16.     MaxRequestsPerProcess 100
    17. </IfModule>
    18. ### fastcgi ###

    建立虚拟主机可以这样配置:

    1. <VirtualHost *:80>
    2.  DocumentRoot /usr/local/apache/htdocs
    3.  ServerName localhost
    4.  Options +ExecCGI
    5.  AddHandler fastcgi-script .fcgi
    6.  AddType application/x-httpd-php .php
    7.  Action application/x-httpd-php /fcgi-bin/php-cgi
    8.  <Directory /usr/local/apache/htdocs>
    9.  Options Indexes ExecCGI
    10.  Order allow,deny
    11.  allow from all
    12.  </Directory>
    13. </VirtualHost>

    详细的fastcgi指令配置请看:http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html

    隐藏nginx版本号

    隐藏nginx版本号是为了防止黑客利用特定版本出现的漏洞攻击,现在介绍方法,主要修改两个地方。
    1、修改nginx.conf文件,在httpd区域中加入server_tokens off;如:

    1. http {
    2. ……省略
    3. sendfile on;
    4. tcp_nopush on;
    5. keepalive_timeout 60;
    6. tcp_nodelay on;
    7. server_tokens off;
    8. …….省略
    9. }

    2、修改/usr/local/nginx/conf/下的php-fpm配置文件。

    1. 找到:
    2. fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
    3. 改为:
    4. fastcgi_param SERVER_SOFTWARE nginx;

    最后重载nginx即可。

    解决nginx跨站浏览问题

    解决nginx跨站浏览有两种方法:
    1、为每一个网站定义一个pool,然后使用chroot指令,但这种方法的缺点是,当你需要建立很多虚拟主机时,消耗的内存是非常大的。
    2、修改php源代码。这种通过修改php源代码的方法可以很好的解决这个问题而不必像第一种额外的内存消耗。
    下面开始介绍第二种方法。
    下载php源码,执行./configure命令,然后开始修改main/fopen_wrappers.c文件

    1. /* {{{ php_check_open_basedir
    2. */
    3. PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
    4. {
    5.         /* Only check when open_basedir is available */
    6.         if (PG(open_basedir) && *PG(open_basedir)) {
    7.                 char *pathbuf;
    8.                 char *ptr;
    9.                 char *end;
    10.                 // add by anxsoft.com
    11.                 char *env_doc_root;
    12.                 if(PG(doc_root)){
    13.                         env_doc_root = estrdup(PG(doc_root));
    14.                 }else{
    15.                         env_doc_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
    16.                 }
    17.                 if(env_doc_root){
    18.                         int        res_root = php_check_specific_open_basedir(env_doc_root, path TSRMLS_CC);
    19.                         efree(env_doc_root);
    20.                         if (res_root == 0) {
    21.                                 return 0;
    22.                         }
    23.                         if (res_root == -2) {
    24.                                 errno = EPERM;
    25.                                 return -1;
    26.                         }
    27.                 }
    28.                 // add by anxsoft.com
    29.  
    30.  
    31.                 pathbuf = estrdup(PG(open_basedir));
    32.  
    33.                 ptr = pathbuf;
    34.  
    35.                 while (ptr && *ptr) {
    36.                         end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
    37.                         if (end != NULL) {
    38.                                 *end = ”;
    39.                                 end++;
    40.                         }
    41.  
    42.                         if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
    43.                                 efree(pathbuf);
    44.                                 return 0;
    45.                         }
    46.  
    47.                         ptr = end;
    48.                 }
    49.                 if (warn) {
    50.                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));
    51.                 }
    52.                 efree(pathbuf);
    53.                 errno = EPERM; /* we deny permission to open it */
    54.                 return -1;
    55.         }
    56.  
    57.         /* Nothing to check… */
    58.         return 0;
    59. }
    60. /* }}} */

    两个 add by anxsoft.com 中间的是修改加上去的,然后保存,退出。之后make 和make install 即可。
    然后修改php.ini文件:

    1. open_basedir = "/tmp/:/var/tmp/"

    经测试php5.2和php5.3同样适用。
    参考:http://www.hostloc.com/thread-6546-1-1.html

    使用logrotate轮询nginx和apache日志

    使用logrotate轮询日志很方便,配置也很简单。

    配置nginx

    1、建立/etc/logrotate.d/nginx文件

    1. vi /etc/logrotate.d/nginx

    2、写入如下内容:

    1. /var/log/nginx/*log {
    2.     daily
    3.     rotate 10
    4.     missingok
    5.     notifempty
    6.     compress
    7.     sharedscripts
    8.     postrotate
    9.         [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
    10.     endscript
    11. }

    注释:
    /var/log/nginx/*log:需要轮询日志路径
    daily:每天轮询
    rotate 10:保留最多10次滚动的日志
    missingok:如果日志丢失,不报错继续滚动下一个日志
    notifempty:当日志为空时不进行滚动
    compress:旧日志默认用gzip压缩
    /var/run/nginx.pid:nginx主进程pid

    配置apache

    1. /var/log/httpd/*log {
    2.     missingok
    3.     notifempty
    4.     sharedscripts
    5.     postrotate
    6.         /sbin/service httpd reload > /dev/null 2>/dev/null || true
    7.     endscript
    8. }

    平滑升级nginx

    这篇文章主要介绍如何在不停止旧nginx的基础上升级nginx。
    1、到http://nginx.org/en/download.html下载最新稳定版本的nginx。
    2、备份nginx二进制文件和配置文件

    1. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
    2. cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.old

    3、执行./configure命令,如

    1. ./configure –with-http_ssl_module –with-openssl=/path/to/openssl_src

    可以执行/usr/local/nginx/sbin/nginx -V命令查看以前的编译参数。
    4、执行make install clean来安装新的nginx。
    5、为旧的nginx主进程发送USR2信号,这会启动新的nginx主进程而保留旧的nginx主进程。

    1. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

    6、发送WINCH信号到旧的nginx主进程以杀掉旧的nginx子进程。假设旧的主进程pid是123。

    1. kill -WINCH 123

    7、现在你可以测试网站访问是否正常,如果正常,执行以下命令退出旧的nginx主进程;如果不正常,请看第8步。

    1. kill -QUIT 123

    8、如果发现nginx不能正常服务,假设新的nginx主进程pid是321,分别执行以下命令以旧nginx替代新nginx。

    1. kill -HUP 123 – 这会让旧的nginx主进程重新生成子进程继续服务
    2. kill -QUIT 321 – 关闭新的nginx进程
    3. kill -TERM 321 -强制退出nginx进程

    然后你就可以保证服务器继续上线来检查升级失败的原因。
    参考:http://www.softwareprojects.com/resources/programming/t-recompileupgrade-nginx-binary-with-no-down-time-1520.html

    解决CentOS 64位系统vsftpd 530 login incorrect的问题

    今天在centos 6 64位测试安装vsftpd时发现能正常启动,但用本地用户登录时,发现出现vsftpd 530 login incorrect,出现这种错误会有很多原因。于是开始查看日志/var/log/vsftpd.log,也没发现有价值的信息。再查看/var/log/secure,发现问题了。部分错误代码如下:

    1. Dec  1 08:07:30 localhost vsftpd: PAM adding faulty module: /lib/security/pam_listfile.so
    2. Dec  1 08:07:30 localhost vsftpd: PAM unable to dlopen(/lib/security/pam_unix.so): /lib/security/pam_unix.so: cannot open shared objec
    3. t file: No such file or directory
    4. Dec  1 08:07:30 localhost vsftpd: PAM adding faulty module: /lib/security/pam_unix.so
    5. Dec  1 08:07:30 localhost vsftpd: PAM unable to dlopen(/lib/security/pam_shells.so): /lib/security/pam_shells.so: cannot open shared o
    6. bject file: No such file or directory
    7. Dec  1 08:07:30 localhost vsftpd: PAM adding faulty module: /lib/security/pam_shells.so
    8. Dec  1 08:10:12 localhost vsftpd: PAM unable to dlopen(/lib/security/pam_listfile.so): /lib/security/pam_listfile.so: cannot open shar
    9. ed object file: No such file or directory
    10. Dec  1 08:10:12 localhost vsftpd: PAM adding faulty module: /lib/security/pam_listfile.so
    11. Dec  1 08:10:12 localhost vsftpd: PAM unable to dlopen(/lib/security/pam_unix.so): /lib/security/pam_unix.so: cannot open shared objec
    12. t file: No such file or directory
    13. Dec  1 08:10:12 localhost vsftpd: PAM adding faulty module: /lib/security/pam_unix.so
    14. Dec  1 08:10:12 localhost vsftpd: PAM unable to dlopen(/lib/security/pam_shells.so): /lib/security/pam_shells.so: cannot open shared o
    15. bject file: No such file or directory
    16. Dec  1 08:10:12 localhost vsftpd: PAM adding faulty module: /lib/security/pam_shells.so

    错误的意思是相关的安全验证文件找不到,通过find查找发现都在/lib64目录下,于是打开文件/etc/pam.d/vsftpd把/lib全部替换成/lib64,重启vsftpd,再登录就正常了。

    使用apache2-mpm-itk以不同用户运行apache虚拟主机

    我们运行在apache的虚拟主机,有时候为了安全,需要为每个网站以不同的用户运行,这样当虚拟主机中的一个网站被入侵时,而不会影响到其它的虚拟主机。我们这里使用apache2-mpm-itk补丁来实现这种功能。

    1、下载apache2和itk

    到apache2-mpm-itk网站下载最新版的补丁http://mpm-itk.sesse.net/

    1. wget  http://apache.ziply.com//httpd/httpd-2.2.21.tar.gz
    2. tar xzvf httpd-2.2.21.tar.gz
    3. wget http://mpm-itk.sesse.net/apache2.2-mpm-itk-2.2.17-01.patch

    2、为apache2打上mpm-itk补丁

    1. cd httpd-2.2.21
    2. patch -p1 < ../apache2.2-mpm-itk-2.2.17-01.patch
    3. autoconf

    3、编译apache2

    编译apache2时,带上编译选项–with-mpm=itk,如:

    1. ./configure –with-mpm=itk
    2. make
    3. make install

    4、配置apache2

    1. <VirtualHost *:80>
    2. ServerName www.example.com
    3.  
    4. <IfModule mpm_itk_module>
    5. AssignUserId siteuser sitegroup
    6. </IfModule>
    7. </VirtualHost>

    sitesuer是指apache以siteuser用户运行
    sitegroup是指apache以sitegroup用户组运行