Ubuntu20.04 nginx +php 配置

1、安装nginx php mysql环境

sudo apt-get update

sudo apt-get install nginx

sudo apt-get install php-fpm php-mysql php-curl


2、nginx 配置加上如下 (修改此文件/etc/nginx/sites-available/default)

location ~ \.php$ {

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/run/php/php7.4-fpm.sock;

}

正常情况 default 里有此段代码,去掉注释即可。

3、重启 nginx

sudo systemctl restart nginx

4、验证安装是否准确

在/var/www/html加上test.php文件

cd /var/www/html

sudo vi test.php

输入如下内容:

phpinfo();

?>


5、在浏览器访问 http://localhost/test.php

如果出现 502 Bad Gateway

1、运行:sudo find / -name php7.4-fpm.sock

2、修改nginx配置

location ~ \.php$ {

include snippets/fastcgi-php.conf;

fastcgi_pass unix:上一步找到的路径;

}

例如:

location ~ \.php$ {

include snippets/fastcgi-php.conf;

fastcgi_pass unix:/run/php/php7.4-fpm.sock;

}

3、重启 nginx,并再次验证访问

sudo systemctl restart nginx

你可能感兴趣的:(Ubuntu20.04 nginx +php 配置)