准备阶段

1、操作系统

Red Hat Enterprise Linux Server 6.3

2、配置 IP 为 192.168.10.250

#vim /etc/sysconfig/network-scripts/ifcfg-eth0

clip_p_w_picpath001

#service network restart

3、配置防火墙,开启22端口、80端口、3306端口

#vi /etc/sysconfig/iptables  #编辑防火墙配置文件

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
(允许80端口通过防火墙)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙)

4、关闭SELinux

#vim /etc/selinux/config

修改SELINUX=disabled 并重启系统。

clip_p_w_picpath002

或者用命令:

[root@test ~]# setenforce 0  #用命令不用重启系统,但这只是暂的关闭 SELinux,系统重启后即失效 。

5、下载软件包

nginx-1.2.2.tar.gz

mysql-5.5.25a.tar.gz

php-5.4.4.tar.gz

其它相关软件包和依赖包:

cmake-2.8.8.tar.gz   #MySQL编译工具

eaccelerator-0.9.6.1.zip  #php加速器,使PHP程序代码执效率能提高1-10倍;

xcache-2.0.1.tar.gz #php加速器,用来替代eaccelerator

google-perftools-1.9.1.tar.gz 

ImageMagick-6.7.8-2.tar.gz  #功能强大、稳定而且免费的工具集和开发包;

imagick-3.1.0RC2.tgz  #PHP的一个扩展程序,它是默认的GD图象函数库的绝佳替代方案。

libiconv-1.14.tar.gz

libmcrypt-2.5.8.tar.gz

mcrypt-2.6.8.tar.gz

memcache-3.0.6.tgz

memcached-1.4.13.tar.gz  #高性能的分布式的内存对象缓存系统;可替换 memcache-3.0.6.tgz

mhash-0.9.9.9.tar.bz2

ncurses-5.9.tar.gz

openssl-1.0.1c.tar.gz

pcre-8.31.tar.gz

php-fpm-0.6-103~5.3.1.tar.gz

wordpress-3.4.1-zh_CN.zip   #wordpress个人博客

所有的软件包都放在 /data1/lnmp/ 这个目录下面。

注意:

eAccelerator 是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使PHP程序代码执效率能提高1-10倍。

Memcache 是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。php中的所讲的memcache是PHP的一个扩展,是一个客户端,用于连接 memcached。

ImageMagick 是一套功能强大、稳定而且免费的工具集和开发包,可以用来读写和处理超过89种基本格式的图片文件。利用ImageMagick,你可以根据web应用程序的需要动态生成图片, 还可以对一个(或一组)图片进行改变大小、旋转、锐化、减色或增加特效等操作,并将操作的结果以相同格式或其它格式保存。

6、安装编译工具及库文件

根据情况选择相关软件包:

[root@test ~]# yum install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd  kernel keyutils  patch  perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch

脚本实现检查各软件包安装情况:

必要时选择安装这些软件包,也可源码安装:

for i in ` rpm -q gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss-ldap openldap-clients openldap-servers bison bison-devel readline readline-devel libmcrypt libmcrypt-devel net-snmp-devel libXpm* libxml2 libxml2-devel patch|grep 'not installed' | awk '{print $2}' `; 

do

yum -y install $i;

done

注意:

至少要安装 gcc gcc-c++ 等工具。其它的软件包可以用 YUM 安装,也可以用源码安装。

安装 nginx

一、安装相关依赖包(即支持库)

注意

这里也可以通过 YUM 安装相关的依赖包。

在安装 nginx 时,可以根据错误提示,选择安装依赖包。可以用 YUM 安装,也可以用源码安装。

1、安装 pcre

[root@test lnmp]# tar -xzf pcre-8.31.tar.gz

[root@test lnmp]# cd pcre-8.31

[root@test pcre-8.31]# ./configure

[root@test pcre-8.31]# make && make install

2、安装 openssl

[root@test lnmp]# tar openssl-1.0.1c.tar.gz

[root@test lnmp]# cd openssl-1.0.1c

[root@test openssl-1.0.1c]# ./config

[root@test openssl-1.0.1c]# make && make install

注意:

新的ssl库在/usr/local/ssl/。也可以指定路径,如:#config --prefix=/usr/local/openssl/;凡是安装其它软件包时涉及到ssl的,需要指定openssl的安装路径/usr/local/openssl/。

二、源码安装 nginx

1、建立运行 nginx 用户和组

[root@test ~]# groupadd nginx

[root@test ~]# useradd -M -g nginx -s /sbin/nologin nginx

2、建立相关目录

[root@test ~]# mkdir /var/log/nginx

[root@test ~]# mkdir -p /tmp/nginx/client

[root@test ~]# mkdir /tmp/nginx/proxy

[root@test ~]# mkdir /tmp/nginx/fastcgi

3、源码安装 nginx

[root@test ~]# cd /data1/lnmp/

[root@test lnmp]# tar -xzf nginx-1.2.2.tar.gz

[root@test lnmp]# cd nginx-1.2.2

[root@test  nginx-1.2.2] #./configure --prefix=/usr/local/nginx \

--user=nginx \

--group=nginx \

--with-http_stub_status_module \  #安装可以查看nginx状态的程序

--with-http_ssl_module \

--with-http_flv_module \

--with-http_gzip_static_module \

--pid-path=/var/run/nginx.pid \   #修改nginx.conf文件中定义的nginx.pid位置

--lock-path=/var/lock/nginx.lock \  #修改nginx.conf文件中定义的nginx.lock位置

--error-log-path=/var/log/nginx/error.log \  #修改nginx.conf文件中定义的error.log位置

--http-log-path=/var/log/nginx/access.log \  #修改nginx.conf文件中定义的access.log位置

--http-client-body-temp-path=/var/tmp/nginx/client/ \

--http-proxy-temp-path=/var/tmp/nginx/proxy/ \

--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ \

--with-pcre=/usr/local/lib \  #启用正规表达式

--with-openssl=/usr/local/ssl  #启用ssl

[root@test nginx-1.2.2]# make

[root@test nginx-1.2.2]# make install

clip_p_w_picpath003

三、启动 nginx

[root@test ~]# /usr/local/nginx/sbin/nginx

注意:

#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

这样在使用 nginx 命令时,就不用使用绝对路径下达个这指令了。

现在来访问nginx服务:

clip_p_w_picpath004

注意:

在访问nginx前,先关闭iptables,SELinux。待所有的服务配置完成后,根据需再决定是否开启相关安全功能。一般生产环境中都不会开启iptables和SELinux。

四、建立 nginx 管理脚本

1、在 /etc/init.d/ 目录下创建 nginxd 文件 内容如下:

[root@test ~]# vim /etc/init.d/nginxd

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.1.3.0 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

.  /etc/rc.d/init.d/functions

# Source networking configuration.

.  /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid

}

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

2、设置开机自动启动 nginxd

[root@test ~]# chmod 755 /etc/rc.d/init.d/nginxd

[root@test ~]# chkconfig --add nginxd

[root@test ~]# chkconfig --level 35 nginxd on

[root@test ~]# service nginxd restart

五、 nginx 相关命令

1、nginx 命令

#nginx -h #帮助

#nginx -v #显示版本

#nginx -V #显示版本和配置信息

#nginx -t #测试配置

#nginx -q #测试配置时,只输出错误信息

#nginx -s stop #停止服务器

#nginx -s reload #重新加载配置

#nginx -s quit #从容停止nginx

#nginx -s reopen #不知道,估计和reload类似

#nginx -p prefix_path #默认为/usr/local/nginx(nginx安装路径)

#nginx -c conf_file_path #配置文件路径,默认为conf/nginx.conf,有多个配置文件时很有用,用这个可以启动多个不同的nginx监听不同端口

2、停止 nginx

停止nginx的方法有很多,一般通过发送系统信号给nginx主进程的方式来停止nginx。

nginx 支持下面几种信号:

● TERM,INT 快速关闭;

● QUIT  从容关闭;

● HUP  平滑重启,重新加载配置文件;

● USER1 重新打开日志文件;在切割日志时使用;

● USER2 平滑升级可执行程序;

● WINCH  从容关闭工作进程。

查看nginx进程的ID

clip_p_w_picpath005

其中一个备注信息为:master process的进程,它是主进程,进程号为8810。另外的四个进程备注信息为worker process,它们是子进程。

nginx.conf中指定了pid文件存放的路径 /var/run/nginx.pid,该文件中就是nginx当前的主进程号。

clip_p_w_picpath006

(1)从容停止 nginx

[root@test ~]# kill -QUIT 8810

[root@test ~]# kill -QUIT `/var/run/nginx.pid`

(2)快速停止 nginx

[root@test ~]# kill -TERM 8810

[root@test ~]# kill -TERM `/var/run/nginx.pid`

[root@test ~]# kill -INT 8810

[root@test ~]# kill -INT `/var/run/nginx.pid`

(3)强制停止所有 nginx 进程

[root@test ~]# pkill -9 nginx

 

六、fastcgi_params 和 fastcgi.conf

默认这两个文件是存在的,且内容相同。文件内容

[root@test ~]# vim /usr/local/nginx/conf/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200;

七、每天定时切割 nginx 日志

(1)创建脚本

[root@test ~]# vim /usr/local/nginx/sbin/cut_nginx_log.sh

#!/bin/bash

#This script run at 00:00

#

#The Nginx logs path

logs_path="/var/log/nginx/"

mkdir -p ${logs_path}$(date +%Y)/$(date +%m)/

mv ${logs_path}access.log ${logs_path}$(date +%Y)/$(date +%m)/access_$(date +F -d -1days).log

kill -USR1 'cat /var/run/nginx.pid`

(2)设置 crontab,每天 00:00 切割日志

[root@test ~]# crontab -e

00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh

安装MySQL

一、安装依赖包

1、ncurses

[root@test ~]# cd /data1/lnmp/

[root@test lnmp]# tar -xzf ncurses-5.9.tar.gz

[root@test lnmp]# cd ncurses-5.9

[root@test ncurses-5.9]# ./configure

[root@test ncurses-5.9]# make && make install

2、bison

[root@test lnmp]# tar -xzf bison-2.5.tar.gz

[root@test lnmp]# cd bison-2.5

[root@test bison-2.5]# ./configure

[root@test bison-2.5]# make && make install

二、安装用于编译 MySQL 的 cmake 工具

[root@test lnmp]# tar -xzf cmake-2.8.8.tar.gz

[root@test lnmp]# cd cmake-2.8.8

[root@test cmake-2.8.8]# ./configure

[root@test cmake-2.8.8]# make && make install

三、源码安装 MySQL

1、建立运行 MySQL 的用户和组

[root@test ~]# groupadd mysql

[root@test ~]# useradd -M -g mysql -s /sbin/nologin mysql

2、建立相关目录

[root@test ~]# mkdir /usr/local/mysql

[root@test ~]# mkdir /var/lock/mysql

[root@test ~]# mkdir /var/run/mysql

[root@test ~]# mkdir /var/log/mysql

[root@test ~]# mkdir /data1/mysql_db

[root@test ~]# chown -R mysql:mysql /usr/local/mysql/

[root@test ~]# chown mysql:mysql /var/run/mysql/

[root@test ~]# chown mysql:mysql /var/lock/mysql/

[root@test ~]# chown mysql:mysql /var/log/mysql/

[root@test ~]# chown mysql:mysql /data1/mysql_db/

--------------------------------------------------------------------------

注意:

之所以要创建/var/lock/mysql和/var/run/mysql目录,这是因为mysql server是用mysql这个用户启动的。而/var/lock/和/var/run/这两个目录只有root才有写的权限,所以mysql这个用户不能在其中建立mysql.lock和mysql.pid文件。所以要在/var/lock/和/var/run/这两个目录分别再建立一个目录,例如:mysql。并把这个目录所有者和群组改为mysql这具用户和群组。此种做法也是为了便于管理,例如把日志存放在/var/log/mysql/,日志只会在这一个目录下轮转,便于管理;#chown mysql:mysql /var/run/mysql/ /var/lock/mysql/ /var/log/mysql/

错误:

如果数据库 /data1/mysql_db/ 目录权限设置不正确,在启动 mysql 时可能会出现下面的错误。

[root@test ~]# tail /var/log/mysql.log

120713 17:15:21 mysqld_safe Starting mysqld daemon with databases from /data1/mysql_db

/usr/local/mysql/bin/mysqld: File './mysql-bin.index' not found (Errcode: 13)

120713 17:15:21 [ERROR] Aborting

120713 17:15:21 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

120713 17:15:21 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended

[root@test ~]#

解决方法:

这里是用mysql这个用户启动数据库的。所以要把建立的数据库目录 /data1/mysql_db/ 的所有者和群组更改为mysql。

[root@test scripts]# ./mysql_install_db --user=mysql --datadir=/data1/mysql_db/ --asedir=/usr/local/mysql/

[root@test ~]#chown -R mysql:mysql /data1/mysql_db/

#如果上面初始化数据库时指定了 --user=mysql,这一步就不用做了。只需把 mysql_db/ 这个目录的所有者和群组改为 mysql 这个用户即可,而这一步,我们在建立数据库目录的时候就已经做了。

这一步最好在初始化完数据库时再做一下。初始化完成后,可以先检查这个目录下的新建立的文件和目录的权限是否正确,如果正确则不用再做这一步了。

为了安全可以指定用户 mysql 初始化数据库:

#./mysql_install_db --user=mysql \

--defaults-file=/etc/my.cnf \

--basedir=/usr/local/mysql/ \

--datadir=/data1/mysql_db

--------------------------------------------------------------------------

3、源码安装 MySQL

[root@test ~]# cd /data1/lnmp/

[root@test lnmp]# tar -xzf mysql-5.5.25a.tar.gz

[root@test lnmp]# cd mysql-5.5.25a

[root@test mysql-5.5.25a]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DSYSCONFDIR=/etc \

-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \

-DMYSQL_DATADIR=/data1/mysql_db \

-DEXTRA_CHARSETS=all \

-DDEFAULT_CHARSET=utf8 \

-DWITH_READLINE=1

-DWITH_SSL=system \

-DWITH_DEBUG=0 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_ARCHIVE_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DINSTALL_LAYOUT=STANDALONE \

-DENABLED_LOCAL_INFILE=1 \

-DENABLED_PROFILING=1 \

-DMYSQL_MAINTAINER_MODE=0 \

-DWITH_EMBEDDED_SERVER=1 \

-DMYSQL_TCP_PORT=3306 \

-DMYSQL_USER=mysql

[root@test mysql-5.5.25a]# make

[root@test mysql-5.5.25a]# make install

--------------------------------------------------------------------------------------------------

cmake 代码:

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_DATADIR=/data1/mysql_db -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_DEBUG=0 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DINSTALL_LAYOUT=STANDALONE -DENABLED_LOCAL_INFILE=1 -DENABLED_PROFILING=1 -DMYSQL_MAINTAINER_MODE=0 -DWITH_EMBEDDED_SERVER=1 -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql

--------------------------------------------------------------------------------------------------

[root@test mysql-5.5.25a]# cmake . LH

注意:

在编译的时候有可能会出现以下错误。

-- Could NOT find Curses (missing:  CURSES_LIBRARY CURSES_INCLUDE_PATH)

CMake Error at cmake/readline.cmake:83 (MESSAGE):

  Curses library not found.  Please install appropriate package,

      remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.

Call Stack (most recent call first):

  cmake/readline.cmake:127 (FIND_CURSES)

  cmake/readline.cmake:217 (MYSQL_USE_BUNDLED_LIBEDIT)

  CMakeLists.txt:268 (MYSQL_CHECK_READLINE)

-- Configuring incomplete, errors occurred!

错误原因:

这是因为没有安装curses library的原因,如果是Debian/Ubuntu平台则要安装libncurses5-dev,RHEL平台安装ncurses-devel。

解决方法:

[root@test mysql-5.5.25a]# yum install ncurses-devel #或源码安装

[root@test mysql-5.5.25a]# rm -rf CMakeCache.txt    #删除CMakeCache.txt文件

[root@test mysql-5.5.25a]# cmake .  #再重新编译,即可!

--------------------------------------------------------------------------------------------------

4、建立 MySQL 配置文件

[root@test mysql-5.5.25a]# cp support-files/my-huge.cnf /etc/my.cnf

[root@test mysql-5.5.25a]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld

[root@test mysql-5.5.25a]# chmod 755 /etc/rc.d/init.d/mysqld

[root@test mysql-5.5.25a]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /ect/profile

[root@test mysql-5.5.25a]# source /etc/profile

--------------------------------------------------------------------------------------------------

注意:

在mysql-5.5.25a/support-files下共有5种my.cnf配置文件。

★ my-small.cnf:适用于小型数据库,该配置文件专为物理内存小于64MB的服务器而设计。

★ my-medium.cnf:适用于物理内存在32MB到64MB之间的专用于运行MySQL服务器,或物理内存在128MB以上。

★ my-large.cnf:适用于物理内存在512MB以上的专用于运行MySQL数据库的服务器。

★ my-huge.cnf:适用于物理内存在1GB到2GB之间的专用于运行MySQL数据库服务器。

★ my-innodb-heavy-4G.cnf:适用于服务器物理内存在4GB以上,且需要运行复杂查询的MySQL数据库。

--------------------------------------------------------------------------------------------------

5、修改 /etc/rc.d/init.d/mysqld 脚本文件

根据 cmake 时指定的配置参数和 my.cnf 配置文件中的参数修改 mysqld 这个脚本中的变量;my.cnf 配置文件中的参数优先于 cmake 时指定的参数。如果 my.cnf 配置文件没有设置对应的参数,则数据库运行时会以 cmake 时指定的参数为准。my.cnf 中参数优先级高于 cmake 时的配置参数。

[root@test ~]# vim /etc/rc.d/init.d/mysqld

basedir=/usr/local/mysql

datadir=/data1/mysql_db

lockdir='/var/lock/mysql'

lock_file_path="$lockdir/mysql.lock"

mysqld_pid_file_path = /var/run/mysql/mysql.pid

6、修改 /etc/my.cnf 配置文件

根据 cmake 时的配置修改 my.cnf 文件的部分参数。如果 my.cnf 配置文件没有设置对应的参数,则数据库运行时会以 cmake 时指定的参数为准。my.cnf 中参数优先级高于 cmake 时的配置参数。

[root@test ~]# vim  /etc/my.cnf

# The following options will be passed to all MySQL clients

[client]

#password       = your_password

port            = 3306

socket          = /tmp/mysql.sock

# The MySQL server

[mysqld]

user            = mysql

port            = 3306

socket          = /tmp/mysql.sock

basedir = /usr/local/mysql

datadir         = /data1/mysql_db

pid-file        = /var/run/mysql/mysql.pid

log-error       = /var/log/mysql/mysql.log

character-set-server = utf8

7、初始化数据库

[root@test scripts]# pwd

/usr/local/mysql/scripts

[root@test scripts]# ll

total 16

-rwxr-xr-x. 1 mysql mysql 14816 Jul 13 14:50 mysql_install_db

[root@test scripts]# ./mysql_install_db --user=mysql --datadir=/data1/mysql_db/ --asedir=/usr/local/mysql/

[root@test ~]# chown -R mysql:mysql /data1/mysql_db/

#如果上面初始化数据库时指定了 --user=mysql,这一步就不用做了。只需把 mysql_db/ 这个目录的所有者和群组改为 mysql 这个用户即可,而这一步,我们在建立数据库目录的时候就已经做了。

--------------------------------------------------------------------------------------------------

注意:

mysql_install_db 脚本的目的是生成新的 mysql 授权表。它不覆盖已有的 mysql 授权表,并且它不影响任何其它数据。

如果你想要重新创建授权表,首先停止 mysqld 服务器(如果它正运行)。然后重新命名数据目录下的 mysql/目录并保存,然后运行 mysql_install_db。

本例中可用的参数:

#./mysql_install_db --user=mysql \

--defaults-file=/etc/my.cnf \

--basedir=/usr/local/mysql/ \

--datadir=/data1/mysql_db

--------------------------------------------------------------------------

8、设置开机启动 mysql

[root@test ~]# chkconfig --level 35 mysqld on

[root@test ~]# service mysql start

--------------------------------------------------------------------------

注意:启动MySQL时出现以下错误:

[root@test ~]# service mysqld start

Starting MySQL.The server quit without updating PID file (/[FAILED]mysql/mysql.pid).

[root@test ~]# tail /var/log/mysql.log

120713 16:26:12 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

120713 16:26:12 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended

120713 16:41:36 mysqld_safe Starting mysqld daemon with databases from /data1/mysql_db

/usr/local/mysql/bin/mysqld: Character set 'all' is not a compiled character set and is not specified in the '/usr/local/mysql/share/charsets/Index.xml' file

120713 16:41:36 [ERROR] Aborting

120713 16:41:36 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

120713 16:41:36 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended

[root@test ~]#

错误原因:

这是因为我在编译 MySQL 的时候加上了 -DDEFAULT_CHARSET=all 参数。最好设为-DDEFAULT_CHARSET=utf8。

解决方法:

修改配置文件/etc/my.cnf文件,在[mysqld]下加入character-set-server = utf8。

[mysqld]

character-set-server = utf8

--------------------------------------------------------------------------------------------------

9、设置 mysql 用户 root 的密码

[root@test ~]# mysqladmin -u root -p password 123456

[root@test ~]# mysql -u root -p -S /tmp/mysql.sock

Enter password:  <-----输入上面设置的数据库管理员密码:123456,回车即可登录数据库。

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.5.25a-log Source distribution

Copyright (c) 2000, 2011, 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>

如下图:

clip_p_w_picpath007

在初始化 mysql 数据库后,其中默认生成了4个数据库;每个数据库下都会有若干个表。

安装 PHPFastCGI模式

必要时可以用 YUM 选择安装以下相关软件包:

#yum install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss-ldap openldap-clients openldap-servers bison bison-devel readline readline-devel libmcrypt libmcrypt-devel net-snmp-devel libXpm* libxml2 libxml2-devel patch

一、安装相关依赖包(即支持库)

1、libiconv (对文本进行编码间的转换,用它来处理中文各种编码之间的转换)

[root@test lnmp]# tar -xzf libiconv-1.14.tar.gz

[root@test lnmp]# cd libiconv-1.14

[root@test libiconv-1.14]# ./configure

[root@test libiconv-1.14]# make && make install

2、libmcrypt (实现加密功能的库)

[root@test lnmp]# tar -xzf libmcrypt-2.5.8.tar.gz

[root@test lnmp]# cd libmcrypt-2.5.8

[root@test libmcrypt-2.5.8]# ./configure

[root@test libmcrypt-2.5.8]# make && make install

[root@test libmcrypt-2.5.8]# cd libltdl/

[root@test libltdl]# ./configure --enable-ltdl-install

[root@test libltdl]# make && make install

3、mhash (哈稀函数库)

[root@test lnmp]# tar xjf mhash-0.9.9.9.tar.bz2

[root@test lnmp]# cd mhash-0.9.9.9

[root@test mhash-0.9.9.9]# ./configure

[root@test mhash-0.9.9.9]# make && make install

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/libmcrypt.so.4.4.8

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

[root@test mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

[root@test mhash-0.9.9.9]# ldconfig  #不重新加模块,下面编译 mcrypt 时,检查编译环境会出错。

-----------------------------------------------------------------------------------------------------------------------------------------

在#./configure 检查 php 编译环境时可能会出现以下错误

checking whether mhash &gt;= 0.8.15... no

configure: error: "You need at least libmhash 0.8.15 to compile this program. http://mhash.sf.net/"

原因:

mhash编译默认安装路径是 /usr/local/,相关库在 /usr/local/lib 下。而 php 只搜索 /usr/lib/ 这个位置。所以会出现找不到 mhash 相关库的情况。

解决方法:

通过软链接的方法把 /usr/local/lib 目录下的 mhash 相关的库文件链接到 /usr/lib/ 目录下。

-----------------------------------------------------------------------------------------------------------------------------------------

4、mcrypt

[root@test lnmp]# tar -xzf mcrypt-2.6.8.tar.gz

[root@test lnmp]# cd mcrypt-2.6.8

[root@test mcrypt-2.6.8]# ./configure

[root@test mcrypt-2.6.8]# make && make install

二、源码安装 php(FastCGI 模式)

PHP-FPM 是 一个 PHP FastCGI 进程管理器,即 PHP FastCGI Progress Manager。

FastCGI 是一个可伸缩的,高速地在 web server 和脚本语言间交互的接口。FastCGI 的主要优点是把动态语言和 web server 分离开来。这种技术允许把 web server 和动态语言运行在不同的主机上,以大规模扩展和改进安全性而不损失生产效率。

php-fpm 可以和任何支持远端 FastCGI 的 web server 工作。

php-5.3.3 版本之后,php-fpm 已经被包含在 core 里边了,不再是第三方的包了。不需要再打补丁了。

PHP-FPM 提供了更好的 PHP 进程管理方式,可以有效控制内存和进程、可以平滑重载 PHP 配置,比 spawn-fcgi 具有更多有点,所以被 PHP 官方收录了。在 ./configure 的时候带 -–enable-fpm 参数即可开启 PHP-FPM。

1、安装 php

[root@test lnmp]# mkdir /usr/local/php

[root@test lnmp]# tar -xzf php-5.4.4.tar.gz

[root@test lnmp]# cd php-5.4.4

[root@test php-5.4.4]# ./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 \

--with-iconv-dir=/usr/local \

--with-freetype-dir \

--with-jpeg-dir \

--with-png-dir \

--with-zlib \

--with-gd \

--with-libxml-dir=/usr \

--enable-xml \

--disable-rpath \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-inline-optimization \

--with-curl \

--with-curlwrappers \

--enable-mbregex \

--enable-fpm \

--with-pdo-mysql=/usr/local/mysql \ #可选参数,如果这里指定了,在安装pdo_mysql时就不用指定了。

--enable-mbstring \

--with-mcrypt \

--enable-gd-native-ttf \

--with-openssl \

--with-mhash \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-zip \

--enable-soap \

--without-pear

[root@test php-5.4.4]# make ZEND_EXTRA_LIBS='-liconv'

[root@test php-5.4.4]# make install

configure 代码如下:

-----------------------------------------------------------------------------------------------------------------------------------------

./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 --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-gd --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --with-pdo-mysql=/usr/local/mysql --enable-mbstring --with-mcrypt --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear

-----------------------------------------------------------------------------------------------------------------------------------------

可能出现的错误:

1、configure: error: xml2-config not found. Please check your libxml2 installation.

解决方法:

http://www.xmlsoft.org/

源码安装 libxml2 或通过 yum 安装 libxml2-devel。

(1)[root@test lnmp]# tar -xzf libxml2-tests-2.8.0.tar.gz

(2)[root@test lnmp]#yum install  libxml2-devel

2、 configure: error: Please reinstall the libcurl distribution -

    easy.h should be in <curl-dir>/include/curl/

解决方法:

http://curl.haxx.se/

源码安装 curl 或通过 yum 安装 libcurl-devel。

(1)[root@test lnmp]# tar -xzf curl-7.26.0.tar.gz

(2)[root@test lnmp]#yum install libcurl-devel

3、configure: error: jpeglib.h not found.

解决方法:

http://www.ijg.org/

源码安装 jpegsrc 或通过 yum 安装 libjpeg-devel。

(1)[root@test lnmp]# tar -xzf jpegsrc.v8d.tar.gz

(2)[root@test lnmp]#yum install libjpeg-devel

4、configure: error: png.h not found.

解决方法:

http://www.libpng.org/pub/png/libpng.html

源码安装 libpng 或通过 yum 安装 libpng-devel。

(1)[root@test lnmp]# tar -xzf libpng-1.5.12.tar.gz

(2)[root@test lnmp]#yum install libpng-devel

4、configure: error: freetype.h not found.

解决方法:

http://www.freetype.org/

源码安装 freetype 或通过 yum 安装 libfreetype-devel。

(1)[root@test lnmp]# tar -xzf freetype-2.4.10.tar.gz

(2)[root@test lnmp]#yum install libfreetype-devel

编译的时候可能出现的错误:

5、Generating phar.php

/data1/lnmp/php-5.4.4/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory

make: *** [ext/phar/phar.php] Error 127

解决方法:

#echo "/usr/local/mysql/lib/" &gt;&gt; /etc/ld.so.conf.d/mysql-x86_64.conf

6、PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

两种解决方法:

(1)#./configure --without-pear

(2)#wget http://pear.php.net/go-pear.phar

    #php go-pear.phar

7、configure: WARNING: unrecognized options: --enable-discard-path, --enable-safe-mode

警告原因:

之前 php-5.3.x 编译时加了 --enable-safe-mode --enable-discard-path --enable-fastcgi --enable-force-cgi-redirect选项,但是这个选项没必要,且会带来许多的麻烦,详解可以 google 一下,php-5.4.x 已经去掉了该选项,编译时可以 #./configure --help | grep “safe-mode” 查看一下,没有信息输出,表示已经不支持!

-----------------------------------------------------------------------------------------------------------------------------------------

2、建立相关配置文件

[root@test php-5.4.4]# cp php.ini-production /usr/local/php/etc/php.ini

[root@test php-5.4.4]# vim /etc/profile

找到 export PATH=$PATH:/usr/local/mysql/bin 这一行,

修改为:export PATH=$PATH:/usr/local/mysql/bin:/usr/local/php/bin:/usr/local/php/sbin:

[root@test php-5.4.4]# source /etc/profile

二、安装 php 扩展模块

--------------------------

memcache-3.0.6.tgz

eaccelerator-0.9.6.1.tar.gz

xcache-2.0.1.tar.gz   #php加速器,用来替代eaccelerator

PDO_MYSQL-1.0.2.tgz

ImageMagick-6.7.8-2.tar.gz

imagick-3.1.0RC2.tar.gz

--------------------------

1、memcache

[root@test lnmp]# tar -xzf memcache-3.0.6.tgz

[root@test lnmp]# cd memcache-3.0.6

[root@test memcache-3.0.6]# phpize

[root@test memcache-3.0.6]# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config

[root@test memcache-3.0.6]# make

[root@test memcache-3.0.6]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

2、eaccelerator (PHP加速)

[root@test lnmp]# tar -xzf eaccelerator-0.9.6.1.tar.gz

[root@test lnmp]# cd eaccelerator-0.9.6.1

[root@test eaccelerator-0.9.6.1]# phpize

[root@test eaccelerator-0.9.6.1]# ./configure --enable-eaccelerator=shared \

--with-php-config=/usr/local/php/bin/php-config

[root@test eaccelerator-0.9.6.1]# make

-----------------------------------------------------------------------------------------------------------------------------------------

在编译的时候会出现与新版本的 PHP 不兼容的问题。而到目前为止,没有更高的版本,也就是说 eaccelerator 不支持新版的 php

如果使用 php 5.4 以上版本,就只能放弃使用 eaccelerator 加速了。

所以这里 eaccelerator 没有安装成功。可以采用 xcache 加速 PHP

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c: In function ‘eaccelerator_restore’:

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c:878: warning: assignment discards qualifiers from pointer target type

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c: In function ‘eaccelerator_compile_file’:

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c:1317: error: ‘zend_class_entry’ has no member named ‘line_start’

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c: At top level:

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c:1913: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘eaccelerator_functions’

/data1/lnmp/eaccelerator-0.9.6.1/eaccelerator.c:1938: error: ‘eaccelerator_functions’ undeclared here (not in a function)

make: *** [eaccelerator.lo] Error 1

-----------------------------------------------------------------------------------------------------------------------------------------

附:

由于 eaccelerator不支持新版的php;我用xcache替代它下载最新版支持php5.4xcache-2.0.1.tar.gz

[root@test lnmp]# tar -xzf xcache-2.0.1.tar.gz

[root@test lnmp]# cd xcache-2.0.1

[root@test xcache-2.0.1]# phpize

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

[root@test xcache-2.0.1]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@test xcache-2.0.1]# make

[root@test xcache-2.0.1]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

3、PDO_MYSQL (数据库连接的支持)

[root@test lnmp]# tar -xzf PDO_MYSQL-1.0.2.tgz

[root@test lnmp]# cd PDO_MYSQL-1.0.2

[root@test PDO_MYSQL-1.0.2]# phpize

[root@test PDO_MYSQL-1.0.2]# ./configure --with-php-config=/usr/local/php/bin/php-config \

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

[root@test PDO_MYSQL-1.0.2]# make

[root@test PDO_MYSQL-1.0.2]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

-----------------------------------------------------------------------------------------------------------------------------------------

在编译make的时候有可能会出现下面的错误:

In file included from /data1/lnmp/PDO_MYSQL-1.0.2/pdo_mysql.c:31:

/data1/lnmp/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:25:19: error: mysql.h: No such file or directory

In file included from /data1/lnmp/PDO_MYSQL-1.0.2/pdo_mysql.c:31:

/data1/lnmp/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:36: error: expected specifier-qualifier-list before ‘MYSQL’

/data1/lnmp/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:48: error: expected specifier-qualifier-list before ‘MYSQL_FIELD’

/data1/lnmp/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:53: error: expected specifier-qualifier-list before ‘MYSQL_RES’

make: *** [pdo_mysql.lo] Error 1

问题原因:

这是因为在编译时需要 MySQL 的头的文件。而它按默认搜索找不到头文件的位置,所以才出现这个问题。

解决方法:

将 /usr/local/mysql/include/ 目录下的 MySQL 头文件链接到 /usr/local/include/ 的目录下。

# ln -s /usr/local/mysql/include/* /usr/local/include/

-----------------------------------------------------------------------------------------------------------------------------------------

4、ImageMagick (Linux下强大的图象处理函数,与GD类似)

[root@test lnmp]# tar -xzf ImageMagick-6.7.8-2.tar.gz

[root@test lnmp]# cd ImageMagick-6.7.8-2

[root@test ImageMagick-6.7.8-2]# ./configure

[root@test ImageMagick-6.7.8-2]# make && make install

5、imagick (连接 PHP 和 ImageMagick 的通道)

[root@test lnmp]# tar -xzf imagick-3.1.0RC2.tgz

[root@test lnmp]# cd imagick-3.1.0RC2

[root@test imagick-3.1.0RC2]# ./configure --with-php-config=/usr/local/php/bin/php-config

[root@test imagick-3.1.0RC2]# make

[root@test imagick-3.1.0RC2]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

Installing header files:          /usr/local/php/include/php/

-----------------------------------------------------------------------------------------------------------------------------------------

make 编译的时候可能会出现下面的错误:

In file included from /data1/lnmp/imagick-3.1.0RC2/imagick_file.c:21:

/data1/lnmp/imagick-3.1.0RC2/php_imagick.h:49:31: error: wand/MagickWand.h: No such file or directory

解决方法:

[root@test imagick-3.1.0RC2]# yum install gtk+-devel #不清楚该软件包的作用。

#指定 ImageMagick 库的安装路径。这里采用将其路径宣告为当前环境变量的方法:

[root@test imagick-3.1.0RC2]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

[root@test imagick-3.1.0RC2]# rm -rf Makefile config.status config.h   #删除相关文件后,再重新编译。

./configure 的时候,会出现下面两行,说明 MagickWand.h 头文件可以被找到。这就把问题解决了。

checking ImageMagick MagickWand API configuration program... found in /usr/local/bin/MagickWand-config

checking if ImageMagick version is at least 6.2.4... found version 6.7.8 Q16

checking for MagickWand.h header file... found in /usr/local/include/ImageMagick/wand/MagickWand.h

-----------------------------------------------------------------------------------------------------------------------------------------

三、配置 php

1、修改 php.ini 文件,使 php 支持已安装的扩展功能

[root@test ~]# vim /usr/local/php/etc/php.ini

找到 extension_dir = "/";修改如下:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525"

extension = "imagick.so"

extension = "memcache.so"

extension = "pdo_mysql.so"

再查找 output_buffering = Off 修改为 output_buffering = On

short_open_tag = Off 修改为 short_open_tag = On (开启短格式支持)

expose_php = On 修改为 expose_php = Off (在curl中隐藏php版本号)

date.timezone = Asia/Shanghai

我们安装的相关扩展模块都在 /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525 目录下:

clip_p_w_picpath008

可以看到有一个xcache.so,没有 eaccelerator.so 模块,这是因为 eaccelerator 不支持较新版的 php ,这里没有安装成功,我用xcache替代eaccelerator。

2、配置 eAccelerator 或 xcache 加速PHP

1)现在假定这里 eaccelerator 安装成功了

[root@test ~]# mkdir /usr/local/eaccelerator_cache

[root@test ~]# vim /usr/local/php/etc/php.ini

php.ini 文件末尾加上以下配置:

[eaccelerator]

zend_extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so"

eaccelerator.shm_size = "64"

eaccelerator.cache_dir = "/usr/local/eaccelerator_cache"

eaccelerator.enable = "1"

eaccelerator.optimizer = "1"

eaccelerator.check_mtime = "1"

eaccelerator.debug = "0"

eaccelerator.filter = ""

eaccelerator.shm_max = "0"

eaccelerator.shm_ttl = "3600"

eaccelerator.shm_prune_period = "3600"

eaccelerator.shm_only = "0"

eaccelerator.compress = "1"

eaccelerator.compress_level = "9"

[root@test ~]# vim /etc/sysctl.conf  #加入 kernel.shmmax = 4294967295

[root@test ~]# sysctl -p

2)配置 xcache,整合phpxcache

[root@test ~]# cd /data1/lnmp/xcache-2.0.1

[root@test xcache-2.0.1]# cat xcache.ini &gt;&gt; /usr/local/php/etc/php.ini #xcache.ini文件在xcache的源码目录中。

[root@test ~]# vim /usr/local/php/etc/php.ini

找到 zend_extension 开头的行,修改如下:
zend_extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so

注意:

如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。配置完成后,重启php-fpm即生效。

2、配置 php-fpm

(1)建立相关配置文件

[root@test ~]# cd /usr/local/php/etc/

[root@test etc]# mv php-fpm.conf.default php-fpm.conf

[root@test etc]# cd /data1/lnmp/php-5.4.4

[root@test php-5.4.4]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

[root@test php-5.4.4]# vim /etc/rc.d/init.d/php-fpm

找到:php_fpm_PID=${prefix}/var/run/php-fpm.pid,删掉${prefix};

修改为:php_fpm_PID=/var/run/php-fpm.pid;

之所以这样修改是因为我在 php-fpm.conf 中设置的pid的位置为:/var/run/php-fpm.pid

(2)修改 php-fpm.conf 配置文件

[root@test etc]# vim php-fpm.conf

[global]

pid = /var/run/php-fpm.pid

error_log = /var/log/php/php-fpm.log

log_level = notice

[www]

listen = 127.0.0.1:9000

user = nginx #这是因为 nginx web 服务也是个这个用户。

group = nginx

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 5000

[root@test etc]# php-fpm -t  #测试 php-fpm 配置

(3)设置开机启动 php-fpm (通过管理 FastCGI 进程)

[root@test ~]# chmod 755 /etc/rc.d/init.d/php-fpm

[root@test ~]# chkconfig --level 35 php-fpm on

[root@test ~]# chkconfig --list php-fpm

php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@test ~]# service php-fpm

Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload}

(4)启动 php-fpm

[root@test ~]# service php-fpm start

[root@test ~]# /usr/local/php/sbin/php-fpm

clip_p_w_picpath010

clip_p_w_picpath012

-----------------------------------------------------------------------------------------------------------------------------------------

除了用 php 自带的 php-fpm 脚本来管理 FastCGI 进程,还可以通过 php-fpm 命令来管理进程:

#/usr/local/php/sbin/php-fpm {start|stop|quit|restart|reload|logrotate}

--start 启动 php 的 fastcgi 进程

--stop 强制终止 php 的 fastcgi 进程

--quit 平滑终止 php 的 fastcgi 进程

--restart 重启 php 的 fastcgi 进程

--reload 重新平滑加载 php 的 php.ini

--logrotate 重新启用 log 文件

-----------------------------------------------------------------------------------------------------------------------------------------

整合 Nginx PHP

一、整合 nginx 与 PHP

1、修改 /usr/local/nginx/conf/nginx.conf 配置文件

(1)使 nginx 支持 php 格式主页

[root@test ~]# vim /usr/local/nginx/conf/nginx.conf

location / {

            root   html;   #必须设置 web 根目录

            index index.php index.html index.htm;  #添加 php 格式的主页

        }

--------------------------------------------------------------------------

注意:

nginx 默认的 web 根目录是 /usr/local/nginx/html它的 nginx.conf 配置文件中 web 的根目录参数默认为:root html;这里使用相对路径,相对于 nginx 安装目录的路径。

--------------------------------------------------------------------------

(2)启用如下选项

location ~ \.php$ {

root           html;  #必须设置 web 根目录

fastcgi_pass 127.0.0.1:9000;  #定义 fastcgi 所监听的 IP 地址和端口号,也可以是 socket

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

#PHP 使用 SCRIPT_FILENAME 参数决定需要执行哪个脚本;这里一定要设为 web 目录的绝对路径或是变量 $document_root这里非常重要,配置错误有可能会出现php空白页,或找不到页面的错误。

fastcgi_param  QUERY_STRING     $query_string; #QUERY_STRING 包含请求中的某些参数。

            include        fastcgi_params;

        }

如果要处理POST请求,则需要另外增加三个参数:

fastcgi_param  REQUEST_METHOD   $request_method;

fastcgi_param  CONTENT_TYPE     $content_type;

fastcgi_param  CONTENT_LENGTH   $content_length;

[root@test ~]# service nginxd reload

-----------------------------------------------------------------------------------------------------------------------------------------

参考资料:

http://howtocn.org/nginx:nginx%E6%A8%A1%E5%9D%97%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E4%B8%AD%E6%96%87%E7%89%88:standardhttpmodules:httpcore#root

http://howtocn.org/nginx:nginx%E6%A8%A1%E5%9D%97%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E4%B8%AD%E6%96%87%E7%89%88:standardhttpmodules:fastcgi#fastcgi_param

http://howtocn.org/nginx:directiveindex

-----------------------------------------------------------------------------------------------------------------------------------------

2、编辑 fastcgi.conf 和 fastcgi_params

默认这两个文件是存在的,不需要修改,默认即可。

文件内容:

[root@test ~]# vim /usr/local/nginx/conf/fastcgi.conf

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200;

二、测试 nginx 与 PHP

[root@test ~]# vim /usr/local/nginx/html/index.php

输入以下内容:

<?php

phpinfo();

?>

注意:测试phpMySQL

可以使用下面的php代码用来测试php是否可以连接MySQL数据库:

-------------------------------------------------------

<?php

$link=mysql_connect('localhost','root','数据库密码');

if ($link)

echo "successful";

else

echo "Faile";

mysql_close();

?>

--------------------------------------------------------

测试这个网页:http://192.168.10.250;看到下图,即测试成功。

clip_p_w_picpath013

三、基于域名的虚拟主机

虚拟主机可以在一台服务器上面,建立多个可以独立访问web网站,且具有独立域名和完整的Internet服务器功能(www、FTP、Email等),同一台服务器上的虚拟主机是完全独立的。

利用虚拟主机,不用为每个要运行的网站提供一台单独的nginx服务器或单独运行一组nginx进程。虚拟主机提供了在同一台服务器,同一组nginx进程上运行多个网站的功能。

Nginx可以配置多种虚拟主机:1)基于IP的虚拟主机;(2)基于端口的虚拟主机;(3)基于域名的虚拟主机

这里以最常见的虚拟主机为例:基于域名的虚拟主机

第一个虚拟机:WordPress博客:

Web服务IP:192.168.10.250:80

域名:www.wordpressbt.com

Web根目录:/data1/html/wordpress/

使用的php主页:wordpress-3.4.1

第二个虚拟机:php相关info

Web服务IP:192.168.10.250:80

域名:www.phpinfo.com

Web根目录:/data1/html/phpinfo/

使用的php主页:通过建立一个php主页,列出当前php相关配置信息。

index.php内容如下:

<?php

phpinfo();

?>

DNSnginx位于同一台Server:

IP:192.168.10.250

访问web的客户端:

OS:Windows 7 64bit ultimate

IP:192.168.10.10

DNS:192.168.10.250

浏览器:chrome

客户端与服务器通过一台Cisco Catalyst 2960交换机相连。

1、建立相关文件和目录

[root@test ~]# mkdir -p /data1/html/phpinfo

[root@test ~]# cp index.php /data1/html/phpinfo/ #将创建的index.php复制到www.phpinfo.com虚拟机web根目录

[root@test ~]# cd /data1/lnmp/

[root@test lnmp]# unzip wordpress-3.4.1-zh_CN.zip

[root@test lnmp]# mv wordpress ../html/

[root@test lnmp]# chmod -R nginx:nginx /data1/html/

2、修改 /usr/local/nginx/conf/nginx.conf配置文件

[root@test ~]# vim /usr/local/nginx/conf/nginx.conf

http段加入以下内容:

第一个虚拟机:www.wordpressbt.com

server {

        listen       80;

        server_name  www.wordpressbt.com;

        access_log  /var/log/nginx/access_wp.log;

        location / {

            root   /data1/html/wordpress;

            index  index.html index.htm index.php;

        }

        location ~ \.php$ {

            root           /data1/html/wordpress;

           fastcgi_pass   127.0.0.1:9000;

           fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME /data1/html/wordpress/$fastcgi_script_name;

            fastcgi_param  QUERY_STRING     $query_string;

            include        fastcgi_params;

        }

    }

第二个虚拟机:www.phpinfo.com

server {

        listen       80;

        server_name  www.phpinfo.com;

        access_log  /var/log/nginx/access_info.log;

        location / {

            root   /data1/html/phpinfo;

            index  index.html index.htm index.php;

        }

        location ~ \.php$ {

            root           /data1/html/phpinfo;

           fastcgi_pass   127.0.0.1:9000;

           fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME /data1/html/phpinfo/$fastcgi_script_name;

            fastcgi_param  QUERY_STRING     $query_string;

            include        fastcgi_params;

        }

    }

3、修改DNS记录

DNS和nginx是在同一台Server上,DNS是以前配置好的。现在直接拿来用。这里只添加正向查询区域

(1)修改 named.zones 主配置文件

[root@test ~]# vim /var/named/chroot/etc/named.zones

加入以下两个区域:

zone "wordpressbt.com" IN {

        type master;

        file "wordpressbt.com.zero"; #这是相对路径,相对于安装目录的路径 var/named/目录。

        allow-update { none;};

};

zone "phpinfo.com" IN {

        type master;

        file "phpinfo.com.zero";

        allow-update { none;};

};

(2)创建 wordpressbt.com.zero 和 phpinfo.com.zero 区域文件

[root@test ~]# vim /var/named/chroot/var/named/wordpressbt.com.zero

clip_p_w_picpath014

[root@test ~]# vim /var/named/chroot/var/named/phpinfo.com.zero

clip_p_w_picpath015

[root@test ~]# service named restart

4、修改用来访问web服务的Windows 7客户端IP地址和DNS

打开本地链接,修改如下图:

clip_p_w_picpath016

5、通过域名访问测试验证

(1)访问域名:www.wordpressbt.com

成功访问,测试成功。

clip_p_w_picpath017

单击“创建配置文件”,如下图:

clip_p_w_picpath018

单击“现在就开始!”,如下图:

clip_p_w_picpath019

在数据库中创建一个用于存储wordpress相关数据的数据库,数据库名也为wordpress,也可以是其它的名字,再添加一个用户对该数据库有完全控制权限,用户名为:wpuser。在上面这个页面填写相关正确信息,再提交。根据提示一步一步做就可以了。这样我们就在自己服务器上建立一个wordpress博客了。下图是wordpress后台。

clip_p_w_picpath021

(2)访问域名:www.phpinfo.com

成功访问,测试成功。

clip_p_w_picpath022

现在终于完成了LNMP环境,安装很简单。最重要的是根据实际的生产环境调优。合理配置 nginx.conf、php.ini、php-fpm.conf、my.cnf,充分发挥硬件性能。没有一挫而就。

nginx真的很强大;在生产中,可以根据实际情况进行动静分离,MySQL读写分离,负载均衡,以及高可用的架构。

2012年7月18日 完成。