例子:
curl -I 163.com
HTTP/1.1 301 Moved Permanently
Server: nginx  (特点:非常快,理论上比apache快很多。比较小巧,编译完后大概有几十k)
Date: Mon, 30 Apr 2012 01:45:53 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: http://www.163.com/
Vary: User-Agent
Vary: Accept

—>lnmp
注意:一台新的虚拟机(还原虚拟机,vmreset/gls-resetvm),关掉selinux和火墙

只有源码是原身的,可以根据自己的要求来定制.


========================================================================================================================
1. Mysql 安装

实验:注意在做实验之前最好先将之前的mysql php卸载掉。因为如果你不卸载的话系统会默认去选用用rpm包安装的。不会优先使用你用源码编译生成的包的

安装cmake。
[root@desktop71 ~]# yum install cmake -y
或者:
[root@desktop71 ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> cd pub/docs/lamp/update/lnmp/
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> get cmake-2.8.4.tar.gz
5477628 bytes transferred                       
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> quit
[root@desktop71 ~]#
[root@desktop71 ~]# tar zxf cmake-2.8.4.tar.gz
[root@desktop71 ~]# cd cmake-2.8.4
[root@desktop71 cmake-2.8.4]# ./configure
[root@desktop71 cmake-2.8.4]# make && make install(相当于yum install cmake)

[root@desktop71 ~]# yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel

注意要把mysql,php,httpd等卸载或忽略掉
[root@desktop71 ~]# rpm -qa | grep mysql    #查看系统上是否安装了mysql
mysql-libs-5.1.47-4.el6.x86_64#系统上已经安装mysql,所以要卸载掉或忽略掉
[root@desktop71 ~]# rpm -e mysql-libs –nodeps    #忽略mysql-libs
[root@desktop71 ~]# rpm -qa | grep mysql
[root@desktop71 ~]# rpm -qa | grep php
[root@desktop71 ~]# rpm -qa | grep apache
[root@desktop71 ~]# rpm -qa | grep httpd

实验步骤:
(1)

[root@desktop71 ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> cd pub/docs/lamp/update/lnmp/
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> mget mysql-5.5.12.tar.gz httpd-2.2.19.tar.bz2
29003718 bytes transferred in 1 second (24.03M/s)        
Total 2 files transferred
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> quit
[root@desktop71 ~]# ls
………………………………..
httpd-2.2.19.tar.bz2  mysql-5.5.12.tar.gz  
…………………………………
[root@desktop71 ~]# tar zxf mysql-5.5.12.tar.gz
[root@desktop71 ~]# cd mysql-5.5.12

–>指定安装路径
[root@desktop71 mysql-5.5.12]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \    #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \          #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \        #Unix socket文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \                    #安装myisam存储引擎
-DENABLED_LOCAL_INFILE=1 \                                   #允许从本地导入数据       
-DWITH_PARTITION_STORAGE_ENGINE=1 \               #安装数据库分区
-DWITH_SSL=yes \                                                          #支持SSL
-DDEFAULT_CHARSET=utf8 \                                       #使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci \                 #校验字符
-DEXTRA_CHARSETS=all \                                            #安装所有扩展字符集
-DMYSQL_TCP_PORT=3306                                          #mysql监听端口

…………………………………………………………………………………..
–>未添加的选项:
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                    #安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \                    #安装 archive 存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \                    #安装 blackhole 存储引擎
-DWITH_READLINE=1 \快捷键功能
…………………………………………………………………………………..

–>注意:
在配置环境的时候此处的cmake 相当与之前的./configure.在上面的选项中,第1,2,3,4,\安装目录\数据库存放目录 \Unix socket 文件路径\安装 myisam 存储引擎 \支持 SSL\校验字符 \安装所有扩展字符集最好要写上,要不一会可能会出错的。

–>倘若出错,或者由于某种原因导致重新编译的时候,需要清除旧的对象文件和缓存信息。
[root@desktop71 mysql-5.5.12]# rm -rf CmakeCache.txt
[root@desktop71 mysql-5.5.12]# make clean或者make tidy

[root@desktop71 mysql-5.5.12]# make
[root@desktop71 mysql-5.5.12]# make install

[root@desktop71 ~]# useradd -M -s /sbin/nologin mysql        #创建一个mysql登录用户,但不能登录系统
##(id mysql (如果此时mysql的组id和本身的id不一样,那么你可以使用groupmod -g XX mysql))

[root@desktop71 ~]# cd /usr/local/lnmp/mysql/
[root@desktop71 mysql]# cd support-files/
[root@desktop71 support-files]# cp my-medium.cnf /etc/my.cnf    #根据你的主机内存复制mysql配置文件
[root@desktop71 support-files]# cd ..
[root@desktop71 mysql]# pwd
/usr/local/lnmp/mysql
[root@desktop71 mysql]# chown -R mysql.mysql .
[root@desktop71 mysql]# cd scripts/
[root@desktop71 scripts]# ./mysql_install_db –user=mysql –basedir=/usr/local/lnmp/mysql/ –datadir=/usr/local/lnmp/mysql/data/        #显示OK是对的
[root@desktop71 scripts]# cd ..
[root@desktop71 mysql]# pwd
/usr/local/lnmp/mysql
[root@desktop71 mysql]# chown -R root .
[root@desktop71 mysql]# chown -R mysql data
[root@desktop71 mysql]# cd support-files/
[root@desktop71 support-files]# cp mysql.server /etc/init.d/mysqld
[root@desktop71 support-files]# ll /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10680 Apr 30 11:26 /etc/init.d/mysqld        #要有可执行权限
[root@desktop71 support-files]# cd ..
[root@desktop71 mysql]# pwd
/usr/local/lnmp/mysql
[root@desktop71 mysql]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@desktop71 mysql]# cd bin/
[root@desktop71 bin]# vi ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
#或者 echo “export PATH=\$PATH:/usr/local/mysql/bin” >> ~/.bash_profile
[root@desktop71 bin]# source ~/.bash_profile
[root@desktop71 bin]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/lnmp/mysql/bin
[root@desktop71 bin]# /etc/init.d/mysqld start
Starting MySQL……                                       [  OK  ]
 
[root@desktop71 bin]# mysql
……………………..
mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+——————–+
4 rows in set (0.11 sec)

mysql> quit
Bye
[root@desktop71 bin]# mysql_secure_installation     #按提示完成mysql安全设置,生产环境推荐使用
Enter current password for root (enter for none):(回车
Set root password? [Y/n] y
New password: (westos)
Re-enter new password: (westos)
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n]
y
[root@desktop71 bin]# mysql -pwestos
mysql> show databases;
+——————–+
| Database           |
+——————–+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+——————–+
4 rows in set (0.00 sec)
mysql> quit
Bye

[root@desktop71 bin]#
 

========================================================================================================================
2.PHP安装

php的安装也是相当重要的,整个lamp就是为它来操作的
lamp=linux apache mysql php

[root@desktop71 ~]# cd /usr/local/lnmp/mysql/lib
[root@desktop71 lib]# ln -s /usr/local/lnmp/mysql/lib/ /usr/local/lib64
#不然php编译的时候找不到mysql的库文件

[root@desktop71 ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> cd pub/docs/lamp/update/lnmp/
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> get php-5.3.6.tar.bz2
10952171 bytes transferred                          
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> get libiconv-1.13.1.tar.gz libmcrypt-2.5.8.tar.bz2 mcrypt-2.6.8.tar.gz mhash-0.9.9.9.tar.bz2
6719316 bytes transferred                                
Total 4 files transferred
lftp 192.168.0.254:/pub/docs/lamp/update/lnmp> quit
[root@desktop71 ~]# ls
……………………………………………………….
mhash-0.9.9.9.tar.bz2  libiconv-1.13.1.tar.gz   php-5.3.6.tar.bz2   
libmcrypt-2.5.8.tar.bz2   mcrypt-2.6.8.tar.gz     
……………………………………………………….

[root@desktop71 ~]# tar zxf libiconv-1.13.1.tar.gz#加强系统对支持字符编码转换的功能
[root@desktop71 ~]# cd libiconv-1.13.1
[root@desktop71 libiconv-1.13.1]# ./configure –libdir=/usr/local/lib64/
[root@desktop71 libiconv-1.13.1]# make
[root@desktop71 libiconv-1.13.1]# make install

[root@desktop71 ~]# tar jxf libmcrypt-2.5.8.tar.bz2 # mcrypt mhash是php加密算法扩展库
[root@desktop71 ~]# cd libmcrypt-2.5.8/    
[root@desktop71 libmcrypt-2.5.8]# ./configure –libdir=/usr/local/lib64/
[root@desktop71 libmcrypt-2.5.8]# make
[root@desktop71 libmcrypt-2.5.8]# make install
[root@desktop71 libmcrypt-2.5.8]# cd libltdl
[root@desktop71 libltdl]# ./configure –libdir=/usr/local/lib64/ –enable-ltdl-install
[root@desktop71 libltdl]# make
[root@desktop71 libltdl]# make install

#(注意mhash要在mcrypt前面的噢!否则会报错的!)
[root@desktop71 ~]# tar jxf mhash-0.9.9.9.tar.bz2
[root@desktop71 ~]# cd mhash-0.9.9.9/
[root@desktop71 mhash-0.9.9.9]# ./configure –libdir=/usr/local/lib64/    #./configure时可能会报这个错:/bin/rm: cannot remove `libtoolT’: No such file or directory 直接忽略
[root@desktop71 mhash-0.9.9.9]# make
[root@desktop71 mhash-0.9.9.9]# make install

[root@desktop71 mhash-0.9.9.9]# ldconfig /usr/local/lib64/

[root@desktop71 ~]# tar zxf mcrypt-2.6.8.tar.gz
[root@desktop71 ~]# cd mcrypt-2.6.8/
[root@desktop71 mcrypt-2.6.8]# ./configure –libdir=/usr/local/lib64/
[root@desktop71 mcrypt-2.6.8]# make
[root@desktop71 mcrypt-2.6.8]# make install
[root@desktop71 mcrypt-2.6.8]# ldconfig /usr/local/lib64/

[root@desktop71 ~]# yum install -y net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openldap-devel
[root@desktop71 ~]# useradd -M -s /sbin/nologin nginx  #(—M不创建家目录,-s没有shell)

[root@desktop71 ~]# tar jxf php-5.3.6.tar.bz2
[root@desktop71 ~]# cd php-5.3.6
[root@desktop71 php-5.3.6]# ./configure –prefix=/usr/local/lnmp/php \    #指定php的安装路径
–with-config-file-path=/usr/local/lnmp/php/etc \            #指定php的主配置文件在什么地方
–with-mysql=/usr/local/lnmp/mysql/ \    #加入mysql的支持。DIR是mysql的基本目录。
–with-openssl \            #加密
–with-snmp \
–with-gd \            #图片
–with-curl \
–with-libxml-dir \
–with-png-dir \
–with-jpeg-dir \
–with-freetype-dir \
–without-pear \            #不要自带版本
–with-gettext \
–with-gmp \
–enable-inline-optimization \
–enable-soap \
–enable-ftp \
–enable-sockets \
–enable-mbstring \
–with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config \    #包括mysqli的支持。FILE是mysql_config的路径
–enable-fpm \
–with-fpm-user=nginx \
–with-fpm-group=nginx \
–with-libdir=lib64 \
–with-ldap \
–with-ldap-sasl \
–with-mcrypt \
–with-mhash

[root@desktop71 php-5.3.6]# make ZEND_EXTRA_LIBS=’-liconv’ #(一定要加这个选项否则会出现:/root/php-5.3.6/ext/iconv/iconv.c:2624: undefined reference to `libiconv’ /r类似的错误
[root@desktop71 php-5.3.6]# make install

[root@desktop71 php-5.3.6]# cp php.ini-production /usr/local/lnmp/php/etc/
[root@desktop71 php-5.3.6]# cd /usr/local/lnmp/php/etc/
[root@desktop71 etc]# ls
php-fpm.conf.default  php.ini-production
[root@desktop71 etc]# mv php.ini-production php.ini
[root@desktop71 etc]# ls
php-fpm.conf.default  php.ini
[root@desktop71 etc]# cd
[root@desktop71 ~]# cd php-5.3.6
[root@desktop71 php-5.3.6]# cd sapi/
[root@desktop71 sapi]# cd fpm/
[root@desktop71 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@desktop71 fpm]# ll /etc/init.d/php-fpm
-rw-r–r– 1 root root 2068 Apr 30 16:51 /etc/init.d/php-fpm
[root@desktop71 fpm]# chmod +x /etc/init.d/php-fpm
[root@desktop71 fpm]# ll /etc/init.d/php-fpm
-rwxr-xr-x 1 root root 2068 Apr 30 16:51 /etc/init.d/php-fpm
[root@desktop71 fpm]# cd /usr/local/lnmp/php/etc/
[root@desktop71 etc]# ls
php-fpm.conf.default  php.ini
[root@desktop71 etc]# vi php.ini
cgi.fix_pathinfo=0    #防止Nginx文件类型错误解析漏洞
………………………………………………………………………………………
#如果不为0 而为1的话,那么在你用ll /use/local/nginx/html
#curl -I http://localhost/test.php(在你安装好nginx之后你关闭httpd默认访问的是/usr/local/nginx/html/index.html)
#curl -I http://localhost/test.txt/test.php
#curl -I http://localhost/test1.jpg
#curl -I http://localhost/test1.jpg/test1.php
#看Content Type。。。。。
#改为0正常的时候,可以防止漏洞,此时你在web网页的时候登录:http://desktop20.example.com/test.txt/test.php 会出现 No input file specified
………………………………………………………………………………………

[root@desktop71 etc]# ls
php-fpm.conf.default  php.ini
[root@desktop71 etc]# cp php-fpm.conf.default php-fpm.conf
[root@desktop71 etc]# vi php-fpm.conf    #去掉以下几行的注释
pid = run/php-fpm.pid
pm.max_children = 50
pm.start_servers = 20    #在生产环境中一定要做压力测试,找到最合适的进程数组合
pm.min_spare_servers = 5
pm.max_spare_servers = 35
p;m.max_requests = 500
[root@desktop71 etc]# /etc/init.d/php-fpm start
Starting php-fpm  done
[root@desktop71 etc]# vi ~/.bash_profile           
[root@desktop71 etc]# source ~/.bash_profile
[root@desktop71 etc]# cat $PATH
cat: /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/lnmp/mysql/bin:/root/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin:

========================================================================================================================
3.nginx安装

[root@desktop71 ~]# yum install pcre-devel -y

[root@desktop71 ~]# lftp 192.168.0.254
lftp 192.168.0.254:~> get pub/docs/lamp/update/lnmp/nginx-1.0.8.tar.gz
683559 bytes transferred                                 
lftp 192.168.0.254:/> quit

[root@desktop71 ~]# tar zxf nginx-1.0.8.tar.gz
[root@desktop71 ~]# cd nginx-1.0.8
[root@desktop71 nginx-1.0.8]# cd src/
[root@desktop71 src]# ls
core  event  http  mail  misc  os
[root@desktop71 src]# cd core/
[root@desktop71 core]# ls
nginx.c           ngx_crypt.c  ngx_murmurhash.c       ngx_regex.h
nginx.h           ngx_crypt.h  ngx_murmurhash.h       ngx_resolver.c
ngx_array.c       ngx_cycle.c  ngx_open_file_cache.c  ngx_resolver.h
ngx_array.h       ngx_cycle.h  ngx_open_file_cache.h  ngx_sha1.h
ngx_buf.c         ngx_file.c   ngx_output_chain.c     ngx_shmtx.c
ngx_buf.h         ngx_file.h   ngx_palloc.c           ngx_shmtx.h
ngx_conf_file.c   ngx_hash.c   ngx_palloc.h           ngx_slab.c
ngx_conf_file.h   ngx_hash.h   ngx_parse.c            ngx_slab.h
ngx_config.h      ngx_inet.c   ngx_parse.h            ngx_spinlock.c
ngx_connection.c  ngx_inet.h   ngx_queue.c            ngx_string.c
ngx_connection.h  ngx_list.c   ngx_queue.h            ngx_string.h
ngx_core.h        ngx_list.h   ngx_radix_tree.c       ngx_times.c
ngx_cpuinfo.c     ngx_log.c    ngx_radix_tree.h       ngx_times.h
ngx_crc32.c       ngx_log.h    ngx_rbtree.c
ngx_crc32.h       ngx_md5.c    ngx_rbtree.h
ngx_crc.h         ngx_md5.h    ngx_regex.c
[root@desktop71 core]# cd ..
[root@desktop71 src]# cd ..
[root@desktop71 nginx-1.0.8]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@desktop71 nginx-1.0.8]# cd auto/cc/
[root@desktop71 cc]# vi gcc
# debug
#CFLAGS=”$CFLAGS -g”    #关掉debug
[root@desktop71 cc]# cd ..
[root@desktop71 auto]# cd ..
[root@desktop71 nginx-1.0.8]# cd src/core/
[root@desktop71 core]# vi nginx.h
#define NGINX_VER          “nginx/”
[root@desktop71 core]# cd ..
[root@desktop71 src]# cd ..
[root@desktop71 nginx-1.0.8]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@desktop71 nginx-1.0.8]# pwd
/root/nginx-1.0.8
[root@desktop71 nginx-1.0.8]# ./configure –prefix=/usr/local/lnmp/nginx –user=nginx –group=nginx –with-http_stub_status_module –with-http_ssl_module
[root@desktop71 nginx-1.0.8]# make
[root@desktop71 nginx-1.0.8]# make install