ubuntu系統下安裝php7.4


視頻地址

原文地址

一.下載/更新php源

  1. 打開下載網址

    https://launchpad.net/~ondrej/+archive/ubuntu/php

  2. 先安裝一下這個命令 add-apt-repository

    apt-get install software-properties-common

  3. 添加第三方源:

    add-apt-repository ppa:ondrej/php

  4. 更新本地源

    apt-get update

二.安裝php7.4

  1. 安裝

    apt-get install php7.4 php7.4-fpm php7.4-mysql php7.4-gd php7.4-mbstring
    選擇6. Asia
    選擇70. Shanghai

  2. 啟動php

    service php7.4-fpm start #啟動fpm

  3. 查看進程

    root@7c609eaf61d3:/etc/init.d# ps aux|grep php 
    
    root     11864  0.0  0.0 342724 10104 ?        Ss   07:05   0:00 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
    
    www-data 11865  0.0  0.0 345020  9672 ?        S    07:05   0:00 php-fpm: pool www
    
    www-data 11866  0.0  0.0 345020  9672 ?        S    07:05   0:00 php-fpm: pool www
    
    root     11868  0.0  0.0  11464  1004 pts/1    S+   07:06   0:00 grep --color=auto php   
    
  4. 查看版本

    root@7c609eaf61d3:/etc/init.d# php -v #查看進程
    PHP 7.4.8 (cli) (built: Jul 13 2020 16:45:47) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.8, Copyright (c), by Zend Technologies
    

安裝php主要的就三個
phpcli #命令行
php7.4-fpm #和nginx配合的多進程管理 多數使用這個
module #和apache配合的

  1. 查看監聽的端口

    安裝net-tools

    可以用altupn命令查看監聽的端口

    apt-get install net-tools
    root@7c609eaf61d3:/etc/init.d# netstat -altupn|grep 9000
    root@7c609eaf61d3:/etc/init.d# netstat -altupn|grep 80  
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      23/nginx: master pr 
    tcp        0      0 172.17.0.3:47020        124.200.113.110:80      TIME_WAIT   -                   
    tcp6       0      0 :::80                   :::*                    LISTEN      23/nginx: master pr
    
     以上可以看到9000端口沒有被監聽,只監聽了80端口
    

    fpm監聽有兩種方式

  • a.監聽端口,一般為9000端口
  • b.監聽socket

三.修改配置

3.1 修改www.conf 文件

vim /etc/php/7.4/fpm/pool.d/www.conf
/listen = #可以找到監聽方式 listen = /run/php/php7.4-fpm.sock

說明默認使用sock方式配合nginx方式工作

修改以下幾處配置

1.打開在控制台顯示php的錯誤

;php_flag[display_errors] = off 改為 php_flag[display_errors] = on

;php_admin_flag[log_errors] = on 改為 php_admin_flag[log_errors] = on

2.打開日志

;access.log = log/$pool.access.log 改為 access.log = log/$pool.access.log

打開日志后,需要新建日志文件/usr/log/www.access.log,

/var/log/php7.4-fpm.log文件里

mkdir -p /usr/log

vim /usr/log/www.access.log

保存並退出

如果沒有這個文件,php會啟動不了,不報錯,錯誤日志會寫入日志文件,

cat /etc/php/7.4/fpm/php-fpm.conf里可以查到php錯誤日志會寫會

error_log = /var/log/php7.4-fpm.log

四.配置域名

vim /etc/hosts

127.0.0.1 phptest.haimait.hm

五.nginx的配置文件

5.1 sock方式和nginx配合工作

  1. 修改php監聽方式

    vim /etc/php/7.4/fpm/pool.d/www.conf

    這里我們使用監聽sock的方式配合nginx工作

    listen = /run/php/php7.4-fpm.sock

  2. 重啟php

    service php7.4-fpm reload

  3. 修改nginx配置文件

    vim /etc/nginx/conf.d/phptest.haimait.hm.conf

    server {
        listen       80;
        server_name  phptest.haimait.hm;
        access_log  /var/log/nginx/phptest.haimait.hm.access.log;
        error_log   /var/log/nginx/phptest.haimait.hm.error.log;
        root   /wwwroot/html/phptest;
        location / {
            index  index.php index.html index.htm;
        }
        location ~ \.php$ {
            root           /wwwroot/html/phptest;
            #fastcgi_pass這里的路徑要的/etc/php/7.4/fpm/pool.d/www.conf 里listen = 里的配置的一致
            fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
            #fastcgi_pass  127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #�user root
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    保存退出后

  4. 重啟nginx

    root@7c609eaf61d3:/etc/nginx/conf.d# service nginx restart
    * Restarting nginx nginx 
    
  5. curl測試

curl http://127.0.0.1/index.php 測試成功

5.2監聽9000端口和nginx配合工作(推薦)

  1. 修改php監聽方式

    vim /etc/php/7.4/fpm/pool.d/www.conf

    這里我們改為使用監聽9000端口的方式配合nginx

    listen = /run/php/php7.4-fpm.sock 改為listen = 127.0.0.1

    重啟php

  2. service php7.4-fpm reload

  3. 修改nginx配置文件

    vim /etc/nginx/conf.d/phptest.haimait.hm.conf

server {
    listen       80;
    server_name  phptest.haimait.hm;
    access_log  /var/log/nginx/phptest.haimait.hm.access.log;
    error_log   /var/log/nginx/phptest.haimait.hm.error.log;
    root   /wwwroot/html/phptest;
    location / {
        index  index.php index.html index.htm;
    }
    location ~ \.php$ {
        root           /wwwroot/html/phptest;
        #fastcgi_pass這里的路徑要的/etc/php/7.4/fpm/pool.d/www.conf 里listen = 里的配置的一致
        #fastcgi_pass   unix:/run/php/php7.4-fpm.sock;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #�user root
        #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
    }
}

保存退出后

  1. 重啟nginx
root@7c609eaf61d3:/etc/nginx/conf.d# service nginx restart
* Restarting nginx nginx 
  1. curl測試

    curl http://127.0.0.1/index.php 測試成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM