1.安装php 7.2
2.php配置
3.nginx配置
4.测试
5.报错与解决
6.利用upstream实现负载均衡
1.安装php 7.2
启动容器:
1 liwangdeMacBook-Air:~ liwang$ docker run -i -t --name php --net mynetwork --ip 172.18.0.5 -p 9000:9000 centos /bin/bash
复制php至容器
1 liwangdeMacBook-Air:~ liwang$ docker cp soft/php-7.2.5.tar.gz php:/soft
安装插件
1 [root@6aaa15f97607 php-7.2.5]# yum install gcc gcc-c++ make libxml2 libxml2-devel libpng libpng-devel -y
编译php
1 [root@6aaa15f97607 html]# ./configure --prefix=/usr/local/php --with-pdo-mysql --enable-mysqlnd --with-gd --enable-gd-jis-conv --enable-fpm --with-mysqli=mysqlnd 2 [root@6aaa15f97607 html]# make && make install
2.配置PHP
1.复制php-fpm.conf
1 [root@6aaa15f97607 php-7.2.5]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
2.复制www.conf
1 [root@6aaa15f97607 php-7.2.5]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
3.修改www.conf
查看php容器ip地址
1 liwangdeMacBook-Air:~ liwang$ docker inspect -f {{.NetworkSettings.Networks.mynetwork.IPAddress}} php 2 172.18.0.5 3 liwangdeMacBook-Air:~ liwang$
修改www.con listen为容器ip,具体如下:
1 [root@6aaa15f97607 php-fpm.d]# sed -n "34,38p" /usr/local/php/etc/php-fpm.d/www.conf 2 ; '/path/to/unix/socket' - to listen on a unix socket. 3 ; Note: This value is mandatory. 4 listen = 172.18.0.5:9000 5 6 ; Set listen(2) backlog. 7 [root@6aaa15f97607 php-fpm.d]#
4.启动php-fpm
1 [root@6aaa15f97607 php-fpm.d]#/usr/local/php/sbin/php-fpm 2 [root@6aaa15f97607 php-fpm.d]#
5.查看启动状态
1 [root@6aaa15f97607 php-fpm.d]#netstat -tulnp 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 4 tcp 0 0 172.18.0.5:9000 0.0.0.0:* LISTEN 13407/php-fpm: mast 5 tcp 0 0 127.0.0.11:37421 0.0.0.0:* LISTEN - 6 udp 0 0 127.0.0.11:41037 0.0.0.0:* - 7 [root@6aaa15f97607 php-fpm.d]# ps aux 8 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND 9 root 1 0.0 0.1 11784 2740 pts/0 Ss 11:56 0:00 /bin/bash 10 root 13407 0.0 0.3 50052 6472 ? Ss 14:14 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) 11 nobody 13408 0.0 0.5 50116 10328 ? S 14:14 0:00 php-fpm: pool www 12 nobody 13409 0.0 0.5 50116 10328 ? S 14:14 0:00 php-fpm: pool www 13 root 13430 0.0 0.1 47496 3384 pts/0 R+ 15:13 0:00 ps aux 14 [root@6aaa15f97607 php-fpm.d]#
3.nginx配置
1.nginx.conf配置如下:
注意index中需要设置添加index.php,location php需要注意fastcgi_pass和fastcgi_param项
1 [root@dbc19df20116 www]# sed -n "32,82p" /usr/local/nginx/conf/nginx.conf | egrep -v "#|^$" 2 keepalive_timeout 65; 3 server { 4 listen 80; 5 server_name localhost; 6 location / { 7 root html; 8 index index.php index.html index.htm; 9 } 10 error_page 500 502 503 504 /50x.html; 11 location = /50x.html { 12 root html; 13 } 14 location ~ \.php$ { 15 root html; 16 fastcgi_pass 172.18.0.5:9000; 17 fastcgi_index index.php; 18 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; 19 include fastcgi_params; 20 } 21 } 22 [root@dbc19df20116 www]#
2.由于fastcgi_pass连接至不同的服务器,故php服务器需要设置index root
1 [root@6aaa15f97607 php-fpm.d]# ls -l /usr/local/nginx/html/index.php 2 -rw-r--r-- 1 root root 21 May 14 14:24 /usr/local/nginx/html/index.php 3 [root@6aaa15f97607 php-fpm.d]#
4.测试
1 [root@dbc19df20116 www]# curl localhost 2 Hello,This is PHP server site 3 [root@dbc19df20116 www]#
5.报错与解决
1 [root@dbc19df20116 www]# cat /usr/local/nginx/logs/error.log 2 2018/05/14 14:08:09 [notice] 25607#0: signal process started 3 2018/05/14 14:08:09 25611#0: sched_setaffinity() failed (22: Invalid argument) 4 2018/05/14 14:15:37 [error] 25611#0: *32 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.18.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://172.18.0.5:9000", host: "localhost" 5 [root@dbc19df20116 www]#
1.如果nginx与php服务器是分开部署的话,如错误日志所属,那么172.18.0.5这台服务器上需要有nginx的root文件,既index.php,添加后解决此问题。
2.如果nginx与php是在同一服务器,且php是使用127.0.0.1进行连接的话,那么可以修改nginx.conf为:
(fastcgi_param SCRIPT_FILENAME
$document_root
$fastcgi_script_name
;)
6.利用upstream实现负载均衡
环境:在服务器172.18.0.5上构建两台php,其端口分别为9000以及9001
1 [root@6aaa15f97607 ~]# netstat -tulnp 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 4 tcp 0 0 127.0.0.11:43655 0.0.0.0:* LISTEN - 5 tcp 0 0 172.18.0.5:9000 0.0.0.0:* LISTEN 46/php-fpm: master 6 tcp 0 0 172.18.0.5:9001 0.0.0.0:* LISTEN 17/php-fpm: master 7 udp 0 0 127.0.0.11:50582 0.0.0.0:* - 8 [root@6aaa15f97607 ~]#
在nginx本地构建一台php,其端口为9000
1 [root@dbc19df20116 conf]# netstat -tulnp 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 4 tcp 0 0 127.0.0.11:35111 0.0.0.0:* LISTEN - 5 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 42/php-fpm: master 6 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23/nginx: master pr 7 udp 0 0 127.0.0.11:57311 0.0.0.0:* - 8 [root@dbc19df20116 conf]#
修改nginx配置文件nginx.conf
upstream作用域http标签内
其后状态值含义如下:
weight:值越大,负载则越重
backup:当其他机器忙或则down时,数据才会请求backup机器,因此,此机器负载最轻
down:表示该机器不参与负载
例如,如下配置,将负载压在172.18.0.5上,127.0.0.1则作为备份机
upstream siteidc{
server 172.18.0.5:9000 weight=2;
server 172.18.0.5:9001 weight=1;
server 127.0.0.1:9000 backup;
}
location设置如下,将fastcgi_pass压为upstream的值即可。
location ~ \.php$ {
root html;
fastcgi_pass siteidc;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
整体nginx.conf文件如下:
1 [root@dbc19df20116 conf]# cat nginx.conf | grep -v "^$" | grep -v "#" 2 worker_processes 4; 3 worker_cpu_affinity 00000001 00000010 00000011 00000100; 4 worker_rlimit_nofile 1048576; 5 events { 6 use epoll; 7 worker_connections 65535; 8 } 9 http { 10 upstream siteidc{ 11 server 172.18.0.5:9000 weight=2; 12 server 172.18.0.5:9001 weight=1; 13 server 127.0.0.1:9000 backup; 14 } 15 include mime.types; 16 default_type application/octet-stream; 17 sendfile on; 18 keepalive_timeout 65; 19 server { 20 listen 80 default backlog=1024; 21 server_name localhost; 22 location / { 23 root html; 24 index index.php index.html index.htm; 25 } 26 error_page 500 502 503 504 /50x.html; 27 location = /50x.html { 28 root html; 29 } 30 location ~ \.php$ { 31 root html; 32 fastcgi_pass siteidc; 33 fastcgi_index index.php; 34 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; 35 include fastcgi_params; 36 } 37 } 38 server { 39 listen 80; 40 server_name www.wang-li.top; 41 42 location / { 43 root html/www; 44 index index.html index.php; 45 } 46 location ~ \.php$ { 47 root html/www; 48 fastcgi_pass 172.18.0.5:9001; 49 fastcgi_index index.php; 50 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/www$fastcgi_script_name; 51 include fastcgi_params; 52 } 53 } 54 } 55 [root@dbc19df20116 conf]#
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:安装php7.2并且整合nginx且分开部署 - Python技术站