LNMP架构搭建

 

php编译安装:

[root@wangchao ~]# cd /usr/local/src/

[root@wangchao src]# wget wget  http://cn2.php.net/distributions/php-5.4.37.tar.bz2

[root@wangchao src]# rm -rf /usr/local/php/           //删除上次LAMP安装时做的PHP

[root@wangchao src]# tar jxvf php-5.4.37.tar.bz2

[root@wangchao src]# cd php-5.4.37

[root@wangchao php-5.4.37]# ./configure \

> --prefix=/usr/local/php \

> --with-config-file-path=/usr/local/php/etc \

> --enable-fpm \

> --with-fpm-user=php-fpm \

> --with-fpm-group=php-fpm \

> --with-mysql=/usr/local/mysql \

> --with-mysql-sock=/tmp/mysql.sock \

> --with-libxml-dir \

> --with-gd \

> --with-jpeg-dir \

> --with-png-dir \

> --with-freetype-dir \

> --with-iconv-dir \

> --with-zlib-dir \

> --with-mcrypt \

> --enable-soap \

> --enable-gd-native-ttf \

> --enable-ftp \

> --enable-mbstring \

> --enable-exif \

> --enable-zend-multibyte \

> --disable-ipv6 \

> --with-pear \

> --with-curl \

> --with-openssl

 

如有报错,试试先执行如下命令:

[root@wangchao php-5.4.37]#yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

 

 

 

 

 

[root@wangchao php-5.4.37]# make

[root@wangchao php-5.4.37]# echo $?

0

[root@wangchao php-5.4.37]# echo $?

0

[root@wangchao php-5.4.37]# cp php.ini-production /usr/local/php/etc/php.ini

//拷贝一份配置文件

[root@wangchao php-5.4.37]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

//拷贝启动脚本

[root@wangchao php-5.4.37]# vim !$          //查看脚本

vim /etc/init.d/php-fpm

[root@wangchao php-5.4.37]# chmod 755 /etc/init.d/php-fpm           //授执行的权限

[root@wangchao php-5.4.37]# chkconfig --add php-fpm                //加入系统服务项

[root@wangchao php-5.4.37]# chkconfig php-fpm on                //开机自启动

[root@wangchao php-5.4.37]# service php-fpm start

failed                                                //启动失败,还少相关配置

[root@wangchao php-5.4.37]# cd /usr/local/php/etc/

[root@wangchao etc]# mv php-fpm.conf.default php-fpm.conf       //重命名成php-fpm.conf

[root@wangchao etc]# /usr/local/php/sbin/php-fpm -t             //检查配置文件是否有错

[root@wangchao etc]# !ser                    //重启还是失败,无用户

service php-fpm start

[root@wangchao etc]# useradd -s /sbin/nologin php-fpm            //增加用户

[root@wangchao etc]# !ser

service php-fpm start

[root@wangchao etc]# ps aux|grep php-fpm                       //查看进程是否启动

[root@wangchao etc]# netstat -lnp                              //查看监听的端口

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      19600/php-fpm

 

 

 

[root@wangchao etc]#  ls

pear.conf  php-fpm.conf  php.ini

 

 

 

 

 

Nginx编译安装:

 

[root@wangchao etc]# cd /usr/local/src/

[root@wangchao src]# ls

[root@wangchao src]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

[root@wangchao src]# tar zxvf nginx-1.6.2.tar.gz

[root@wangchao src]# cd nginx-1.6.2

[root@wangchao nginx-1.6.2]# ./configure   --prefix=/usr/local/nginx   --with-pcre

[root@wangchao nginx-1.6.2]# echo $?

0

[root@wangchao nginx-1.6.2]# make

[root@wangchao nginx-1.6.2]# echo $?

0

[root@wangchao nginx-1.6.2]# make install

[root@wangchao nginx-1.6.2]# echo $?

0

[root@wangchao nginx-1.6.2]# cd /usr/local/nginx/

[root@wangchao nginx]# ls

conf  html  logs  sbin

[root@wangchao nginx]# ls sbin/nginx                      //nginx的可执行文件

sbin/nginx

[root@wangchao nginx]# /usr/local/nginx/sbin/nginx          //启动nginx80端口被占用

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

[root@wangchao nginx]# apachectl stop

[root@wangchao nginx]# /usr/local/nginx/sbin/nginx          //启动nginx

 

[root@wangchao nginx]# ps aux |grep nginx           //查看进程有一主一次

root     21901  0.0  0.0   5380   636 ?        Ss   05:18   0:00 nginx: master process /usr/local/nginx/sbin/nginx

nobody   21902  0.0  0.0   5564   988 ?        S    05:18   0:00 nginx: worker process

[root@wangchao nginx]# netstat -lnp             //查看监听端口

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

 

 

 

 

nginx无自己的启动脚本(可以自己写一份)

解析php,使nginxphp联系在一起

 

 

 

 

测试php解析

[root@wangchao ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;

            include        fastcgi_params;

        }

//将目录改到web网站根目录下

 

客户端访问,到nginx默认页面

spacer.gif

 

 

[root@wangchao ~]# cat /usr/local/nginx/html/index.html     

//查看文件,刚访问的页面配置为该文件

[root@wangchao ~]# cd /usr/local/nginx/html/

[root@wangchao html]# vim info.php

<?php

phpinfo();

?>

[root@wangchao html]# /usr/local/nginx/sbin/nginx -t          //查看配置文件有无错误

[root@wangchao html]# /usr/local/nginx/sbin/nginx -s reload      //重新加载

客户端访问,正常解析

spacer.gif

[root@wangchao html]# curl localhost

[root@wangchao html]# curl localhost/info.php

//使用curl测试

 

nginx启动脚本和配置文件

nginxapachect 文件工具重启,需手动写一个

[root@wangchao html]# vim /etc/init.d/nginx            //打开空文件,写入

#!/bin/bash

# chkconfig: - 30 21

# description: http service.

# Source Function Library

. /etc/init.d/functions

# Nginx Settings

 

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

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

NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"

 

start() {

        echo -n $"Starting $prog: "

        mkdir -p /dev/shm/nginx_temp

        daemon $NGINX_SBIN -c $NGINX_CONF

        RETVAL=$?

        echo

        return $RETVAL

}

 

stop() {

        echo -n $"Stopping $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -TERM

        rm -rf /dev/shm/nginx_temp

        RETVAL=$?

        echo

        return $RETVAL

}

 

reload(){

        echo -n $"Reloading $prog: "

        killproc -p $NGINX_PID $NGINX_SBIN -HUP

        RETVAL=$?

        echo

        return $RETVAL

}

 

restart(){

        stop

        start

}

 

configtest(){

    $NGINX_SBIN -c $NGINX_CONF -t

    return 0

}

 

case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  reload)

        reload

        ;;

  restart)

        restart

        ;;

  configtest)

        configtest

        ;;

  *)

        echo $"Usage: $0 {start|stop|reload|restart|configtest}"

        RETVAL=1

esac

exit $RETVAL

 

 

[root@wangchao html]# chmod 755 !$

chmod 755 /etc/init.d/nginx

[root@wangchao html]# chkconfig --add nginx

[root@wangchao html]# chkconfig nginx on

[root@wangchao html]# service nginx start

[root@wangchao html]# service nginx stop

[root@wangchao html]# service nginx restart

 

[root@wangchao html]# vim /usr/local/nginx/conf/nginx.conf  //打开默认配置文件,使用gg调整光标到首行,然后使用dG命令清空原先配置,写入:

user nobody nobody;

worker_processes 2;

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events

{

    use epoll;

    worker_connections 6000;

}

http

 

{

    include mime.types;

    default_type application/octet-stream;

    server_names_hash_bucket_size 3526;

    server_names_hash_max_size 4096;

    log_format  wang  '$remote_addr $http_x_forwarded_for [$time_local]'

    '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';

    sendfile on;

    tcp_nopush on;

    keepalive_timeout 30;

    client_header_timeout 3m;

    client_body_timeout 3m;

    send_timeout 3m;

    connection_pool_size 256;

    client_header_buffer_size 1k;

    large_client_header_buffers 8 4k;

    request_pool_size 4k;

    output_buffers 4 32k;

    postpone_output 1460;

    client_max_body_size 10m;

    client_body_buffer_size 256k;

    client_body_temp_path /usr/local/nginx/client_body_temp;

    proxy_temp_path /usr/local/nginx/proxy_temp;

    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

    fastcgi_intercept_errors on;

    tcp_nodelay on;

    gzip on;

    gzip_min_length 1k;

    gzip_buffers 4 8k;

    gzip_comp_level 5;

    gzip_http_version 1.1;

    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

    include vhosts/*.conf;

}

 

 

[root@wangchao html]# pwd

/usr/local/nginx/html

root@wangchao html]# cd /usr/local/nginx/conf/

[root@wangchao conf]# mkdir vhosts

[root@wangchao conf]# cd vhosts/

[root@wangchao conf]# vim default.conf

server

{

    listen 80 default_server;

    server_name localhost;

    index index.html index.htm index.php;

    root /tmp/1233;

    deny all;

}

//使默认为错误403

root@wangchao conf]# /usr/local/nginx/sbin/nginx -t

[root@wangchao conf]# /etc/init.d/nginx reload

[root@wangchao vhosts]# curl -x127.0.0.1:80 111.com

<head><title>403 Forbidden</title></head>

//curl测试出现403错误

 

 

[root@wangchao vhosts]# vim 111.conf

server

{

    listen 80;

    server_name 111.com;

    index index.html index.htm index.php;

    root /data/www;

 

    location ~ \.php$ {

        include fastcgi_params;

       # fastcgi_pass unix:/tmp/php-fcgi.sock;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

}

 

 

 

 

 

root@wangchao conf]# /usr/local/nginx/sbin/nginx -t

[root@wangchao conf]# /etc/init.d/nginx reload

[root@wangchao vhosts]# /etc/init.d/nginx reload

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      19600/php-fpm

[root@wangchao vhosts]# curl -x127.0.0.1:80 111.com -I

HTTP/1.1 301 Moved Permanently

 

[root@wangchao vhosts]# curl -x127.0.0.1:80 111.com/forum.php -I

HTTP/1.1 200 OK

 

 

 

php-fpm配置文件

[root@wangchao ~]# ls /usr/local/php/etc/php-fpm.conf

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

// php-fpm.conf服务相关配置文件

[root@wangchao ~]# ls /usr/local/php/etc/php.ini

/usr/local/php/etc/php.ini

//全局服务配置

[root@wangchao ~]# vim /usr/local/php/etc/php-fpm.conf         //查看配置

[root@wangchao ~]# > /usr/local/php/etc/php-fpm.conf

[global]

pid = /usr/local/php/var/run/php-fpm.pid

error_log = /usr/local/php/var/log/php-fpm.log

[www]

listen = /tmp/www.sock

user = php-fpm

group = php-fpm

listen.owner = nobody                       //和后面的nginx的一致

listen.group = nobody                       // 同上

pm = dynamic

pm.max_children = 50

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 500

rlimit_files = 1024

 

[root@wangchao ~]# /usr/local/php/sbin/php-fpm -t

[root@wangchao ~]# /etc/init.d/php-fpm restart

[root@wangchao ~]# ps aux |grep php-fpm                     //(查看poolwww)

 

[root@wangchao ~]# ls /usr/local/nginx/conf/vhosts/

111.conf  default.conf

[root@wangchao ~]# cat /usr/local/nginx/conf/vhosts/111.conf

[root@wangchao ~]# vim /usr/local/php/etc/php-fpm.conf

[www]

slowlog = /tmp/www_slow.log

request_slowlog_timeout = 1

php_admin_value[open_basedir]=/data/www/:/tmp/

 

//慢反应日志,反应慢时记录日志

 

 

 

 

配置文件参考

[root@wangchao html]# vim /usr/local/nginx/conf/nginx.con

user nobody nobody;

worker_processes 2;

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

events

{

    use epoll;

    worker_connections 6000;

}

http

 

{

    include mime.types;

    default_type application/octet-stream;

    server_names_hash_bucket_size 3526;

    server_names_hash_max_size 4096;

    log_format wang  '$remote_addr $http_x_forwarded_for [$time_local]'

    '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';

    sendfile on;

    tcp_nopush on;

    keepalive_timeout 30;

    client_header_timeout 3m;

    client_body_timeout 3m;

    send_timeout 3m;

    connection_pool_size 256;

    client_header_buffer_size 1k;

    large_client_header_buffers 8 4k;

    request_pool_size 4k;

    output_buffers 4 32k;

    postpone_output 1460;

    client_max_body_size 10m;

    client_body_buffer_size 256k;

    client_body_temp_path /usr/local/nginx/client_body_temp;

    proxy_temp_path /usr/local/nginx/proxy_temp;

    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

    fastcgi_intercept_errors on;

    tcp_nodelay on;

    gzip on;

    gzip_min_length 1k;

    gzip_buffers 4 8k;

    gzip_comp_level 5;

    gzip_http_version 1.1;

    gzip_types text/plain application/x-javascript text/css text/htm application/xml;

    include vhosts/*.conf;

}