Ubuntu 上安装Nginx,配置支持php解析

ubuntu 是linux是著名分发包,以前记得界面做的很绚丽,注重往桌面方向发展,现在服务器端用的也非常多。

nginx是继apache后著名的web容器,以反向代理著称,业界非常出名。

php是网站开发大神语音,号称世界上最强大的语言就是php语言,距离开发语言鄙视链的顶端。

下面就是在ubuntu下安装nginx,并支持php解析的配置。

首先来个传统两件套

sudo apt update
sudo apt upgrade

不知道这个是干什么的朋友,参考此文

再来一个

sudo apt install nginx

安装nginx,不是哥们,这就完了? 是的这就完了。 不不不,还有怎么启动,开机怎么重启呢?

启动nginx

sudo systemctl start nginx

设置开机启动

sudo systemctl enable nginx

检查状态

sudo systemctl status nginx

网站目录在 /var/www/html

支持php设置

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

server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    # 配置 PHP 处理
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        
        # 根据 PHP 版本调整
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;  # Ubuntu 20.04/22.04
        
        # 对于旧版本可能是:
        # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # 禁止访问 .htaccess 文件
    location ~ /\.ht {
        deny all;
    }
}

《Ubuntu 上安装Nginx,配置支持php解析》上有1条评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注