Ubuntu20.04.1 LTS安装PHP8.0.1

这是目前全网最新的安装指南了。

首先,20.04.1版本的Ubuntu没有包括8.0.1PHP,所以需要安装第三方的PPA库来进行安装,下面的命令增加第三方PPT到Ubuntu

sudo apt update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/nginx

与Apache不同,Nginx没有对php文件的内建支持,因此使用php-fpm(fastCGI process manager)来处理PHP文件。

目前最新的php版本是8.0.1,但是aliyun没法自动安装这个源,手动安装太麻烦,因此我只有先用8.0来试试。

和Apache2不同,Nginx自己没有自建队php的处理支持,所以需要分别安装Nginx和PHP

#sudo apt update
#sudo apt install nginx php8.0-fpm

重新启动Nginx和PHP-FRM

sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

检查服务状态如下

systemctl status php8.0-fpm.service

现在你可以编辑服务器配置,方便Nginx处理PHP文件。

由于Nginx没有内建PHP支持,所以需求手工编辑server部分来支撑PHP,如下

sudo nano /etc/nginx/sites-available/default

编辑下文粗体部分

   # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one

然后reload两个服务

sudo systemctl reload nginx
sudo systemctl reload php8.0-fpm

参考文档如下
https://websiteforstudents.com/install-php-8-0-on-ubuntu-20-04-18-04/

Leave a Reply

Your email address will not be published. Required fields are marked *