nginx配置启动php的2种方式

14 篇文章 0 订阅

一.sock方式(宝塔一键安装)

1.nginx配置文件

server
{
    listen 100;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/manager/public;

    include enable-php-70.conf;

        if (!-e $request_filename) {
                rewrite ^/(.+)$ /index.php last;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
        location /.git
        {
                deny all;
                return 403;
        }
    access_log  /www/wwwlogs/manager.com.log;
    error_log  /www/wwwlogs/manager.com.error.log;
}

2.enable-php-70.conf:

location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi-70.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            include pathinfo.conf;
        }

二.编译安装方式

server {
    listen       80;
    server_name  xx.xxx.com;
  index index.html index.php;
  root /alidata/www/apays;

  if (!-e $request_filename) {
    rewrite ^/(.+)$ /index.php last;
  }

  location ~ .*\.(php|php5)?$
  {
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  }
  location ~ .*\.(js|css)?$
  {
    expires 1h;
  }
  location /.git
  {
    deny all;
    return 403;
  }
  access_log  /alidata/log/nginx/access/xx.xxxx.com.log;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Nginx配置 PHP 需要使用 PHP-FPM(FastCGI Process Manager)。下面是一个简单的 Nginx 配置例子: ``` server { listen 80; server_name example.com; root /var/www/example.com; index index.php; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } ``` 在上面的配置中,当请求路径以 .php 结尾时,请求会被转发到 PHP-FPM 处理。其中 fastcgi_pass 的值应该根据您的 PHP-FPM 配置进行修改。 为了使用 PHP,还需要在服务器上安装 PHP 以及 PHP-FPM。然后,将 Nginx 配置重新加载即可。 ### 回答2: Nginx是一款高性能的Web服务器软件,可以用于配置和管理PHP服务器。 首先,我们需要确保服务器上已经安装了NginxPHP。在安装完成后,我们打开Nginx配置文件,通常位于/etc/nginx/nginx.conf。 在配置文件中,我们需要找到一个名为“server”的配置块,这个块是用来配置单个虚拟主机的。在这个块中,我们需要添加一些额外的配置来处理PHP文件。 首先,我们添加一个location块,用于处理PHP文件的请求。可以通过以下代码添加到server块中: location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 上述代码指定了当请求以.php结尾时,将使用FastCGI模块将请求传递给本地地址127.0.0.1:9000,这是PHP-FPM默认的监听地址和端口。 其次,我们需要设置index.php作为默认的索引文件。通过在server块中添加以下代码来实现: index index.php index.html; 最后,我们需要设置document root,即站点的根目录。我们可以通过在server块中添加以下代码来实现: root /var/www/html; 将上述配置添加到nginx.conf后,我们需要重新启动Nginx服务器,使配置生效: systemctl restart nginx 现在,我们的Nginx已经正确配置PHP支持。可以尝试在网站目录中创建一个index.php文件,并在文件中添加一些PHP代码进行测试。在浏览器中访问服务器的IP地址或域名,应该能够看到PHP代码正确执行的结果。 总结一下,通过配置Nginx并添加相应的location块,我们可以实现NginxPHP的集成,使Nginx可以处理PHP文件的请求,并将请求传递给PHP-FPM进行处理。 ### 回答3: Nginx是一款高性能的Web服务器,它可以与PHP-FPM(FastCGI Process Manager)配合使用,提供对PHP脚本的支持。下面是一个简单的Nginx配置PHP的步骤: 1. 首先,确保你的服务器已经安装了NginxPHP-FPM。可以通过执行以下命令来检查它们是否已安装: ``` nginx -v php-fpm -v ``` 2. 打开Nginx配置文件,通常是`/etc/nginx/nginx.conf`,并找到`http`部分的配置块。 3. 在`http`的配置块中,添加以下内容来定义PHP处理脚本的位置: ``` location ~ \.php$ { fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } ``` 这段配置告诉Nginx,请求以.php结尾的文件将通过FastCGI协议传递给PHP-FPM处理。 4. 保存并关闭Nginx配置文件,然后重启Nginx服务以使更改生效: ``` sudo service nginx restart ``` 5. 确保PHP-FPM服务正在运行,可以使用以下命令启动或重新启动它: ``` sudo service php-fpm start ``` 6. 修改你的PHP脚本,将其放置在Nginx的Web根目录下(通常是`/usr/share/nginx/html`),并确保文件具有适当的权限。 现在,当你访问以.php结尾的文件时,Nginx将会将请求传递给PHP-FPM处理,然后PHP-FPM将执行脚本并将结果返回给Nginx,最终将结果返回给客户端。这样就完成了Nginx配置PHP的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值