1.1 php编译安装

cd /usr/local/src/

./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 --disable-ipv6 --with-curl

出错  安装

yum install -y libcurl-devel

 make

rm -rf /usr/local/php  之前安装过删除后 make install  或者更改名称

make install

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

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

chmod 755 /etc/init.d/php-fpm

chkconfig --add php-fpm

service php-fpm start

cd /usr/local/php/etc/

ls

mv php-fpm.conf.default php-fpm.conf  少启动文件

/usr/local/php/sbin/php-fpm -t

useradd -s /sbin/nologin  php-fpm  出错 少用户

ps aux |grep php-fpm

Nginx编译安装

cd /usr/local/src/

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

tar zxvf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure --prefix=/usr/local/nginx --with-pcre

yum install -y pcre-devel

make

make isntall

cd /usr/local/nginx/

ls

 ls sbin/nginx
/usr/local/nginx/sbin/nginx  启动nginx

ps aux |grep httpd

/usr/local/apache2/bin/apachetl stop  占用80 端口就停止

 ps aux |grep nginx
netstat -lnp

1.3 测试php解析

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

打开php相关的配置

location ~ \.php [

编辑第五行文件位置 /usr/local/nginx/html$

vim info.php

<?php
phpinfo();
?>

/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload

mv info.php /usr/local/nginx/html/

curl localhost

1.4 nginx启动脚本和配置文件

 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

     chmcd 755 !$    /                   chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

service nginx stop

service nginx start

service nginx restart

service nginx configtest

vim /usr/local/nginx/conf/nginx.conf  配置文件

添加脚本

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 combined_realip '$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;

 }

/usr/local/nginx/sbin/nginx  -t
cd /usr/local/nginx/conf/

mkdir vhosts

cd vhosts/

vim default.conf

server
{
    listen 80 default_server;
    server_name localhost;
    index index.html index.htm index.php;
    root /tmp/1233;
    deny all;
}
mkdir /tmp/1233

/etc/init.d/nginx reload

curl -x127.0.0.1:80 akjljlkkld.akjljkljl.com
正确是这样子

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

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;
 }

}

 

 /usr/local/nginx/sbin/nginx  -t
 /etc/init.d/nginx reload

curl -x127.0.0.1:80 111.com -I  不加-I  什么都没有

正确是这样子

HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Sun, 27 Mar 2016 23:57:16 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.4.45
location: forum.php

curl -x127.0.0.1:80 111.com/forum.php -I

正确会出

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sun, 27 Mar 2016 23:58:56 GMT
Content-Type: text/html; charset=gbk
Connection: keep-alive
X-Powered-By: PHP/5.4.45
Set-Cookie: bc79_2132_saltkey=icC8cKCh; expires=Tue, 26-Apr-2016 23:58:38 GMT; path=/; httponly
Set-Cookie: bc79_2132_lastvisit=1459119518; expires=Tue, 26-Apr-2016 23:58:38 GMT; path=/
Set-Cookie: bc79_2132_sid=CZyCFu; expires=Mon, 28-Mar-2016 23:58:38 GMT; path=/
Set-Cookie: bc79_2132_lastact=1459123118%09forum.php%09; expires=Mon, 28-Mar-2016 23:58:38 GMT; path=/
Set-Cookie: bc79_2132_onlineusernum=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/
Set-Cookie: bc79_2132_sid=CZyCFu; expires=Mon, 28-Mar-2016 23:58:38 GMT; path=/

 vim 111.conf

       fastcgi_pass unix:/tmp/php-fcgi.sock;          启动
     #  fastcgi_pass 127.0.0.1:9000;              关闭
/etc/init.d/nginx reload

curl -x127.0.0.1:80 111.com/forum.php -I

正确会是这样子

HTTP/1.1 502 Bad Gateway
Server: nginx/1.6.2
Date: Mon, 28 Mar 2016 00:03:15 GMT
Content-Type: text/html
Content-Length: 172
Connection: keep-alive

 php-fpm配置文件

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

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

!vim

[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
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

slowlog = /tmp/www_slow.log 后加
request_slowlog_timeout = 1 后加
php_admin_value[open_basedir]=/data/www/:/tmp 后加

 

[www1]
listen = /tmp/www1.sock
user = php-fpm
group = php-fpm
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
      

ps aux |grep php-fpm

  /usr/local/php/sbin/php-fpm -t

/etc/init.d/php-fpm restart

!ps

常见的502问题解决

cd /usr/local/nginx/conf/vhosts/
mv 111.conf  test.conf

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload   /    /etc/init.d/nginx reload

vim ../nginx.conf

cat /usr/local/nginx/logs/nginx
ls -l /tmp/www.sock

 ps aux |grep nginx

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

 

group = php-fpm  下一行 加入
listen.owner = nobody
listen.group = nobody

 /usr/local/php/sbin/php-fpm -t

/etc/init.d/php-fpm restart

nginx用户认证

cd /usr/local/nginx/conf/vhosts/

vim test.comf

root /data/www;
    location ~ .*admin\.php$ {

    auth_basic "aminglinux auth";
    auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
      include fastcgi_params;
       fastcgi_pass unix:/tmp/www.sock;
      # fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

yum -y install httpd

  htpasswd -c /usr/local/nginx/conf/.htpasswd aming

/usr/local/nginx/sbin/nginx -t

/etc/init.d/nginx reload

 curl -x127.0.0.1 -uaming:aming  www.test.com/admin.php

1.8 nginx域名跳转

vim test.com   修改和保存

server
{
    listen 80;
    server_name www.test.com wwww.aaa.com  www.bbb.com;
    if ($host != 'www.test.com')
    {
   rewrite ^/(.*)$ http://www/test.com/$1 permanent;
   }
    index index.html index.htm index.php;
    root /data/www;

    location ~ \.php$ {
       auth_basic "amiglingx auth";
       auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
       include fastcgi_params;
       fastcgi_pass unix:/tmp/www.sock;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}

 /usr/local/nginx//sbin/nginx -t

 /usr/local/nginx//sbin/nginx -s reload

curl -x127.0.0.1:80 www.bbb.com/ddfadfa -I
出现Location: http://www/test.com/ddfadfa  正确

百度  site:www.aminglingx.com

nginx不记录指定文件类型日志

 vim ../nginx.conf

 vim test.conf

 添加

access_log /tmp/access.log aming;

/usr/local/nginx//sbin/nginx -t
!curl

ls /tmp/access.log
cat  /tmp/access.log

vim test.conf     添加

 location ~ .*\.(gif|jpg|jpeg|peg|png|bmp|swf)$
    {
       access_log off;
    }
/usr/local/nginx//sbin/nginx -t

/usr/local/nginx//sbin/nginx -s reload

 > /tmp/access.log
vim test.com

location ~( statoc|cache)

     {
       access_log off;
    }

  /usr/local/nginx//sbin/nginx -t

/usr/local/nginx//sbin/nginx -s reload

2.0 nginx日志切割

 vim /usr/local/sbin/nginx_logrotate.sh 

#!/bin/bash
d='date -d "-1 day" +%F'
[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log
mv /tmp/acces.log /tmp/nginx_log/$d.log
/etc/init.d/nginx reload > /dev/null
cd /tmp/nginx_log/
gzip -f $d.log

sh -x /usr/local/sbin/nginx_logrotate.sh

2.1 nginx配置静态文件过期时间

 vim test.conf

 location ~ .*\.(gif|jpg|jpeg|peg|png|bmp|swf)$
    {
        access_log off;
        expires 15d;
    }
     location ~ \.(js|css)
    {
        access_log off;
        expires 2h;
    }
     location ~ (statoc|cache)
     {
        access_log off;
     }

 /usr/local/nginx//sbin/nginx -t

 /usr/local/nginx//sbin/nginx -s reload

 nginx配置防盗链

    location ~ .*\.(gif|jpg|jpeg|peg|png|bmp|swf|flv|rar|zip|bz2)$
    {
        access_log off;
        expires 15d;
       valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;
       if ($invalid_referer)
      {
       return 403;
      }

  /usr/local/nginx//sbin/nginx -t
/usr/local/nginx//sbin/nginx -s reload

 nginx访问控制

vim test.conf

location ~ \.php$ {
      allow 127.0.0.1;
     deny all;

/usr/local/nginx//sbin/nginx -t

cal/nginx//sbin/nginx -t 

/usr/local/nginx//sbin/nginx -s reload

 curl -x127.0.0.1:80 www/test.com/admin.php -I

 2.4 nginx禁止指定user_agent

vim test.conf

if  ($http_user_agent ~ 'curl|baidu|111111')
     {
         return 403;
     }

cal/nginx//sbin/nginx -t 

/usr/local/nginx//sbin/nginx -s reload

nginx代理详解

vim proxy.cong
server   {

listen 80;
    server_name www.baidu.com;

    location / {
        proxy_pass http://220.181.57.217/;
        #proxy_set_jeader Host $host;      多个 打开
    }

  }

/usr/local/nginx//sbin/nginx -t

/usr/local/nginx//sbin/nginx -s reload

curl -x127.0.0.1:80  www.baidu.com

yum -y install bind*

vim proxy.conf

upstream aming {
   server  220.181.111.188:80;
   server  220.181.57.217;
 }
server {
    listen 80;
    server_name www.baidu.com;

    location / {
        proxy_pass http://aming/;
       proxy_set_jeader Host $host;
    }

  }

cal/nginx//sbin/nginx -t

 


/usr/local/nginx//sbin/nginx -s reload