Ubuntu安装php+nginx环境

环境

  • Ubuntu: 20.04
  • Nginx: 1.18.0
  • PHP: 7.4

安装Nginx

1
sudo apt install nginx -y

安装PHP

Ubuntu 20.04官方源自带的是PHP7.4,直接安装这个版本。

安装php

1
sudo apt install php-fpm

启动php

1
/etc/init.d/php7.4-fpm start

设置自动启动

1
sudo systemctl enable php7.4-fpm

启用PHP

在nginx的server域增加以下代码(或者直接在default站点中取消注释掉以下代码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Add index.php to the list if you are using PHP
index index.html index.htm index.php;

<-- Hide serveral lines -->

# 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/php7.4-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}

安装扩展

安装:mysqli_connect

1
sudo apt install php-mysql

修改/etc/php/7.4/fpm/php.ini,取消以下行的注释:

1
extension=mysqli

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:mb_strlen

1
sudo apt install php-mbstring

修改/etc/php/7.4/fpm/php.ini,取消以下行的注释:

1
extension=mbstring

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:bcmath(含bccomp)

1
sudo apt install php-bcmath

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:curl

1
sudo apt install php-curl

修改/etc/php/7.4/fpm/php.ini,取消以下行的注释:

1
extension=curl

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:zip

1
sudo apt install php-zip

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:gd

1
sudo apt install php-gd

修改/etc/php/7.4/fpm/php.ini,取消以下行的注释:

1
extension=gd2

重启php:

1
sudo /etc/init.d/php7.4-fpm restart

安装:dom

1
sudo apt install php-xml

重启php:

1
sudo /etc/init.d/php7.4-fpm restart


转载请注明出处:Ubuntu安装php+nginx环境
原文地址:https://www.xiaotanzhu.com/ubuntu/2022-05-07-install-php-basis.html