dfeg 发表于 2015-11-16 09:38:25

lnmp源码编译安装Linux(6.4)+Nginx(1.4.1)+Mysql(5.6.12)+Php(5.5.0)

内核2.6.32,系统为CentOS6.4的64为系统,ip地址为192.168.2.103。如下:
1234567#uname -r2.6.32-358.el6.x86_64#cat /etc/redhat-releaseCentOSrelease 6.4 (Final)#ifconfig | grep addr: |awk '{print $2}' |awk -F: '{print $2}'192.168.2.103127.0.0.1

配置本地yum源:
创建本地yum源 ,挂载光盘并创建repo文件 :
12345678#mount /dev/cdrom /media/cdrom/#vim /etc/yum.repos.d/local.repo//添加如下内容name=localbaseurl=file:///media/cdromenabled=1gpgcheck=0#yum clean all

解决依赖关系:

   在编译安装nginx、mysql、和php时依赖的包提前安装:
1#yum -y install gcc pcre pcre-devel gcc-c++ autoconf libxml2 libxml2-devel zlib zlib-devel glibc libjepg libjepg-devel libpng libpng-devel glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldapopenldap-devel nss_ldap openldap-clients openldap-servers make

   简单介绍一下:


12345678910111213GCC(GNUCompiler Collection,GNU编译器套装),是一套由GNU开发的编程语言编译器。PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.autoconf是一个用于生成可以自动地配置软件源代码包以适应多种Unix类系统的shell脚本的工具.libxml2是一个用来解析XML文档的函数库,它用C语言写成,并且能为多种语言所调用.zlib是提供资料压缩之用的函式库.glibc:GNUC 库(GNU C Library,又称为glibc)是一种按照LGPL许可协议发布的,公开源代码的,免费的,方便从网络下载的C的编译程序.ncurses(new curses)是一个程序库,它提供了API,可以允许程序员编写独立于终端的基于文本的用户界面。cURL是一个利用URL语法在命令行下工作的文件传输工具,它支持文件上传和下载,所以是综合传输工具.e2fsprogs(又称为e2fsprograms)是用以维护ext2,ext3和ext4文件系统的工具程序集。OpenSSL是套开放源代码的SSL套件,其函式库是以C语言所写成,实作了基本的传输层资料加密功能。OpenLDAP是轻型目录访问协议(LightweightDirectory Access Protocol,LDAP)的自由和开源的实现.make编译工具。gd:graphicdevice,图像工具库,gd库是php处理图形的扩展库,gd库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表。(TTP image filter module requires the GD library.)

一、编译安装nginx
   1、 为nginx创建用户和组:
12#groupadd -r nginx#useradd -r -g nginx -s /bin/false -M nginx
   2、下载nginx源码包,并安装:nginx-1.4.1.tar.gz
1234#tar xf nginx-1.4.1.tar.gz#cd nginx-1.4.1#./configure--user=nginx --group=nginx --prefix=/usr--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 --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf--with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_gzip_static_module --with-file-aio#make && make install

3、 为nginx提供启动脚本

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127#vim /etc/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 "configurearguments:" | sed 's/[^*]*--user=\([^]*\).*/\1/g' -`   options=`$nginx-V 2>&1 | grep 'configurearguments:'`   for opt in $options; do       if [`echo $opt| grep '.*-temp-path'`]; then         value=`echo $opt| cut -d "=" -f2`         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 2esac为启动脚本赋予执行权限、添加至服务管理列表并开机自动启动:
123#chmod +x /etc/init.d/nginx#chkconfig --add nginx#chkconfig nginx on
4、 启动服务并测试,(注意iptables规则可能阻止访问):
12# service nginx start正在启动nginx:                                           [确定]

   nginx配置成功!





二、安装mysql:

1、首先下载文件:mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz

   2、为mysql创建数据目录、用户和组:
123#mkdir -pv /mysql/data#groupadd -r mysql#useradd -g mysql -r -s /bin/false -M -d /mysql/data mysql
3、 解压并初始化安装
12345678910#tar vxf mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz#mv mysql-5.6.12-linux-glibc2.5-x86_64 /usr/local/mysql#cd /usr/local/mysql#lsbin   datainclude         libmysql-testscripts sql-benchCOPYING docsINSTALL-BINARYmanREADME   share    support-files#chown mysql:mysql /mysql/data/#chown mysql:mysql ./*#./scripts/mysql_install_db --user=mysql --datadir=/mysql/data#chown -R root ./* 安装完成之后,主配置文件为/usr/local/mysql/my.cnf,可根据需要进行文件配置,这里添加如下项:

12345678# mkdir /var/run/mysqld# vim my.cnfdatadir= /mysql/databasedir= /usr/local/mysqlport= 3306socket= /tmp/mysql.sockpid_file= /var/run/mysqld/mysqld.pid

4、将mysql的头文件连接进系统头文件路径:
1#ln -sv /usr/local/mysql/include/usr/include/mysql

5、输出mysql库文件:

12#echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf#ldconfig

6、添加mysql命令的环境变量


123# vim /etc/profilePATH=$PATH:/usr/local/mysql/bin#. /etc/profile

7、为mysql添加启动脚本 ,添加进服务列表并开机自动启动

123#cp support-files/mysql.server/etc/rc.d/init.d/mysqld#chkconfig --add mysqld#chkconfig mysqld on
至此mysql安装完成,启动mysql服务


1#service mysqld start



三:编译安装php

php依赖很多库,这里将php依赖的库编译安装进/usr/local/phpextend文件夹里


1# mkdir /usr/local/phpextend

   1、编译安装jpegsrc,增强对jpeg格式图片的处理功能。jpegsrc.v9.tar.gz
1234#tar xf jpegsrc.v9.tar.gz#cd jpeg-9/#./configure --prefix=/usr/local/phpextend --enable-shared --enable-static#make && make install
   2、编译安装libpng,增强对png格式图片的处理功能。libpng-1.6.2.tar.gz
1234#tar xf libpng-1.6.2.tar.gz#cd libpng-1.6.2#./configure --prefix=/usr/local/phpextend#make && make install
   3、编译安装freetype,增强php对字体的处理功能。 freetype-2.4.12.tar.gz
1234#tar xf freetype-2.4.12.tar.gz#cd freetype-2.4.12#./configure --prefix=/usr/local/phpextend#make && make install
   4、编译安装mhash,使php支持多种哈稀演算法。 mhash-0.9.9.9.tar.gz
1234#tar xf mhash-0.9.9.9.tar.gz#cd mhash-0.9.9.9#./configure --prefix=/usr/local/phpextend#make && make install
5、编译安装libmcrypt,使php支持多种加密算法。libmcrypt-2.5.8.tar.gz
1234#tar xf libmcrypt-2.5.8.tar.gz#cd libmcrypt-2.5.8#./configure --prefix=/usr/local/phpextend#make && make install
    6、编译安装libevent,支持select、epoll、kqueue等事件调度机制。 libevent-2.0.21-stable.tar.gz

1234#tar xf libevent-2.0.21-stable.tar.gz#cd libevent-2.0.21-stable#./configure --prefix=/usr/local/phpextend#make && make install
   7、编译安装libiconv,实现一个字符编码到另一个字符编码的转换。 libiconv-1.14.tar.gz


1234#tar xf libiconv-1.14.tar.gz#cd libiconv-1.14#./configure --prefix=/usr/local/phpextend#make && make install
   8、输出库文件
12#echo "/usr/local/phpextend/lib" >> /etc/ld.so.conf.d/phpextend.conf#ldconfig
   9、编译安装php。   php-5.5.0.tar.bz2
12345#tar xf php-5.5.0.tar.bz2#cd php-5.5.0#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fastcgi --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --disable-debug --disable-ipv6 --with-iconv-dir --with-freetype-dir=/usr/local/phpextend--with-jpeg-dir=/usr/local/phpextend --with-png-dir=/usr/local/phpextend --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm--enable-mbstring--with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap#make       //如果报错“make: *** Error 1”使用命令“make ZEND_EXTRA_LIBS='-liconv' ”进行编译#make install
   提供php主配置文件:


1#cp php.ini-development /usr/local/php/etc/php.ini为php-fpm提供启动脚本,添加至服务列表并开机启动:


1234#cp sapi/fpm/init.d.php-fpm/etc/rc.d/init.d/php-fpm#chmod +x /etc/init.d/php-fpm#chkconfig --add php-fpm#chkconfig php-fpm on
   10、启用fastcgi:

   fastcgi比较重要,这里介绍一下:fastcgi是http server和动态脚本语言间通信的接口,nginx支持fastcgi;同时fastcgi也被许多脚本语言支持,包括php。传统的cgi接口性能很差,每次http server遇到动态程序时,都需要重启脚本解释器来执行解析,然后将结果返回给http server。fastcgi采用C/S架构,将http server和脚本解析器分开,同时在脚本解析器上启动一个或多个脚本解析守护进程,当htttserver遇到动态程序时,可以直接交付给fastcgi进程执行,然后将得到的结果返回给浏览器,这样http server只处理静态请求或将动态服务器的结果返回给客户端,从而提高性能。

   nginx通过fastcgi来调用php,而php-fpm是fastcgi的进程管理器。fastcgi的主要作用就是将动态语言和http server分离开来,所以nginx和php/php-fpm经常配置在不同服务器上,各司其职,从而提高性能。
123456789101112131415#cd /usr/local/php/etc/#cp php-fpm.conf.default php-fpm.conf#vi php-fpm.conf    //一般配置的依据如下内存小于4G服务器(值可逐级递减):修改如下参数:pm=dynamicpm.max_children=40pm.start_servers=10pm.min_spare_servers=10pm.max_spare_servers=40 ******************************内存大于4G服务器(值可逐级递增):修改如下参数:pm=staticpm.max_children=100

11、配置ngin支持php:
编辑nginx的主配置文件:

123456789101112location/ {            roothtml;            index index.html index.htm index.php;    //添加index.php      }//启用下面的配置location~ \.php$ {            root          html;            fastcgi_pass127.0.0.1:9000;            fastcgi_index index.php;            fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;            include       fastcgi_params;      }   编辑fastcgi_params参数,将里面的内容全部替换为以下内容:

123456789101112131415161718#vim/etc/nginx/fastcgi_paramsfastcgi_param GATEWAY_INTERFACECGI/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;启动php-fpm,重启nginx

1234567#service php-fpm startStartingphp-fpmdone#service nginx restartnginx:the configuration file /etc/nginx/nginx.confsyntax is oknginx:configuration file /etc/nginx/nginx.conf test issuccessful停止nginx:                                             [确定]正在启动nginx:tig

   添加php测试页面:
1234#vim /usr/html/index.php<?phpphpinfo();?>浏览器上访问:






   12、安装php加速器。
   eaccelerator是一个开源的php加速器,将编译后的php代码opcode缓存进共享内存,下次访问直接调用opcode从而提高速度。

123456789101112131415#unzip eaccelerator-0.9.6.1.zip#cd eaccelerator-0.9.6.1#lsAUTHORS       control.phpdoc                     ea_dasm.c   ea_restore.hMakefile.in opcodes.h         win32bugreport.php COPYING      eaccelerator.c          ea_dasm.h   ea_store.c    mm.c         optimize.cChangeLog   dasm.php   eaccelerator.h          ea_info.c   ea_store.h    mm.h         PHP_Highlight.phpconfig.m4   debug.c      eaccelerator.ini      ea_info.h   fnmatch.c   NEWS         READMEconfig.w32    debug.h      eaccelerator_version.hea_restore.cfnmatch.h   opcodes.c    README.win32#/usr/local/php/bin/phpizeConfiguring for:PHPApi Version:         20121113ZendModule Api No:      20121212ZendExtension Api No:   220121212#./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config#make && make install

到此lnmp的环境搭建完成。
http://asange.blog.iyunv.com/7125040/1229976
页: [1]
查看完整版本: lnmp源码编译安装Linux(6.4)+Nginx(1.4.1)+Mysql(5.6.12)+Php(5.5.0)