龚哥哥 - 山里男儿 爱生活、做自己!
PHP单例模式 设计模式 面向对象
发表于 2015-8-31 | 浏览(5622) | PHP
<?php

/**
 * 单例模式
 * @author  Devil
 * @version v_0.0.1
 */
class Singleton
{
    private $parem;

    /**
     * [__construct 构造方法]
     * @param [mixed] $param [参数]
     */
    private function __construct($param)
    {
        $this->param = $param;
    }

    /**
     * [Instantiate 静态方法, 用于实例化类]
     * @param  [mixed] $param [参数]
     * @return [object]   [类对象]
     */
    public static function Instantiate($param)
    {
        static $object = null;
        if(!is_object($object)) $object = new self($param);
        return $object;
    }

    /**
     * [Show 测试方法]
     */
    public function Show()
    {
        print_r($this->param);
    }
}

/**
 * 使用列子
 */
$param = array('test', 'demo', 'devil');
$obj = Singleton::Instantiate($param);
$obj->Show();

/**
 * $obj = Singleton::Instantiate($param);
 * 不管在项目中多少次这么对类进行实例化,都不会重复创建类对象。
 * $object 被定义成静态变量,不能被第二次赋值。
 * 只要$object是一个对象就直接返回当前对象,则进行实例化并返回。
 *
 * 单例模式可以防止重复创建对象,减轻内存开销。
 */

?>

阅读全文

ssh免密码登录远程Linux服务器
发表于 2015-8-31 | 浏览(4893) | 服务器

环境说明

客户机:Mac OS X
服务器:CentOS 6.5
客户端:OpenSSH,OS X及大多数Linux都内置了OpenSSH.’ssh -v’命令可以查看版本.

大致流程

1.在客户机创建一对密钥文件,包括公钥文件(~/.ssh/id_rsa.pub),私钥文件(~/.ssh/id_rsa).

2.把公钥放到服务器上(~/.ssh/authorized_keys),在使用ssh登录时,ssh程序会发送私钥去和服务器上的公钥做匹配。如果匹配成功就可以自动登录了。

客户机配置

1.查看~/.ssh文件夹,若已经存在有公钥文件(id_rsa.pub),私钥文件(id_rsa),则可以跳过客户端配置.

2.生成密钥文件.
$ ssh-keygen 
然后一路回车.
然后~/.ssh下会生成id_rsa.pub和id_rsa, 其中id_rsa文件起到唯一标识你的客户机的作用.
注意:不要改这两个文件的文件名,ssh登陆时会读取id_rsa文件.

服务器配置  使用root身份

1.修改sshd配置文件(/etc/ssh/sshd_config).
找到以下内容,并去掉注释符”#“
=========================
  RSAAuthentication yes
  PubkeyAuthentication yes
  AuthorizedKeysFile  .ssh/authorized_keys
=========================

2.配置authorized_keys文件。
注意如果不是root用户,这里操作需要切换免密码登录的用户。如:su test
若’~/.ssh/authorized_keys’不存在,则建立.ssh文件夹和authorized_keys文件.
将上文中客户机id_rsa.pub的内容拷贝到authorized_keys中.
PS:可以在客户机中执行命令来拷贝:
cat ~/.ssh/id_rsa.pub | ssh user@host “cat - >> ~/.ssh/authorized_keys”

注意:
1) .ssh目录的权限必须是700
2) .ssh/authorized_keys文件权限必须是600 

3.重启sshd.
$ /etc/init.d/sshd restart

测试

客户机执行:ssh -v user@host (-v 调试模式)
会显示一些登陆信息.
若登陆失败,或者仍然要输入密码,可以在服务器查看日志文件:/var/log/secure.

若登陆成功,则以后就可以用’ssh user@host’ 直接登陆了,不用输入密码.

ssh端口号修改
vim /etc/ssh/sshd_config
修改 Port 22
重启sshd即可
service sshd restart

阅读全文

ssh登录远程Linux服务器出现Host key verification failed.
发表于 2015-8-31 | 浏览(5338) | 服务器
gongfuxiangdeMacBook-Pro:~ qualifes$ ssh root@192.168.14.126
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
e4:cc:4a:c3:ce:0e:b3:14:80:5a:70:d0:4c:e2:a8:1e.
Please contact your system administrator.
Add correct host key in /Users/qualifes/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/qualifes/.ssh/known_hosts:6
RSA host key for 192.168.14.126 has changed and you have requested strict checking.
Host key verification failed.

这是Linux重装或openssh-server重装引起的,执行以下命令即可

ssh-keygen -R IP  //把IP换成你要连的服务器就可以了
如:ssh-keygen -R 192.168.14.126

阅读全文

PHP支付宝接口类 WEB版 即时到帐接口
发表于 2015-8-31 | 浏览(5760) | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version v_1.0.0
 */
class Alipay
{
    private $config;

    /**
     * [__construct 构造方法, 初始化配置信息]
     */
    public function __construct()
    {
        $this->config = array(
            'alipay_key'        =>  '', //key
            'alipay_partner'    =>  '', //partner
            'alipay_account'    =>  '', //支付宝账户名称
            'notify_url'        =>  '', //异步通知地址
            'call_back_url'     =>  '', //同步返回地址
        );
    }

    /**
     * [Payment 生成即时到帐支付信息]
     * @param  [array] $order [订单数据]
     */
    public function Payment($order)
    {
        $param = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $this->config['alipay_partner'],
            '_input_charset'    => 'utf-8',
            'notify_url'        => $this->config['notify_url'],
            'return_url'        => $this->config['call_back_url'],

            /* 业务参数 */
            'subject'           => $order['name'],
            'out_trade_no'      => $order['number_id'],
            'price'             => $order['total_fee'],
            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $this->config['alipay_account']
        );
        ksort($param);
        $string = '';
        $sign  = '';

        foreach($param AS $key=>$val)
        {
            $string .= "$key=" .urlencode($val). "&";
            $sign  .= "$key=$val&";
        }

        $string = substr($string, 0, -1);
        $sign  = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        header('location:https://mapi.alipay.com/gateway.do?'.$string. '&sign='.$sign.'&sign_type=MD5');
    }

    /**
     * [Respond 异步请求处理]
     * @return [string] [成功success, 失败其它]
     */
    public function Respond()
    {
        /* 参数处理 */
        if (!empty($_POST))
        {
            foreach($_POST as $key => $val)
            {
                $_GET[$key] = $val;
            }
        }
        $param = $_GET;
        ksort($param);

        /* 判断是否已经处理过 */
        $this->IsRespond($param);

        /* 签名校验 */
        $sign = '';
        foreach($param AS $key=>$val)
        {
            if($key != 'sign' && $key != 'sign_type' && $key != 'code')
            {
                $sign .= "$key=$val&";
            }
        }
        $sign = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        if($Sign == $param['sign'] && $param['trade_status'] == 'TRADE_SUCCESS')
        {
            //$param['out_trade_no'] 参数是唯一标识符
            exit('success');
        }
    }

    /**
     * [IsRespond 是否处理过操作]
     * @param [array] $param [参数]
     */
    private function IsRespond($param)
    {
        //$param['out_trade_no'] 参数是唯一标识符
        //如果处理过了可以直接exit('success');
    }
}

?>

阅读全文

Nginx常见配置文件参数设置 nginx.conf配置
发表于 2015-8-31 | 浏览(5738) | 服务器
虚拟机在同一个配置文件进行配置 nginx.conf

#运行用户
user www www;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#全局错误日志
error_log  /data/log/error.log;

#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    use epoll;

    #单个后台worker process进行的最大并发连接数
    worker_connections  65535;
}

http{
    #配置信息
    include mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型
    #charset utf-8; #默认编码
    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #上传文件大小限制
    large_client_header_buffers 4 64k; #设定请求缓
    client_max_body_size 8m; #设定请求缓
    sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2; #压缩等级
    #gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript image/jepg image/gif image/png;
    gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript;
    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

    #文件缓存
    open_file_cache max=65535 inactive=60s; #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
    open_file_cache_valid 80s; #这个是指多长时间检查一次缓存的有效信息。
    open_file_cache_min_uses 1; #参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

    #开启404
    fastcgi_intercept_errors on;

    # https
    server {
        listen443;
        server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
        if ( $host != 'www.gongfuxiang.com' ) {
            rewrite ^/(.*)$ https://www.gongfuxiang.com/$1 permanent;
        }

        access_log/data/log/access.log;
        error_log/data/log/error.log warn;
        indexindex.html index.htm index.php;
        root/data/www;
        autoindexoff;
        include/data/www/.htaccess;

        sslon;
        ssl_certificate/data/https_rsa/gongfuxiang.pem;
        ssl_certificate_key/data/https_rsa/gongfuxiang.key;
        ssl_session_timeout5m;

        #4000,500错误定义
        error_page  404 http://$server_name;
        #error_page  500 502 503 504  /50x.html;

        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        #JS和CSS缓存时间设置
        location ~ .*.(js|css)?$
        {
            expires 1h;
        }

        #所有静态文件由nginx直接读取不经过tomcat或resin
        location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
            expires 15d;
        }
     }

    # http
    server {
        listen80 default;
        server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
        if ( $host != 'www.qualifes.com' ) {
            rewrite ^/(.*)$ http://www.gongfuxiang.com/$1 permanent;
        }

        access_log/data/log/access.log;
        error_log/data/log/error.log warn;
        indexindex.html index.htm index.php;
        root/data/www;
        autoindexoff;
        include/data/www/.htaccess;
        #400,500错误定义
        error_page  404 http://$server_name;
        #error_page  500 502 503 504  /50x.html;

        #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        #JS和CSS缓存时间设置
        location ~ .*.(js|css)?$
        {
            expires 1h;
        }

        #所有静态文件由nginx直接读取不经过tomcat或resin
        location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {
            expires 15d;
        }
    }
}

阅读全文

Nginx配置多个虚拟机
发表于 2015-8-31 | 浏览(5616) | 服务器
虚拟机分开管理方式
nginx.conf 文件配置如下

#运行用户
user www www;

#启动进程,通常设置成和cpu的数量相等
worker_processes  1;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

#全局错误日志
error_log  /data/log/error.log;

#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    use epoll;

    #单个后台worker process进行的最大并发连接数
    worker_connections  65535;
}

http{
    #配置信息
    include mime.types; #文件扩展名与文件类型映射表
    default_type application/octet-stream; #默认文件类型

    #charset utf-8; #默认编码
    server_names_hash_bucket_size 128; #服务器名字的hash表大小
    client_header_buffer_size 32k; #上传文件大小限制
    large_client_header_buffers 4 64k; #设定请求缓
    client_max_body_size 8m; #设定请求缓
    sendfile on; #开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    autoindex on; #开启目录列表访问,合适下载服务器,默认关闭。
    tcp_nopush on; #防止网络阻塞
    tcp_nodelay on; #防止网络阻塞
    keepalive_timeout 120; #长连接超时时间,单位是秒

    #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #gzip模块设置
    gzip on; #开启gzip压缩输出
    gzip_min_length 1k; #最小压缩文件大小
    gzip_buffers 4 16k; #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2; #压缩等级
    #gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript image/jepg image/gif image/png;
    gzip_types text/plain text/css text/javascrip application/javascript application/x-javascript;
    #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
    gzip_vary on;
    #limit_zone crawler $binary_remote_addr 10m; #开启限制IP连接数的时候需要使用

    #文件缓存
    open_file_cache max=65535 inactive=60s; #这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
    open_file_cache_valid 80s; #这个是指多长时间检查一次缓存的有效信息。
    open_file_cache_min_uses 1; #参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

    #开启404
    fastcgi_intercept_errors on;
    #载入虚拟机配置文件, 载入vhost目录下的所有配置文件, 目录位与nginx.conf同级
    include vhost/*.conf;
}

vhost/demo.conf 文件配置如下
# https
server {
        listen443;
        server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
        if ( $host != 'www.gongfuxiang.com' ) {
        rewrite ^/(.*)$ https://www.gongfuxiang.com/$1 permanent;
    }
    access_log/data/log/access.log;
    error_log/data/log/error.log warn;
    indexindex.html index.htm index.php;
    root/data/www;
    autoindexoff;
    include/data/www/.htaccess;

    sslon;
    ssl_certificate/data/https_rsa/gongfuxiang.pem;
    ssl_certificate_key/data/https_rsa/gongfuxiang.key;
    ssl_session_timeout5m;

    #4000,500错误定义
    error_page  404 http://$server_name;
    #error_page  500 502 503 504  /50x.html;

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    #JS和CSS缓存时间设置
    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    #所有静态文件由nginx直接读取不经过tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

# http
server {
    listen80 default;
    server_namewww.gongfuxiang.com gongfuxiang.com *.gongfuxiang.com;
    if ( $host != 'www.qualifes.com' ) {
        rewrite ^/(.*)$ http://www.gongfuxiang.com/$1 permanent;
    }
    access_log/data/log/access.log;
    error_log/data/log/error.log warn;
    indexindex.html index.htm index.php;
    root/data/www;
    autoindexoff;
    include/data/www/.htaccess;
    #400,500错误定义
    error_page  404 http://$server_name;
    #error_page  500 502 503 504  /50x.html;

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    #JS和CSS缓存时间设置
    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    #所有静态文件由nginx直接读取不经过tomcat或resin
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

如果有新的虚拟机添加,则在vhost目录下拷贝一个虚拟机配置文件进行修改项目路径以及其它配置即可。

阅读全文

MySQL查询重复出现次数最多的记录
发表于 2015-8-31 | 浏览(6177) | 数据库

单表查询

SELECT DISTINCT COUNT(*) AS count, lid FROM user_label GROUP BY lid ORDER BY count DESC LIMIT 0, 10;

联表查询user_label 用户标签关联表(uid, lid), label 标签表(lid, name)

SELECT DISTINCT count(u.lid) AS count, u.lid, l.name FROM `label` AS l INNER JOIN `user_label` AS u ON u.lid=l.lid GROUP BY u.lid ORDER BY count DESC LIMIT 0, 10

阅读全文

CentOS_6.5安装GitLab_7.51
发表于 2015-8-31 | 浏览(5246) | 服务器

一、把所有包升级到最新版本+rpm包安装

yum -y upgrade

如果网速不好,可以在360云盘下载

http://yunpan.cn/cVfDfDSvuZUff (提取码:a6c7)

curl -O https://downloads-packages.s3.amazonaws.com/centos-6.6/gitlab-7.5.1_omnibus.5.2.0.ci-1.el6.x86_64.rpm
sudo yum install openssh-server
sudo yum install postfix
sudo yum install cronie
sudo service postfix start
sudo chkconfig postfix on
sudo rpm -ivh gitlab-7.5.1_omnibus.5.2.0.ci-1.el6.x86_64.rpm

二、修改配置文件 (sudo gitlab-ctl reconfigure前需要配置的)

vim /etc/gitlab/gitlab.rb
external_url 'hostname'  ->  将其修改为  -> external_url='hostname'
如:external_url = 'www.gongfuxiang.com'

三、重置配置和启动 GitLab

sudo gitlab-ctl reconfigure
sudo lokkit -s http -s ssh

四、修改访问域名和项目域名

1;web端(gitlab-rails)
vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
  ## GitLab settings
  gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: www.gongfuxiang.com
    port: 80
    https: false

2;git-shell的
vim /opt/gitlab/embedded/service/gitlab-shell/config.yml
gitlab_url: "http://www.gongfuxiang.com"

五、邮件配置(用户注册的时候使用)

vim /opt/gitlab/embedded/service/gitlab-rails/config/environments/production.rb

在  # config.action_mailer.delivery_method = :sendmail 下加入、# 表示注释。
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
  :address              => "smtp.163.com",
  :port                 => 587,
  :domain               => 'username@163.com',
  :user_name            => 'username@163.com',
  :password             => 'password',
  :authentication       =>  :plain,
  :enable_starttls_auto => true
}

修改显示的邮箱,打开以下文件,找到指定位置,修改即可
vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
# Email address used in the "From" field in mails sent by GitLab
 email_from: username@163.com
 # Email address of your support contact (default: same as email_from)
 support_email: username@163.com

六、gitlab服务开启+重启

gitlab-ctl stop停止
gitlab-ctl start 开启
gitlab-ctl restart重启

安装完成

访问 http://www.gongfuxiang.com

管理员: root

初始密码: 5iveL!fe

阅读全文

CentOS下使用Ngin反向代理配置服务器集群
发表于 2015-8-31 | 浏览(6566) | 服务器

目录结构

/data/soft/src       软件库
/data/soft/nginx    nginx安装目录
/data/www       项目根目录

1.安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /data/soft/src
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

2.安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /data/soft/nginx 目录下的详细步骤:

cd /data/soft/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/data/soft/nginx/nginx \--conf-path=/data/soft/nginx/nginx.conf \--pid-path=/data/soft/nginx/nginx.pid \--with-http_ssl_module \--with-pcre=/data/soft/src/pcre-8.35 --with-http_gzip_static_module --with-http_stub_status_module
make && make install

其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。

cp /data/soft/nginx/nginx /etc/init.d/
/etc/init.d/nginx               启动nginx
/etc/init.d/nginx -s reload     重启nginx
/etc/init.d/nginx -s stop       关闭nginx

添加nginx开机启动
echo "/etc/init.d/nginx" >> /etc/rc.local

3.配置nginx.conf    文件位置 /data/soft/nginx/nginx.conf

user  www;# 工作进程的属主
worker_processes  4;# 工作进程数,一般与 CPU 核数等同

#error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 

#pid        logs/nginx.pid; 

events { 
    use epoll;#Linux 下性能最好的 event 模式
    worker_connections  2048;# 每个工作进程允许最大的同时连接数
} 

http
{
    include       mime.types; 
    default_type  application/octet-stream; 

    #log_format  main  '$remote_addr - $remote_user [$time_local] $request ' 
    #                  '"$status" $body_bytes_sent "$http_referer" ' 
    #                  '"$http_user_agent" "$http_x_forwarded_for"'; 

    #access_log  off; 
    access_log  logs/access.log;# 日志文件名

    sendfile        on; 
    #tcp_nopush     on; 
    tcp_nodelay     on; 

    keepalive_timeout  65; 

    #include     gzip.conf; #gzip压缩配置

    # 集群中的所有后台服务器的配置信息
    upstream tomcats { 
        server 192.168.14.126:81 weight=10;
        server 192.168.14.126:82 weight=10;
        server 192.168.44.126:83 weight=10;
    }

    server { 
        listen       80;#HTTP 的端口
        server_name  localhost; 

        charset utf-8; 

        #access_log  logs/host.access.log  main; 

        location ~ ^/NginxStatus/ { 
            stub_status on; #Nginx 状态监控配置
            access_log off; 
        } 

        location ~ ^/(WEB-INF)/ { 
            deny all; 
        } 

        location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { 
            root /data/www;
            expires 24h; 
        } 

        location / { 
            proxy_pass http://tomcats; # 反向代理
            proxy_redirect off;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 

        error_page 404 /html/404.html; 

        error_page 502 503 /html/502.html; 
        error_page 500 504 /50x.html; 
        location = /50x.html{ 
            root   html; 
        }
    }

    #引入虚拟机
    include vhost/*.conf;
}

4.在vhost目录下添加虚拟机      文件名称 /data/soft/nginx/vhost/one.conf

server {
    listen              *:81; #对于虚拟机配置端口
    server_name         localhost;
    access_log          /data/log/one_access.log;
    error_log           /data/log/one_error.log warn;
    index               index.html index.htm index.php;
    root                /data/www/one; #对应/data/www目录下创建三个目录
    autoindex           off;
    error_page          404 http://$server_name;

    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

创建文件 /data/www/one/index.html   内容:Th is One Node

按照此内容添加3个文件,listen和root对应修改

5.配置完成

重启nginx
访问 192.168.14.126 ,就可以看见不同效果了

阅读全文

Nginx配置支持跨域访问PHP接口
发表于 2015-8-31 | 浏览(7725) | 服务器
location ~ \.php$ {
      #允许跨域访问
      #add_header 'Access-Control-Allow-Origin' '*'; #不限域名
      add_header 'Access-Control-Allow-Origin' 'http://dev.gongfuxiang.com'; #限制域名的方式
      add_header 'Access-Control-Allow-Credentials' 'true';
      add_header 'Access-Control-Allow-Methods' 'OPTION, POST, GET';
      add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Content-Type';

      #PHP配置
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
      include        fastcgi_params;
}

阅读全文

TOP