Linux下源码搭建LNMP环境

时间:2022-09-18 14:18:10

 1.安装gcc glib 包含着开发工具中
      Development Tools --开发工具 
      Legacy Software Development  -- 传统软件开发工具 
      Development Libraries �C开发工具库 
      Java development �C源代码基于java 
如果是图形的软件包 
      Kde环境需要安装: 
      Kde software development 
      Gnome环境需要安装: 
      Gnome software development 
      X software development

2.所需软件包

libevent-1.4.14b-stable.tar.gz

libiconv-1.13.1.tar.gz

mysql-5.5.15-linux2.6-i686.tar.gz

nginx-1.0.11.tar.gz

pcre-8.12.zip

php-5.3.7.tar.bz2

系统环境支持

[root@xcu ~]# yum install -y gcc openssl-devel zlib-devel pcre-devel

3.安装步骤:

安装Mysql

[root@xcu ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/

[root@xcu ~]# cd /usr/local/

[root@xcu local]# ln -s mysql-5.5.15-linux2.6-i686 mysql

[root@xcu local]# cd mysql

[root@xcu mysql]# groupadd mysql

[root@xcu mysql]# useradd -g mysql -s /sbin/nologin -M mysql      #创建用户和组

[root@xcu mysql]# chown -R mysql .        #修改目录权限

[root@xcu mysql]# chgrp -R mysql .

[root@xcu mysql]# ./scripts/mysql_install_db --user=mysql       #安装mysql数据库

[root@xcu mysql]# chown -R root .

[root@xcu mysql]# chown -R mysql data

[root@xcu mysql]# vim /etc/profile

添加环境变量PATH=$PATH:/usr/local/mysql/bin

[root@xcu mysql]# vim /etc/ld.so.conf.d/mysql.conf

添加mysql库文件/usr/local/mysql/lib

[root@xcu mysql]# ldconfig -v |grep mysql #重新加载系统库文件

Linux下源码搭建LNMP环境

加载成功。

[root@xcu mysql]# ln -s /usr/local/mysql/include /usr/include/mysql  #链接库

[root@xcu mysql]# cp support-files/my-large.cnf /etc/my.cnf         #主配置文档

[root@xcu mysql]# cp support-files/mysql.server /etc/init.d/mysqld    #开机文件脚本

[root@xcu mysql]# chkconfig --add mysqld                       #加入到系统服务列表

[root@xcu mysql]# chkconfig mysqld on      \\开机自启动

[root@xcu mysql]# service mysqld start

//测试mysql数据库是否可用

Linux下源码搭建LNMP环境

                           图-6 mysql数据库检测

安装Nginx

[root@xcu ~]# tar -zxvf nginx-1.0.11.tar.gz

[root@xcu ~]# cd nginx-1.0.11

[root@xcu nginx-1.0.11]# groupadd nginx

[root@xcu nginx-1.0.11]# useradd -g nginx -s /bin/false -M nginx #创建用户和组

[root@xcu nginx-1.0.11]# ./configure \

    --prefix=/usr/local/nginx \

    --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  \

    --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

[root@xcu nginx-1.0.11]# make && make install

[root@xcu nginx-1.0.11]# cd /etc/nginx/

[root@xcu nginx]# vim /etc/init.d/nginxd                  #脚本程序

#!/bin/sh

    #

    # nginx - this script starts and stops the nginx daemon

    #

    # chkconfig:   - 85 15

    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

    #               proxy and IMAP/POP3 proxy server

    # processname: nginx

    # config:      /etc/nginx/nginx.conf

    # config:      /etc/sysconfig/nginx

    # pidfile:     /var/run/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/sbin/nginx"

    prog=$(basename $nginx)

    NGINX_CONF_FILE="/etc/nginx/nginx.conf"

    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

    lockfile=/var/lock/subsys/nginx

    make_dirs() {

       # make required directories

       user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

       options=`$nginx -V 2>&1 | grep 'configure arguments:'`

       for opt in $options; do

           if [ `echo $opt | grep '.*-temp-path'` ]; then

               value=`echo $opt | cut -d "=" -f 2`

               if [ ! -d "$value" ]; then

                   # echo "creating" $value

                   mkdir -p $value && chown -R $user $value

               fi

           fi

       done

    }

    start() {

        [ -x $nginx ] || exit 5

        [ -f $NGINX_CONF_FILE ] || exit 6

        make_dirs

        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

        sleep 1

        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

[root@xcu nginx]# chmod a+x /etc/init.d/nginxd

[root@xcu nginx]# chkconfig  --add  nginxd

[root@xcu nginx]# chkconfig nginxd on

[root@xcu nginx]# mkdir -pv /www/html

mkdir: created directory `/www'

mkdir: created directory `/www/html'

[root@xcu nginx]# vim  /etc/nginx/nginx.conf              #修改后如图-7

Linux下源码搭建LNMP环境

                            图-7nginx.conf的配置

[root@xcu nginx]# cd  /www/html/

[root@xcu html]# vim  index.html

添加<h1>Hello! This is my Nginx Page!</h1>

[root@xcu html]# service  nginxd  start

本地测试服务是否可用,如图-8

Linux下源码搭建LNMP环境

                                  图-8本地测试网页站点

安装Php

安装php支持的库文件

[root@xcu ~]# tar -zxvf libevent-1.4.14b-stable.tar.gz -C /usr/local

[root@xcu ~]# cd /usr/local/libevent-1.4.14b-stable/

[root@xcu libevent-1.4.14b-stable]# ./configure

[root@xcu libevent-1.4.14b-stable]# make && make install

[root@xcu ~]# tar -zxvf libiconv-1.13.1.tar.gz -C /usr/local

[root@xcu ~]# cd /usr/local/libiconv-1.13.1/

[root@xcu libiconv-1.13.1]# ./configure

[root@xcu libiconv-1.13.1]# make && make install

[root@xcu ~]# tar -jxvf php-5.3.7.tar.bz2 -C /usr/src/

[root@xcu ~]# cd /usr/src/php-5.3.7/

[root@xcu php-5.3.7]# ./configure --prefix=/usr/local/php \

--with-mysql=/usr/local/mysql \

--with-openssl \

--enable-fpm \

--with-libevent-dir=/usr/local \

--with-mysqli=/usr/local/mysql/bin/mysql_config \

--enable-mbstring \

--with-freetype-dir \ 

--with-jpeg-dir --with-png-dir \

--with-zlib \

--with-libxml-dir=/usr \

--enable-xml \

--with-iconv-dir=/usr/local

[root@xcu php-5.3.7]# make ZEND_EXTRA_LIBS='-liconv'

[root@xcu php-5.3.7]# make install

[root@xcu php-5.3.7]#/usr/src/php-5.3.7/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

[root@xcu php-5.3.7]# ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar

[root@xcu php-5.3.7]# cd /usr/local/php/etc/

[root@xcu etc]# cp -p php-fpm.conf.default php-fpm.conf

[root@xcu etc]# cd ../sbin/       

[root@xcu sbin]# ./php-fpm

Linux下源码搭建LNMP环境

#这里需要改写配置文档,参照错误提示进行修改,修改后如图-9

[root@xcu sbin]# vim ../etc/php-fpm.conf

Linux下源码搭建LNMP环境

                      图-9php-fpm.conf的文件配置

#总体测试,使Nginx和Php、Mysql进行关联

[root@xcu ~]# vim /etc/nginx/nginx.conf

Linux下源码搭建LNMP环境

                        图-10nginx.conf的文件配置

[root@xcu ~]# cd /www/html/

[root@xcu html]# mv index.html index.php 

[root@xcu html]# vim index.php

Linux下源码搭建LNMP环境

[root@xcu html]# service nginxd restart

Linux下源码搭建LNMP环境

                               图-11环境搭建检测

说明Nginx和Php、Mysql之间的环境搭建成功。