一、安装所需软件包
安装LEMP环境所需软件包
[root@server ~]# yum -y 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 libevent libevent-devel

二、安装mysql
[root@server ~]# yum install libaio
[root@server ~]#  useradd -s /sbin/nologin -M mysql

[root@server ~]# tar zxvf mysql-5.5.29.tar.gz
[root@server ~]# cd mysql-5.5.29

[root@server mysql-5.5.29]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DINSTALL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DMYSQL_USER=mysql

[root@server mysql-5.5.29]# make && make install

[root@server mysql-5.5.29]# cd /usr/local/mysql
[root@server mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

[root@server mysql]# chown -R mysql.mysql .
[root@server mysql]# cp support-files/my-medium.cnf  /etc/my.cnf

[root@server mysql]#  vim /etc/my.cnf
添加以下内容
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
basedir         = /usr/local/mysql
datadir         = /data/mysql
user            = mysql
character_set_server    = utf8

配置Mysql服务启动脚本
[root@server mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@server mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql

启动mysql
[root@server mysql]# service mysqld start
Starting MySQL... SUCCESS!

[root@server mysql]# ln -s /usr/local/mysql/bin/* /usr/bin/

[root@server mysql]# mysqladmin -uroot password
New password:
Confirm new password:


三、安装php
(1)安装相关软件包
[root@server ~]# cd soft/
[root@server soft]# tar zxvf libiconv-1.13.1.tar.gz
[root@server soft]# cd libiconv-1.13.1
[root@server libiconv-1.13.1]# ./configure --prefix=/usr/local/

[root@server libiconv-1.13.1]# make
[root@server libiconv-1.13.1]# make install

##############################################

[root@server soft]# tar zxvf libmcrypt-2.5.8.tar.gz
[root@server soft]# cd libmcrypt-2.5.8
[root@server libmcrypt-2.5.8]# ./configure
[root@server libmcrypt-2.5.8]# make 
[root@server libmcrypt-2.5.8]# make install
[root@server libmcrypt-2.5.8]# /sbin/ldconfig
[root@server libmcrypt-2.5.8]# cd libltdl/
[root@server libltdl]# ./configure --enable-ltdl-install
[root@server libltdl]# make
[root@server libltdl]# make install

##############################################

[root@server soft]# tar zxvf mhash-0.9.9.9.tar.gz
[root@server soft]# cd mhash-0.9.9.9
[root@server mhash-0.9.9.9]# ./configure
[root@server mhash-0.9.9.9]# make
[root@server mhash-0.9.9.9]# make install

(2)安装php
[root@server soft]# tar jxvf php-5.4.11.tar.bz2
[root@server soft]# cd php-5.4.11

[root@server php-5.4.11]#./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-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear

[root@server php-5.4.11]# make ZEND_EXTRA_LIBS='-liconv'
[root@server php-5.4.11]# make install
如果在make的过程中出现以下错误:

/root/dev/php-5.3.10/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

解决办法:

ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib64/

如果用的是32位系统的话,则用

ln -s /usr/local/mysql/lib/libmysqlclient.so.18  /usr/lib/

如果按上面的操作,再次执行 make ZEND_EXTRA_LIBS='-liconv' 后提示以下错误:

"chmod: cannot access `ext/phar/phar.phar': No such file or directory"

只需要重装执行上面的configure命令即可.只需要在./configure的后面加上--without-pear 即可解决

如果在make install的时候还提示上面类似的错误,只需要重新从./configure开始再执行一下就可以了.


[root@server php-5.4.11]# cp php.ini-production /usr/local/php/etc/php.ini
[root@server php-5.4.11]# cd /usr/local/php/etc/
[root@server etc]# cp -a php-fpm.conf.default php-fpm.conf
[root@server etc]# cd ..
[root@server php]# sbin/php-fpm          // 启动php-fpm

(3)安装php扩展

        Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

[root@server soft]# tar zxvf memcache-2.2.5.tgz
[root@server soft]# cd memcache-2.2.5
[root@server memcache-2.2.5]# /usr/local/php/bin/phpize
[root@server memcache-2.2.5]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@server memcache-2.2.5]# make
[root@server memcache-2.2.5]# make install

##########################################
eAccelerator 是一个免费开源的PHP加速、优化、编译和动态缓存的项目,它可以通过缓存PHP代码编译后的结果来提高PHP脚本的性能,使得一向很复杂和离我们很远的PHP脚本编译问题完全得到解决。通过使用eAccelerator,可以优化你的PHP代码执行速度,降低服务器负载,可以提高PHP应用执行速度最高达10倍。

eAccelerator 通过把经过编译后的PHP代码缓存到共享内存中,并在用户访问的时候直接调用从而起到高效的加速作用。它的效率非常高,从创建共享内存到查找编译后的代码都在非常短的时间内完成,对于不能缓存到共享内存中的文件和代码,eAccelerator还可以把他们缓存到系统磁盘上。

[root@server soft]# tar zxvf eaccelerator-eaccelerator-42067ac.tar.gz
[root@server soft]# cd eaccelerator-eaccelerator-42067ac
[root@server eaccelerator-eaccelerator-42067ac]# /usr/local/php/bin/phpize
[root@server eaccelerator-eaccelerator-42067ac]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@server eaccelerator-eaccelerator-42067ac]# make
[root@server eaccelerator-eaccelerator-42067ac]# make install

##########################################

[root@server soft]# tar zxvf PDO_MYSQL-1.0.2.tgz
[root@server soft]# cd PDO_MYSQL-1.0.2
[root@server PDO_MYSQL-1.0.2]# /usr/local/php/bin/phpize
[root@server PDO_MYSQL-1.0.2]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
[root@server PDO_MYSQL-1.0.2]# make
[root@server PDO_MYSQL-1.0.2]# make install

编译安装的时候出现下面错误:
[root@server PDO_MYSQL-1.0.2]# make/software/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:25:19: error: mysql.h: No such file or directory
In file included from /software/PDO_MYSQL-1.0.2/pdo_mysql.c:31:
/software/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:36: error: expected specifier-qualifier-list before 'MYSQL'
/software/PDO_MYSQL-1.0.2/php_pdo_mysql_int.h:48: error: expected specifier-qualifier-list before 'MYSQL_FIELD'
/software/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

解决办法:
[root@server PDO_MYSQL-1.0.2]# ln -s /usr/local/mysql/include/* /usr/local/include/

##########################################

(4)修改php.ini文件,配置扩展
[root@server ~]# vim /usr/local/php/etc/php.ini
找到这一行extension_dir = "./"   替换成
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525"

添加下面2行
extension = "memcache.so"
extension = "pdo_mysql.so"

修改output_buffering = On
找到expose_php = On  修改为 expose_php = off    //隐藏http头信息里的php信息
找到cgi.fix_pathinfo=1 修改为cgi.fix_pathinfo=0  //防止Nginx文件类型错误解析漏洞。
date.timezone = Asia/Shanghai

配置eAccelerator加速PHP:
[root@server ~]# mkdir -p /usr/local/eaccelerator_cache

[root@server ~]# vim /usr/local/php/etc/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"

注意,zend_extension 的路径要写对,no-debug-non-zts-xxxxxxxx/eacceleratort.so
上面的数字视具体情况而定

配置php-fpm
创建www用户,php-fpm和nginx统一使用这个
[root@server ~]# groupadd www
[root@server ~]# useradd -g www www

[root@server ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

启动php-fpm
[root@server ~]# /usr/local/php/sbin/php-fpm 
[root@server ~]# netstat -nutlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      48229/php-fpm   


四、安装nginx
(1)安装正则表达式库,支持rewite
[root@server soft]# tar zxvf pcre-8.21.gz 
[root@server soft]# cd pcre-8.21
[root@server pcre-8.21]# ./configure
[root@server pcre-8.21]# make 
[root@server pcre-8.21]# make install

(2)安装nginx
[root@server soft]# tar zxvf nginx-1.2.6.tar.gz 
[root@server soft]# cd nginx-1.2.6
[root@server nginx-1.2.6]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module  --without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre=/root/soft/pcre-8.21
[root@server nginx-1.2.6]# make
[root@server nginx-1.2.6]# make install

注意:
       nginx 编译时注意,nginx 1.2.1 版以上都不支持 pcre-8.30 以下的版本,故编译时候要指定 pcre 的位置:否则会报此类错误:ngx_regex.c:307: undefined reference to `pcre_free_study'

修改 /usr/local/nginx/conf/nginx.conf,删除user nobody;行前面的注释,并修改为 user www www;
将以下几行前面的注释删除,将修改fastcgi_param后面的路径

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

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }   

修改完配置之后,通过下面命令检查nginx配置是否正确
[root@server nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

五、全局配置
[root@server ~]# vim /etc/rc.local 
在最后添加以下3行
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

创建测试页
[root@server ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

打开浏览器测试

实验所需软件包下载:
http://down.51cto.com/data/751622