nginx简单配置多个php服务实例教程

 更新时间:2023年01月15日 09:55:19   作者:西虹市大陆  
nginx安装刚安装好是不能访问php文件的,需要我们进行配置,下面这篇文章主要给大家介绍了关于nginx简单配置多个php服务的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

nginx简单配置php服务(多个)

摘要:

大部分网站开发语言都要运行在服务器,比如主流的nginx、apache等等,部署服务器环境对于大部分人来说是比较陌生和复杂的,其实搞懂了之后是很简单易用的。今天就记录下部署php+nginx。

系统:mac、linux

1、安装好php和nginx程序,并运行

2、找到nginx.conf文件,默认在/etc/nginx目录下,如果找不到用一下命令查询

sudo find / -name nginx.conf

3、修改nginx.conf文件

默认的nginx.conf配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

把server下的这段#号去掉并修改即可,将 PHP 脚本传递给在 127.0.0.1:9000 上侦听的 FastCGI 服务器

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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;
        }

访问 localhost

参数参考:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;#脚本文件请求的路径
fastcgi_param  QUERY_STRING       $query_string; #请求的参数;如?app=123
fastcgi_param  REQUEST_METHOD     $request_method; #请求的动作(GET,POST)
fastcgi_param  CONTENT_TYPE       $content_type; #请求头中的Content-Type字段
fastcgi_param  CONTENT_LENGTH     $content_length; #请求头中的Content-length字段。
 
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; #脚本名称 
fastcgi_param  REQUEST_URI        $request_uri; #请求的地址不带参数
fastcgi_param  DOCUMENT_URI       $document_uri; #与$uri相同。 
fastcgi_param  DOCUMENT_ROOT      $document_root; #网站的根目录。在server配置中root指令中指定的值 
fastcgi_param  SERVER_PROTOCOL    $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。  
 
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;#cgi 版本
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;#nginx 版本号,可修改、隐藏
 
fastcgi_param  REMOTE_ADDR        $remote_addr; #客户端IP
fastcgi_param  REMOTE_PORT        $remote_port; #客户端端口
fastcgi_param  SERVER_ADDR        $server_addr; #服务器IP地址
fastcgi_param  SERVER_PORT        $server_port; #服务器端口
fastcgi_param  SERVER_NAME        $server_name; #服务器名,域名在server配置中指定的server_name

配置多个服务:

nginx.conf文件有一行

include servers/*;

代表会读取servers文件夹下的所有配置文件,没有可以自己加上,并创建文件夹,servers文件夹下创建一个站点配置文件site1.conf。

server {
    listen       80;#端口
    server_name  site1.com;#你的站点域名/ip
    root         /data/site1/public; #你的站点目录,绝对路径即可
    index index.php index.html index.htm;

    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    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;
    }
}

总结

到此这篇关于nginx简单配置多个php服务的文章就介绍到这了,更多相关nginx配置php服务内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • 基于PHP异步执行的常用方式详解

    基于PHP异步执行的常用方式详解

    本篇文章是对PHP异步执行的常用方式进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php登陆页的密码处理方式分享

    php登陆页的密码处理方式分享

    这篇文章介绍了php登陆页的密码处理方式,有需要的朋友可以参考一下
    2013-10-10
  • ThinkPHP查询语句与关联查询用法实例

    ThinkPHP查询语句与关联查询用法实例

    这篇文章主要介绍了ThinkPHP查询语句与关联查询用法,以实例的形式常见的查询方法,包括数组作为查询条件及对象方式来查询等技巧,需要的朋友可以参考下
    2014-11-11
  • Yii框架获取当前controlle和action对应id的方法

    Yii框架获取当前controlle和action对应id的方法

    这篇文章主要介绍了Yii框架获取当前controlle和action对应id的方法,可实现获取当前controlle或action对应id的功能,是非常实用的技巧,需要的朋友可以参考下
    2014-12-12
  • laravel 验证错误信息到 blade模板的方法

    laravel 验证错误信息到 blade模板的方法

    今天小编就为大家分享一篇laravel 验证错误信息到 blade模板的方法,具有很好的参考价值。希望对大家有所帮助。一起跟随小编过来看看吧
    2019-09-09
  • 小程序微信支付功能配置方法示例详解【基于thinkPHP】

    小程序微信支付功能配置方法示例详解【基于thinkPHP】

    这篇文章主要介绍了小程序微信支付功能配置方法,结合实例形式分析了基于thinkPHP的微信小程序支付功能相关操作技巧与注意事项,需要的朋友可以参考下
    2019-05-05
  • 浅析Yii2缓存的使用

    浅析Yii2缓存的使用

    一个有缓存的框架可以说是一个好的框架。下面小编通过本文给大家介绍yii2中是如何使用缓存的,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-05-05
  • PHP 芝麻信用接入的注意事项

    PHP 芝麻信用接入的注意事项

    本文给大家整理了接入芝麻api借口的两点注意事项,对php 芝麻信用接入感兴趣的朋友通过本文一起学习吧
    2016-12-12
  • PHP统计二维数组元素个数的方法

    PHP统计二维数组元素个数的方法

    数据表里面的字段 content 存储了一个以逗号分割的字符串,最大有20个数,最大数字为40。比如3,24,33,40类似字样的数字序列。其实就是一个保存了多项投票结果的字段啦。现在需要统计每个数字的个数,也就是每个投票项有多少人投了,并排序
    2013-11-11
  • ThinkPHP整合百度Ueditor图文教程

    ThinkPHP整合百度Ueditor图文教程

    这篇文章主要介绍了ThinkPHP整合百度Ueditor的方法,图文并茂,非常的详细,希望对大家能有所帮助
    2014-10-10

最新评论

?


http://www.vxiaotou.com