nginx+php for proxy and load balancing (1 nginx,2 PHP)

Source: Internet
Author: User
Tags geoip

Experiment to prepare 3 virtual machines

192.168.239.136/192.168.239.140/192.168.239.144


1 Installing PHP 192.168.239.140/192.168.239.144

Do these preparations before installing (install the base environment)

yum-y Groupinstall "Development Tools" "Development Libraries"

Yum Install-y epel-release

yum install-y libxml2-devel.x86_64 openssl-devel.x86_64 bzip2-devel.x86_64 libjpeg-turbo-devel.x86_64 libjpeg-turbo-static.x86_64 libpng-devel.x86_64 libpng-static.x86_64 freetype-devel.x86_64 libstdc++ libstdc++- Devel compat-libstdc++-33 libstdc++-static gcc libmcrypt-devel lrzsz gcc gcc-c++ make mans vim tree unzip wget Curl Lua-de Vel lua-static GeoIP geoip-devel patch libxml2-devel libxslt libxslt-devel gd gd-devel autoconf M4 pcre-devel.x86_64 pcre -static.x86_64 lrzsz NTP libcurl-devel.x86_64 libtool-ltdl-devel.x86_64


Download PHP source package to/USR/LOCAL/SRC CD/USR/LOCAL/SRC

Unzip the CD directory Useradd-s/sbin/nologin php-fpm


./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=mysqlnd--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--enabl E-ftp--enable-mbstring--enable-exif--enable-zend-multibyte--disable-ipv6--with-pear--with-curl

Make && make install


CP php.ini-development/usr/local/php-fpm/etc/php.ini copy configuration file

CP SAPI/FPM/INIT.D.PHP-FPM/ETC/INIT.D/PHP-FPM Copy startup script

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

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

Vim!$ Add the following configuration


[Global]

PID =/usr/local/php-fpm/var/run/php-fpm.pid
Error_log =/usr/local/php-fpm/var/log/php-fpm.log


[www]
Listen = 192.168.239.140:9000 (IP is php native IP or 0.0.0.0, cannot be 127.0.0.1)
user = PHP-FPM
Group = PHP-FPM
Listen.owner = Nobody
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

Slowlog =/usr/local/php-fpm/var/slow.log
Request_slowlog_timeout = 1

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


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

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

/ETC/INIT.D/PHP-FPM start NETSTAT-LNPT See if there is a listening 192.168.239.140:9000

Mkdir-p/tmp/tmp

vim/tmp/tmp/index.php Create the dynamic test file, write the following

Dongtaiqingqiu 1


2. The second installation of PHP (the installation and compilation are unchanged, the configuration file IP changed to 192.168.239.144:9000 last index.php Dongtaiqingqiu 1 to DONGTAIQINGQIU2)


3. Installing Nginx

Download source package to/usr/local/src CD/USR/LOCAL/SRC

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

Unzip the CD to extract the directory

Yum Install-y pcre-devel

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

Make

Make install


4. Writing Nginx Startup scripts

Vim/etc/init.d/nginx Add the following configuration

#!/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 "$" in

Start

Start

;;

Stop

Stop

;;

Reload

Reload

;;

Restart

Restart

;;

Configtest)

Configtest

;;

*)

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

Retval=1

Esac

Exit $RETVAL

After saving, execute

chmod A+x/etc/init.d/nginx or chmod 755! $ Modify Permissions

Service Nginx Start-up Nginx


5. Configuring the Reverse Proxy

>/usr/local/nginx/conf/nginx.conf

vim/usr/local/nginx/conf/nginx.conf Add the following configuration

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;

}

Configure agents

Mkdir/usr/local/nginx/conf/vhosts

vi/usr/local/nginx/conf/vhosts/php.conf Add the following configuration

Upstream Mysvr {

#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.

Server 192.168.239.144:9000 weight=2;

Server 192.168.239.140:9000 weight=1;

}

Server

{

Listen 80;

server_name www.q.com;

Index index.html index.htm index.php;

root/usr/local/nginx/html;

Location ~ \.php$ {

Include Fastcgi_params;

Fastcgi_pass Mysvr;

Fastcgi_index index.php;

Fastcgi_param Script_filename/tmp/tmp$fastcgi_script_name;

}

}


/usr/local/nginx/sbin/nginx-t Detection configuration file

/usr/local/nginx/sbin/nginx-s Reload (direct reload configuration, no restart required)

vim/usr/local/nginx/html/1.html Create a static test file, write

Jingtaifangwen


Set Local resolution

Modify Windons hosts (address c:\windows\system32\drivers\etc\hosts) Notepad to open, add a line to the bottom of the configuration

192.168.239.136 www.q.com


Testing: It is best to use Google Chrome (shown below for agent success)

Access to the www.q.com display is the Nginx default page

Accessing the Jingtaifangwen displayed by www.q.com/.html

Access www.q.com/index.php multiple refreshes several times will show Dongtaifangwen1 once dongtaifangwen2 two times


Read this article there are any errors and do not understand, please leave a message, the landlord will promptly deal with, or contact qq:363138642, add reasons for the remarks, thank you!!!!!

This article is from the Linux system Learning extension blog, so be sure to keep this source http://zhangxiaoxiong.blog.51cto.com/11657691/1786283

nginx+php for proxy and load balancing (1 nginx,2 PHP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.