LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

LNMP环境搭建
 linux NGINX MYSQL PHP (fastcgi)
版本:
Linux:redhat 5.4 rhel-server-5.4-i386-dvd.iso(VMware下的镜像文件)
NGINX :nginx-1.0.11.tar.gz
MYSQL :mysql-5.5.15-linux2.6-i686.tar.gz
PHP :php-5.4.14.tar.bz2
下面便开始安装:
1.安装mysql    //绿色安装包(二进制)
解压缩到/usr/local/目录下:
[root@redhat ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/
然后进入我们解压缩释放的目录,(由于目录名称较长,不便于操作,我们可以建立一个软连接)
[root@redhat ~]# cd /usr/local/
[root@redhat local]# ln -s mysql-5.5.15-linux2.6-i686 mysql
然后便直接进入mysql即可:
接下来需要的操作:
[root@redhat mysql]# groupadd -r mysql               //增加一个组mysql
[root@redhat mysql]# useradd -r -g mysql mysql –M      //给组mysql增加一个用户mysql,‘-M’表示不指定家目录
[root@redhat mysql]# chown -R mysql .                //改变当前目录(mysql)下的所有文件的所有者为mysql
[root@redhat mysql]# chgrp -R mysql .              //改变mysql目录下所有文件的所属组为mysql
[root@redhat mysql]# scripts/mysql_install_db --user=mysql          //初始化脚本
然后再把权限改回来(管理员root),但是data目录的所有者仍为mysql:
[root@redhat mysql]# chown -R root .
[root@redhat mysql]# chown -R mysql data
[root@redhat mysql]# cp support-files/my-medium.cnf /etc/my.cnf      //拷贝mysql的配置文件,并重命名为my.cnf
[root@redhat mysql]# cp support-files/mysql.server /etc/init.d/mysqld //拷贝mysql的控制文件,并重命名为mysqld
[root@redhat mysql]# chmod a+x /etc/init.d/mysqld               //改变mysqld的权限(增加执行权限)
[root@redhat mysql]# chkconfig --add mysqld
[root@redhat mysql]# chkconfig --list |grep mysql
mysqld           0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
接下来便可以开启mysql服务了:
[root@redhat mysql]# service mysqld start
Starting MySQL....                                          [确定]
[root@redhat mysql]# service mysqld stop
Shutting down MySQL.                                        [确定]
[root@redhat mysql]# service mysqld restart
MySQL server PID file could not be found!                   [失败]
Starting MySQL..                                            [确定]
[root@redhat mysql]# netstat -tupln |grep 3306         //查看端口状态(mysql的端口即为3306)
tcp         0      0 :::3306                     :::*                        LISTEN      598/mysqld         
[root@redhat mysql]#
然后需要修改搜索路径文件,不然只有进入/usr/local/mysql/bin/目录下才可以连接我们的数据库:
[root@redhat mysql]# vim /etc/profile
我们需要在第44行添加:
PATH=$PATH:/usr/local/mysql/bin
然后保存退出即可。
[root@redhat mysql]# . /etc/profile               //刷新
然后便可以连接数据库,并进行相关操作了:
给数据库管理员root添加密码,我们此处设置密码为123。
[root@redhat mysql]# mysqladmin -u root -p password '123'
Enter password:            //此处直接回车
[root@redhat mysql]#
接下来需要把头文件、库文件 导到环境里去:
先是库文件:创建mysql.conf文件,内容为库文件lib的路径:/usr/local/mysql/lib
[root@redhat mysql]# vim /etc/ld.so.conf.d/mysql.conf
然后刷新缓存文件:(ldconfig)
[root@redhat mysql]# ldconfig
然后可以查看结果:
[root@redhat mysql]# ldconfig -pv |grep mysql
        libtcmalloc_minimal.so.0 (libc6) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0
        libmysqlclient.so.18 (libc6) => /usr/local/mysql/lib/libmysqlclient.so.18
        libmysqlclient.so (libc6) => /usr/local/mysql/lib/libmysqlclient.so
[root@redhat mysql]#
如上图所示,已经成功导入库文件,接下来是库文件:
[root@redhat mysql]# ln -s include /usr/include/mysql
至此,mysql部分的操作已经可以结束了。
 
 
 
2.NGINX 安装
 
 nginx 的安装和配置
 a.先安装pcre 和livevent {源码安装   }
libevent的官网    libevent.org 最新版本是2.0.18(接下来的实验用的是:libevent-2.0.16-stable.tar.gz)
首先安装pcre:
[root@redhat ~]# mount /dev/cdrom /mnt/cdrom/    //mnt/cdrom/为挂载点,如果没有 请自行创建
mount: block device /dev/cdrom is write-protected, mounting read-only
[root@redhat ~]# cd /mnt/cdrom/Server/
[root@redhat Server]# rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm
Preparing...                 ########################################### [100%]
  1:pcre-devel              ########################################### [100%]
[root@redhat Server]#
接下来安装libevent:
在主目录下,已经有了libevent的包,
接下来便是源码安装:
[root@redhat ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src/
然后进入/usr/local/src/ libevent-2.0.16-stable/目录下:直接安装即可
[root@redhat libevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevent (后面跟上--prefix=/usr/local/libevent是为了方便以后的卸载,直接删除目录即可)
[root@redhat libevent-2.0.16-stable]# make && make install
然后切换到/usr/local/libevent/目录下:
把库文件及头文件作相应的输出:
[root@redhat libevent]# vim /etc/ld.so.conf.d/libevent.conf
/usr/local/libevent/lib
刷新缓存并查看库文件的输出:
[root@redhat libevent]# ldconfig
[root@redhat libevent]# ldconfig -pv |grep libevent
        libevent_pthreads-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_pthreads-2.0.so.5
        libevent_openssl-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_openssl-2.0.so.5
        libevent_extra-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_extra-2.0.so.5
        libevent_core-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_core-2.0.so.5
        libevent-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent-2.0.so.5
        libevent-1.1a.so.1 (libc6) => /usr/lib/libevent-1.1a.so.1
[root@redhat libevent]#
建立软连接:头文件的输出:
[root@redhat libevent]# ln -s /usr/local/libevent/include /usr/include/libevent
至此libevent已经搞定了。
 
 b.安装nginx
拆包:
[root@redhat ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/
切换到/usr/local/src/nginx-1.0.11/目录下,建立组和账号:
[root@redhat nginx-1.0.11]# groupadd -r nginx
[root@redhat nginx-1.0.11]# useradd -r -g nginx -s /sbin/nologin -M nginx   
[root@redhat nginx-1.0.11]#
   nginx 的安装配置
[root@redhat nginx-1.0.11]#(在此处将下面的代码粘贴执行即可)
 ./configure \
 --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/ \
 --with-pcre
 
接下来执行make及make install即可:
[root@redhat nginx-1.0.11]# make && make install
然后可以进入各级目录下,查看是否安装成功:
接下来需要编辑nginx的控制脚本:
[root@redhat ~]# vim /etc/init.d/nginx
下面为脚本部分:
 
#!/bin/sh
#set -x
NGINXD='/usr/local/nginx/sbin/nginx'
#CONFFILE='/usr/local/apache2/conf/httpd.conf'
#description: nginx web server
#chkconfig: 2345 88 60
if [ -f /etc/init.d/functions ];then
. /etc/init.d/functions
fi
start(){
      [ -f /var/lock/subsys/nginx ]&& echo "nginx is started" && exit
 echo -n "starting nginx......"
     sleep 1
   $NGINXD && RETVAL=0 ||RETVAL=1
 [ $RETVAL -eq 0 ]&& touch /var/lock/subsys/nginx && echo "ok" || echo "fail"
}
 stop(){
     [ ! -f /var/lock/subsys/nginx ] && echo "nginx is stoped..." && exit
           echo -n "stoping nginx........"
       sleep 1
     /bin/rm -rf /var/lock/subsys/nginx
   killproc nginx && RETVAL=0 ||RETVAL=1
   [ $RETVAL -eq 0 ] && rm -rf /var/lock/subsys/nginx && echo "ok" || echo "fail"
   }
case $1 in
start)
start
;;
stop)
stop
;;
restart)
 stop
 start
;;
*)
echo "start|stop|restart"
;;
esac
 
保存退出后,需要给nginx文件加上可执行权限:
[root@redhat ~]# chmod a+x /etc/init.d/nginx
[root@redhat ~]# chkconfig --add nginx       //设置开机启动
[root@redhat ~]# chkconfig --list |grep nginx
nginx            0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@redhat init.d]#
当我们试图启动nginx的时候,会出现如下错误:
[root@redhat init.d]# service nginx start
starting nginx......nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
fail
[root@redhat init.d]#
所以我们需要创建/var/tmp/nginx/client/目录,然后便可以正常启动了。
[root@redhat init.d]# mkdir -pv /var/tmp/nginx/client
mkdir: 已创建目录 “/var/tmp/nginx”
mkdir: 已创建目录 “/var/tmp/nginx/client”
[root@redhat init.d]# service nginx start
starting nginx......ok
[root@redhat init.d]#
然后我们可以在宿主机上测试是否可以访问到nginx:
如上图所示,我们已经成功访问到了nginx。
那么我们nginx的安装及测试已经完成了。
接下来便是php的安装部分了: 
 
3. php的安装
  重点 支持fastcgi(我们此处使用的是php-5.4.14.tar.bz2,php5.3之前的版本,需要打补丁之后才能支持fastcgi)
1.还是跟上面一样,拆包:
[root@redhat ~]# tar -jxvf php-5.4.14.tar.bz2 -C /usr/local/src/
然后切换到/usr/local/src/php-5.4.14/目录下:
[root@redhat ~]# cd /usr/local/src/php-5.4.14/
接下来执行下面的命令:直接复制粘贴即可:
 
   ./configure    \
      --prefix=/usr/local/php \
       --enable-fpm   \
       --enable-sockets \
       --with-mysql=/usr/local/mysql \
        --with-mysqli=/usr/local/mysql/bin/mysql_config \
        --enable-mbstring \
        --enable-xml \
        --with-png-dir \
        --with-png \
        --with-jpeg-dir \
        --with-zlib \
         --with-freetype-dir \
        --with-config-file-path=/etc/php \
         --with-config-file-scan-dir=/etc/php5.d
        然后便是执行make &&make install
[root@redhat php-5.4.14]# make && make install
 2.php.ini 文件 
       进入源码目录
首先创建/etc/php 以及/etc/php5.d目录:
[root@redhat php-5.4.14]# mkdir /etc/php /etc/php5.d
然后进行拷贝:
[root@redhat php-5.4.14]# cp php.ini-production /etc/php
 
 3. php-fpm的控制脚本
[root@redhat php-5.4.14]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    改变权限
[root@redhat php-5.4.14]# chmod a+x /etc/init.d/php-fpm
 4. 产生php-fpm的配置文件
    进入php的安装目录下的etc   
[root@redhat php-5.4.14]# cd /usr/local/php/etc/
[root@redhat etc]# cp php-fpm.conf.default php-fpm.conf
 5. 启动php-fpm
当然,还是先设置成开机启动:
[root@redhat etc]# chkconfig --add php-fpm
[root@redhat etc]# chkconfig --list |grep php-fpm
php-fpm          0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@redhat etc]#
接下来便可以开启服务了:
[root@redhat etc]# service php-fpm start
Starting php-fpm done
[root@redhat etc]#
同时还可以查看相关端口及进程:
[root@redhat etc]# netstat -tupln|grep fpm
tcp         0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      7832/php-fpm.conf) 
[root@redhat etc]# ps aux |grep fpm
root       7832 0.0 0.4 30672 2196 ?        Ss   13:46   0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)                                                                   
nobody     7833 0.0 0.3 30672 2028 ?        S    13:46   0:00 php-fpm: pool www                                                                                                           
nobody     7834 0.0 0.3 30672 2028 ?        S    13:46   0:00 php-fpm: pool www                                                                                                           
root       7843 0.0 0.1   5024   660 pts/1    R+   13:49   0:00 grep fpm
[root@redhat etc]#
至此,Php的安装部分结束了。 
4. nginx +php
 
   1.编辑nginx的配置文件:/etc/nginx/nginx.conf :
[root@redhat etc]# vim /etc/nginx/nginx.conf
需要修改的地方为第45行及65—71行:
45              index index.php index.html index.htm;
 65          location ~ \.php$ {
 66              root           html;
 67              fastcgi_pass   127.0.0.1:9000;
 68              fastcgi_index index.php;
 69           fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
 70              include        fastcgi_params;
 71          }
   2.测试
       进入nginx的网站工作目录
 [root@redhat php]# cd /usr/local/nginx/html/
[root@redhat html]# ll
总计 8
-rw-r--r-- 1 root root 383 04-24 20:54 50x.html
-rw-r--r-- 1 root root 151 04-24 20:54 index.html
[root@redhat html]# mv index.html index.php
[root@redhat html]#
我们可以通过修改index.php文件,来测试nginx和php的结合是否成功:
[root@redhat html]# vim index.php
下面为代码部分:(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@redhat html]# service nginx restart
okoping nginx........                                       [确定]
starting nginx......ok
[root@redhat html]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@redhat html]#
接下来边去访问之前的页面,我们会发现多出了cgi-fcgi部分:
然后测试跟数据库的结合:
还是编辑index.php文件:
[root@localhost html]# vim index.php
<?php
$link=mysql_connect('127.0.0.1','root','123');
if($link)
echo "ok";
else
echo "not";
?>
然后刷新刚才的页面:
停止mysql服务之后,再刷新:
[root@redhat html]# service mysqld stop
Shutting down MySQL..                                       [确定]
[root@redhat html]#
测试成功。
至此,我们的LNMP环境已经搭建好了。