首先来介绍一下Nginx.Nginx是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。Nginx不仅可以作为web服务器,也可以作为负载均衡器,之前也有文章介绍,大家可以看一下.

MySQL是一款开源免费的数据软件,MySQL是一个小型关系型数据库管理系统,其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库.

PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。


首先我们下载nginx,在Linux下执行下面命令:

cd /usr/src                                           # 一般软件源码放在这个目录下
wget http://nginx.org/download/nginx-1.0.13.tar.gz    # 下载

nginx会有几个依赖包,我们首先安装依赖,不要安装过程中会报错:

yum  -y install zlib-devel pcre-devel openssl-devel perl perl-devel perl-ExtUtils-Embed gd gd-devel


首先我们解压源码包:

tar -zxvf nginx-1.0.13.tar.gz


然后我们进行预编译,一般预编译会带上一些参数,已达到我们想要安装的效果,比如启用某个功能,禁用某个功能:
进入源码包目录进行预编译:

cd nginx-1.0.13
./configure --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_p_w_picpath_filter_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_degradation_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-pcre


make && make install

编写sysv启动脚本
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

保存退出。

赋予脚本可执行权限
chmod +x /etc/init.d/nginx

加入开机启动管理
chkconfig --add nginx
chkconfig --level 35 nginx on

好了,下面我们启动nginx:

service nginx start


安装MySQL:

安装依赖:

yum -y install ncurses-devel cmake

创建MySQL用户:

useradd -M -s /sbin/nologin mysql  # -M不创建home目录,-s指定shell为不登录

然后进行安装:

tar -zxvf mysql-5.0.95.tar.gz
cd mysql-5.0.95
cmake -DCMAKE_INSTALL_PREFIX=/data/mysql 
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1
 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 
-DMYSQL_DATADIR=/data/mysql/data -DMYSQL_USER=mysql 
-DMYSQL_TCP_PORT=3306
make
make install

安装完成后复制配置文件和启动脚本:

cp support-files/my-medium.cnf /etc/my.cnf         # 复制配置文件
cp support-files/mysql.server /etc/init.d/mysqld   # 复制启动脚本
chmod +x /etc/init.d/mysqld         # 给启动脚本执行权限

为了以后方便我们为所有的二进制可执行文件和动态链接库文件做一个软连接:

ln -s /usr/local/mysql/bin/* /usr/local/bin/              # 为可执行的二进制文件做软连接
ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/  # 为动态链接库做一个软连接

然后我们初始化数据库:

mysql_install_db --user=mysql  # 用MySQL用户安装数据库

为了MySQL能正常使用我们需要更改一下MySQL安装目录和MySQL的数据库目录的属主和属组:

chown -R root.mysql /usr/local/mysql/           # 更改安装目录属主为root,属组为mysql
chown -R mysql.mysql /usr/local/mysql/var/      # 更改数据库目录属主和属组都为mysql

这里的-R参数用来应用到所有子目录和文件


加入chkconfig

chkconfig --add mysqld

chkconfig mysqld --level 235 on


配置完毕后我们启动mysql:

service mysqld start

现在我们查看MySQL是否启动成功,MySQL占用TCP的3306端口,我们查看端口是否被占用:

netstat -antlp  grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      32143/mysqld


MySQL安装完毕下面我们就来安装PHP,安装PHP前首先要安装几个源码包依赖:
libmcrypt mhash mcrypt
首先来安装几个源码包依赖:

wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2/download
tar -jxvf libmcrypt-2.5.8.tar.bz2   # 这个包是bz2的  使用-j参数解压
cd libmcrypt-2.5.8
./configure
make
make install

####################################################
wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make
make install
# 这两个包安装完成后要把动态链接库做一个软连接到/usr/lib,以为接下来的mcrypt依赖于这两个包
ln -s /usr/local/lib/libmcrypt* /usr/lib
ln -s /usr/local/lib/libmhash.* /usr/lib/
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
###########################################################
wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download
tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
./configure
make
make install

然后下载php:

wget http://cn2.php.net/get/php-5.4.0.tar.bz2/from/this/mirror

安装依赖:

yum –y install libxml2-devel curl-devel libpng-devel openldap-devel gd gd-devel openssl openssl-devel

我们使用nginx调用php的时候使用fpm的方式,在php 5.4中加入了对php-fpm的支持,所以就不需要打补丁了.安装PHP:

tar -jxvf php-5.4.0.tar.bz2
cd php-5.4.0
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo
make
make install

到这里整个LNMP已经安装完成.下面我们就配置php和nginx能运行php网站:
首先为php创建配置文件:

cp php.ini-production /usr/local/php/php.ini # 如果是开发就复制php.ini-development
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/bin/php /usr/bin/


编写sysv的启动脚本

#!/bin/sh  

# chkconfig:   - 84 16   

# Source function library.  

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


# Source networking configuration.  

. /etc/sysconfig/network


# Check that networking is up.  

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


phpfpm="/usr/local/php/sbin/php-fpm"

prog=$(basename ${phpfpm})


lockfile=/var/lock/subsys/phpfpm


start() {

    [ -x ${phpfpm} ] || exit 5

    echo -n $"Starting $prog: "  

    daemon ${phpfpm}

    retval=$?

    echo  

    [ $retval -eq 0 ] && touch $lockfile

    return $retval

}


stop() {

    echo -n $"Stopping $prog: "  

    killproc $prog -QUIT

    retval=$?

    echo  

    [ $retval -eq 0 ] && rm -f $lockfile

    return $retval

}


restart() {

    configtest || return $?

    stop

    start

}


reload() {

    configtest || return $?

    echo -n $"Reloading $prog: "  

    killproc ${phpfpm} -HUP

    RETVAL=$?

    echo  

}


force_reload() {

    restart

}


configtest() {

  ${phpfpm} -t

}


rh_status() {

    status $prog

}


rh_status_q() {

    rh_status >/dev/null 2>&1

}


case "$1" in

    start)

        rh_status_q && exit 0

        $1

        ;;

    stop)

        rh_status_q || exit 0

        $1

        ;;

    restart|configtest)

        $1

        ;;

    reload)

        rh_status_q || exit 7

        $1

        ;;

    status)

        rh_status

        ;;

    *)

        echo $"Usage: $0 {start|stop|status|restart|reload|configtest}"  

        exit 2

esac

保存退出。


配置php-fpm,编辑php-fpm.conf

vi /usr/local/php/etc/php-fpm.conf

找到listen那一行,修改成如下内容:

listen = /var/run/php-fpm/php-fpm.sock   # 使用unix socket

启动php-fpm

mkdir /var/run/php-fpm
/usr/local/php/sbin/php-fpm

然后配置nginx,编辑nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

修改nginx配置文件支持php:

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;         # 添加index.php的首页文件
        }

        # 添加下面内容
        location ~ \.php$ {
            fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index       index.php;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
            include fastcgi_params;
            include fastcgi.conf;
        }

修改完毕后保存退出重启nginx:

pkill -9 nignx
/usr/local/nginx/sbin/nginx

然后在/usr/local/nginx/html下创建index.php,

vi /usr/local/nginx/html/index.php

添加下面内容:


    <?php  

    phpinfo();  

    ?>  


到这里我们的LNMP环境就完全搭建成功,运行你的网站或者学习你的PHP吧.