且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于RHEL5的源码编译搭建LNMP架构

更新时间:2022-09-11 20:47:56

注:此次实验搭建LNMP的服务器IP地址为:172.16.4.1

一、源码编译安装Nginx

1、由于需要这里我们使用的是源码编译安装Nginx,所以我们需要构建开发环境,即需要安装Development librariesDevelopment tools这两个开发包组,但是由于Development libraries包组依赖于Development tools包组,所以当安装Development libraries包组时Development tools里的大部分包其实已经安装过了,所以这里我们只安装Development libraries这个包组,命令如下:

#yum groupinstall “Development libraries” –y

2、解决依赖关系:

Nginx的安装需要依赖于gccopenssl-develpcre-develzlib-devel这四个包,所以在使用源码安装之前请确保这四个包已经安装,命令如下:

#rpm –ivh gcc openssl-devel pcre-devel zlib-devel

注:一般情况下,这些包已经安装过。

3、为nginx建立安全运行账户:

# groupadd -r nginx

# useradd -r -g nginx -s /sbin/nologin -M nginx

4、编译安装nginx-1.0.14

下载nginx-1.0.14.tar.gz到本地,下载地址:

http://nginx.org/download/nginx-1.0.14.tar.gz

执行以下命令:


  1. # tar xf nginx-1.0.14.tar.gz 
  2. # cd nginx-1.0.14 
  3. #./configure \ 
  4. --prefix=/usr \ 
  5. --sbin-path=/usr/sbin/nginx \ 
  6. --conf-path=/etc/nginx/nginx.conf \ 
  7. --error-log-path=/var/log/nginx/error.log \ 
  8. --http-log-path=/var/log/nginx/access.log \ 
  9. --pid-path=/var/run/nginx/nginx.pid  \ 
  10. --lock-path=/var/lock/nginx.lock \ 
  11. --user=nginx \ 
  12. --group=nginx \ 
  13. --with-http_ssl_module \ 
  14. --with-http_flv_module \ 
  15. --with-http_stub_status_module \ 
  16. --with-http_gzip_static_module \ 
  17. --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  18. --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  19. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  20. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  21. --http-scgi-temp-path=/var/tmp/nginx/scgi \ 
  22. --with-pcre 
  23. # make && make install 

4、提供SysV服务脚本/etc/rc.d/init.d/nginx,内容如下:


  1. #!/bin/sh 
  2. # nginx - this script starts and stops the nginx daemon 
  3. # chkconfig:   - 85 15 
  4. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  5. #               proxy and IMAP/POP3 proxy server 
  6. # processname: nginx 
  7. # config:      /etc/nginx/nginx.conf 
  8. # config:      /etc/sysconfig/nginx 
  9. # pidfile:     /var/run/nginx.pid 
  10. # Source function library. 
  11. . /etc/rc.d/init.d/functions 
  12. # Source networking configuration. 
  13. . /etc/sysconfig/network 
  14. Check that networking is up. 
  15. "$NETWORKING" = "no" ] && exit 0 
  16. nginx="/usr/sbin/nginx" 
  17. prog=$(basename $nginx) 
  18. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  19. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  20. lockfile=/var/lock/subsys/nginx 
  21. make_dirs() { 
  22.    # make required directories 
  23.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  24.    options=`$nginx -V 2>&1 | grep 'configure arguments:'
  25.    for opt in $options; do 
  26.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  27.            value=`echo $opt | cut -d "=" -f 2` 
  28.            if [ ! -d "$value" ]; then 
  29.                # echo "creating" $value 
  30.                mkdir -p $value && chown -R $user $value 
  31.            fi 
  32.        fi 
  33.    done 
  34. start() { 
  35.     [ -x $nginx ] || exit 5 
  36.     [ -f $NGINX_CONF_FILE ] || exit 6 
  37.     make_dirs 
  38.     echo -n $"Starting $prog: " 
  39.     daemon $nginx -c $NGINX_CONF_FILE 
  40.     retval=$? 
  41.     echo 
  42.     [ $retval -eq 0 ] && touch $lockfile 
  43.     return $retval 
  44. stop() { 
  45.     echo -n $"Stopping $prog: " 
  46.     killproc $prog -QUIT 
  47.     retval=$? 
  48.     echo 
  49.     [ $retval -eq 0 ] && rm -f $lockfile 
  50.     return $retval 
  51. restart() { 
  52.     configtest || return $? 
  53.     stop 
  54.     sleep 1 
  55.     start 
  56. reload() { 
  57.     configtest || return $? 
  58.     echo -n $"Reloading $prog: " 
  59.     killproc $nginx -HUP 
  60.     RETVAL=$? 
  61.     echo 
  62. force_reload() { 
  63.     restart 
  64. configtest() { 
  65.   $nginx -t -c $NGINX_CONF_FILE 
  66. rh_status() { 
  67.     status $prog 
  68. rh_status_q() { 
  69.     rh_status >/dev/null 2>&1 
  70. case "$1" in 
  71.     start) 
  72.         rh_status_q && exit 0 
  73.         $1 
  74.         ;; 
  75.     stop) 
  76.         rh_status_q || exit 0 
  77.         $1 
  78.         ;; 
  79.     restart|configtest) 
  80.         $1 
  81.         ;; 
  82.     reload) 
  83.         rh_status_q || exit 7 
  84.         $1 
  85.         ;; 
  86.     force-reload) 
  87.         force_reload 
  88.         ;; 
  89.     status) 
  90.         rh_status 
  91.         ;; 
  92.     condrestart|try-restart) 
  93.         rh_status_q || exit 0 
  94.             ;; 
  95.     *) 
  96.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  97.         exit 2 
  98. esac 

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/nginx

nginx的服务加入服务列表:

# chkconfig --add nginx

并且将nginx设为开机启动:

#chkconfig nginx on

5、启动apache,并测试:

#service httpd start

在浏览器中输入172.16.4.1,出现如下界面:

基于RHEL5的源码编译搭建LNMP架构

证明Nginx启动成功!

二、使用绿色版,解压安装Mysql-5.2.20

1、准备数据存放的文件系统:

***创建一个逻辑卷用于存放Mysql的数据,以实现之后对数据进行快照备份,卷的大小根据需要自定义,这里创建一个逻辑卷挂载到/mydata下并创建一个新的目录用于存放Mysql的数据,并设置为开机自动挂载,这里不在给出创建步骤。

2、新建用户以安全方式运行进程:

# groupadd -r mysql

# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql

# chown -R mysql:mysql /mydata/data

3、安装并初始化mysql-5.5.20

下载平台对应的Mysql版本至本地,官方下载地址:

http://dev.mysql.com/downloads/mysql/5.0.html

这里选择Mysql-5.5.20-linux2.6-i386.tar.gz执行以下命令:


  1. # tar xf mysql-5.5.20-linux2.6-i686.tar.gz -C /usr/local 
  2. # cd /usr/local
  3. # ln -sv mysql-5.5.20-linux2.6-i686  mysql 
  4. # cd mysql 
  5. # chown -R mysql:mysql  . 
  6. # scripts/mysql_install_db --user=mysql --datadir=/mydata/data 
  7. # chown -R root  . 

4、为mysql提供主配置文件:

# cd /usr/local/mysql

# cp support-files/my-large.cnf  /etc/my.cnf

并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:

thread_concurrency = 2

另外还需要添加如下行指定mysql数据文件的存放位置:

datadir = /mydata/data

5、为mysql提供sysv服务脚本:

# cd /usr/local/mysql

# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld

添加mysql至服务列表,并将其设为开机启动:

# chkconfig --add mysqld

# chkconfig mysqld on

6、导出mysqlman文件:

#vim /etc/man.conf

添加如下一行:

MANPATH /usr/local/mysql/man

7、导出apache的头文件:

#ln –sv /usr/local/mysql/include /usr/ include/mysql

8、导出库文件,并重新载入库文件:

#echo “/usr/local/mysql/lib” > /etc/ld.so.conf.d/mysql.conf

#ldconfig -v

9、修改环境变量:

#vim /etc/profile

在“PATH=$PATH/usr/local/apache/bin“后面添加:

/usr/local/mysql/bin

10、启动mysql

#service mysqld start

三、源码编译php-5.3.10

1、为编译php-5.3.10提供编译环境:

安装X Software Development这个包组,请配置好yum源后,执行如下命令:

# yum -y groupinstall "X Software Development"

nginx启动fastcgi,需要对php进行一些功能扩展,需要更新升级或者源码编译以下安装包,因为在RHEL4上已经安装过,如果使用rpm对其进行升级,则升级时具有依赖关系,所以这里使用源码安装以下四个包:

libevent-2.0.16-stable.tar.gz

libiconv-1.13.1.tar.gz

libmcrypt-2.5.8.tar.gz

mhash-0.9.9.9.tar.bz2

下载地址:

http://down.51cto.com/data/361648

其中有源码包和rpm包,如果下载rpm对其进行升级,则跳过第2345,执行以下命令:

#rpm –Uvh libevent-devel-2.0.17-2.i386.rpm libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm mcrypt-2.6.8-1.el5.i386.rpm mhash-devel-0.9.9-1.el5.centos.i386.rpm --nodeps

2、编译安装libevent

首先将libevent-2.0.16-stable.tar.gz的源码包下载到本地:执行以下命令:


  1. # tar zxvf libevent-1.4.14b-stable.tar.gz 
  2. # cd libevent-1.4.14b-stable 
  3. # ./configure 
  4. # make && make install 
  5. # make verify 

3、编译安装libiconv:这个包为可选,也可以不安装:

下载libiconv-1.13.1.tar.gz源码包至本地,执行以下命令:


  1. # tar zxvf libiconv-1.13.1.tar.gz 
  2. # cd libiconv-1.13.1 
  3. # ./configure 
  4. # make && make install 

4、编译安装libmcrypt

下载libmcrypt-2.5.8.tar.gz源码包至本地,执行以下命令:


  1. # tar zxvf libmcrypt-2.5.8.tar.gz 
  2. # cd libmcrypt-2.5.8 
  3. # ./configure 
  4. # make && make install 
  5. # ldconfig -v 
  6. # cd libltdl 
  7. # ./configure --with-gmetad --enable-gexec --enable-ltdl-install 
  8. # make && make install 

5、编译安装mhash

下载mhash-0.9.9.9.tar.bz2源码包至本地,执行以下命令:


  1. # tar jxvf mhash-0.9.9.9.tar.bz2 
  2. # cd mhash-0.9.9.9 
  3. # ./configure 
  4. # make && make install 
  5. # ln -sv /usr/local/lib/libmcrypt* /usr/lib/ 
  6. # ln -sv /usr/local/lib/libmhash.* /usr/lib/ 

6、编译安装php-5.3.10

下载php-5.3.10.tar.bz2源码包至本地,下载地址:

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

执行以下命令:

# tar jxvf php-5.3.10.tar.bz2

# cd php-5.3.10

注:在此要检查bzip2-develcurl-devel这两个开发包有没有安装,如果没有安装则将其安装后在执行以下命令。


  1. # ./configure --prefix=/usr/local/phpnginx \ 
  2. --with-mysql=/usr/local/mysql \ 
  3. --with-openssl \ 
  4. --enable-fpm \ 
  5. --with-mysqli=/usr/local/mysql/bin/mysql_config \ 
  6. --enable-mbstring \ 
  7. --with-freetype-dir \ 
  8. --with-jpeg-dir \ 
  9. --with-png-dir \ 
  10. --with-zlib-dir \ 
  11. --with-libxml-dir=/usr \ 
  12. --enable-xml \ 
  13. --with-mhash \ 
  14. --with-mcrypt  \ 
  15. --with-config-file-path=/etc/phpnginx \ 
  16. --with-config-file-scan-dir=/etc/phpnginx \ 
  17. --with-bz2 \ 
  18. --with-curl \ 
  19. --with-iconv=/usr/local 

如果没有执行步骤3,没有安装libiconv,则执行以下命令:


  1. # make 
  2. # make install 
  3. # mkdir /etc/phpnginx 
  4. # cp php.ini-production /etc/phpnginx/php.ini 

如果执行了步骤3,并且安装了libiconv,则执行以下命令:


  1. # make ZEND_EXTRA_LIBS='-liconv' 
  2. # make install 
  3. # mkdir /etc/phpnginx 
  4. # cp php.ini-production /etc/phpnginx/php.ini 

7、启用fastcgi

# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

# vim /usr/local/php/etc/php-fpm.conf

启用如下选项:

pid = run/php-fpm.pid

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

8、为php-fpm提供SysV服务脚本:

#cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm

#chmod +x /etc/rc.d/init.d/php-fpm

#chkconfig –-add php-fpm

#chkconfig php-fpm on

9、而后启动php-fpm

#service php-fpm start

四、整合NginxPHP构建LNMP

1、启用nginxfastcgi

编辑/etc/nginx/nginx.conf,启用如下选项:

location ~ \.php$ {

root           html;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

include        fastcgi_params;

}

2、编辑fastcgi_params参数:

#vim /etc/nginx/fastcgi_params

将里面的内容全部替换为以下内容:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

3、并在所支持的主页面格式中添加php格式的主页,类似如下:

location / {

root   html;

index  index.php index.html index.htm;

}

4、提供默认的php网页:

/usr/html/index.html重命名为index.php

#mv /usr/html/index.html /usr/html/index.php

添加以下内容:

<?php

phpinfo();

?>

5、而后重启nginx

#service nginx restart

在浏览器中输入 172.16.4.1,出现如下界面:

基于RHEL5的源码编译搭建LNMP架构

证明PHP安装成功!

6、测试能否连接上Mysql

编辑/usr/htnl/index.php,添加如下内容:

<?php

$link=mysql_connect('localhost','root','');

if ($link)

              echo "Successfuly";

else

              echo "Faile";

mysql_close();

?>

保存退出。

在浏览器中输入 172.16.4.1,出现以下内容:

基于RHEL5的源码编译搭建LNMP架构

证明php可以连接到Mysql

 

 

到此LNMP平台构建成功。








本文转自 向阳草米奇 51CTO博客,原文链接:http://blog.51cto.com/grass51/824694,如需转载请自行联系原作者