from:
http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-lemp-on-ubuntu-12.04-lts
1PreliminaryNote
InthistutorialIusethehostnameserver1.example.comwiththeIPaddress192.168.0.100.Thesesettingsmightdifferforyou,soyouhavetoreplacethemwhereappropriate.I'mrunningallthestepsinthistutorialwithrootprivileges,somakesureyou'reloggedinasroot:sudosu2InstallingMySQL5
InordertoinstallMySQL,werunapt-getinstallmysql-servermysql-clientYouwillbeaskedtoprovideapasswordfortheMySQLrootuser-thispasswordisvalidfortheuserroot@localhostaswellasroot@server1.example.com,sowedon'thavetospecifyaMySQLrootpasswordmanuallylateron:NewpasswordfortheMySQL"root"user:<--yourrootsqlpassword
RepeatpasswordfortheMySQL"root"user:<--yourrootsqlpassword3InstallingNginx
NginxisavailableasapackageforUbuntu12.04whichwecaninstallasfollows:apt-getinstallnginxStartnginxafterwards:/etc/init.d/nginxstartTypeinyourwebserver'sIPaddressorhostnameintoabrowser(e.g.http://192.168.0.100),andyoushouldseethefollowingpage:
ThedefaultnginxdocumentrootonUbuntu12.04is/usr/share/nginx/www.4InstallingPHP5
WecanmakePHP5workinnginxthroughPHP-FPM(PHP-FPM(FastCGIProcessManager)isanalternativePHPFastCGIimplementationwithsomeadditionalfeaturesusefulforsitesofanysize,especiallybusiersites)whichweinstallasfollows:apt-getinstallphp5-fpmPHP-FPMisadaemonprocess(withtheinitscript/etc/init.d/php5-fpm)thatrunsaFastCGIserveronport9000.5Configuringnginx
Thenginxconfigurationisin/etc/nginx/nginx.confwhichweopennow:vi/etc/nginx/nginx.confTheconfigurationiseasytounderstand(youcanlearnmoreaboutithere:http://wiki.nginx.org/NginxFullExampleandhere:http://wiki.nginx.org/NginxFullExample2)First(thisisoptional)adjustthenumberofworkerprocessesandsetthekeepalive_timeouttoareasonablevalue:[...]
worker_processes4;
[...]
keepalive_timeout2;
[...]
|
Thevirtualhostsaredefinedinserver{}containers.Thedefaultvhostisdefinedinthefile/etc/nginx/sites-available/default-let'smodifyitasfollows:nano/etc/nginx/sites-available/default
[...]
server{
listen80;##listenforipv4;thislineisdefaultandimplied
listen[::]:80defaultipv6only=on;##listenforipv6
root/usr/share/nginx/www;
indexindex.phpindex.htmlindex.htm;
#Makesiteaccessiblefromhttp://localhost/server_name_;
location/{
#Firstattempttoserverequestasfile,then
#asdirectory,thenfallbacktoindex.html
try_files$uri$uri//index.html;
#Uncommenttoenablenaxsionthislocation
#include/etc/nginx/naxsi.rules
}
location/doc/{
alias/usr/share/doc/;
autoindexon;
allow127.0.0.1;
denyall;
}
#Onlyfornginx-naxsi:processdeniedrequests
#location/RequestDenied{
#Forexample,returnanerrorcode
#return418;
#}
#error_page404/404.html;
#redirectservererrorpagestothestaticpage/50x.html
#
error_page500502503504/50x.html;
location=/50x.html{
root/usr/share/nginx/www;
}
#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000
#
location~\.php${
try_files$uri=404;
fastcgi_split_path_info^(.+\.php)(/.+)$;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
includefastcgi_params;
}
#denyaccessto.htaccessfiles,ifApache'sdocumentroot
#concurswithnginx'sone
#
location~/\.ht{
denyall;
}
}
[...]
|
Uncommentbothlistenlinestomakenginxlistenonport80IPv4andIPv6.server_name_;makesthisadefaultcatchallvhost(ofcourse,youcanaswellspecifyahostnameherelikewww.example.com).I'veaddedindex.phptotheindexline.root/usr/share/nginx/www;meansthatthedocumentrootisthedirectory/usr/share/nginx/www.TheimportantpartforPHPisthelocation~\.php${}stanza.Uncommentittoenableit.PleasenotethatI'veaddedthelinetry_files$uri=404;topreventzero-dayexploits(seehttp://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHPandhttp://forum.nginx.org/read.php?2,88845,page=3).Alternatively,ifyoudon'twanttousethetry_files$uri=404;line,youcansetcgi.fix_pathinfo=0;in/etc/php5/fpm/php.ini(don'tforgettoreloadPHP-FPMafterwards).Nowsavethefileandreloadnginx:
/etc/init.d/nginxreloadNowcreatethefollowingPHPfileinthedocumentroot/usr/share/nginx/www:nano/usr/share/nginx/www/info.phpNowwecallthatfileinabrowser(e.g.http://192.168.0.100/info.php):
Asyousee,PHP5isworking,andit'sworkingthroughFPM/FastCGI,asshownintheServerAPIline.Ifyouscrollfurtherdown,youwillseeallmodulesthatarealreadyenabledinPHP5.MySQLisnotlistedtherewhichmeanswedon'thaveMySQLsupportinPHP5yet.6GettingMySQLSupportInPHP5
TogetMySQLsupportinPHP,wecaninstallthephp5-mysqlpackage.It'sagoodideatoinstallsomeotherPHP5modulesaswellasyoumightneedthemforyourapplications.YoucansearchforavailablePHP5moduleslikethis:apt-cachesearchphp5Picktheonesyouneedandinstallthemlikethis:apt-getinstallphp5-mysqlphp5-curlphp5-gdphp5-intlphp-pearphp5-imagickphp5-imapphp5-mcryptphp5-memcachephp5-mingphp5-psphp5-pspellphp5-recodephp5-snmpphp5-sqlitephp5-tidyphp5-xmlrpcphp5-xslXcacheisafreeandopenPHPopcodecacherforcachingandoptimizingPHPintermediatecode.It'ssimilartootherPHPopcodecachers,suchaseAcceleratorandAPC.ItisstronglyrecommendedtohaveoneoftheseinstalledtospeedupyourPHPpage.Xcachecanbeinstalledasfollows:apt-getinstallphp5-xcacheNowreloadPHP-FPM:/etc/init.d/php5-fpmreloadNowreloadhttp://192.168.0.100/info.phpinyourbrowserandscrolldowntothemodulessectionagain.Youshouldnowfindlotsofnewmodulesthere,includingtheMySQLmodule:
7MakingPHP-FPMUseAUnixSocket
BydefaultPHP-FPMislisteningonport9000on127.0.0.1.ItisalsopossibletomakePHP-FPMuseaUnixsocketwhichavoidstheTCPoverhead.Todothis,open/etc/php5/fpm/pool.d/www.conf...nano/etc/php5/fpm/pool.d/www.conf...andmakethelistenlinelookasfollows:[...] ;listen=127.0.0.1:9000 listen=/tmp/php5-fpm.sock [...]
|
ThenreloadPHP-FPM:/etc/init.d/php5-fpmreloadNextgothroughyournginxconfigurationandallyourvhostsandchangethelinefastcgi_pass127.0.0.1:9000;tofastcgi_passunix:/tmp/php5-fpm.sock;,e.g.likethis:nano/etc/nginx/sites-available/default[...] location~\.php${ try_files$uri=404; fastcgi_split_path_info^(.+\.php)(/.+)$; fastcgi_passunix:/tmp/php5-fpm.sock; fastcgi_indexindex.php; includefastcgi_params; } [...]
|
Finallyreloadnginx:/etc/init.d/nginxreload8CGI/PerlScripts
IfyouwanttoserveCGI/Perlscriptswithnginx,pleasereadthistutorial:ServingCGIScriptsWithNginxOnDebianSqueeze/Ubuntu11.04Therecommendedwayistousefcgiwrap(chapter4).9Links
nginx:http://nginx.net/nginxWiki:http://wiki.codemongers.com/MainPHP:http://www.php.net/PHP-FPM:http://php-fpm.org/MySQL:http://www.mysql.com/Ubuntu:http://www.ubuntu.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理