LNMP的编译安装与xcache、memcached的安装配置

来源:互联网 发布:java调用其他类的函数 编辑:程序博客网 时间:2024/05/16 05:31

大纲:

一、前言

二、系统环境与软件版本

三、编译环境的准备

四、编译安装nginx及其配置

五、编译安装、配置mysql

六、编译安装PHP

七、整合nginx与PHP

八、安装配置PHP加速器xcache

九、安装配置memcached

十、安装memcached的PHP扩展


一、前言

    由于公司的服务器采用的是LNMP的架构,平时接触相对较多,今天会系统的把LNMP的安装配置过程写成博文,有关nginx的其他高级功能的配置,mysql的相关知识,会在后面的时间里陆续写成博客。


二、系统环境与软件版本

    操作系统:CentOS 5.8    内核版本号:2.6.18-308.el5

    平台    :x86_64

   软件版本

    nginx-1.6.1.tar.gz

    mysql-5.6.20.tar.gz   

    php-5.4.32.tar.bz2

    xcache-3.2.0.tar.gz   

    memcached-1.4.20.tar.gz


三、编译环境的准备

    1、使用yum安装开发包

1
[root@vm_101 ~]# yum -y groupinstall "Development Tools" "Development Libraries"

    2、安装nginx的依赖包

1
[root@vm_101 ~]# yum -y install pcre pcre-devel zlib zlib-devel


四、编译安装nginx及其配置

    1、解压包

1
2
[root@vm_101 ~]# tar xf nginx-1.6.1.tar.gz -C /opt/
[root@vm_101 ~]# cd /opt/nginx-1.6.1/

    2、为nginx创建用户与用户组

1
2
[root@vm_101 nginx-1.6.1]# groupadd -r nginx
[root@vm_101 nginx-1.6.1]# useradd -r -g nginx -s /sbin/nologin nginx

    3、创建nginx的安装目录

1
[root@vm_101 nginx-1.6.1]# mkdir /usr/local/nginx

    4、编译安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@vm_101 nginx-1.6.1]# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--with-pcre
…………
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library
  nginx path prefix: "/usr/local/nginx"
  nginx binary file"/usr/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file"/etc/nginx/nginx.conf"
  nginx pid file"/var/run/nginx/nginx.pid"
  nginx error log file"/var/log/nginx/error.log"
  nginx http access log file"/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/tmp/nginx/client/"
  nginx http proxy temporary files: "/var/tmp/nginx/proxy/"
  nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"
  nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi"
  nginx http scgi temporary files: "scgi_temp"
        ###如果出现报错的话,应该是由于软件包的依赖关系所导致的,自行安装相应的软件包即可
[root@vm_101 nginx-1.6.1]# make && make install

5、为nginx提供启动脚本

    在nginx的官网上提供了相应的启动脚本(http://wiki.nginx.org/InitScripts),结合刚才的配置参数做出相应的修改即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
[root@vm_101 nginx-1.6.1]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
  
# Source function library.
/etc/rc.d/init.d/functions
  
# Source networking configuration.
/etc/sysconfig/network
  
# Check that networking is up.
"$NETWORKING" "no" ] && exit 0
  
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
  
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  
lockfile=/var/lock/nginx.lock
  
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
  
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
  
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
  
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
  
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
  
force_reload() {
    restart
}
  
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
  
rh_status() {
    status $prog
}
  
rh_status_q() {
    rh_status >/dev/null 2>&1
}
  
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

    6、为启动脚本添加执行权限

1
[root@vm_101 nginx-1.6.1]# chmod +x /etc/init.d/nginx

    7、启动nginx

1
2
[root@vm_101 nginx-1.6.1]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

    8、将nginx添加进服务列表,并设置为开机自启

1
2
3
4
[root@vm_101 nginx-1.6.1]# chkconfig --add nginx 
[root@vm_101 nginx-1.6.1]# chkconfig nginx on
[root@vm_101 nginx-1.6.1]# chkconfig nginx --list
nginx          0:off1:off2:on3:on4:on5:on6:off

    9、查看端口号

1
2
3
4
5
6
[root@vm_101 nginx-1.6.1]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   17211  root    6u  IPv4 124804      0t0  TCP *:http (LISTEN)
nginx   17212 nginx    6u  IPv4 124804      0t0  TCP *:http (LISTEN)
[root@vm_101 nginx-1.6.1]# netstat -tunlp | grep 80
tcp    0      0 0.0.0.0:80          0.0.0.0:*             LISTEN      17211/nginx

  10、测试一下(根据服务器的IP地址进行访问)

   wKiom1RgAfqRxoOeAAIFLjoNlDk597.jpg

可以发现网页访问正常。

    注:如果出现无法访问的情况,请检查iptables的配置信息,或者直接关闭iptables

11、配置nginx

    1、编辑nginx的主配置文件

1
2
3
4
5
[root@vm_101 ~]# vim /etc/nginx/nginx.conf        ##在HTTP段添加一条配置项:
http {
    include       mime.types;
    include       server.conf;        ###添加这一行
    default_type  application/octet-stream;

    2、在/etc/nginx/目录下创建server.conf文件

1
2
3
4
5
6
7
8
9
10
11
[root@vm_101 ~]# vim /etc/nginx/server.conf
server {
        listen       80;
        server_name  www.test.com;
location / {
            root   /home/html;
            index  index.html;
        }
}
[root@vm_101 ~]# vim /home/html/index.html
<h1>test file</h1>

    3、本地绑定hosts

win7的路径为:C:\Windows\System32\drivers\etc\hosts

Linux的路径为: /etc/hosts

在上述文件中加入:

192.168.0.101    www.test.com

1
2
3
4
[root@vm_101 ~]# /etc/init.d/nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reloading nginx:                                           [  OK  ]

wKioL1RgA5SwFfJQAADotVAfScw526.jpg

正常访问。


五、编译安装、配置mysql

    1、创建用户与相应的目录

1
2
3
4
5
[root@vm_101 ~]# groupadd -r mysql 
[root@vm_101 ~]# useradd -r -g mysql -s /sbin/nologin mysql
[root@vm_101 ~]# mkdir /home/mysql         #mysql的数据目录
[root@vm_101 ~]# chown -R mysql:mysql /home/mysql/
[root@vm_101 ~]# mkdir /usr/local/mysql    #mysql的安装目录

    2、解压缩

1
2
[root@vm_101 ~]# tar zxvf mysql-5.6.20.tar.gz -C /opt/
[root@vm_101 ~]# cd /opt/mysql-5.6.20/

    3、编译安装

    从MySQL5.5开始就要用cmake安装了,已不能用./configure编译安装,我们查看一下mysql5.6.12的安装目录,从下面的安装目录我们可以看到,里面根本没有configure文件,下面我们来说说cmake,

cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。

    cmake指定编译选项的方式不同于make,其实现方式对比如下

./configure 对应的是 cmake .        #注意:Linux命令行下cmake后面是需要带一个"."的

./configure --help 对应的是 cmake . -LH 或者是 ccmake 

编译安装前,请确保自己的服务器已经安装过cmake

如果没有请执行:yum -y install cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@vm_101 mysql-5.6.20]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/home/mysql \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL:STRING=bundled \
-DWITH_ZLIB:STRING=bundled \
-DENABLE_DOWNLOADS=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

出现报错:

1
2
3
4
5
6
7
8
-- Library mysqlclient depends on OSLIBS -lpthread;m;rt;dl
-- Download failed, error: 7;"couldn't connect to server"
-- To enable google test, please download http://googlemock.googlecode.com/files/gmock-1.6.0.zip to the directory /opt/mysql-5.6.20/source_downloads
-- If you are inside a firewall, you may need to use an http proxy: export http_proxy=http://example.com:80
-- Library mysqlserver depends on OSLIBS -lpthread;m;rt;crypt;dl
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/mysql-5.6.20

自己手动下载(可能需要翻墙)该文件,上传至相应的目录,并删除CMakeCache.txt文件,重新执行cmake命令,又出现报错:

1
2
CMake Error: Problem with tar_extract_all(): Invalid argument
CMake Error: Problem extracting tar/usr/local/src/mysql-5.6.13/source_downloads/gmock-1.6.0.zip

谷歌了一下,需要个手动解压该文件:

1
2
3
4
5
6
[root@vm_101 mysql-5.6.20]# cd source_downloads/
[root@vm_101 source_downloads]# unzip gmock-1.6.0.zip 
[root@vm_101 source_downloads]# cd gmock-1.6.0
[root@vm_101 source_downloads]# cd gmock-1.6.0
[root@vm_101 gmock-1.6.0]# ./configure
[root@vm_101 gmock-1.6.0]# make

重新编译:

1
2
[root@vm_101 gmock-1.6.0]# cd /opt/mysql-5.6.20/
[root@vm_101 mysql-5.6.20]# rm CMakeCache.txt -f

cmake之后正常,

1
[root@vm_101 mysql-5.6.20]# make && make install

    漫长的等待……

    4、编译参数说明

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
##指定安装文件的安装路径时常用的选项
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql    ##指定残可安装路径(默认的就是/usr/local/mysql)
-DMYSQL_DATADIR=/home/mysql          ##mysql的数据文件路径
-DSYSCONFDIR=/etc                ##配置文件路径
    ##编译过程中启用其他存储引擎时指令介绍
-DWITH_INNOBASE_STORAGE_ENGINE=1        ##使用INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1            ##常应用于日志记录和聚合分析,不支持索引
-DWITH_BLACKHOLE_STORAGE_ENGINE=1      ##黑洞存储引擎
    ##编译过程中取消一些存储引擎指令介绍
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
    ##编译进过程中功能启用的指令介绍
-DWITH_READLINE=1       ##支持批量导入mysql数据
-DWITH_SSL=system       ##mysql支持ssl会话,实现基于ssl的数据复
-DWITH_ZLIB=system      ##压缩库
-DWITH_LIBWRAP=0        ##是否可以基于WRAP实现访问控制
    ##其他功能指令
-DMYSQL_TCP_PORT=3306                   ##默认端口
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock       ##默认套接字文件路径
-DENABLED_LOCAL_INFILE=1                ##是否启用LOCAL_INFILE功能
-DEXTRA_CHARSETS=all                    ##是否支持额外的字符集
-DDEFAULT_CHARSET=utf8                  ##默认编码机制
-DDEFAULT_COLLATION=utf8_general_ci     ##设定默认语言的排序规则
-DWITH_DEBUG=0                          ##DEBUG功能设置
-DENABLE_PROFILING=1                    ##性能分析功能是否启用

    5、先查看一下安装完成后都生成了哪些文件

1
2
3
[root@vm_101 mysql-5.6.20]# cd /usr/local/mysql/
[root@vm_101 mysql]# ls 
bin  COPYING  data  docs  include  INSTALL-BINARY  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

    6、为mysql提供启动脚本

1
2
3
4
5
[root@vm_101 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@vm_101 mysql]# chkconfig --add mysqld    #添加进服务列表
[root@vm_101 mysql]# chkconfig mysqld on       #设置为开机自启
[root@vm_101 mysql]# chkconfig mysqld --list 
mysqld         0:off1:off2:on3:on4:on5:on6:off

    7、初始化mysql

1
2
3
4
5
6
7
8
[root@vm_101 mysql]# /usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/home/mysql/ \
--user=mysql
New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
[root@vm_101 mysql]# cp my.cnf /etc/        #为mysql提供配置文件

    8、启动mysql

1
2
[root@vm_101 mysql]# /etc/init.d/mysqld start
Starting MySQL.                                            [  OK  ]

    9、将mysql的命令添加进环境变量中

1
2
3
[root@vm_101 mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin    ##在新建的文件中加入这一行
[root@vm_101 mysql]# source /etc/profile

    10、将mysql的man手册添加进man命令的查找路径

1
2
[root@vm_101 mysql]# vim /etc/man.config
MANPATH /usr/local/mysql/man        ##添加这一行

    11、输出mysql的库文件

1
2
3
[root@vm_101 mysql]# vim /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib                ##添加这一行
[root@vm_101 mysql]# ldconfig

    12、输出mysql的头文件到系统头文件

1
2
[root@vm_101 mysql]# ln -sv /usr/local/mysql/include/ /usr/include/mysql
create symbolic link `/usr/include/mysql' to `/usr/local/mysql/include/'

    13、测试链接mysql

1
2
3
4
5
6
7
8
9
10
[root@vm_101 mysql]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.20 Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

    14、为mysql用户添加密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@vm_101 mysql]# /usr/local/mysql/bin/mysqladmin -uroot password "123456"
Warning: Using a password on the command line interface can be insecure.
[root@vm_101 mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@vm_101 mysql]# mysql -uroot -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.20 Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


六、编译安装PHP

    1、解压缩

1
2
3
[root@vm_101 ~]# tar xf php-5.4.32.tar.bz2 -C /opt/
[root@vm_101 ~]# cd /opt/php-5.4.32/
[root@vm_101 php-5.4.32]# mkdir /usr/local/php        ###创建安装目录

    2、配置PHP的编译参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 [root@vm_101 php-5.4.32]# ./configure --prefix=/usr/local/php \
 --with-config-file-path=/usr/local/php/etc \
 --with-mysql=/usr/local/mysql \
 --with-mysqli=/usr/local/mysql/bin/mysql_config \
 --disable-debug --disable-ipv6 --with-iconv-dir \
 --with-freetype-dir=/usr/local/phpextend \
 --with-jpeg-dir=/usr/local/phpextend \
 --with-png-dir=/usr/local/phpextend \
 --with-zlib --with-libxml-dir=/usr \
 --enable-xml --disable-rpath --enable-bcmath \
 --enable-shmop --enable-sysvsem \
 --enable-inline-optimization --with-curl \
 --enable-mbregex --enable-fpm --enable-mbstring  \
 --with-gd --enable-gd-native-ttf --with-openssl \
 --with-mhash --enable-pcntl \
 --enable-sockets --with-xmlrpc --enable-zip --enable-soap

出现以下提示后,说明配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+
Thank you for using PHP.
config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: main/php_config.h is unchanged
config.status: executing default commands

     3、编译安装

1
[root@vm_101 php-5.4.32]# make && make install

    4、为PHP提供配置文件

1
[root@vm_101 php-5.4.32]# cp php.ini-production /usr/local/php/etc/php.ini

    5、为php-fpm提供启动脚本并将其添加进服务列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@vm_101 php-5.4.32]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@vm_101 php-5.4.32]# chmod +x /etc/init.d/php-fpm
[root@vm_101 php-5.4.32]# chkconfig --add php-fpm
[root@vm_101 php-5.4.32]# chkconfig php-fpm on
[root@vm_101 php-5.4.32]# chkconfig php-fpm --list 
php-fpm        0:off1:off2:on3:on4:on5:on6:off
[root@vm_101 php-5.4.32]# cd /usr/local/php/etc/
[root@vm_101 etc]# ls 
pear.conf  php-fpm.conf.default  php.ini
[root@vm_101 etc]# cp php-fpm.conf.default php-fpm.conf
[root@vm_101 etc]# vim php-fpm.conf
user = nginx            #为了保持跟nginx一致,避免以后出现因为文件权限的问题,
group = nginx            #导致网页访问异常。
[root@vm_101 etc]# /etc/init.d/php-fpm start
Starting php-fpm  done
[root@vm_101 etc]# ps -ef | grep php 
root     13466     1  0 19:56 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)                                                                    
nginx    13467 13466  0 19:56 ?        00:00:00 php-fpm: pool www                                                                                                            
nginx    13468 13466  0 19:56 ?        00:00:00 php-fpm: pool www                                                                                                            
root     13471 13367  0 19:57 pts/0    00:00:00 grep php

七、整合nginx与PHP

    1.什么是 FastCGI

    FastCGI是一个可伸缩地、高速地在HTTP server和动态脚本语言间通信的接口。多数流行的HTTP server都支持FastCGI,包括Apache、Nginx和lighttpd等。同时,FastCGI也被许多脚本语言支持,其中就有PHP。

    FastCGI是从CGI发展改进而来的。传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后将结果返回给HTTP服务器。这在处理高并发访问时几乎是不可用的。另外传统的CGI接口方式安全性也很差,现在已经很少使用了。

    FastCGI接口方式采用C/S结构,可以将HTTP服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当HTTP服务器每次遇到动态程序时,可以将其直接交付给FastCGI进程来执行,然后将得到的结果返回给浏览器。这种方式可以让HTTP服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。

    2.Nginx+FastCGI运行原理

 Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程。如下图:

wKiom1RgDIWCFlchAAVoMv6cIic520.jpg

   

(以上摘自《高性能Linux服务器构建实战》P22-23,作者:高俊峰)


3、在Nginx的配置文件中添加:

[root@vm_101 html]# vi /etc/nginx/server.conf

server {

        listen       80;

        server_name  www.test.com;

location / {

            root   /home/html;

            index  index.php;

        }

location ~ \.php$ {

            root   /home/html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

}

4、启动php-fpm

[root@vm_101 html]# /etc/init.d/php-fpm start

Starting php-fpm  done

5、创建php的测试页:

[root@vm_101 html]# vim /home/html/index.php

<?php

phpinfo();

?>

访问以下:

wKiom1RgD6uh9svJAAQP-oSpOYI789.jpg

八、安装配置PHP加速器Xcache

    1、解压:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@vm_101 ~]# tar xf xcache-3.2.0.tar.gz -C /opt/
[root@vm_101 ~]# cd /opt/xcache-3.2.0
    2、安装:(安装过程可以参考官方提供的./INSTALL
[root@vm_101 xcache-3.2.0]# /usr/local/php/bin/phpize --clean && /usr/local/php/bin/phpize
Cleaning..
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@vm_101 xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
.
.
.
[root@vm_101 xcache-3.2.0]# make && make install 
Build complete.
Don't forget to run 'make test'.
 
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
[root@vm_101 xcache-3.2.0]# make test

     3、修改相关的配置文件:

[root@vm_101 xcache-3.2.0]# cat /opt/xcache-3.2.0/xcache.ini >> /usr/local/php/etc/php.ini

[root@vm_101 etc]# /etc/init.d/php-fpm restart

查看刚才创建的php测试页中的内容(xcache相关):

wKiom1RgKX6Cn-b4AAKbtVL3qGs452.jpg

    4、添加xcache的监控网页版的显示:

[root@vm_101 html]# cd /opt/xcache-3.2.0/

[root@vm_101 xcache-3.2.0]# cp -a htdocs/ /home/html/xcache

[root@vm_101 html]# cd /opt/xcache-3.2.0/

[root@vm_101 xcache-3.2.0]# cd /home/html/xcache/

[root@vm_101 xcache]# ls 

cacher  common  config.default.php  config.example.php  coverager  diagnosis  index.php

[root@vm_101 xcache]# cp config.default.php config.php

    5、xcache加密

浏览器访问:http://www.test.com/xcache/cacher/mkpassword.php

输入:name

    password

wKiom1RgLMji5rphAAHGaDF-xe4359.jpg

将生成的用户名和加密后的密码添加到xcache的配置文件中:

[root@vm_101 xcache]# vim /usr/local/php/etc/php.ini

wKioL1RgLgnwF_mYAAEYTCCbtHc568.jpg


[root@vm_101 ~]# /etc/init.d/php-fpm restart

浏览器访问:http://www.test.com/xcache

输入之前设置的用户名、密码:

wKioL1RgPkySD87YAAR9Itzku5E374.jpg到此,xcache的安装配置就好了,有关xcache的详细配置及其使用请查看其官网

http://xcache.lighttpd.net/


九、安装配置memcached


0 0
原创粉丝点击