解决PHP8.0.1@Ubuntu20.04.1中PHP无法工作的问题

前两周在阿里云Ubuntu20.04上安装最新php8.0后,测试phpinfo,总是无法出现成功的测试结果,往往是直接下载info.php这个文件,这事情困扰我2周,今天终于搞定了。

Screen Shot 2021-01-23 at 10.48.51 AM

第一步:升级系统最新补丁

首先要确定php和nginx都更新到了最新状态

sudo apt-get update && apt-get upgrade

检查nginx版本

nginx -v

测试nginx配置文件是否有错误

nginx -t

Screen Shot 2021-01-23 at 11.04.16 AM

检查php状态

systemctl status php8.0-fpm.service

Screen Shot 2021-01-23 at 11.06.32 AM

第二步a:修改php和nginx的配置文件

修改php.ini

./etc/php/8.0/fpm/php.ini

这里我没有做任何修改
Screen Shot 2021-01-23 at 11.09.32 AM

第二步b修改nginx默认站点配置

这里是关键:

不是修改“./etc/nginx/nginx.conf”这个文件!!

进入/etc/nginx/sites-available,修改dafault,当然,先做好备份
然后编辑下面绿色部分,取消掉注释,然后把php7.4-fpm修改为php8.0-fpm,注意后面的括号也要去掉注释

Screen Shot 2021-01-23 at 11.17.45 AM

最后,不要忘记删除info.php这个文件。

参考如下:Update! Configure Nginx to Read PHP on Ubuntu 16.04

Ubuntu完全卸载nginx服务器

方法1

使用‘sudo apt-get –purge remove nginx’彻底删除

# sudo service nginx stop
# sudo apt-get --purge remove nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8
  libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libtiff5 libwebp6 libxpm4 libxslt1.1 nginx-common nginx-core
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nginx*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 45.1 kB disk space will be freed.
Do you want to continue? [Y/n] y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
        LANGUAGE = (unset),
        LC_ALL = (unset),
        LC_CTYPE = "UTF-8",
        LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
(Reading database ... 117708 files and directories currently installed.)
Removing nginx (1.18.0-0ubuntu1) ...

方法2

自动移除全部不使用的软件包

sudo apt-get autoremove

列出与nginx相关的软件,并依次删除显示的软件,这个方法更加通用,我照猫画虎使用在php上面,也非常靠谱(php8.0-fpm)

dpkg --get-selections|grep nginx
sudo apt-get --purge remove nginx
sudo apt-get --purge remove nginx-common
sudo apt-get --purge remove nginx-cor

使用ps命令查看相关进程,-A显示有限信息,-l显示详细信息 -e显示所有进程,aux显示系统所有进程

ps -le | grep nginx

参考材料如下
https://blog.csdn.net/adley_app/article/details/79223221

ps命令
http://c.biancheng.net/view/1062.html

删除apache2
https://blog.csdn.net/dazhi_100/article/details/43121179

ubuntu升级软件和安装Nginx

首先看下有没有需要升级的软件

# apt list --upgradable
Listing... Done
linux-generic/focal-updates 5.4.0.60.63 amd64 [upgradable from: 5.4.0.54.57]
linux-headers-generic/focal-updates 5.4.0.60.63 amd64 [upgradable from: 5.4.0.54.57]
linux-image-generic/focal-updates 5.4.0.60.63 amd64 [upgradable from: 5.4.0.54.57]

还好,刚才已经把软件包升级过了,现在只要升级系统就可以。

升级软件包命令

sudo apt-get update 更新源
sudo apt-get upgrade 更新已安装的包

升级系统命令如下

sudo apt-get dist-upgrade 升级系统

然后在看下,都是最新的了。

#apt list --upgradable
Listing... Done
root@MDaliyun ~ # sudo apt update
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu focal InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu focal-updates InRelease
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu focal-security InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

下来安装Nginx

安装Nginx

sudo apt install nginx

查看Nginx状态

 sudo systemctl status nginx

root@MDaliyun / # vi /etc/nginx/nginx.conf

编辑 nginx.conf 文件。
找到 server{…},并将 server 大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动。

server {
 listen       80;
 root   /usr/share/nginx/html;
 server_name  localhost;
 #charset koi8-r;
 #access_log  /var/log/nginx/log/host.access.log  main;
 #
 location / {
       index index.php index.html index.htm;
 }
 #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   /usr/share/nginx/html;
 }
 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 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;
 }
}

参考文档如下
https://www.myfreax.com/how-to-install-nginx-on-ubuntu-18-04/