LinuxÏ´ÓÁ㿪ʼ°²×°ÅäÖÃNginx·þÎñÆ÷+PHP¿ª·¢»·¾³

5年以前  |  阅读数:408 次  |  编程语言:PHP 

NginxÊǶiÂÞ˹È˱aдµÄÊ®ÖÇaÁ¿¼¶µÄHTTPþÎñÆ÷£¬ÒÔʼþÇý¶¯µÄ½Ê½±aд£¬ËuÒÔÓÐdz£ºÃµÄÐÔÄÜ£¬Í¬Ê±Ò²ÊÇÒ»¸odz£¸ßЧµÄ´Ïo´uÀi¡¢¸ºÔØƽºa¡£ÆaÓµÓÐÆ¥ÅaLighttpdµÄÐÔÄÜ£¬Í¬Ê±»¹Ã»ÓÐLighttpdµÄÄÚ´aeйÂ(C)ÎÊÌa£¬¶øÇÒLighttpdµÄmod_proxyÒ²ÓÐÒ»Ð(C)ÎÊÌa²¢ÇҺܾÃûÓиuС£

Òo´ËÎÒ´oËaÓÃÆaÌae´uApacheÓ¦ÓÃÓÚLinuxþÎñÆ÷ÉÏ¡£µ«ÊÇNginx²¢²»Ö§³Öcgi½Ê½ÔËÐУ¬Ô­ÒoÊÇ¿ÉÒÔ¼oÉÙÒo´Ë´øÀ´µÄÒ»Ð(C)³ÌÐoÉϵÄÂ(C)¶´¡£ÄÇôÎÒÃDZØÐeʹÓÃFastCGI*½Ê½À´Ö´ÐÐPHP³ÌÐo¡£

ÏÂÃaeÊÇÎҳɹ¦µØÅaÖÃNginx + PHP5 FastCGIµÄ¹ý³Ì

Ê×ÏÈ°²×°»o±aÒeNginx

°²×°Nginx

Ô´Âe°u¿ÉÒÔÔÚ¹Ù*½Ö÷Ò³ÉÏÏÂÔØ¡£Ubuntu 7.10¿ÉÒÔÖ±½Óͨ¹ýapt°²×°£¬Ò²¿ÉÒÔ´ÓÕaÀiÏÂÔØ×iеÄdeb°u£º


    sudo apt-get install nginx

Èç¹uÒª×Ô¼º±aÒeµÄ»°£¬ÐeҪȱ£×Ô¼ºÒѾ­ÓбaÒeÆ÷ºÍPCREµÄ¿a(ÓÃÓÚNginxµÄrewriteÄ£¿e£¬Èç¹u²»ÐeÒªÕa¸oÄ£¿e¿ÉÒÔÔÚconfigureʱʹÓÃ./configure ¨Cwithout-rewrite),±aÒe½*¨ÈçÏ£º


    wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gz
    tar zxvf nginx-0.5.34.tar.gz
    cd nginx-0.5.34
    ./configure #ĬÈÏÅaÖð²×°Â*¾¶Îª/usr/local/nginx ¿ÉÒÔ×*¼Ó--prefix=/usrÉeÖõ½/usr
    make && make install # installÒªÇoÓÐrootȨÏÞ

Ubuntu°²×°Ö®ºoµÄÎļþ½a¹¹´oÖÂΪ£º

  • ËuÓеÄÅaÖÃÎļþ¶¼ÔÚ/etc/nginxÏ£¬²¢ÇÒÿ¸oÐeÄaÖ÷»uÒѾ­°²ÅÅÔÚÁË/etc/nginx/sites-availableÏÂ
  • ³ÌÐoÎļþÔÚ/usr/sbin/nginx
  • ÈÕÖ¾*ÅÔÚÁË/var/log/nginxÖÐ
  • ²¢ÒѾ­ÔÚ/etc/init.d/Ï´´½¨ÁËÆo¶¯½Å±¾nginx
  • ĬÈϵÄÐeÄaÖ÷»uµÄĿ¼ÉeÖÃÔÚÁË/var/www/nginx-default

¶ø×Ô¼ºÀuÓÃĬÈÏÅaÖñaÒeµÄ£¬Ôo*ÅÔÚ/usr/local/nginxÏ£¬ÒÔÏÂÊÇĿ¼½a¹¹£º

  • /usr/local/nginx/conf ÅaÖÃĿ¼
  • /usr/local/nginx/html ĬÈϵÄÍøÕ¾¸uĿ¼
  • /usr/local/nginx/logs ÈÕÖ¾ºÍpidÎļþĿ¼
  • /usr/local/nginx/sbin Ö´ÐÐÎļþĿ¼

ÏÂÃae¿ÉÒÔÆo¶¯nginxÀ´¿´¿´Ð§¹u£¨Çeȱ£80¶Ë¿ÚûÓÐÆaËuþÎñÔÚʹÓã(C)£º

UbuntuÇeÔËÐУº


    sudo /etc/init.d/nginx start

ÆaËuÇeÔËÐУº


    /usr/local/nginx/sbin/nginx

È»ºo¾Í¿ÉÒÔͨ¹ýhttp://localhost/À´¿´¿´Ð§¹uÁË¡£

ÒªÅaÖÃnginxµÄ×Ô¶¯ÔËÐУ¬¿ÉÒÔ½«/usr/local/nginx/sbin/nginxÌi¼Óµ½/etc/rc.localÖУ¬Ubuntu¿ÉÒÔÖ´ÐÐ


    update-rc.d nginx defaults

°²×°PHP5

ÖÁÓÚÈçºÎÔÚLinuxÉÏ°²×°PHP£¬ÓкܶaÎÄÕ£¬ÉoÖÁºÜ¶aƽ̨É϶¼ÓÐÏֳɵÄÈi¼þ°u£¬ÎÞÐe×Ô¼º±aÒe¡£

1. ÏȽøÐÐphp¸¨Öu°uµÄ°²×°ÈçÏÂ
³£ÓõİuÓÐ

  • zlib-1.2.3.tar.bz2
  • jpegsrc.v6b.tar.gz libpng-1.2.22.tar.bz2 libmcrypt-2.5.8.tar.gz
  • mhash-0.9.9.9.tar.gz mcrypt-2.6.8.tar.gz

Àý£º


    tar -jxf zlib-1.2.3.tar.bz2

½aѹ


    tar zxf tar -jxf zlib-1.2.3.tar.bz2

½øÈeµ½


     cd zlib-1.2.3 

ºoÖ´ÐÐ


    ./configure 

È»ºoÖ´ÐÐ


    make make install 

ÆaÓaµÄ°²×°½¨Ïaͬ¡£
2. ÒÔÉϵĸ¨Öu°u°²×°ÍeÖ®ºo£¬½øÐÐphp°uµÄ°²×°
²½Öe


    tar -zxvf php-5.2.14.tar.gz && cd php-5.2.14

½¨Í¬ÉÏ Ö´ÐÐ


    ./configure --prefix=/usr/local/php5 

ºoÃae¼ÓÉÏÐeÒª¼ÓÔصÄÄ£¿e


    ./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql5 --with-freetype-dir --with-zlib --with-png-dir --with-iconv --with-libxml-dir --with-jpeg-dir --with-curl --with-gd --enable-ftp --enable-zip --enable-mbstring --with-mcrypt=/usr/local/libmcrypt 

£¨ÕaÐ(C)Ϊ¼ÓÔصÄÄ£¿e£¬²»ÊÇÈ«²¿£(C)°´Enter Ö´ÐС£
PhpµÄÅaÖÃÎļþΪ php.ini¡£

PHP5µÄCGI½Ê½µÄÒ»´oÓÅÊÆÊÇÄÚÖÃÁËFastCGIµÄÖ§³Ö£¬Ö»ÐeÖ¸Ã÷°o¶¨µÄµØֺͶ˿ڲÎÊý±a¿ÉÒÔÒÔFastCGIµÄ*½Ê½ÔËÐУ¬ÈçÏ£º


    php-cgi -b 127.0.0.1:9000

ÈçºÎÅaÖÃÆaÓenginxÒ»ÆðÔËÐÐÄØ£¿

ÅaÖÃNginxµÄPHP FastCGI

Çe½«ÒÔÏÂÄÚÈݱ£´aeΪfastcgi_paramsÎļþ£¬±£´aeÓÚ/usr/local/nginx/confÏ£¨Ubuntu¿É±£´aeÓÚ/etc/nginxÏ£(C)£¬ËuΪÎÒÃǵÄFastCGIÄ£¿eÉeÖÃÁË»u±¾µÄ»*¾³±aÁ¿£º


    #fastcgi_params
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE  nginx;
    fastcgi_param QUERY_STRING    $query_string;
    fastcgi_param REQUEST_METHOD   $request_method;
    fastcgi_param CONTENT_TYPE    $content_type;
    fastcgi_param CONTENT_LENGTH   $content_length;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME    $fastcgi_script_name;
    fastcgi_param REQUEST_URI    $request_uri;
    fastcgi_param DOCUMENT_URI    $document_uri;
    fastcgi_param DOCUMENT_ROOT   $document_root;
    fastcgi_param SERVER_PROTOCOL  $server_protocol;
    fastcgi_param REMOTE_ADDR    $remote_addr;
    fastcgi_param REMOTE_PORT    $remote_port;
    fastcgi_param SERVER_ADDR    $server_addr;
    fastcgi_param SERVER_PORT    $server_port;
    fastcgi_param SERVER_NAME    $server_name;
    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param REDIRECT_STATUS  200;

ÇeÌرð×¢Òa¡±fastcgi_script_name¡±Ò»ÐУ¬PHP-CGIÌرðÐeÒª´ËÐÐÐÅÏ¢À´È*¶¨PHPÎļþµÄλÖá£

ÁiÍaÐeÒªÔÚPHP-CGIµÄÅaÖÃÎļþ£¨Ubuntu ÉÏ´ËÅaÖÃÎļþλÓÚ/etc/php5/cgi/php.ini£(C)ÖУ¬´o¿ªcgi.fix_pathinfoÑ¡Ïi£º


    cgi.fix_pathinfo=1;

ÕaÑuphp-cgi*½ÄÜÕý³£Ê¹ÓÃSCRIPT_FILENAMEÕa¸o±aÁ¿¡£

½ÓÏÂÀ´ÔÚnginxµÄÅaÖÃÖÐÕe¶ÔphpÎļþÅaÖÃÆaÀuÓÃFastCGI½ø³ÌÀ´Ö´ÐУº


    server {
      index index.php;
      root /usr/local/nginx/html;

      location ~ .*.php$ {
        include /usr/local/nginx/conf/fastcgi_params; #Çe¸u¾Ý×Ô¼º±£´aeµÄÂ*¾¶½øÐÐÉeÖÃ
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000; #Çe¸u¾Ý×Ô¼ºµÄFastCGI°o¶¨µÄµØÖ*ºÍ¶Ë¿Ú½øÐÐÅaÖÃ
      }
    }

֪ͨNginxÖØÐÂÔØÈeÅaÖÃ:


    kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

UbuntuÓû§¿ÉÒÔʹÓÃinit½Å±¾£ºsudo /etc/init.d/nginx reload

È»ºoÆo¶¯php-cgi -b 127.0.0.1:9000

¼ÙÉeÄaÔÚÎĵµ¸uĿ¼*ÅÁËindex.php£¬²¢°uº¬¡±phpinfo();¡±µÄÄÚÈÝ£¬ÏÖÔÚÔÙ¿´http://localhost/index.php±aÓ¦¸ÃÄÜ¿´µ½phpµÄµ÷ÊÔÐÅÏ¢ÁË¡£

ÅaÖÃphp½ø³Ì

Ö±½ÓʹÓÃphp-cgiµÄFastCGIÔËÐнʽÓÐÁ½¸oÎÊÌa£¨Ã²ËÆÓ¦¸ÃÓнa¾o½°¸£¬Èç¹uÖªµÀµÄ»°¿ÉÒԽ̽ÌÎÒ£(C)£º

1.Èç¹u½ø³Ì±ÀÀ££¬ÄÑÒÔÅaÖÃÖØÐÂÆo¶¯
2.µ¥½ø³ÌµÄЧÂʵÍ
Òo´Ë£¬ÎÒÃÇ¿ÉÒÔÀuÓÃLighttpdµÄspawn-fcgiÀ´¿ØÖƽø³ÌµÄÔËÐС£»ñµÃspawn-fcgiµÄ½¨ÈçÏ£º


    wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #»ñÈ¡LighttpdµÄÔ´Âe°u
    tar -xvjf lighttpd-1.4.18.tar.bz2
    cd lighttpd-1.4.18
    ./configure #±aÒe
    make
    cp src/spawn-fcgi /usr/local/bin/spawn-fcgi #È¡³ospawn-fcgiµÄ³ÌÐo

ÏÂÃaeÎÒÃǾͿÉÒÔʹÓà spawn-fcgi À´¿ØÖÆphp-cgiµÄFastCGI½ø³ÌÁË

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
²ÎÊýº¬ÒaÈçÏÂ

  • -f Ö¸¶¨µ÷ÓÃFastCGIµÄ½ø³ÌµÄÖ´ÐгÌÐoλÖ㬸u¾ÝϵͳÉÏËu×°µÄPHPµÄÇe¿o¾ßÌaÉeÖÃ
  • -a °o¶¨µ½µØÖ*addr
  • -p °o¶¨µ½¶Ë¿Úport
  • -s °o¶¨µ½unix socketµÄÂ*¾¶path
  • -C Ö¸¶¨²uÉuµÄFastCGIµÄ½ø³ÌÊý£¬Ä¬ÈÏΪ5£¨½oÓÃÓÚPHP£(C)
  • -P Ö¸¶¨²uÉuµÄ½ø³ÌµÄPIDÎļþÂ*¾¶
  • -uºÍ-g FastCGIʹÓÃʲôÉi*Ý£¨-u Óû§ -g Óû§×e£(C)ÔËÐУ¬UbuntuÏ¿ÉÒÔʹÓÃwww-data£¬ÆaËuµÄ¸u¾ÝÇe¿oÅaÖã¬Èçnobody¡¢apacheµÈ

È»ºoÎÒÃÇ¿ÉÒÔ½«ÕaÐдuÂe¼ÓÈeµ½/etc/rc.localÎļþµ×²¿£¬ÕaÑuϵͳÆo¶¯µÄʱºoÒ²¿ÉÒÔͬʱÆo¶¯PHPµÄFastCGI½ø³Ì¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例