ngnix 集群和配置(mysql ,fast php 安装)

Nginx 部分:

nginx 的安装

nginx依赖以下一些软件库,在安装之前请确保安装了这些软件库,它们包括:gccopensslzlibpcre(可通过rpm -qa命令查询是否已安装),其中前三个库可通过系统盘进行安装,这里仅简单说说pcre的安装:

下载地址: http://nginx.org/download/nginx-1.4.2.tar.gz

安装准备: nginx依赖于pcre库,要先安装pcre,gzip module需要zlib库,需要安装zlib。

tar -zxvf zlib-1.2.8.tar.gz

./configure

make && make install

 

yum install pcre pcre-devel

 cd /usr/local/src/

 wget http://nginx.org/download/nginx-1.4.2.tar.gz

tar zxvfnginx-1.4.2.tar.gz

cd nginx-1.4.2

./configure --prefix=/usr/local/nginx

 --add-module=/usr/local/src/ngx_http_consistent_hash-master/

 

 --prefix=/usr/local/ngnix/--add-module=/usr/local/src/ngx_http_consistent_hash-master/

 

make && make install 


Nginx启动:

nginx -t -c /usr/nginx/conf/nginx.conf

或者

/usr/nginx/sbin/nginx -t

 

更改配置文件后启动

/usr/nginx/sbin/nginx -s reload


访问不同检查时按这三种方式检查:

1)ping 192.168.1.102 //ping地址

2)telnet 192.168.1.102 8000 // 查看端口号。 ctrl+] 退出telnet模式。

3)curl 192.168.1.102   内网访问,正常应该有返回数据。

一般情况下有防火墙:

关闭防火墙:

service iptables stop

修改iptable,允许通过端口:

修改/etc/sysconfig/iptables文件,添加以下内容

iptables -I INPUT -p tcp --dport 80 -j ACCEPT //允许80端口通过防火墙

iptables-D INPUT -p tcp --dport 80 -j ACCEPT//拒绝80端口通过防火墙


重启iptables,生效:

1)iptables-restore </etc/sysconfig/iptables(推荐这种,因为有主库和从库时,从库不会掉)

2)

/etc/rc.d/init.d/iptablessave

/etc/init.d/iptables restart

3)

chkconfig–level 35 iptables off

查看防火墙状态:

cat/etc/sysconfig/iptables 可以查看 防火墙 iptables 配置文件内容

/etc/init.d/iptablesstatus 

chkconfig–level 35 iptables off


rpm 安装的问题:

rpm -e --nodeps httpd ;删除系统自带的httpd

 ./apachectl -t -D DUMP_MODULES//查看apache载人的模块

 rpm -ql libxml2.x86_64 0:2.7.6-17.el6_6.1//

 查询软件装到什么位置

具体nginx配置内容:

1) 第一份配置:


#user  nobody;

worker_processes  2;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

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 logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip on;

   upstream mcserver

   {

 

     consistent_hash $request_uri;

     server 127.0.0.1:11211;

     server 127.0.0.1:11212;

     server 127.0.0.1:11213;

   }

   upstream imageserver

   {

      server 10.18.93.120:81 weight=2;

      server 10.18.93.120:82;

   }

 

   server{

  

   listen 84;

   root html;

   access_log logs/83.log main;

   }

   server{

   listen 81;

   server_name localhost;

   root html;

   access_log logs/81.log main;

 

   }

 

   server{

   listen 82;

   server_name localhost;

   root html;

   access_log logs/82.log main;

  

   }

 

   server{

   listen 2022;

   server_name z.com;

   location = /index.htm {

   root html;

   index index.htm;

   access_log  logs/zom.log  main;  

   }

}

   server {

        listen       80;

        server_name  localhost;

 

        gzip on;

        gzip_buffers 32 4k;

        gzip_comp_level 6;

        gzip_min_length 200;

        gzip_types text/css text/xmlapplication/x-javascript;

 

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

       location /ecshop

       {

          root html;

          index index.html;

          rewrite"goods-(\d{0,7})\.html" ecshop/goods.php?id=$1;

       }

 

       location ~ \.(jpg|png|gif)$

       {

 

           proxy_set_header X-Forwarded-For$remote_addr;

           proxy_pass http://imageserver;

      

       }

       location ~ \.php$

      {

      # proxy_pass http://10.18.93.120:8080;

 

      # proxy_set_header X-Forwarded-For$remote_addr;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /usr/local/ngnix/html/$fastcgi_script_name;

       # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include        fastcgi_params;

        }

       location / {

       

           set $memcached_key $request_uri;

           memcached_pass  mcserver;

           error_page 404 /callback.php;

           # root   html/;

           # index  index.php;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to thestatic page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        # proxy the PHP scripts to Apachelistening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #   proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGIserver listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #   root           html;

        #   fastcgi_pass   127.0.0.1:9000;

        #   fastcgi_index  index.php;

        #   fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

        #   include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, ifApache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #   deny  all;

        #}

    }

 

 

    # another virtual host using mix of IP-, name-,and port-based configuration

    #

    #server {

    #   listen       8000;

    #   listen       somename:8080;

    #   server_name  somename  alias another.alias;

 

    #   location / {

    #       root   html;

    #       index  index.html index.htm;

    #   }

    #}

 

 

    # HTTPS server

    #

    #server {

    #   listen       443;

    #   server_name  localhost;

 

    #   ssl                  on;

    #   ssl_certificate      cert.pem;

    #   ssl_certificate_key  cert.key;

 

    #   ssl_session_timeout  5m;

 

    #   ssl_protocols  SSLv2 SSLv3 TLSv1;

    #   ssl_ciphers  HIGH:!aNULL:!MD5;

    #   ssl_prefer_server_ciphers   on;

 

    #   location / {

    #       root   html;

    #       index  index.html index.htm;

    #   }

    #}

 

}

2)第二份配置:


#user nobody;

worker_processes  1;

worker_rlimit_nofile 102400;

#error_log logs/error.log;

#error_log logs/error.log  notice;

#error_log logs/error.log  info;

 

#pid       logs/nginx.pid;

 

 

events {

   worker_connections  50000;

}

 

 

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  logs/access.log  main;

 

   sendfile        on;

   #tcp_nopush     on;

 

   #keepalive_timeout  0;

   keepalive_timeout  65;

 

   #gzip  on;

 

  upstream phpserver

   {

     server 10.18.93.124:9001;       

     server 10.18.93.124:9002;

     server 10.18.93.124:9003;

      server10.18.93.124:9004;

   }

 

  upstream memcacheserver

  {

    consistent_hash $request_uri;

    server 10.18.93.123:11211;

    server 10.18.93.123:11212;

    server 10.18.93.123:11213;

    server 10.18.93.123:11214;

    server 10.18.93.123:11215;

    server 10.18.93.123:11216;

    server 10.18.93.123:11217;

    server 10.18.93.123:11218;

  }

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

 

       #error_page  404              /404.html;

 

       # redirect server error pages to the static page /50x.html

       #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

       }

 

       # proxy the PHP scripts to Apache listening on 127.0.0.1:80

       #

       #location ~ \.php$ {

       #    proxy_pass   http://127.0.0.1;

       #}

 

       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

       #

       location ~ \.php$ {

           root           /var/www;

            fastcgi_pass   phpserver;

           fastcgi_index  index.php;

           fastcgi_param  SCRIPT_FILENAME  /var/www/$fastcgi_script_name;

           include        fastcgi_params;

       }

       location /

       {

           root /var/www;

       

           set $memcached_key $request_uri;

           memcached_pass  memcacheserver;

           error_page 404  /info.php;

        }

       # deny access to .htaccess files, if Apache's document root

       # concurs with nginx's one

       #

       #location ~ /\.ht {

       #    deny  all;

       #}

    }

 

 

    #another virtual host using mix of IP-, name-, and port-based configuration

    #

   #server {

   #    listen       8000;

   #    listen       somename:8080;

   #    server_name  somename alias  another.alias;

 

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

 

 

    #HTTPS server

    #

   #server {

   #    listen       443;

   #    server_name  localhost;

 

   #    ssl                  on;

   #    ssl_certificate      cert.pem;

   #    ssl_certificate_key  cert.key;

 

   #    ssl_session_timeout  5m;

 

   #    ssl_protocols  SSLv2 SSLv3 TLSv1;

   #    ssl_ciphers  HIGH:!aNULL:!MD5;

   #   ssl_prefer_server_ciphers   on;

 

   #    location / {

   #        root   html;

   #        index  index.html index.htm;

   #    }

   #}

 

}


3)第三份配置

#user  nobody;
worker_processes  1;


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


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




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


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


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       80;




        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        #deny all;


        #rewrite ^/(.*)  www.liyulong.com;


         rewrite ^/(.*)  /index/$1 permanent;

         #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /var/html;
            fastcgi_pass   172.16.10.22:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/html/$fastcgi_script_name;
            include        fastcgi_params;
        }


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




       server {
              listen       80;
              server_name  www.liyulong.com www.ttoy.com;


              root  /var/html/aa;
         #charset koi8-r;


             access_log  logs/liyulong80.log  main;


             



              location / {


                  index  index.html index.htm;
              }


           }


         server {
            listen       8080;
              server_name  www.liyulong.com;
              root /var/html/bb;
              #charset koi8-r;


             #access_log  logs/host.access.log  main;


              location / {


                  index  index.html index.htm;
              }


           }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;


    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_timeout  5m;



    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}


nginx日志切割:

 mv access.log www.$(date -d 1day +%F).log

nginx -s reload


php部分:

注意: 我们编译的PHP 要有如下功能:

连接mysql, gd,ttf, 以fpm(fascgi)方式运行

 

Yum -y install libpng libpng-devel

Yum -y install libxml2 libxml2-devel

Libxml2是个C语言的XML程式库,能简单方便的提供对XML文件的各种操作,并且支持XPATH查询,及部分的支持XSLT转换等功能

//--with-mysql=/usr/local/mysql \

 

./configure --prefix=/usr/local/fastphp --

最完整php:

yum -y installlibxml2-devel

yum install -y bzip2-devel

yum -y installlibpng-devel

 yum -yinstall freetype-devel

yum -y install libcurl libcurl-devel

yum -y install openssl openssl-devel

yum -y install jpeglib-devel

yum install libjpeg-devel

安装:

./configure--prefix=/usr/local/fastphp--with-config-file-path=/usr/local/fastphp/ --with-mysql=mysqlnd--enable-fpm --with-gd--enable-gd-native-ttf --enable-gd-jis-conv


/*

./configure  --prefix=/usr/local/fastphp \

--with-mysql=mysqlnd \ 

--enable-mysqlnd \

--with-gd \

--enable-gd-native-ttf\

--enable-gd-jis-conv\

--enable-fpm\


*/

拷贝配置文件:

 cp/usr/local/src/php-5.4.37/php.ini-development /usr/local/fastphp/lib/php.ini


安装完成后将

php-fpm.conf中,一定要修改为

127.0.0.1:9000 修改为10.18.93.124:9000  //监听nginx服务的10.18.93.124的9000端口。

否则会被拒绝。

在php.ini中添加

memcache.hash_strategy=consistent

 

 

在php-fpm.conf

listen = 10.18.93.124:9001

pm = static

pm.max_children = 5

ps -aux |grep php

 

查看开启的进程。

 

/tmp/mysql.sock

 

Fast-php 运行:

/usr/local/php/sbin/php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y/usr/local/php/etc/php-fpm.conf

 

./sbin/php-fpm -y ./etc/php-fpm9001.conf



 

Mysql部分:

 

 

需要安装gcc gcc-c++.

Yum -y install gccgcc-c++

1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件

(1)先安装cmake(mysql5.5以后是通过cmake来编译的)

[root@ rhel5 local]#tar -zxv -f cmake-2.8.4.tar.gz
[root@ rhel5 local]#cd cmake-2.8.4
[root@ rhel5 cmake-2.8.4]#./configure
[root@ rhel5 cmake-2.8.4]#make
[root@ rhel5 cmake-2.8.4]#make install


(2)创建mysql的安装目录及数据库存放目录

[root@ rhel5~]#mkdir -p /usr/local/mysql                 //安装mysql 
[root@ rhel5~]#mkdir -p /usr/local/mysql/data            //存放数据库

(3)创建mysql用户及用户组

[root@ rhel5~]groupadd mysql
[root@ rhel5~]useradd -r -g mysql mysql

(4)安装mysql

 

yum install ncurses-devel

yum install bison

 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all-DENABLED_LOCAL_INFILE=1

/*有问题慎用,用上面的

[root@ rhel5 local]#tar -zxv -f mysql-5.5.10.tar.gz
[root@ rhel5 local]#cd mysql-5.5.10
[root@ rhel5 mysql-5.5.10]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql\
-DMYSQL_DATADIR=/usr/local/mysql/data\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
 */ 
[root@ rhel5 mysql-5.5.10]#make
[root@ rhel5 mysql-5.5.10]#make install

参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql       //安装目录

-DINSTALL_DATADIR=/usr/local/mysql/data        //数据库存放目录

-DDEFAULT_CHARSET=utf8                       //使用utf8字符

-DDEFAULT_COLLATION=utf8_general_ci           //校验字符

-DEXTRA_CHARSETS=all                           //安装所有扩展字符集

-DENABLED_LOCAL_INFILE=1                       //允许从本地导入数据

 

注意事项:

重新编译时,需要清除旧的对象文件和缓存信息。

# make clean

# rm -f CMakeCache.txt

# rm -rf /etc/my.cnf

2.配置

(1)设置目录权限

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql

[root@ rhel5 mysql]# chown -R mysql:mysql data

(2)

[root@ rhel5 mysql]# cp support-files/my-medium.cnf /etc/my.cnf //将mysql的启动服务添加到系统服务中



(3)创建系统数据库的表

yum -yinstall perl perl-devel

[root@ rhel5 mysql]# cd /usr/local/mysql
[root@ rhel5 mysql]# scripts/mysql_install_db --user=mysql
 

(4)设置环境变量

[root@ rhel5~]# vi /root/.bash_profile

在PATH=$PATH:$HOME/bin添加参数为:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

[root@ rhel5~]#source /root/.bash_profile

 

 

chmod 1757 /tmp

(5)手动启动mysql

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# ./bin/mysqld_safe --user=mysql   //启动MySQL,但不能停止
./mysqld_safe --user=mysql --skip-grant-tables --skip-networking


启动日志写在此文件下:/usr/local/mysql/data/localhost.err

关闭MySQL服务
为mysqld设置密码:mysqladmin -u root password "newpass"
mysqladmin -u root password 123456
杀死mysqld的进程 pkill -9 mysqld
查看进程是否杀死:ps –auf |grep mysqld

[root@ rhel5 mysql]# mysqladmin -u root -p shutdown  //这里MySQL的root用户还没有配置密码,所以为空值。需要输入密码时,直接点回车键即可。

(6)另一种简单的启动mysql的方法(mysql已经被添加到系统服务中)

[root@ rhel5~]# service mysql.server start  
[root@ rhel5~]# service mysql.server stop
[root@ rhel5~]# service mysql.server restart
 
错误:
mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
解决:
没有安装库:
/usr/local/mysql/scripts/mysql_install_db --user=mysql
 

如果上述命令出现:mysql.server 未识别的服务

则可能mysql还没添加到系统服务中,下面用另一种方法添加:

[root@ rhel5 mysql]# cp support-files/mysql.server  /etc/init.d/mysql //将mysql的启动服务添加到系统服务中

注意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql。在有的系统中,mysql.server在/usr/local/mysql/share/mysql/mysql.server中,而本系统中,mysql.server在/usr/local /mysql/support-files/mysql.server中。

然后再用#service mysql start 来启动mysql即可。


(7)修改MySQL的root用户的密码以及打开远程连接

[root@ rhel5~]# mysql -u root mysql

mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";  //为root添加远程连接的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password  from user where User='root'; 
mysql>flush privileges;
mysql>exit

重新登录:mysql -u root -p

若还不能进行远程连接,则关闭防火墙
[root@ rhel5~]# /etc/rc.d/init.d/iptables stop

ERROR 1045(28000): Access denied for user ’root’@’localhost’ (using password: NO)

 

mysql>GRANT ALLPRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '******' WITH GRANT OPTION;
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql //
登入mysql并且使用mysql库。
mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’;
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <
输入新设的密码newpassword>
mysql>

mysqldnd即mysql native driver简写,即是由PHP源码提供的mysql驱动连接代码.它的目的是代替旧的libmysql驱动.

因为mysqlnd内置于PHP源代码,故你在编译安装php时就不需要预先安装mysql server也可以提供mysql client API (mysql_connect, pdo , mysqli), 这将减化一些工作量.

--with-mysql=/usr/local/mysql 这实际上就是使用了mysql官方自带的libmysql驱动, 这是比较老的驱动, PHP 5.3开始已经不建议使用它了, 而建议使用mysqlnd.

/var/lib/mysql/mysql.sock  是mysqlnd安装时默认的mysql.sock

/tmp/mysql.sock   是源码安装时生成的mysql.sock

Access denied for user'root'@'10.18.93.124' (using password: NO)

Mysql服务器需要添加一个10.18.93.124的账号

 

Connect not 10.18.93.122

没有关闭防火墙:service iptables stop

 Access denied for user 'root'@'10.18.93.124'(using password: NO)

 

not rute host;

一定要关闭防火墙

Service iptables stop;

 

 

 

Memcahched部分

一定先安装libevent

./configure --prefix=/usr/local/memcached--with-libevent=/usr/local/libevent

 

./configure--prefix=/usr/local/memched

Make && make install

./bin/memcached -m 64 -p 11211-vv -u nobody (-d 后台运行)

./bin/memcached -m 64 -p 11212-vv -u nobody (-d 后台运行)

 

注意;需要安装在php服务器中

如果需要要用php访问memcached,需要编辑memecached的php扩展

1,进入扩展文件夹:memecahe-2.2.7:

2/usr/local/fashphp/bin/phpize

./configure--with-php-config=/usr/local/fastphp/bin/php-config

在/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525会生成memecach.so

在php.ini中加入

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

Apache httpd部分:

安装:

首先下载apr,apr-util,pcre

 

Apr-util安装:

./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr

 

./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre  --enable-so --enable-rewrite

 

如果缺少libxm2

Yuminstall libxml2//libxml2核心功能

Yuminstall libxml2-devel //libxml的头文件

http://downloads.php.net/pierre/

http://httpd.apache.org

http://php.net/downloads.php

http://pecl.php.net/

 

Httpd.conf设置:

Listen:80 //监听的端口

ServerNamelocalhost:80 //虚拟的web站点

DocumentRoot"/usr/local/httpd/htdocs"  //资源文件的根目录

ErrorLog"logs/error_log"  //错误log

 

 <IfModule dir_module>

  DirectoryIndex index.html  //默认访问的文件

  </IfModule>

 

Includeconf/extra/httpd-mpm.conf //开启mpm机制

 

 

<IfModulempm_prefork_module>

   StartServers          5

   MinSpareServers       5

   MaxSpareServers      10

   ServerLimit    8000

   MaxClients         8000

   MaxRequestsPerChild   0

</IfModule>

 

使用directory段设置根目录

<directory/>

</directory>

Deny:设定禁止访问apache服务器主机

Allowfrom all

Allowfrom 127.0.0.1

Allowoverride

1.none:不读取.htaccess文件

2.all:读取.htaccess文件,修改原来的访问权限

设置资源文件的权限

<directory/usr/local/httpd/htmldos>

</directory>

 

设置默认首页依次为index.html index.php index.thm

<ifmodulledir_module>

  DirectoryIndex index.html index.php index.htm

</ifmodule>

 

 

2.

#AddHandler application/x-httpd-php.php //遇到php文件需要httpdphp模块解析

 AddType application/x-httpd-php .php  遇到php文件需要httpdphp模块解析

 

3. Listen 8080

 DocumentRoot"/usr/local/httpd/htdocs"

 <Directory "/usr/local">

Options Indexes FollowSymLinks

 AllowOverride None

    Require all granted

    #Require all denied

 </Directory>

 Include conf/extra/httpd-vhosts.conf //打开这个

/usr/local/httpd/conf/extra/httpd-vhosts.conf

 <VirtualHost *:8080>

        ServerAdmin webmaster@dummy-host.example.com

        DocumentRoot "/usr/local/ngnix/html/"

          ServerName 10.18.93.120

          ServerAliaswww.dummy-host.example.com

          ErrorLog "logs/dummy-host.example.com-error_log"

          CustomLog"logs/dummy-host.example.com-access_log" common

     </VirtualHost>

注意:httpd.conf中的<Directory"/usr/local">一定要比httpd-vhosts.conf中的 DocumentRoot "/usr/local/ngnix/html/" 中目录大。否则可能会forbidn,拒绝访问。

 

 

 

 

 

 

相应的php的安装需要这样

yum -y install libcurl libcurl-devel

 yum -y install opensslopenssl-devel

 

yum install  libjpeg-devel

./configure  --prefix=/usr/local/php\

--with-mysql=mysqlnd \

--enable-mysqlnd \

--with-gd \

--enable-gd-native-ttf\

--enable-gd-jis-conv

--with-apxs2=/usr/local/httpd/bin/apxs

--with-mysql=/usr/local/mysql

 

之后再php的httpd.conf文件中会出现

 LoadModulephp5_module        modules/libphp5.so

在/usr/local/httpd/modules/libphp5.so

将php作为httpd的一个模块。

 

 

 

进群功能,memcache代码测试:

<?php

 

//print_r($_SERVER);

//$array=array(1,2,3);

//print_r($array);

$uri=$_SERVER['REQUEST_URI'];

//echo $uri;

$pos=strpos($uri,'.');

$id=substr($uri,5,$pos-5);

 

$con=mysql_connect('10.18.93.121','liyulong','liyulong');

 

$mem=new memcache();

$mem->addServer('10.18.93.122',11211);

$mem->addServer('10.18.93.122',11212);

$mem->addServer('10.18.93.122',11213);

$mem->addServer('10.18.93.122',11214);

 

if($con)

{

 

       $sql='use person';

       mysql_query($sql,$con);

       $sql='set names utf8';

       mysql_query($sql,$con);

       $sql='select * from classwhere id='.$id;

      $rs=mysql_query($sql,$con);

      $user=mysql_fetch_assoc($rs);

       print_r($user);

 

     $mem->add($uri,$user['name']);

      $mem->close();

 

 

 

   echo 'conect sucessfull';

}

else

{

    echo 'connect fail';

}

 

 

Nginx 大并发的设置:

ulimit -n  查看允许打开的最大字符数

ulimit -n 50000 设置允许打开的最大字符数。

 

events{

Worker_connections 10240;

}

 

worker_processes 1;

worker_rlimit_nofile 10000; Mysql部分:


 

Mysql部分:

 

 

需要安装gcc gcc-c++.

Yum -y install gccgcc-c++

1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件

(1)先安装cmake(mysql5.5以后是通过cmake来编译的)

[root@ rhel5 local]#tar -zxv -f cmake-2.8.4.tar.gz
[root@ rhel5 local]#cd cmake-2.8.4
[root@ rhel5 cmake-2.8.4]#./configure
[root@ rhel5 cmake-2.8.4]#make
[root@ rhel5 cmake-2.8.4]#make install


(2)创建mysql的安装目录及数据库存放目录

[root@ rhel5~]#mkdir -p /usr/local/mysql                 //安装mysql 
[root@ rhel5~]#mkdir -p /usr/local/mysql/data            //存放数据库

(3)创建mysql用户及用户组

[root@ rhel5~]groupadd mysql
[root@ rhel5~]useradd -r -g mysql mysql

(4)安装mysql

 

yum install ncurses-devel

yum install bison

 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all-DENABLED_LOCAL_INFILE=1

/*有问题慎用,用上面的

[root@ rhel5 local]#tar -zxv -f mysql-5.5.10.tar.gz
[root@ rhel5 local]#cd mysql-5.5.10
[root@ rhel5 mysql-5.5.10]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql\
-DMYSQL_DATADIR=/usr/local/mysql/data\
-DDEFAULT_CHARSET=utf8\
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
 */ 
[root@ rhel5 mysql-5.5.10]#make
[root@ rhel5 mysql-5.5.10]#make install

参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql       //安装目录

-DINSTALL_DATADIR=/usr/local/mysql/data        //数据库存放目录

-DDEFAULT_CHARSET=utf8                       //使用utf8字符

-DDEFAULT_COLLATION=utf8_general_ci           //校验字符

-DEXTRA_CHARSETS=all                           //安装所有扩展字符集

-DENABLED_LOCAL_INFILE=1                       //允许从本地导入数据

 

注意事项:

重新编译时,需要清除旧的对象文件和缓存信息。

# make clean

# rm -f CMakeCache.txt

# rm -rf /etc/my.cnf

2.配置

(1)设置目录权限

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql

[root@ rhel5 mysql]# chown -R mysql:mysql data

(2)

[root@ rhel5 mysql]# cp support-files/my-medium.cnf /etc/my.cnf //将mysql的启动服务添加到系统服务中



(3)创建系统数据库的表

yum -yinstall perl perl-devel

[root@ rhel5 mysql]# cd /usr/local/mysql
[root@ rhel5 mysql]# scripts/mysql_install_db --user=mysql
 

(4)设置环境变量

[root@ rhel5~]# vi /root/.bash_profile

在PATH=$PATH:$HOME/bin添加参数为:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

[root@ rhel5~]#source /root/.bash_profile

 

 

chmod 1757 /tmp

(5)手动启动mysql

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# ./bin/mysqld_safe --user=mysql   //启动MySQL,但不能停止
./mysqld_safe --user=mysql --skip-grant-tables --skip-networking


启动日志写在此文件下:/usr/local/mysql/data/localhost.err

关闭MySQL服务
为mysqld设置密码:mysqladmin -u root password "newpass"
mysqladmin -u root password 123456
杀死mysqld的进程 pkill -9 mysqld
查看进程是否杀死:ps –auf |grep mysqld

[root@ rhel5 mysql]# mysqladmin -u root -p shutdown  //这里MySQL的root用户还没有配置密码,所以为空值。需要输入密码时,直接点回车键即可。

(6)另一种简单的启动mysql的方法(mysql已经被添加到系统服务中)

[root@ rhel5~]# service mysql.server start  
[root@ rhel5~]# service mysql.server stop
[root@ rhel5~]# service mysql.server restart
 
错误:
mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended
解决:
没有安装库:
/usr/local/mysql/scripts/mysql_install_db --user=mysql
 

如果上述命令出现:mysql.server 未识别的服务

则可能mysql还没添加到系统服务中,下面用另一种方法添加:

[root@ rhel5 mysql]# cp support-files/mysql.server  /etc/init.d/mysql //将mysql的启动服务添加到系统服务中

注意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql。在有的系统中,mysql.server在/usr/local/mysql/share/mysql/mysql.server中,而本系统中,mysql.server在/usr/local /mysql/support-files/mysql.server中。

然后再用#service mysql start 来启动mysql即可。


(7)修改MySQL的root用户的密码以及打开远程连接

[root@ rhel5~]# mysql -u root mysql

mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";  //为root添加远程连接的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password  from user where User='root'; 
mysql>flush privileges;
mysql>exit

重新登录:mysql -u root -p

若还不能进行远程连接,则关闭防火墙
[root@ rhel5~]# /etc/rc.d/init.d/iptables stop

ERROR 1045(28000): Access denied for user ’root’@’localhost’ (using password: NO)

 

mysql>GRANT ALLPRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '******' WITH GRANT OPTION;
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql //
登入mysql并且使用mysql库。
mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’;
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <
输入新设的密码newpassword>
mysql>

mysqldnd即mysql native driver简写,即是由PHP源码提供的mysql驱动连接代码.它的目的是代替旧的libmysql驱动.

因为mysqlnd内置于PHP源代码,故你在编译安装php时就不需要预先安装mysql server也可以提供mysql client API (mysql_connect, pdo , mysqli), 这将减化一些工作量.

--with-mysql=/usr/local/mysql 这实际上就是使用了mysql官方自带的libmysql驱动, 这是比较老的驱动, PHP 5.3开始已经不建议使用它了, 而建议使用mysqlnd.

/var/lib/mysql/mysql.sock  是mysqlnd安装时默认的mysql.sock

/tmp/mysql.sock   是源码安装时生成的mysql.sock

Access denied for user'root'@'10.18.93.124' (using password: NO)

Mysql服务器需要添加一个10.18.93.124的账号

 

Connect not 10.18.93.122

没有关闭防火墙:service iptables stop

 Access denied for user 'root'@'10.18.93.124'(using password: NO)

 

not rute host;

一定要关闭防火墙

Service iptables stop;

 

 

 

Memcahched部分

一定先安装libevent

./configure --prefix=/usr/local/memcached--with-libevent=/usr/local/libevent

 

./configure--prefix=/usr/local/memched

Make && make install

./bin/memcached -m 64 -p 11211-vv -u nobody (-d 后台运行)

./bin/memcached -m 64 -p 11212-vv -u nobody (-d 后台运行)

 

注意;需要安装在php服务器中

如果需要要用php访问memcached,需要编辑memecached的php扩展

1,进入扩展文件夹:memecahe-2.2.7:

2/usr/local/fashphp/bin/phpize

./configure--with-php-config=/usr/local/fastphp/bin/php-config

在/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525会生成memecach.so

在php.ini中加入

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

Apache httpd部分:

安装:

首先下载apr,apr-util,pcre

 

Apr-util安装:

./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr

 

./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre  --enable-so --enable-rewrite

 

如果缺少libxm2

Yuminstall libxml2//libxml2核心功能

Yuminstall libxml2-devel //libxml的头文件

http://downloads.php.net/pierre/

http://httpd.apache.org

http://php.net/downloads.php

http://pecl.php.net/

 

Httpd.conf设置:

Listen:80 //监听的端口

ServerNamelocalhost:80 //虚拟的web站点

DocumentRoot"/usr/local/httpd/htdocs"  //资源文件的根目录

ErrorLog"logs/error_log"  //错误log

 

 <IfModule dir_module>

  DirectoryIndex index.html  //默认访问的文件

  </IfModule>

 

Includeconf/extra/httpd-mpm.conf //开启mpm机制

 

 

<IfModulempm_prefork_module>

   StartServers          5

   MinSpareServers       5

   MaxSpareServers      10

   ServerLimit    8000

   MaxClients         8000

   MaxRequestsPerChild   0

</IfModule>

 

使用directory段设置根目录

<directory/>

</directory>

Deny:设定禁止访问apache服务器主机

Allowfrom all

Allowfrom 127.0.0.1

Allowoverride

1.none:不读取.htaccess文件

2.all:读取.htaccess文件,修改原来的访问权限

设置资源文件的权限

<directory/usr/local/httpd/htmldos>

</directory>

 

设置默认首页依次为index.html index.php index.thm

<ifmodulledir_module>

  DirectoryIndex index.html index.php index.htm

</ifmodule>

 

 

2.

#AddHandler application/x-httpd-php.php //遇到php文件需要httpdphp模块解析

 AddType application/x-httpd-php .php  遇到php文件需要httpdphp模块解析

 

3. Listen 8080

 DocumentRoot"/usr/local/httpd/htdocs"

 <Directory "/usr/local">

Options Indexes FollowSymLinks

 AllowOverride None

    Require all granted

    #Require all denied

 </Directory>

 Include conf/extra/httpd-vhosts.conf //打开这个

/usr/local/httpd/conf/extra/httpd-vhosts.conf

 <VirtualHost *:8080>

        ServerAdmin webmaster@dummy-host.example.com

        DocumentRoot "/usr/local/ngnix/html/"

          ServerName 10.18.93.120

          ServerAliaswww.dummy-host.example.com

          ErrorLog "logs/dummy-host.example.com-error_log"

          CustomLog"logs/dummy-host.example.com-access_log" common

     </VirtualHost>

注意:httpd.conf中的<Directory"/usr/local">一定要比httpd-vhosts.conf中的 DocumentRoot "/usr/local/ngnix/html/" 中目录大。否则可能会forbidn,拒绝访问。

 

 

 

 

 

 

相应的php的安装需要这样

yum -y install libcurl libcurl-devel

 yum -y install opensslopenssl-devel

 

yum install  libjpeg-devel

./configure  --prefix=/usr/local/php\

--with-mysql=mysqlnd \

--enable-mysqlnd \

--with-gd \

--enable-gd-native-ttf\

--enable-gd-jis-conv

--with-apxs2=/usr/local/httpd/bin/apxs

--with-mysql=/usr/local/mysql

 

之后再php的httpd.conf文件中会出现

 LoadModulephp5_module        modules/libphp5.so

在/usr/local/httpd/modules/libphp5.so

将php作为httpd的一个模块。

 

 

 

进群功能,memcache代码测试:

<?php

 

//print_r($_SERVER);

//$array=array(1,2,3);

//print_r($array);

$uri=$_SERVER['REQUEST_URI'];

//echo $uri;

$pos=strpos($uri,'.');

$id=substr($uri,5,$pos-5);

 

$con=mysql_connect('10.18.93.121','liyulong','liyulong');

 

$mem=new memcache();

$mem->addServer('10.18.93.122',11211);

$mem->addServer('10.18.93.122',11212);

$mem->addServer('10.18.93.122',11213);

$mem->addServer('10.18.93.122',11214);

 

if($con)

{

 

       $sql='use person';

       mysql_query($sql,$con);

       $sql='set names utf8';

       mysql_query($sql,$con);

       $sql='select * from classwhere id='.$id;

      $rs=mysql_query($sql,$con);

      $user=mysql_fetch_assoc($rs);

       print_r($user);

 

     $mem->add($uri,$user['name']);

      $mem->close();

 

 

 

   echo 'conect sucessfull';

}

else

{

    echo 'connect fail';

}

 

 

Nginx 大并发的设置:

ulimit -n  查看允许打开的最大字符数

ulimit -n 50000 设置允许打开的最大字符数。

 

events{

Worker_connections 10240;

}

 

worker_processes 1;

worker_rlimit_nofile 10000;

修改ipv4 tcp _tw

echo 50000 >/proc/sys/net/core/somaxconn

echo 1>/proc/sys/net/ipv4/tcp_tw_recyle

echo 1>/proc/sys/net/ipv4/tcp_tw_reuse

echo 0>/proc/sys/net/ipv4/tcp_syncookies 



 ./ab -c 5000 -n 10000 http://10.18.93.124/user2.html  //ab压力测

 










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值