ubuntu20.04安装配置nginx和php

环境为Docker下的Ubuntu 20.04,要为其配置nginx和php,其中php以fastcgi的方式运行(php-fpm)。

因为是Docker环境,故以下操作都直接以root身份运行。

换国内源

cp /etc/apt/sources.list /etc/apt/sources.list.bak

编辑文件/etc/apt/sources.list,更改为以下内容

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security multiverse

改完之后执行apt update建立缓存。

安装

可直接使用apt安装

apt update
apt install nginx
apt install php php-fpm php-redis php-gmp php-dev

安装的软件版本是 nginx 1.18,php是7.4,附带着还装了apache2。

额外配置

修改php-fpm配置文件/etc/php/7.4/fpm/pool.d/www.conf,改监听端口

;listen = /run/php/php7.4-fpm.sock
listen = 0.0.0.0:9000

添加站点

/etc/nginx/conf.d下新建default.conf,表示默认站点,文件内容

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost 127.0.0.1;

    access_log  /var/log/nginx/host.access.log;

    location / {
        root   /var/www/html;
        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$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi.conf;
    }
}

网站根目录为/var/www/html,nginx和php-fpm共用一个目录。

1 thought on “ubuntu20.04安装配置nginx和php”

Leave a Comment

豫ICP备19001387号-1