CentOS 编译 nginx+php+mysql 安装手记

来源:互联网 发布:yum 没有可用软件包 编辑:程序博客网 时间:2024/05/01 23:22

 近期需要搭建一台web测试机,系统环境要与生产环境机器中的一模一样。

 先介绍一下环境:

 系统:CentOS 5.2 

 Nginx:0.9.5

 php:5.3.5

 MySQL:5.1.30

 

花了我一天半的时间,不断报错,不断检查,不断修改,不断百度,不断谷歌,今天中午终于搞完了,特写此安装手记,以便需要的人参考。

可能有人要问,为什么不采用现在最流行的lamp环境呢,web服务不能用apache么?为什么非要用nginx呢?

这里需要说一下,Nginx 是一个轻量级的高性能 Http WebServer,以事件驱动方式编写,因此相比 Apache 而言,Nginx 更加稳定、性能更好,资源占用较低。相对于apache来说,可以承载的了更大的并发 现在有很多大型门户的web服务器已经放弃了lamp而使用lnmp

所以,本身nginx和php两个一点都不认识的,所以安装起来要比lamp麻烦的多

 

一、安装前准备:

     准备好程序安装所需的开发包,这里就通过yum去装了,也为了省事。

        yum -y install ntp vim-enhanced gcc gcc-c++gcc-g77 flex bison autoconf automake bzip2-devel  /
ncurses-devel openssl-devel libtool*zlib-devel libxml2-devel libjpeg-devel libpng-devel libtiff-devel  /
fontconfig-devel freetype-devel libXpm-develgettext-devel curl curl-devel pam-devel /
e2fsprogs-devel krb5-devel libidn libidn-devel

 

一、Nginx的安装:

           

       1. pcre-8.00.tar.gz

         tar zxvf pcre-8.00.tar.gz

         cd pcre-8.00.tar.gz

         ./configure && make && makeinstall

       2. nginx-0.9.5.tar.gz

         ./configure --user=www --group=www --prefix=/usr/local/nginx/--with-http_stub_status_module --with-http_ssl_module--with-md5=/usr/lib --with-sha1=/usr/lib--with-openssl=/usr/lib             

     #此次手记中,所有的安装目录我都放在/usr/local/  各位可以自己修改

             make

             make install

      3.接下来安装GD和相关的lib包

      以下是需要用的源码包,可以从网上下到。

              jxvf gd-2.0.35.tar.bz2

              libiconv-1.12.tar.gz
              libmcrypt-2.5.8.tar.bz2

 

 开始安装

            tar jxvf gd-2.0.35.tar.bz2
            cd gd-2.0.35
            ./configure --prefix=/usr/local/gd2

             make

             make install

            

              mhash-0.9.9.tar.gz

              tar -zxvf  mhash-0.9.9.tar.gz

               cd mhash-0.9.9

              ./configure --prefix=/usr && make&& make install

             

               libiconv-1.12.tar.gz

                tar zxvf libiconv-1.12.tar.gz

                cd libiconv-1.12 
                ./configure --prefix=/usr && make&& make install

 

 

               libmcrypt-2.5.8.tar.bz2


                tar jxvf libmcrypt-2.5.8.tar.bz2

                 cd libmcrypt-2.5.8
                ./configure --prefix=/usr && make&& make install

              

          

         vim /etc/ld.so.conf

 

           添加:

                    /usr/lib

                    include ld.so.conf.d/*.conf 

                    /usr/lib

             #如果已有就不用添加。

 

启动nginx:

/usr/local/nginx/sbin/nginx start

 

查看一下80端口是否启来

netstat -antpl

 

 

二、MySQL的安装:

         tar zxvf mysql-5.1.30.tar.gz

         cd mysql-5.1.30

         ./configure /
"--prefix=/usr/local/mysql" /
"--localstatedir=/data/mysql/data" /
"--with-comment=Source" /
"--with-server-suffix=-Linuxtone.Org" /
"--with-mysqld-user=mysql" /
"--without-debug" /
"--with-big-tables" /
"--with-charset=utf8" /
"--with-collation=utf8_chinese_ci" /
"--with-extra-charsets=all" /
"--with-pthread" /
"--enable-static" /
"--enable-thread-safe-client" /
"--with-client-ldflags=-all-static" /
"--with-mysqld-ldflags=-all-static" /
"--enable-assembler" /
"--with-plugins=all" /
"--without-ndb-debug"

              make
              make install        

 

useradd mysql -d /data/mysql -s/sbin/nologin      #创建mysql用户

/usr/local/mysql/bin/mysql_install_db--user=mysql  #安装默认数据库

cd /usr/local/mysql

以下是权限的设定:

chown -R root:mysql . 
mkdir -p /data/mysql/data
chown -R mysql /data/mysql/data

 

cp share/mysql/my-huge.cnf /etc/my.cnf      #复制mysql配置文件 
cp share/mysql/mysql.server/etc/rc.d/init.d/mysqld  #复制mysql启动文件 
chmod 755 /etc/rc.d/init.d/mysqld 
chkconfig --add mysqld                          #添加mysql随系统启动 
/etc/rc.d/init.d/mysqld start                     #启动mysql

/usr/local/mysql/bin/mysql -u root -p    #进入mysql  默认密码为空

 

   没问题后quit退出 下面进行最关键的一部 安装php

 

 

 

三、php的安装:

    php和nginx的整合是通过php-FastCGI 

    FastCGI 是一个可伸缩、高速的在web server和脚本语言间通迅的接口。被许多脚本语言所支持,包括 php

    多数流行的web server都支持 FastCGI。

    正常情况下,nginx和php直接是完全不认识的,我们就是通过php-fastcgi将二者整合。

 

    php5.3.0之前的版本,php-FastCGI 是需要单独安装的。但是在这之后,php-FastCGI 就整合在了php的源码包中,不必再去单独安装。在这里我用的就是php5.3.5的版本,内置了php-fpm ,编译时开启,并且编译后不存在 php-cgi 文件了

   下面开始安装。

 

     php-5.3.5.tar.gz

 

    tar zxvf php-5.3.5.tar.gz

    cd php-5.3.5

    先执行./buildconf --force

    如果报错,可能是你的 autoconf不是 2.13 版本的,PHP5.3.5的bug,需要安装 autoconf为2.13不版本:
    yum install autoconf2.13

    安装完成后再设置一下环境变量:export PHP_AUTOCONF="/usr/bin/autoconf-2.13"

    再次运行:./buildconf --force ,出现 Forcing buildconf ,则运行成功。

    接着我们再继续编译

    ./configure '--prefix=/usr/local/php' '--enable-fastcgi' '--enable-fpm' '--enable-discard-path' '--enable-force-cgi-redirect--with-config-file-path=/usr/local/php/etc' '--enable-zend-multibyte' '--with-mysql=/usr/local/mysql' '--with-libxml-dir' '--with-xmlrpc' '--with-gd=/usr/local/gd2' '--with-jpeg-dir' '--with-png-dir' '--with-bz2' '--with-freetype-dir' '--with-iconv-dir' '--with-zlib-dir' '--with-curl' '--with-curlwrappers' '--with-openssl' '--with-mcrypt' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--enable-sysvsem' '--enable-inline-optimization' '--enable-soap' '--enable-gd-native-ttf' '--enable-ftp' '--enable-mbstring' '--enable-exif' '--disable-debug' '--disable-ipv6'

 

    make

注意一下,我在make的时候总会报错,这个错误一直过不去,

错误提示为:
In file included from /root/php-5.2.17/sapi/cgi/fpm/fpm.c:12:
/root/php-5.2.17/sapi/cgi/fpm/fpm_env.h:20: 错误:与 ‘clearenv’ 类型冲突
/usr/include/stdlib.h:689: 错误:‘clearenv’ 的上一个声明在此
make: *** [sapi/cgi/fpm/fpm.lo] 错误 1

对这个错误我很苦恼,又是谷歌又是百度,愣是没有找到解决办法。

昨天就是因为这个错误,后来就懒得弄了。

今天继续分析错误原因,最终发现应该是iconv包问题

所以在make的时候带上参数

 

make ZEND_EXTRA_LIBS='-liconv'

 

完美make成功  别忘了在再make test一下 呵呵

 

最后 make install

安装成功。。。

 

安装完成之后,复制php.ini-production 到安装目录下的etc/目录下

cp  php.ini-production /usr/local/php/etc/

cd  /usr/local/php/etc/  # 切换到安装目录下的配置文件目录

cp  php-fpm.conf.default  php-fpm.conf

修改php-fpm.conf 启用如下几行,即去掉前面的分号(;)
    pid = run/php-fpm.pid
    error_log = log/php-fpm.log
    log_level = notice
    listen = 127.0.0.1:9000
    listen.allowed_clients = 127.0.0.1
    pm = dynamic
    pm.max_children = 50
    pm.start_servers = 5 
    pm.min_spare_servers = 5
    pm.max_spare_servers = 35
    pm.max_requests = 500
    env[HOSTNAME] = $HOSTNAME
    env[PATH] = /usr/local/bin:/usr/bin:/bin
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp

wq保存退出

 

最后去/etc/init.d下  看是否有php-fpm这个启动脚本

如果有, 删除了 重新写一个

如果没有 直接写一个就行

vim /etc/init.d/php-fpm
    内容如下:
    #! /bin/sh

    ### BEGIN INIT INFO
    # Provides:          php-fpm
    # Required-Start:    $remote_fs $network
    # Required-Stop:     $remote_fs $network
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts php-fpm
    # Description:       starts the PHP FastCGI Process Manager daemon
    ### END INIT INFO

    prefix=/usr/local/php
    exec_prefix=${prefix}

    php_fpm_BIN=${exec_prefix}/sbin/php-fpm
    php_fpm_CONF=${prefix}/etc/php-fpm.conf
    php_fpm_PID=${prefix}/var/run/php-fpm.pid


    php_opts="--fpm-config $php_fpm_CONF"


    wait_for_pid () {
        try=0

        while test $try -lt 35 ; do

            case "$1" in
                'created')
                if [ -f "$2" ] ; then
                    try=''
                    break
                fi
                ;;

                'removed')
                if [ ! -f "$2" ] ; then
                    try=''
                    break
                fi
                ;;
            esac

            echo -n .
            try=`expr $try + 1`
            sleep 1

        done

    }

    case "$1" in
        start)
            echo -n "Starting php-fpm "

            $php_fpm_BIN $php_opts

            if [ "$?" != 0 ] ; then
                echo " failed"
                exit 1
            fi

            wait_for_pid created $php_fpm_PID

            if [ -n "$try" ] ; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
        ;;

        stop)
            echo -n "Gracefully shutting down php-fpm "

            if [ ! -r $php_fpm_PID ] ; then
                echo "warning, no pid file found - php-fpm is not running ?"
                exit 1
            fi

            kill -QUIT `cat $php_fpm_PID`

            wait_for_pid removed $php_fpm_PID

            if [ -n "$try" ] ; then
                echo " failed. Use force-exit"
                exit 1
            else
                echo " done"
            fi
        ;;

        force-quit)
            echo -n "Terminating php-fpm "

            if [ ! -r $php_fpm_PID ] ; then
                echo "warning, no pid file found - php-fpm is not running ?"
                exit 1
            fi

            kill -TERM `cat $php_fpm_PID`

            wait_for_pid removed $php_fpm_PID

            if [ -n "$try" ] ; then
                echo " failed"
                exit 1
            else
                echo " done"
            fi
        ;;

        restart)
            $0 stop
            $0 start
        ;;

        reload)

            echo -n "Reload service php-fpm "

            if [ ! -r $php_fpm_PID ] ; then
                echo "warning, no pid file found - php-fpm is not running ?"
                exit 1
            fi

            kill -USR2 `cat $php_fpm_PID`

            echo " done"
        ;;

        *)
            echo "Usage: $0 {start|stop|force-quit|restart|reload}"
            exit 1
        ;;

    esac    
 

启动 php-fpm

 /etc/init.d/php-fpm  start

 

如果无法执行,可能是由于权限的问题 给个777权限即可

chmod -R 777 /etc/init.d/php-fpm

完了再启动

 

启动过后,用netstat -antpl 看下端口  可以看到9000端口已经启动

 

最后 需要更改一下nginx的配置文件

 

vim /usr/local/nginx/conf/nginx.conf

去掉下面语句前的注释

error_log /var/log/nginx/error.default.log;            #开启错误日志
pid /usr/local/webserver/nginx/nginx.pid;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
       #
        location ~ .*/.(php|php5)?$
        {
                        fastcgi_pass  127.0.0.1:9000;
                        fastcgi_index index.php;
                        include fastcgi.conf;
         }

 

保存退出后 重启nginx

netstat -antpl

看一下nginx的pid 

kill -9 pid的号码 

kill掉第一个以后 再 netstat -antpl

会有一个nginx-worker进程

直接kill掉

 

再启动nginx

cd /usr/local/nginx/sbin 

./nginx

再看下80端口有没有启来

 

 到这里 整个LNMP的配置就结束了  测试一下吧  ^_^

 

可以写一个php文件放在/usr/local/nginx/html

就随便写个查看php信息的文件吧

cd /usr/local/nginx/html

vim phpinfo.php

<?php
phpinfo();
?>

 

打开浏览器 http://主机地址/phpinfo.php

 

如果出来了php的信息页面  恭喜你 配置成功啦

 

 

另注: 本文为作者调通lnmp一时兴起整理出来的文章,提供给需要的朋友。

    本文内容大部分为根据本人在调试中自己写的,还有一部分是从网上摘录的

原创粉丝点击