Nginx 營運架構之(Nginx+PHP)

來源:互聯網
上載者:User

標籤:web架構 nginx php tomcat apache keepalived

  1、首先我們來看下web伺服器的排名情況

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/85/4F/wKiom1ef90Sy23rOAABN1KkcxZY312.png" title="1.png" alt="wKiom1ef90Sy23rOAABN1KkcxZY312.png" />

可以看到Nginx佔有30.6%,Apache 52.0%,Nginx特點如下

1、配置簡單,靈活,輕量。

2、高並發(靜態小檔案),靜態幾萬的並發

3、佔用資源少

4、功能種類比較多(web,cache,proxy),每一個功能都不是特彆強

5、支援epoll模型,使得nginx可以支援高並發 Apache 選擇 select模型

6、nginx可以搭配動態服務(FASTCGI介面)

6、利用nginx可以對ip限速,可以限制串連數


並發能力

nginx 1-3萬  php 300-800  db 300-800.日pv2000萬以下,並發1萬以下都可以用nginx做反向 Proxy


2、nginx的安裝(centos6.7)

[[email protected] tools]#  yum install pcre pcre-devel openssl-devel -y[[email protected] tools]# wget  http://nginx.org/download/nginx-1.6.3.tar.gz [[email protected] tools]# tar xf nginx-1.6.3.tar.gz [[email protected] tools]# cd nginx-1.6.3[[email protected] nginx-1.6.3]# useradd -s /sbin/nologin -M nginx[[email protected] nginx-1.6.3]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx  --with-http_stub_status_module --with-http_ssl_module[[email protected] nginx-1.6.3]# make && make install    [[email protected] nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx[[email protected] nginx-1.6.3]#  /application/nginx/sbin/nginx -t   檢查文法[[email protected] nginx-1.6.3]#  /application/nginx/sbin/nginx   啟動nginx簡單下安裝過程

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M00/85/50/wKiom1ef_H-iOn0-AAJkjYBqO2s309.png" title="2.png" alt="wKiom1ef_H-iOn0-AAJkjYBqO2s309.png" />


650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/85/50/wKioL1ef_JXwY3-tAARYg3AW78U617.png" title="3.png" alt="wKioL1ef_JXwY3-tAARYg3AW78U617.png" />

訪問下nginx服務

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/85/50/wKioL1ef_O6xej5yAAEvEy6KTOQ991.png" title="4.png" alt="wKioL1ef_O6xej5yAAEvEy6KTOQ991.png" />

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/85/50/wKioL1ef_Try6ufPAADoRh7xOCU455.png" title="1.png" alt="wKioL1ef_Try6ufPAADoRh7xOCU455.png" />


一份簡單的nginx配置

[[email protected] conf]# cat nginx.confworker_processes  2;events {    worker_connections  1024;}http {log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘                      ‘$status $body_bytes_sent "$http_referer" ‘                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  www.martin.com martin.com;        location / {            root   html/www;            index  index.php index.html index.htm;        }                         location ~ .*\.(php|php5)?$ {            root  html/www;            fastcgi_pass 127.0.0.1:9000;            fastcgi_index index.php;            include fastcgi.conf;               }         access_log  logs/access_www.log  main;           }                server {        listen       80;        server_name  bbs.martin.com;        location / {            root   html/bbs;            index  index.php index.html index.htm;        }                           location ~ .*\.(php|php5)?$ {            fastcgi_pass 127.0.0.1:9000;            fastcgi_index index.php;            include fastcgi.conf;               }         access_log  logs/access_bbs.log  main;           }###status   server{      listen 80;      server_name status.martin.org;      location / {      stub_status on;      access_log off;        }   }}


3、資料庫MySQL的安裝

[[email protected] tools]# wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.49-linux2.6-x86_64.tar.gz    這是個二進位包,無需編譯 [[email protected] tools]# tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz [[email protected] tools]# useradd -s /sbin/nologin -M mysql[[email protected] tools]# mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49 [[email protected] tools]# ln -s /application/mysql-5.5.49/  /application/mysql[[email protected] tools]#  /application/mysql/scripts/mysql_install_db --basedir=/application/mysql  --datadir=/application/mysql/data/ --user=mysql[[email protected] tools]# sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/support-files/mysql.server [[email protected] tools]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld [[email protected] tools]# chmod +x /etc/init.d/mysqld

過程中的相關

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/85/53/wKiom1egJeGBDbMMAAN6Yr5xO6I411.png" title="1.png" alt="wKiom1egJeGBDbMMAAN6Yr5xO6I411.png" />

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/85/53/wKiom1egJhCDe6flAAEf7NBmUEU634.png" title="2.png" alt="wKiom1egJhCDe6flAAEf7NBmUEU634.png" />


4、php的安裝

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/85/53/wKioL1egJrCg_0kdAARLxU36DYc113.png" title="3.png" alt="wKioL1egJrCg_0kdAARLxU36DYc113.png" />


php的安裝(5.3.27),需要先安裝nginx和mysql

[[email protected] libiconv-1.14]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo[[email protected] tools]# yum install zlib-devel libxml2-devel libjpeg-devel libiconv-devel -y[[email protected] tools]# yum install freetype-devel libpng-devel gd-devel curl-devel libxslt-devel -y[[email protected] libiconv-1.14]# yum -y install libmcrypt-devel mhash mhash-devel mcrypt libxslt-devel [[email protected] tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz [[email protected] tools]# tar xf libiconv-1.14.tar.gz [[email protected] tools]# cd libiconv-1.14[[email protected] libiconv-1.14]# ./configure --prefix=/usr/local/libiconv[[email protected] libiconv-1.14]# make && make install[[email protected] tools]# tar xf php-5.3.27.tar.gz [[email protected] tools]# cd php-5.3.27[[email protected] php-5.3.27]#  ./configure > --prefix=/application/php5.3.27 > --with-mysql=/application/mysql > --with-iconv-dir=/usr/local/libiconv > --with-freetype-dir > --with-jpeg-dir > --with-png-dir > --with-zlib > --with-libxml-dir=/usr > --enable-xml > --disable-rpath > --enable-safe-mode > --enable-bcmath > --enable-shmop > --enable-sysvsem > --enable-inline-optimization > --with-curl > --with-curlwrappers > --enable-mbregex > --enable-fpm > --enable-mbstring > --with-mcrypt > --with-gd > --enable-gd-native-ttf > --with-openssl > --with-mhash > --enable-pcntl > --enable-sockets > --with-xmlrpc > --enable-zip > --enable-soap > --enable-short-tags > --enable-zend-multibyte > --enable-static > --with-xsl > --with-fpm-user=nginx > --with-fpm-group=nginx > --enable-ftp[[email protected] php-5.3.27]# make

報錯如下

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/85/53/wKioL1egL-2gjPhHAAQ8_8-z1dM229.png" title="1.png" alt="wKioL1egL-2gjPhHAAQ8_8-z1dM229.png" />

解決方案:

[[email protected] php-5.3.27]#  ln -s /application/mysql/lib/libmysqlclient.so.18  /usr/lib64/[[email protected] php-5.3.27]# touch ext/phar/phar.phar[[email protected] php-5.3.27]# make && make install


650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/85/54/wKiom1egMmbi5_bYAAN0FSxmanY868.png" title="1.png" alt="wKiom1egMmbi5_bYAAN0FSxmanY868.png" />

[[email protected] php-5.3.27]# ln -s /application/php5.3.27/ /application/php[[email protected] php-5.3.27]# cp php.ini-production /application/php/lib/php.ini[[email protected] php-5.3.27]#  cd /application/php/etc/[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf[[email protected] etc]# cat php-fpm.conf[global][www]user = nginxgroup = nginxlisten = 127.0.0.1:9000pm = dynamicpm.max_children = 5pm.start_servers = 2pm.min_spare_servers = 1pm.max_spare_servers = 3[[email protected] etc]#  /application/php/sbin/php-fpm

[[email protected] etc]#  netstat -lantup|grep php-fpm

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/85/53/wKioL1egMjSQ3eIyAAB2Qv_kCw8728.png" title="3.png" alt="wKioL1egMjSQ3eIyAAB2Qv_kCw8728.png" />

 

/application/nginx/html/www[[email protected] www]# cat index.php <?phpphpinfo()?>

開啟瀏覽器訪問測試

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/85/54/wKiom1egNwqg23qxAAMmszGDSyc888.png" title="6.png" alt="wKiom1egNwqg23qxAAMmszGDSyc888.png" />


至此整個架構完成,下次我們再介紹nginx+tomcat   nginx+keepalived

本文出自 “厚德載物” 部落格,謝絕轉載!

Nginx 營運架構之(Nginx+PHP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.