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
 

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

 

make    && make install

vi /etc/profile

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

更新:source /etc/profile

php -v

然后执行下图一系列操作

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左右

 

 

   

 

 

 

 

 

 

<think>好的,我现在要回答用户关于如何从源码编译并搭建PHP开发环境的问题。首先,我需要回忆一下相关的步骤,并参考用户提供的引用内容,确保信息的准确性。 用户提到了三个引用,其中引用[2]和[3]是关于CentOS系统下LNMP环境的源码编译安装,而引用[1]是Ubuntu下的PHP环境搭建。虽然用户的问题是关于源码编译PHP,但可能需要结合这些引用中的步骤。 首先,搭建PHP开发环境通常需要安装PHP及其依赖,配置Web服务器(如Nginx或Apache),以及数据库(如MySQL/MariaDB)。用户特别提到“源码编译”,所以需要从PHP的官方网站下载源码包,然后编译安装。 接下来,我需要考虑依赖项的安装。根据引用[2],在安装Nginx时,他们安装了gcc、gcc-c++、make、pcre-devel等。PHP编译同样需要这些基础编译工具,可能还需要其他库如libxml2、openssl等。需要确保这些依赖已经安装。 然后,下载PHP源码包,解压,进入目录,配置编译选项。配置步骤可能需要指定安装路径、启用相关模块(如MySQL支持、FPM等)。例如,使用./configure命令,设置--prefix指定安装目录,--with-fpm-user和--with-fpm-group来配置PHP-FPM的用户和组,以及启用mbstring、mysql等扩展。 编译完成后,执行make和make install进行安装。然后,需要配置php.ini文件,调整相关设置,比如时区、上传文件大小限制等。 接下来,配置Web服务器。如果使用Nginx,需要修改Nginx的配置文件,将PHP请求通过FastCGI传递给PHP-FPM处理。引用[2]中提到了NginxPHP-FPM的交互过程,这需要正确设置Nginx的location块,指定FastCGI的socket或端口。 此外,还需要启动PHP-FPM服务,并设置开机自启。可能需要复制配置文件模板到正确的位置,并启动服务。 测试环节也很重要,创建phpinfo.php文件,通过浏览器访问,确认PHP是否正确解析。引用[1]中提到了在指定目录创建测试文件,并通过浏览器访问的方法,这里可以沿用类似的步骤。 最后,可能还需要安装数据库,如MySQL或MariaDB,但用户的问题主要集中在PHP环境搭建,所以这部分可能不需要详细展开,但可以提及。 在整理步骤时,需要注意不同Linux发行版的包管理工具不同,用户的问题没有指定系统,但引用中提到了Ubuntu和CentOS。需要指出不同系统下依赖安装命令的差异,比如CentOS用yum,Ubuntu用apt。 需要确保每一步的命令正确,例如创建用户组、配置时的参数是否正确,避免遗漏关键步骤。同时,提醒用户注意权限问题,比如Web服务器的运行用户是否有权限访问PHP-FPM的socket或端口。 可能遇到的问题包括依赖项缺失导致编译失败,配置选项错误导致模块未启用,或者NginxPHP-FPM通信失败。需要在步骤中给出相应的解决建议,例如检查错误日志,确认服务是否运行等。 总结,步骤大致如下: 1. 安装编译工具和依赖库 2. 下载并解压PHP源码 3. 配置编译选项,编译并安装 4. 配置php.ini和PHP-FPM 5. 配置Web服务器(如Nginx)支持PHP 6. 启动服务并测试 需要参考引用中的内容,例如引用[2]中安装Nginx的依赖部分,可以类比到PHP的依赖安装;引用[3]提到的Tengine是基于Nginx的,可能配置步骤类似。同时,测试部分参考引用[1]的方法。 确保使用正确的代码块格式,将命令用反引号包裹,并按照用户的系统级指令,正确使用中文字符和LaTeX格式(虽然这个问题中可能涉及较少数学公式,但如果有版本号之类的可能需要用$包裹,例如PHP 7.4.3写作$7.4.3$?不过通常版本号可能不需要LaTeX。需要检查用户示例中的使用情况,示例中变量使用了$,所以可能版本号不需要,暂时不处理)。 最后,生成相关问题,可能涉及优化编译选项、常见错误解决、环境配置调优等。</think>### 从源码编译并搭建PHP开发环境步骤 #### 1. 安装编译依赖工具和库 在编译PHP前,需安装必要的编译工具和依赖库。根据系统类型选择命令: - **CentOS/RHEL**: ```bash sudo yum -y install gcc make libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libzip-devel sqlite-devel ``` - **Ubuntu/Debian**: ```bash sudo apt-get update sudo apt-get install -y build-essential libxml2-dev libssl-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libzip-dev libsqlite3-dev ``` #### 2. 下载PHP源码包 访问[PHP官网](https://www.php.net/downloads)获取最新稳定版源码链接,使用`wget`下载并解压: ```bash wget https://www.php.net/distributions/php-8.2.0.tar.gz tar -zxvf php-8.2.0.tar.gz cd php-8.2.0 ``` #### 3. 配置编译选项 运行`./configure`配置编译参数,需指定安装路径、启用PHP-FPM及常用扩展: ```bash ./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-mbstring \ --enable-zip \ --with-curl \ --with-openssl \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-zlib ``` > **注意**:若需其他扩展(如GD库),添加`--with-gd`并确保依赖已安装[^2]。 #### 4. 编译并安装 ```bash make -j$(nproc) # 使用多核加速编译 sudo make install ``` #### 5. 配置PHP环境 - **复制配置文件**: ```bash sudo cp php.ini-production /usr/local/php/etc/php.ini sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf ``` - **修改PHP-FPM用户组**(与Web服务器用户一致): ```bash sudo groupadd www sudo useradd -g www www sudo sed -i &#39;s/user = nobody/user = www/g&#39; /usr/local/php/etc/php-fpm.d/www.conf sudo sed -i &#39;s/group = nobody/group = www/g&#39; /usr/local/php/etc/php-fpm.d/www.conf ``` #### 6. 配置Web服务器(以Nginx为例) - **Nginx虚拟主机配置**: ```nginx server { listen 80; server_name localhost; root /home/www; index index.php index.html; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` 重启Nginx: ```bash sudo systemctl restart nginx ``` #### 7. 启动PHP-FPM并测试 - **启动服务**: ```bash sudo /usr/local/php/sbin/php-fpm ``` - **创建测试文件**: ```bash echo "<?php phpinfo(); ?>" > /home/www/phpinfo.php ``` 访问 `http://localhost/phpinfo.php`,若显示PHP信息页面则成功[^1]。 #### 8. 设置开机自启(可选) - **创建Systemd服务文件**(`/etc/systemd/system/php-fpm.service`): ```ini [Unit] Description=PHP FastCGI Process Manager After=network.target [Service] Type=simple ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize User=www Group=www [Install] WantedBy=multi-user.target ``` - **启用服务**: ```bash sudo systemctl enable php-fpm sudo systemctl start php-fpm ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值