LNMP源碼構建

作爲一個輕量級的HTTP服務器,Nginx與Apache相比,小巧而精緻:在性能上,它佔用很少的系統資源,能支持更多的併發連接,達到更高的訪問效率;在功能上,Nginx是優秀的代理服務器和負載均衡服務器;在安裝配置上,Nginx安裝簡單、配置靈活。LNMP環境是指Linux下搭建Nginx+MySQL+PHP.

Linux下Web服務器架構之

源碼構建LNMP環境

1.構建的linux環境:

[root@junjie ~]# uname –a   #查看Linux的環境

Linux junjie 2.6.25.19 #1 SMP Mon Feb 20 17:25:04 CST 2012 i686 i686 i386 GNU/Linux

2.準備工作:

準備一:關閉firewall和SELinux(略)

準備二:構建本地yum服務器(略)

準備三:下載以下軟件(我存放在linux的/root/lamp下)

libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2

下載地址:

nginx http://nginx.org/

準備四:構建編譯組環境

使用# yum grouplist all |less查看已安裝的組環境(紅色必選

[root@junjie ~]# yum grouplist all |less

Legacy Software Development

X Software Development

Development Libraries

Development Tools

發現這裏沒有安裝Development Tools,使用下面命令安裝

[root@www ~]# yum groupinstall "Development Tools"

#安裝依賴包:

#nginx會有幾個依賴包,我們首先安裝依賴包,若安裝過程中報錯,可以跳過,不會影#響nginx的正常運行:

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

源碼構建LNMP環境

1.源碼安裝nginx:

#這裏我將下載的軟件存放到/root/lnmp目錄下
[root@junjie ~]# cd /root/lnmp/
[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2
#查看libevent的版本,發現版本過低,現在安裝新的libevent

[root@junjie lnmp]# ldconfig -v |grep libevent                     

      libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2

#解壓libevent的程序文件,使用tar –zxvf,並使用-C指定解壓目錄,參數,z代表
#gzip(也就是後面的.gz文件)x代表解壓,v表示顯示詳細信息,-f使用檔案文件或設備
#(必選參數)(說明:一般一些額外的軟件安裝時經常將其放到/usr/src/目錄下)

[root@junjie lnmp]# tar -zxvf libevent-2.0.18-stable.tar.gz -C /usr/src/

[root@junjie lnmp]# cd /usr/src/
[root@junjie src]# ll
#預編譯(執行源碼包下的configure),編譯(make),編譯安裝(make install)

[root@junjie src]# cd libevent-2.0.18-stable/

[root@junjie libevent-2.0.18-stable]# ls

[root@junjie libevent-2.0.18-stable]# ./configure
[root@junjie libevent-2.0.18-stable]# make
[root@junjie libevent-2.0.18-stable]# make install

[root@junjie libevent-2.0.18-stable]# vim /etc/ld.so.conf.d/libevent.conf

/usr/local/lib

[root@junjie libevent-2.0.18-stable]# ldconfig -v |grep libevent

       libevent_pthreads-2.0.so.5 -> libevent_pthreads.so

       libevent-2.0.so.5 -> libevent.so

       libevent_extra-2.0.so.5 -> libevent_extra.so

       libevent_openssl-2.0.so.5 -> libevent_openssl.so

       libevent_core-2.0.so.5 -> libevent_core.so

       libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2

安裝nginx:

#首先解壓源碼包:

[root@junjie libevent-2.0.18-stable]# cd /root/lnmp

[root@junjie lnmp]# tar -zxvf nginx-1.1.18.tar.gz -C /usr/src/

#進入源碼包目錄:

[root@junjie lnmp]# cd /usr/src/nginx-1.1.18/

[root@junjie nginx-1.1.18]# ll
#添加nginx系統組和用戶

[root@junjie nginx-1.1.18]# groupadd -r nginx

[root@junjie nginx-1.1.18]# useradd -r -g nginx -s /bin/false -M nginx

#進行預編譯(配置):(各項配置說明略)
[root@junjie nginx-1.1.18]# ./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 \

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

>   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

>   --http-scgi-temp-path=/var/tmp/nginx/scgi \

>   --with-pcre
#預編譯完成後就可以進行編譯和安裝:
[root@junjie nginx-1.1.18]# make
[root@junjie nginx-1.1.18]# make install
#啓動測試,發現失敗,按下面方法創建目錄,再次啓動,查看運行端口信息,#nginx佔用TCP的80端口

##pkill -9 nignx      #關閉nginx

##/usr/sbin/nginx  #啓動nginx

[root@junjie nginx-1.1.18]# nginx

nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)

[root@junjie nginx-1.1.18]# mkdir -pv /var/tmp/nginx/client

mkdir: created directory `/var/tmp/nginx'

mkdir: created directory `/var/tmp/nginx/client'

[root@junjie nginx-1.1.18]# nginx

[root@junjie nginx-1.1.18]# netstat -tupln |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      16561/nginx: master

#爲nginx提供一個啓動服務的腳本(請下載附件

[root@junjie nginx-1.1.18]# vim /etc/rc.d/init.d/nginx   (請下載附件)

[root@junjie nginx-1.1.18]# chmod +x /etc/rc.d/init.d/nginx

#設置nginx開機啓動

[root@junjie nginx-1.1.18]# chkconfig --add nginx

[root@junjie nginx-1.1.18]# chkconfig nginx on

[root@junjie nginx-1.1.18]# chkconfig --list nginx

nginx                0:off      1:off      2:on       3:on       4:on       5:on       6:off
#啓動nginx服務,查看nginx端口

[root@junjie nginx-1.1.18]# service nginx stop

Stopping nginx:                                            [ OK ]

[root@junjie nginx-1.1.18]# service nginx start

Starting nginx:                                            [ OK ]

[root@junjie nginx-1.1.18]# service nginx restart

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Stopping nginx:                                            [ OK ]

Starting nginx:                                            [ OK ]

#配置nginx網頁文件,編輯網頁

[root@junjie nginx-1.1.18]# ls /usr/html/

50x.html index.html

[root@junjie nginx-1.1.18]# vim /usr/html/index.html

5 <body bgcolor="white" text="black">

 6 <center><h1>Welcome to my nginx!</h1></center>

 7 --xjzhujunjie
 8 --2012/04/08
 9 </body>
#用瀏覽器可以訪問--客戶端測試顯示如下信息--正常

2.源碼安裝mysql:

[root@junjie ~]#cd /root/lnmp/
[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2

#解壓mysql的主程序文件,使用tar –zxvf,並使用-C指定解壓目錄 

[root@junjie lnmp]# tar -zxvf mysql-5.0.95.tar.gz -C /usr/src/

#切換至/usr/src/的目錄下,並創建軟連接,便於訪問mysql文件
[root@junjie lnmp]# cd /usr/src/                             
[root@junjie src]# ll

[root@junjie src]# ln -s mysql-5.0.95/ mysql

[root@junjie src]# ll
[root@junjie src]# cd mysql
#安裝mysql之前先做一些準備工作,安裝依賴包:顯示已經安裝了

[root@junjie mysql]# yum -y install ncurses-devel

#創建MySQL用戶,-M不創建home目錄,-s指定shell爲不登錄

[root@junjie mysql]# useradd -M -s /sbin/nologin mysql

#進行預編譯(配置):(各項配置說明略)
[root@junjie mysql]# ./configure \
> --prefix=/usr/local/mysql \
> --without-debug \
> --with-extra-charsets=utf8,gbk \
> --enable-assembler \
> --with-mysqld-ldflags=-all-static \
> --with-client-ldflags=-all-static \
> --with-unix-socket-path=/tmp/mysql.sock \
> --with-ssl
#預編譯完成後就可以進行編譯和安裝:
[root@junjie mysql]#make
[root@junjie mysql]#make install
#安裝完成後複製配置文件和啓動腳本,再給啓動腳本執行權限:

[root@junjie mysql]# cp support-files/my-medium.cnf /etc/my.cnf

[root@junjie mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@junjie mysql]# chmod +x /etc/init.d/mysqld

#爲所有的二進制可執行文件和動態鏈接庫文件做一個軟連接

[root@junjie mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

[root@junjie mysql]# ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/

#初始化數據庫,更改MySQL安裝目錄和MySQL的數據庫目錄的屬主和屬組

[root@junjie mysql]# mysql_install_db --user=mysql

[root@junjie mysql]# chown -R root.mysql /usr/local/mysql/

[root@junjie mysql]# chown -R mysql.mysql /usr/local/mysql/var/

#啓動mysql,查看MySQL是否啓動成功,MySQL佔用TCP的3306端口

[root@junjie mysql]# service mysqld start

Starting MySQL..                                           [ OK ]

[root@junjie mysql]# netstat -tupln |grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      14200/mysqld       

#通過mysql命令來連接mysql,測試mysql是否正常安裝,可以看出已安裝:
[root@junjie mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.95-log Source distribution
 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> show databases;

+--------------------+
| Database           |
+--------------------+

| information_schema |

| mysql              |
| test               |
+--------------------+

3 rows in set (0.00 sec)

 
mysql> quit
Bye
[root@junjie mysql]#

3.源碼安裝PHP依賴程序包:

#安裝PHP前首先要安裝幾個源碼包依賴:libmcrypt mhash mcrypt

#這裏我將下載的包放到/root/php/目錄下,安裝過程中若出現錯誤,可以直接#跳過,這裏不會影響後續過程
[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#安裝libmcrypt依賴包:

[root@junjie php]# tar -jxvf libmcrypt-2.5.8.tar.bz2 -C /usr/src/

[root@junjie php]# cd /usr/src/libmcrypt-2.5.8/

[root@junjie libmcrypt-2.5.8]# ./configure
[root@junjie libmcrypt-2.5.8]# make

[root@junjie libmcrypt-2.5.8]# make install

[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#安裝mhash依賴包:

[root@junjie php]# tar -jxvf mhash-0.9.9.9.tar.bz2 -C /usr/src/

[root@junjie php]# cd /usr/src/mhash-0.9.9.9/

[root@junjie mhash-0.9.9.9]# ./configure

[root@junjie mhash-0.9.9.9]# make

[root@junjie mhash-0.9.9.9]# make install

[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.* /usr/lib/

[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt* /usr/lib

[root@junjie ~]# cd /root/php/
[root@junjie php]# ls
libmcrypt-2.5.8.tar.bz2
mcrypt-2.6.8.tar.gz
mhash-0.9.9.9.tar.bz2
#這兩個包安裝完成後要把動態鏈接庫做一個軟連接到/usr/lib,以爲接下來的#mcrypt依賴於這兩個包,安裝mcrypt包:

[root@junjie php]# tar -zxvf mcrypt-2.6.8.tar.gz -C /usr/src/

[root@junjie php]# cd /usr/src/mcrypt-2.6.8/

[root@junjie mcrypt-2.6.8]# ./configure
[root@junjie mcrypt-2.6.8]# make

[root@junjie mcrypt-2.6.8]# make install

#安裝其它依賴包,發現很多已經安裝,不影響後續過程:

[root@junjie mcrypt-2.6.8]#yum –y install libxml2-devel curl-devel libpng-devel openldap-devel

4.源碼安裝PHP:

#使用nginx調用php的時候使用fpm的方式,在php 5.4中加入了對php-fpm
#的支持,所以就不需要打補丁了.以下顯示安裝PHP過程:

[root@junjie mcrypt-2.6.8]# cd /root/lnmp/

[root@junjie lnmp]# ls
libevent-2.0.18-stable.tar.gz
mysql-5.0.95.tar.gz
nginx-1.1.18.tar.gz
php-5.4.0.tar.bz2

#解壓mysql的主程序文件,使用tar –jxvf,並使用-C指定解壓目錄:

[root@junjie lnmp]# tar -jxvf php-5.4.0.tar.bz2 -C /usr/src/

[root@junjie lnmp]# cd /usr/src/php-5.4.0/

[root@junjie php-5.4.0]# ls
#進行預編譯(配置):(各項配置說明略)

[root@junjie php-5.4.0]#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-curl --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap

#預編譯完成後就可以進行編譯和安裝:
[root@junjie php-5.4.0]# make && make install

5.使能nginx調度phphemysql:

#置php和nginx能運行php網站, 首先爲php創建配置文件:

[root@junjie php-5.4.0]# cp php.ini-production /usr/local/php/php.ini

[root@junjie php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@junjie php-5.4.0]# ln -s /usr/local/php/bin/php /usr/bin/

#配置php-fpm,編輯php-fpm.conf,找到listen那一行,修改成如下內容:

[root@junjie php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf

143 listen = /var/run/php-fpm/php-fpm.sock

#創建php-fpm目錄,啓動php-fpm

[root@junjie php-5.4.0]# mkdir /var/run/php-fpm

[root@junjie html]# cd /usr/local/php/sbin/       
[root@junjie sbin]# ls
php-fpm
[root@junjie sbin]# ./php-fpm
#然後配置nginx,編輯nginx配置文件

[root@junjie sbin]# cd /usr/src/php-5.4.0/

[root@junjie php-5.4.0]# vim /etc/nginx/nginx.conf

#修改44行內容,增加index.php,添加index.php的首頁文件選項

44             index index.php index.html index.htm;

#添加46—52

46         location ~ \.php$ {
 47                 fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
 48                 fastcgi_index       index.php;

 49                 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

 50                 include fastcgi_params;
 51                 include fastcgi.conf;
 52         }

#修改完畢後保存退出重啓nginx:

[root@junjie php-5.4.0]# service nginx restart

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

Stopping nginx:                                            [ OK ]

Starting nginx:                                            [ OK ]

6.測試LNMP環境:

#增加網頁測試文件:編輯nginx的配置文件使識別index.php文件
[root@junjie php-5.4.0]# cd /usr/html/
[root@junjie html]# ll

#測試nginx與PHP的連接:#測試成功

[root@junjie html]# vim index.php
 --xjzhujunjie
 --2012/04/08
 phpinfo();
 ?>

#測試nginx與mysql的連接:#測試成功

[root@junjie html]# vim /usr/html/index.php

 --xjzhujunjie
 --2012/04/08
<?php
$link=mysql_connect('127.0.0.1','root','');
if($link)

echo "scuess";

else

echo "fail";

?>

#停止mysql服務,nginx調用mysql失敗
[root@junjie html]# service mysqld stop

Shutting down MySQL..                                      [ OK ]

#測試成功

《完》

至此源碼構建LNMP環境成功!!!

 

附件:nginx啓動腳本: 

 

#nginx啓動腳本

#vim /etc/rc.d/init.d/nginx

#!/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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章