You are on page 1of 16

5/5/2016

CitNginx+PHPFPM+MySQL

CitNginx+PHPFPM+MySQL
April6,2014 kenvk 18Comments(http://azsysadmin.com/caidatnginxphpfpmmysql/#disqus_thread)
MySQL(http://azsysadmin.com/category/mysql2/),Nginx(http://azsysadmin.com/category/nginx2/)
Tags:AmazonEC2(http://azsysadmin.com/tag/amazonec2/),mysql(http://azsysadmin.com/tag/mysql/),nginx(http://azsysadmin.com/tag/nginx/),php
(http://azsysadmin.com/tag/php/),phpfpm(http://azsysadmin.com/tag/phpfpm/)

Choccbn
Saukhitigiithiulmthnoto1InstanceVPSfreemtnmviAmazonEC2.
Hmnaytivitbilmthnosetupmtwebserver(Nginx)chyviPHP(sdngPHPFPM)vcsdliulMysql.

http://azsysadmin.com/caidatnginxphpfpmmysql/

1/16

5/5/2016

CitNginx+PHPFPM+MySQL

Mctiu:GipccbnhiusvwebserversdngNginx,PHPFPM.CcbncthsetupnginnhtwebserverchybngNginx,PHPFPMv
Mysql
TisaolNginxkhngphilApachehaylmtwebserverkhc?
Nginxlopensource,free,highperformanceHTTPserver,chyutptrungvovnxlrequestcngmtthiim,tiutinguynchoh
thngnhRAM,CPU.
Ccbncththamkhothmthngtinlinksau:Thytiliunycngkh,giithchcngkhchitit.Nutivitlithchclcngdchtti
liubndi.Hehe.Thiccbntcvy.
http://www.aosabook.org/en/nginx.html(http://www.aosabook.org/en/nginx.html)
PHPFPMlg?Tisaolichnn?
hiucPHPFPMlg?Thtrchtccbnphihiuccchxlcawebserver.
Vylmthnowebserverchycwebvitbngphp,python,hayruby
ChngqualwebserversginhngchngtrnhcthhiucPHP,Python,ruby,xlnhngrequestvtrvthngtincho
webserverttrvthngtinchobrowser.Nhhnhbndi:

(http://azsysadmin.com/wpcontent/uploads/2014/04/cgi0101.gif)

http://azsysadmin.com/caidatnginxphpfpmmysql/

2/16

5/5/2016

CitNginx+PHPFPM+MySQL

VyCGIlphngphpgipwebserverginhngchngtrnhcthhiuc(PHP,Ruby,Python)xlnhngrequestvtrvdliucho
browser.
MilngiCGIProgramsxlthtomtprocessxl.Vymirequestsinhra1processxl.Vicnylmtntinguyncahthng
nhCPUvRAM,nutrongtrnghpheavyloadgyracrashhthng.
NnFastCGIrai,vicchpersistentprocesses(cthhiulprocessnlunlunchytrngthilunlunchcmtrequesttixl)
cthxlnhiurequestcngmtlc.Chnhviuny,trnhkhivicmirequesttoramtprocessxl,nnsdngtinguynhiuquhn.
NhvyccbnhiursvkhinhimGCI,FastCGI.
VyPHPFPMlg?
PHPFPM(FastCGIProcessManager)lFastCGIvithmmtvitnhnnghtrkhisiteheavyload.
Trongdanhschtngnngthtnhnngnycniutin:
advancedprocessmanagementwithgracefulstop/start
nginnubndngfastcgithunt,thccbnthayiconfigthccbnphistop/startlifastcgi,nhvythsitesdownmtvipht.
Cnviphpfpmthccbncththayiconfigvreloadli,khnglmsitebdown.
Tnhnngkhcthmvoccbncththamkhoy:
http://www.php.net/manual/en/install.fpm.php(http://www.php.net/manual/en/install.fpm.php)

Vylccbnhiucssv
cckhinim.
Byhvomcchnhl
CitNginx+PHPFPM+MySQL
NubnsdngEC2thtrckhicitbnchnhthigian,vmtsgicnthit:
1
2
3
4

sudo
sudo
sudo
sudo

ln -sf /usr/share/zoneinfo/Asia/Saigon /etc/localtime


yum install pcre-devel zlib-devel openssl-devel
yum install gcc
yum install make

http://azsysadmin.com/caidatnginxphpfpmmysql/

3/16

5/5/2016

CitNginx+PHPFPM+MySQL

InstallNginxvPHPFPM
1
2
3
4
5

wget http://nginx.org/download/nginx-1.4.7.tar.gz
tar fx nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module --with-http_stub_status_module
make && make install

1 sudo yum install -y php-fpm

InstallPHPextensions
1 sudo yum install -y php-devel php-mysql php-pdo \
2 php-pear php-mbstring php-cli php-odbc \
3 php-imap php-gd php-xml php-soap

InstallPHPAPC
1 sudo yum install -y php-pecl-apc
1 sudo yum install -y pcre-devel

InstallMySQL
1 sudo yum -y install mysql-server mysql

NginxConfiguration
TaouserNginx:
1 sudo adduser --system --no-create-home --user-group -s /sbin/nologin nginx

Bntovhostdirectory.VicnygipbndqunlhnnusaunytrnVPSccbncthqunlnhiuwebsitekhcnhau.
ThmcsitesavailablelluttcccconfigcatngVhostkhcnhau:
Vdbnc5websitecnghosttrnmtserver,thbnc5filettrongthmcny.
Nusitenobnmunbtlnthchvictosoftlinkvothmcsitesenabledmthi.

http://azsysadmin.com/caidatnginxphpfpmmysql/

4/16

5/5/2016

CitNginx+PHPFPM+MySQL

1 mkdir /usr/local/nginx/sites-available
2 mkdir /usr/local/nginx/sites-enabled

Thmdngincludethmcsitesenabledvoconfigchnhcanginx.
1 sudo vi /usr/local/nginx/conf/nginx.conf
1 include /usr/local/nginx/sites-enabled/*;

Vd,titomtVhostlmysiteconfigchotrangwebmysite.comchnghn.Tovirtualhostmysitefiletrongthmc/usr/local/nginx/sitesavailable
Trcmttitonhngthmcsau:
public_html:thmcwebcabn
logs:thmclog,thcaccesslog,errorlog.
1 mkdir -p /srv/www/mysite/public_html
2 mkdir -p /srv/www/mysite/logs
3 chown -R nginx:nginx /srv/www/mysite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

sudo vi/usr/local/nginx/sites-available/mysite

server {
server_name mysite.com;
access_log /srv/www/mysite/logs/access.log;
error_log /srv/www/mysite/logs/error.log;
root /srv/www/mysite/public_html;

location / {
index index.html index.htm index.php;
}

location ~ \.php$ {
try_files $uri =404;
# Include the standard fastcgi_params file included with nginx
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

http://azsysadmin.com/caidatnginxphpfpmmysql/

5/16

5/5/2016

CitNginx+PHPFPM+MySQL

21 }
22 }

TosoftlinknhngVhostnoenable.Nhtrongphnnytienablevhostmysite
1 ln -s /usr/local/nginx/sites-available/mysite /usr/local/nginx/sites-enabled/mysite

ToscriptrestartNginx:
1 sudo vi /etc/init.d/nginx

CopyvPaste:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.


. /etc/rc.d/init.d/functions

# Source networking configuration.


. /etc/sysconfig/network

# Check that networking is up.


[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {

http://azsysadmin.com/caidatnginxphpfpmmysql/

6/16

5/5/2016

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

CitNginx+PHPFPM+MySQL

[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

http://azsysadmin.com/caidatnginxphpfpmmysql/

7/16

5/5/2016

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

CitNginx+PHPFPM+MySQL

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

1 sudo chmod +x /etc/init.d/nginx


2 sudo /etc/init.d/nginx start

http://azsysadmin.com/caidatnginxphpfpmmysql/

8/16

5/5/2016

CitNginx+PHPFPM+MySQL

PHPFPMConfiguration
1 sudo vi /etc/php-fpm.d/www.conf
1
2
3
4
5
6
7
8
9

[...]
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0664

user = nginx
group = nginx
[...]

1 sudo vi /etc/php.ini
1 <span style="line-height: 1.5em;">cgi.fix_pathinfo=0</span>

AutoStartNginx,PHPFPM,andMySQLinEC2AmazonLinux
1 chkconfig nginx on
2 chkconfig mysqld on
3 chkconfig php-fpm on

KimtracuhnhNginxandFastCGI:
Tofileindex.php
(/srv/www/mysite/public_html/index.php):
<?phpphpinfo()?>
VobrowservgachIPvxemktqu.
Chcbnthnhcng
(http://www.hupso.com/share/)
Tweet

Like

Share

http://azsysadmin.com/caidatnginxphpfpmmysql/

9/16

5/5/2016

CitNginx+PHPFPM+MySQL

CcBiVitLinQuan:

Setupnginx+
Phnquyncbn
CuhnhMysql
Fixmysqlslave
gunicorn+
caMySQL
Replication
error1594
supervisord.
(http://azsysadmin.com/set
(http://azsysadmin.com/phan
(http://azsysadmin.com/cau
(http://azsysadmin.com/fix

nginxgunicorn

quyencobancua

hinhmysql

mysqlslaveerror

supervisord/)
mysql/)
replication/)
1594/)
PrevPost(http://azsysadmin.com/huongdantaovpsamazonec2/)(http://azsysadmin.com/muavaquanlydomainnhuthenao/)

5Comments
Recommend

NextPost
1

azsysadmin.com

Share

Login

SortbyBest

Jointhediscussion
NhtMinh 7monthsago

Hibn,mnhciPHPFPM(Centos6.5),nhngtrncommandline,mnhkhngthnosdngclnhphp?Vmnhcn
xilnhnycicomposervlaravelframework.(MnhcbitkhiciPHPFPMcPHPsnri)

Reply Share

vankhoa011

Mod >NhtMinh

7monthsago

Chobn.Mnhnghlbnthiuphpcliri.Bncththcibngcchyuminstallphp5clithxem.

Reply Share

http://azsysadmin.com/caidatnginxphpfpmmysql/

10/16

5/5/2016

CitNginx+PHPFPM+MySQL

Reply Share

NhtMinh>vankhoa011 7monthsago

ah,thanksbnnh,mnhsetupcri,mnhcnghhocvpsscriptcsetupphpcli,thralnchac
ciphpcli^^

Reply Share

thaodt 9monthsago

Hibn,chomnhhikhiconfigvhostfilechositetrongnginx,khinothmnhsparamfastcgi_passl
fastcgi_passunix:/var/run/phpfpm/phpfpm.sock
vkhinol:
fastcgi_pass127.0.0.1:9000
2cinykhcnhaunhthno?Clichcngnhnhcimgivi2configparam?
Thankbnnhiu.

Reply Share

vankhoa011

Mod >thaodt

9monthsago

Chobn.
Cutrlibncthy,cngkhy.
http://serverfault.com/questio...
Thdavotrnthbnsbitkhcnhaunhthno.ngnhinlunixsockettthntcpsocketvtrnhcmt
sthnhrouting...
CnnudngTCPIP,thbncthloadbancingc,Vdbnc2serverchyphpfpm,vbnconfignginxthny
fastcgi_passfpm_backend
upstreamfpm_backend{
server192.168.100.101:9000

http://azsysadmin.com/caidatnginxphpfpmmysql/

11/16

5/5/2016

CitNginx+PHPFPM+MySQL

server192.168.100.102:9000
server192.168.100.103:9000
server192.168.100.104:9000
keepalive128#usefixednumber
}
Tuvonhucuvbnlachncchnonh.:).
1

Reply Share

ALSOONAZSYSADMIN.COM

HngdntoVPSAmazonEC2

DNSlg?GiithiuvAmazonRoute53

11comments2yearsago

1comment2yearsago

datnguyenthankyou

HongPhcAnhchoemhi,chngtatonhngkhng

thtrctipdngDNScaAmazonRoute53truycpti
ccresourcekhcnhEC2,mphily

Subscribe

AddDisqustoyoursiteAddDisqusAdd

Privacy

Search
Search

http://azsysadmin.com/caidatnginxphpfpmmysql/

12/16

5/5/2016

CitNginx+PHPFPM+MySQL

RecentPosts
Setupnginx+gunicorn+supervisord.(http://azsysadmin.com/setnginxgunicornsupervisord/)

(http://azsysadmin.com/set
nginx
gunicorn
supervisord/)

BuildApachefromsource.(http://azsysadmin.com/buildapachesource/)

(http://azsysadmin.com/build
apache
source/)

CchtoswapfilechoLinux(http://azsysadmin.com/cachtaoswapfilecholinux/)

(http://azsysadmin.com/cach
taoswap
filecho
linux/)

Fixmysqlslaveerror1594(http://azsysadmin.com/fixmysqlslaveerror1594/)

(http://azsysadmin.com/fix
mysql
slave

http://azsysadmin.com/caidatnginxphpfpmmysql/

13/16

5/5/2016

CitNginx+PHPFPM+MySQL

error
1594/)

CuhnhMysqlReplication(http://azsysadmin.com/cauhinhmysqlreplication/)

(http://azsysadmin.com/cau
hinh
mysql
replication/)

Ads

http://azsysadmin.com/caidatnginxphpfpmmysql/

14/16

5/5/2016

http://azsysadmin.com/caidatnginxphpfpmmysql/

CitNginx+PHPFPM+MySQL

15/16

5/5/2016

CitNginx+PHPFPM+MySQL

Tags

AmazonEC2(http://azsysadmin.com/tag/amazonec2/)
godaddy(http://azsysadmin.com/tag/godaddy/) heartbleed(http://azsysadmin.com/tag/heartbleed/)

(http://azsysadmin.com/tag/mysql/)

dns(http://azsysadmin.com/tag/dns/)

mysql

nginx(http://azsysadmin.com/tag/nginx/) php(http://azsysadmin.com/tag/php/) phpfpm

(http://azsysadmin.com/tag/phpfpm/) VPS(http://azsysadmin.com/tag/vps/)

Gipbntrthnhmtsysadminmtcchnhanhchng|AllRightsReserved.

http://azsysadmin.com/caidatnginxphpfpmmysql/

16/16

You might also like