centos7安装 php7.4.3 + php-fpm + nginx负载均衡

去php官网下载最新安装包

解压

检测

./configure --prefix=/home/phpstudy/php --with-config-file-path=/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-xmlrpc --with-openssl --with-mhash --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --enable-ftp --enable-gd --with-openssl-dir --with-zlib-dir --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --with-xsl --enable-mysqlnd-compression-support --with-pear --enable-opcache --disable-fileinfo

configure: error: GNU MP Library version 4.2 or greater required.
yum -y install gmp-devel 

configure: error: Please reinstall readline - I cannot find readline.h
 yum -y install readline-devel
 

当你看到这个 说明编译成功 

centos7安装 php7.4.3 + php-fpm + nginx负载均衡_第1张图片

 

make    && make install

vi /etc/profile

追加:PATH=$PATH:/usr/local/php/bin

更新:source /etc/profile

php -v

然后执行下图一系列操作

centos7安装 php7.4.3 + php-fpm + nginx负载均衡_第2张图片

php -m  查看扩展   php --ini 查看php.ini配置文件位置

配置php.ini 文件

; 不显示错误,默认
display_errors = Off

; 在关闭display_errors后开启PHP错误日志(路径在php-fpm.conf中配置),默认
log_errors = On

; 字符集,默认
default_charset = "UTF-8"

; 文件上传大小,默认值太小,建议修改8M
upload_max_filesize = 2M

; Maximum size of POST data that PHP will accept.  表单最大值,默认是8M,如果表单含有多图上传,大小可能不够。超过该大小后台收不到 表单数据
post_max_size = 8M

; 设置PHP的扩展库路径,默认被注释了,之后一个文件夹 于 你/usr/local/php/lib/php/extensions/ 下的文件夹同名。
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/"

; 设置PHP的时区
date.timezone = PRC

; 开启opcache,默认是0
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

配置php-fpm.conf 文件

; 去掉里分号,方便以后重启。建议修改
; Default Value: none
; 下面的值最终目录是/usr/local/php/var/run/php-fpm.pid
; 开启后可以平滑重启php-fpm
pid = run/php-fpm.pid

; 设置错误日志的路径,可以默认值
; Note: the default prefix is /usr/local/php/var
; Default Value: log/php-fpm.log, 即/usr/local/php/var/log/php-fpm.log
error_log = /var/log/php-fpm/error.log

; Log等级,可以默认值
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; 后台运行,默认yes,可以默认值
; Default Value: yes
; daemonize = yes

; 引入www.conf文件中的配置,可以默认值
include=/usr/local/php/etc/php-fpm.d/*.conf

配置 www.conf 配置文件 

; 设置用户和用户组,默认都是nobody。可以默认值
user = nginx
group = nginx

; 设置PHP监听
; 下面是默认值,不建议使用。可以默认值
listen = 127.0.0.1:9000
; 根据nginx.conf中的配置fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
;listen = /var/run/php-fpm/php-fpm.sock

######开启慢日志。可以默认值
slowlog = /var/log/php-fpm/$pool-slow.log
request_slowlog_timeout = 10s

php-fpm 检测 启动 关闭 重启

检测php-fpm配置文件:/etc/init.d/php-fpm -t
启动: /etc/init.d/php-fpm 
kill -INT `cat /home/phpstudy/php/var/run/php-fpm.pid`      # php-fpm关闭
kill -USR2 `cat /home/phpstudy/php/var/run/php-fpm.pid`     #php-fpm平滑重启

 

二 nginx 安装

安装pcre 

  获取pcre编译安装包,在http://www.pcre.org/上可以获取当前最新的版本

[root@localhost src]# wget ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz

  解压pcre-8.42.tar.gz到/usr/local/

[root@localhost src]# tar xvf pcre-8.42.tar.gz -C /usr/local/

 进入解压缩目录,执行./configure

[root@localhost pcre-8.42]# ./configure

 编译   安装

make && make install

下载nginx源码包 : wget http://nginx.org/download/nginx-1.16.1.tar.gz

解压 进入目录检测编译

./configure --prefix=/home/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.42/

make  && make install

查看版本 /home/nginx/sbin/nginx -v

配置nginx.conf 文件

location / {
            root   /home/www/;
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }

        }

 location ~ \.php$ {
            root           /home/www/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

nginx -t  检测

./sbin/nginx 启动

nginx -s reload  重启

三 配置三台服务器nginx负载均衡

方式一

三台服务器都安装nginx  转发

user  nginx;
worker_processes  auto;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    upstream amok_http {
       server 127.0.0.1:81 weight=1;
       server 192.168.255.134 weight=1;
       server 192.168.255.135 weight=1;
    }
    
    #负载均衡请求分发
    server {
        listen 80;
        #多域名用空格隔开
        server_name www.2amok.com;
 
        #设置默认访问首页
        index index.html index.php;
 
        location / {
            #所有请求反向代理到服务器池中的服务器
            proxy_pass  http://amok_http;
            proxy_set_header Host      $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
 
    }
    
    server {
        listen       81;
        server_name www.2amok.com;

        charset utf-8;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /home/www/;
            index  index.html index.htm index.php;     
	    if (!-e $request_filename) {
		rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        location ~ \.php$ {
            root           /home/www/;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

方式二 直接转发到 本机以及其它两台服务器 php-fpm 9000  端口

使用方式二 其它两台服务器不需要安装nginx  但是 php-fpm 监听端口必须改成  0.0.0.0:9000    修改文件是www.conf       

user  nginx;
worker_processes  auto;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    upstream amok_http {
       server 127.0.0.1:9000 weight=1;
       server 192.168.255.134:9000 weight=1;
       server 192.168.255.135:9000 weight=1;
    }
    
   
    
    server {
        listen       80;
        server_name www.2amok.com;

        charset utf-8;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /home/www/;
            index  index.html index.htm index.php;     
	    if (!-e $request_filename) {
		        rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
            }

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        location ~ \.php$ {
            root           /home/www/;
            fastcgi_pass   amok_http;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 ab 压测了下 两种方式,结果基本相同。 100人 1000 次  3000QPS左右                 200人  3000次   2000QPS左右

 

 

   

 

 

 

 

 

 

你可能感兴趣的:(Linux,nginx,PHP)