lnmp源码安装前准备:
  1.配置yum
  2.查看组软件包安装情况
       yum grouplist | less //查看确保五组包要被安装上
       Development Libraries
       Development Tools
       X Software Development
       Legacy Software Development
       Java Development
  3.注意php软件包最后安装
一.安装niginx
实验准备源码安装包:
libevent-2.0.16-stable.tar.gz //事件触发库(新版本)安装后网站性能有大幅度提升
nginx-1.0.11.tar.gz   //niginx安装包
实验步骤:
1. [root@localhost ~]# yum list |grep pcre
    [root@localhost ~]# yum install pcre-devel //提高网站性能,能够现头部地址重写
2. [root@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src
    [root@localhost libevent-2.0.16-stable]#./configure --help |less 查看默认安装路径
Installation directories:
 --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
   [root@localhost libevent-2.0.16-stable]#./configure 
   [root@localhost libevent-2.0.16-stable]#make
   [root@localhost libevent-2.0.16-stable]#make install
说明: /usr/local // 默认安装位置,形成库文件在/usr/local/lib
系统调用库在默认路径(/lib ;/usr/lib)
为了使系统找到非标准路径的库:
vim /etc/ld.so.conf.d/libevent.conf 
文件内容:/usr/local/lib
    [root@localhost libevent-2.0.16-stable]#ldconfig -v 手工调用库文件
    [root@localhost libevent-2.0.16-stable]# ldconfig -pv |grep libevent
     libevent_pthreads-2.0.so.5 (libc6) => /usr/local/lib/libevent_pthreads-2.0.so.5
     libevent_openssl-2.0.so.5 (libc6) => /usr/local/lib/libevent_openssl-2.0.so.5
     libevent_extra-2.0.so.5 (libc6) => /usr/local/lib/libevent_extra-2.0.so.5
     libevent_core-2.0.so.5 (libc6) => /usr/local/lib/libevent_core-2.0.so.5
     libevent-2.0.so.5 (libc6) => /usr/local/lib/libevent-2.0.so.5
     libevent-1.1a.so.1 (libc6) => /usr/lib/libevent-1.1a.so.1
3. [root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src
    [root@localhost nginx-1.0.11]# groupadd -r nginx
    [root@localhost nginx-1.0.11]# useradd -r -g nginx -s /bin/false -M nginx(帐号名)
    -r 创建系统帐号
    -g 加入组
    -s shell
    -M 不创建家目录 因为是系统帐号
   [root@localhost nginx-1.0.11]# ./configure \
   --prefix=/usr \     安装路径
   --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 \     存放父进程id
   --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/ \ php临时缓存
   --with-pcre    地址重写
    [root@localhost nginx-1.0.11]#make
    [root@localhost nginx-1.0.11]#make install
4. [root@localhost ~]# mkdir -pv /var/tmp/nginx //创建目录
    [root@localhost ~]# nginx   //启动服务
    [root@localhost ~]# netstat -tupln |less //查看80端口
Web访问nginx
 

 

 

5.物理目录站点
   [root@localhost ~]#cd /usr/html
   [root@localhost html]# mkdir abc
   [root@localhost html]# cd abc
   [root@localhost abc]# echo "abc" >index.html
Web访问
 

 

 

6.虚拟目录站点
  [root@localhost ~]# mkdir /qq
  [root@localhost ~]# cd /qq
  [root@localhost qq]# echo "tec" >index.html
  [root@localhost qq]# vim /etc/nginx/nginx.conf
文件内容:43-46 复制
 43         location / {
 44             root   html;
 45             index index.html index.htm;
 46         }
 47
 48         location /tec {    //访问的域名
 49             alias /qq;     //访问的目录
 50             index index.html index.htm;
 51         }
重启服务(没有脚本,不能用service启动,但是可以不用中断用户请求使配置重新生效)
 [root@localhost ~]#pkill -1 nginx
Web访问
 

 

 

7.基于ip地址虚拟主机
  [root@localhost ~]# ifconfig eth0:0 192.168.2.101
  [root@localhost ~]#mkdir /mkt
  [root@localhost ~]#cd /mkt
  [root@localhost mkt]#echo “mkt” >index.html
  [root@localhost ~]# vim /etc/nginx/nginx.conf
文件内容:42-76复制
  server {
       listen       192.168.2.100:80;
       server_name tec.abc.com;
 
       access_log /var/log/tec.access.log;
       error_log   /var/log/tec.error.log;
       root        /qq ;
       index index.html index.htm;
  server {
       listen       192.168.2.101:80;
       server_name mkt.abc.com;
 
       access_log /var/log/mkt.access.log;
       error_log   /var/log/mkt.error.log;
       root        /mkt;
       index index.html index.htm;
客户端修改hosts文件:
192.168.2.100   tec.abc.com
192.168.2.101   mkt.abc.com
客户端Web访问
 

 

 

 

8.基于主机头虚拟主机
  [root@localhost ~]# ifconfig eth0:0 down
  [root@localhost ~]# vim /etc/nginx/nginx.conf
文件内容:
  server {
        listen       192.168.2.100:80;
        server_name  tec.abc.com;
 
        access_log /var/log/tec.access.log;
        error_log   /var/log/tec.error.log;
        root        /qq ;
       index index.html index.htm;
 server {
        listen       192.168.2.100:80;
        server_name mkt.abc.com;
 
        access_log /var/log/mkt.access.log;
        error_log   /var/log/mkt.error.log;
        root        /mkt;
       index index.html index.htm;
客户端修改hosts文件:
192.168.2.100   tec.abc.com
192.168.2.100   mkt.abc.com
客户端Web访问
 

 

 

 

 

二.安装mysql
  实验准备软件包:
  mysql-5.5.15-linux2.6-i686.tar.gz
实验步骤:
  1.[root@localhost ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -  C /usr/local/
     [root@localhost ~]# cd /usr/local/mysql-5.5.15-linux2.6-i686
     [root@localhost mysql-5.5.15-linux2.6-i686]# less INSTALL-BINARY
文件内容:
shell> groupadd mysql
shell> useradd -r -g mysql mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> chown -R mysql .
shell> chgrp -R mysql .
shell> scripts/mysql_install_db --user=mysql
shell> chown -R root .
shell> chown -R mysql data
# Next command is optional
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
2.按照INSTALL-BINARY文件内容顺序安装
  [root@localhost local]# ln -s mysql-5.5.15-linux2.6-i686/ mysql
  [root@localhost local]# cd mysql
  [root@localhost mysql]# groupadd mysql
  [root@localhost mysql]# useradd -r -g mysql mysql
  [root@localhost mysql]# chown -R mysql .
  [root@localhost mysql]# chgrp -R mysql .
  [root@localhost mysql]# scripts/mysql_install_db --user=mysql //以mysql身份运行初始化脚本
  [root@localhost mysql]# chown -R root .   //所有者改回来
  [root@localhost mysql]# chown -R mysql data
  [root@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf
  [root@localhost mysql]# bin/mysqld_safe --user=mysql & //启动服务
  [root@localhost mysql]# netstat -tupln |grep mysql
tcp        0      0 :::3306       :::*        LI STEN      4549/mysqld
换成常用方式启动服务:
  [root@localhost mysql]cp support-files/mysql.server /etc/init.d/mysqld
  [root@localhost mysql]# service mysqld stop
Shutting down MySQL.                                       [确定]
  [root@localhost mysql]# netstat -tupln |grep mysql
  [root@localhost mysql]# service mysqld start
Starting MySQL..                                          [确定]
  [root@localhost mysql]# netstat -tupln |grep mysql
tcp        0      0 :::3306       :::*           LISTEN      5544/mysqld        
3.库文件说明:系统调用库一般是从 /lib 、 /usr/lib ; 但是这次安装的库是在/usr/local/mysql/lib ,为非标准类型的库,所以需要链接到系统上去
方法:
  [root@localhost mysql]# cd /etc/ld.so.conf.d
   vim mysql.conf
文件内容加上:
/usr/local/mysql/lib
  [root@localhost ld.so.conf.d]# ldconfig -v |grep mysql //查看系统是否链接上库
  /usr/local/mysql/lib:
  libmysqlclient.so.18 -> libmysqlclient_r.so.18.0.0   //说明库可以被调用了
4.头文件说明:mysql的头文件include也不在常见目录:/usr/include /; 因为不是标准路径,须向系统指明路径
 方法:
 [root@localhost ld.so.conf.d]# cd /usr/include/
 [root@localhost include]# ln -s /usr/local/mysql/include/ mysql
二.安装php
1.解压并编译安装
 [root@localhost ~]#tar -jxvf php-5.3.7.tar.bz2 -C /usr/local/src/
 [root@localhost php-5.3.7]#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --with-libevent-dir=/usr/local --enable-mbstring --with-zlib
  --with-mysqli=/usr/local/mysql/bin/mysql_config --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --with-iconv-dir=/usr/local
 [root@localhost php-5.3.7]#make
 [root@localhost php-5.3.7]#make insall
2.整合nginx和php
 [root@localhost php-5.3.7]# cp php.ini-production /usr/local/php/etc/php.ini
 [root@localhost php-5.3.7]#cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
 [root@localhost php-5.3.7]#cd /usr/local/php/etc/
vim php-fpm.conf  
文件内容修改:去掉前面的“;”                   
30 error_log = log/php-fpm.log
166 pm.start_servers = 20
171 pm.min_spare_servers = 5
176 pm.max_spare_servers = 35
182 pm.max_requests = 500
3.启动fastcgi服务
[root@localhost etc]# /usr/local/php/sbin/php-fpm &
4.vim /etc/nginx/fastcgi.conf
文件内容修改:
fastcgi_param SERVER_SOFTWARE    nginx;     //修改为nginx;
vim /etc/nginx/fastcgi_params
文件内容修改:  
fastcgi_param SERVER_SOFTWARE    nginx;      //修改为nginx;                    
vim /etc/nginx/nginx.conf
文件内容修改:
43         location / {
44             root   /usr/html;   //修改文件目录为/usr/html
45             index index.php index.html index.htm; //加入index.php网页类型
46         }
65         location ~ \.php$ {
66             root          /usr/html;   //修改文件目录为/usr/html
67             fastcgi_pass   127.0.0.1:9000;
68             fastcgi_index index.php;
69             fastcgi_param SCRIPT_FILENAME /usr/html$fastcgi_script_name;      //修改文件目录为/usr/html
70             include        fastcgi_params;
71         }
四.测试
1.通过nginx调用php
vim /usr/html/index.html
文件内容增加:
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
phpinfo();
?>
[root@localhost ~]# cd /usr/html
[root@localhost html]# mv index.html index.php
Web访问:
 

 

 

3.通过php调动mysql
vim /usr/html/index.php       
文件内容修改:
 <html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
$link=mysql_connect('127.0.0.1','root','');
if ($link)
echo "connect";
else echo "fail"
?>
Web访问: