安装lnmp

安装lnmpphp



环境:
html

[root@scj ~]# cat /etc/issue          (系统)mysql

CentOS release 6.4 (Final)linux

Kernel \r on an \mnginx

[root@scj ~]# uname -r                (内核)c++

2.6.32-358.el6.i686sql




Selinux和Iptables:数据库

关闭selinux:json

vi /etc/sysconfig/selinuxvim

#找到SELINUX=****改为SELINUX=disabled

关闭防火墙:

iptables -F

/etc/init.d/iptables stop

chkconfig iptables off




安装必要的库和依赖包:

yum -y install patch cmake make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap




安装nginx:

##安装依赖包:
##只安装nginx时安装用:
yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

cd /usr/local/src/

安装pcre库:

使nginx支持rewrite URL地址重写,先安装pcre库

wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz

tar -zxf pcre-8.35.tar.gz

cd pcre-8.35

./configure                         (不用指定--prefix)

make

make install


安装nginx:

wget http://nginx.org/download/nginx-1.6.2.tar.gz

tar -zxf nginx-1.6.2.tar.gz 

cd nginx-1.6.2

./configure  --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_gunzip_module --with-http_realip_module

make

make install


启动nginx:

/usr/local/nginx/sbin/nginx          (此命令为启动nginx)

注意:在启动的时候可能会报下面这个错误:

/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

解决办法:

[root@www nginx-1.6.2]# find / -name libpcre.so.1

/usr/local/src/pcre-8.35/.libs/libpcre.so.1

/usr/local/lib/libpcre.so.1

找到库文件的位置,而后将其添加到/etc/ld.so.conf这个文件里:

vi /etc/ld.so.conf

添加一行:/usr/local/lib

执行:ldconfig 

再执行:/usr/local/nginx/sbin/nginx      (启动)


/usr/local/nginx/sbin/nginx -s stop(reload)      #中止


查看是否启动:

[root@www lib]# netstat -tlnp | grep nginx

tcp        0      0 0.0.0.0:80                 0.0.0.0:*                   LISTEN      13765/nginx 




安装mysql:

cd /usr/local/src/

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.22.tar.gz

tar -zxf mysql-5.6.22.tar.gz

cd mysql-5.6.22

mkdir -p /opt/mysql/data               (建立数据目录)

cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1  -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock  -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci  -DEXTRA_CHARSETS:STRING=utf8,gbk  -DWITH_DEBUG=0

make                                  (这个过程可能会很慢,请耐心等待)

make install

groupadd mysql                        (建立mysql用户组)

useradd -s /sbin/nologin -g mysql mysql

/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/opt/mysql/data --defaults-file=/usr/local/mysql/my.cnf --user=mysql          (初始化数据库)

chown -R mysql.mysql /opt/mysql        (对数据目录受权)

vi /usr/local/mysql/my.cnf             (修改配置文件)

添加:datadir=/opt/mysql/data

      basedir=/usr/local/mysql

cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld      (复制启动脚本)

chkconfig --add mysqld

chkconfig mysqld on

/etc/init.d/mysqld start                (启动mysql)

[root@www mysql]# netstat -tlnp | grep mysql

tcp        0      0 :::3306                     :::*                        LISTEN      28839/mysqld

vi /etc/profile                         (修改PATH路径)

添加:export PATH=$PATH:/usr/local/mysql/bin

source /etc/profile

mysqladmin -u root password "123456"    (给root建立密码)

注意:执行此命令可能会报下面的错误:

[root@www mysql]# mysqladmin -u root password "123456"

mysqladmin: connect to server at 'localhost' failed

error: 'Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)'

Check that mysqld is running and that the socket: '/tmp/mysqld.sock' exists!

解决方法:

 [root@scj mysql-5.6.22]# ps -ef | grep mysql

  root      3225     1  0 11:00 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/mysql/data --pid-file=/opt/mysql/data/scj.51.com.pid

  mysql     3449  3225  1 11:00 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/opt/mysql/data/scj.51.com.pid --socket=/var/lib/mysql/mysql.sock

  root      3498 10386  0 11:02 pts/0    00:00:00 grep mysql

 /etc/init.d/mysqld stop

 vi /usr/local/mysql/my.cnf 

 添加:[client]

       socket=/var/lib/mysql/mysql.sock    (socket文件的路径,由上面可知)

 /etc/init.d/mysqld start                   (OK)

 注意:若是还不行能够尝试:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock

 mysql -u root -p123456                     (登录myqsl)




安装php:

yum -y install make gcc-c++ gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel libjpeg-devel libpng-devel libvpx-devel

yum -y install patch cmake make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip libcap


cd /usr/local/src/

安装php的依赖库

安装libmcrypt:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz

tar -zxf libmcrypt-2.5.7.tar.gz

cd libmcrypt-2.5.7

./configure

make

make install

ldconfig

cd /usr/local/src/libmcrypt-2.5.7/libltdl/

./configure --enable-ltdl-install

make

make install

vi /etc/ld.so.conf

添加:/usr/local/lib/

ldconfig 


安装php:

wget http://mirrors.sohu.com/php/php-5.5.10.tar.gz

tar -zxf php-5.5.10.tar.gz

cd php-5.5.10

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-pdo-sqlite --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock --with-mcrypt --with-mhash --with-iconv-dir=/usr/local --with-gd --with-gettext --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --with-curl --with-xmlrpc --with-zlib --with-pear --with-openssl --with-libdir --with-kerberos --enable-cgi --enable-fpm --enable-pdo --enable-opcache --enable-mbstring --enable-gd-native-ttf --enable-xml --enable-exif --enable-zip --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop --enable-json --enable-bcmath --enable-calendar --enable-ftp --enable-inline-optimization --enable-pcntl --enable-mbregex --disable-rpath --enable-filter

                使用mysqlnd驱动链接,不须要安装mysql也可安装php

                参考:http://zhangxugg-163-com.iteye.com/blog/1894990

注意:这里可能会出现下面报错:

configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no

解决方法:

ldconfig

cd /usr/local/src/libmcrypt-2.5.7/libltdl/

./configure --enable-ltdl-install

make

make install

执行后会出现以下所示:

wKiom1UsroGS1mrDAAITKL0v90o038.jpg

vi /etc/ld.so.conf

添加:/usr/local/lib/            (上面添加过了)

ldconfig                          (这一步必定要执行,从新加载库文件,不然编译php会报错)

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la >/dev/null 2>&1

ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so >/dev/null 2>&1

ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 >/dev/null 2>&1

ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 >/dev/null 2>&1

                                  (上面这4行能够不用执行)

而后再:

cd /usr/local/src/php-5.5.10

执行./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-pdo-sqlite --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock --with-mcrypt --with-mhash --with-iconv-dir=/usr/local --with-gd --with-gettext --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --with-curl --with-xmlrpc --with-zlib --with-pear --with-openssl --with-libdir --with-kerberos --enable-cgi --enable-fpm --enable-pdo --enable-opcache --enable-mbstring --enable-gd-native-ttf --enable-xml --enable-exif --enable-zip --enable-soap --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-shmop --enable-json --enable-bcmath --enable-calendar --enable-ftp --enable-inline-optimization --enable-pcntl --enable-mbregex --disable-rpath --enable-filter

make                              (这个过程可能会很慢,请耐心等待)

make install


cp -a /usr/local/src/php-5.5.10/php.ini-production /usr/local/php/etc/php.ini       (拷贝文件)

cp -a /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注释去掉


cp -a /usr/local/src/php-5.5.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

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

chkconfig --add php-fpm

chkconfig php-fpm on

/etc/init.d/php-fpm start              #启动php-fpm

[root@localhost php]# /etc/init.d/php-fpm -h
Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status}



##参考:
[root@www etc]# netstat -tlnp | grep php          (查看是否启动)
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      3222/php-fpm  
注意:
    [root@scj ~]# ps -ef | grep php              #找到php的主进程号
    root     12161     1  0 05:48 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
    kill -SIGUSR2 12161                #从新启动php(相似reload)
    kill -SIGINT 12161                 #马上中止进程
    kill -SIGQUIT 12161                #平滑中止进程
    SIGINT, SIGTERM   马上中止进程
    SIGQUIT    平滑终止进程
    SIGUSR1    从新打开日志文件
    SIGUSR2    平滑重载全部worker进程并从新载入配置和二进制模块(相似reload)

     



nginx与php整合:

整合:

cd /usr/local/nginx/conf/

vi nginx.conf

找到以下几行,去掉注释,并修改,注意红色部分

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

如图:

wKioL1Uste-inaiOAADyx8StFiw119.jpg

/usr/local/nginx/sbin/nginx -t         (检查配置文件是否正确)

/usr/local/nginx/sbin/nginx -s reload  (reload从新加载配置文件,关闭并启动新的worker进程)


测试:

vi /usr/local/nginx/html/index.php

<?php

phpinfo();

?>

打开浏览器,访问:192.168.186.129/index.php

如图:

wKiom1UsvGfifey2AAQurZImW-A168.jpg







附加:

使用/etc/init.d/nginx脚本统一管理nginx,php的启动,关闭,重启等等:

vi /etc/init.d/nginx

#!/bin/bash
#
#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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

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

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    /usr/local/php/sbin/php-fpm -y /usr/local/php/etc/php-fpm.conf
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    pkill -9 php-fpm
    pkill -9 php-fpm
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
    killall -9 nginx
}

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

chmod 755 /etc/init.d/nginx

chkconfig nginx on

相关文章
相关标签/搜索