搜索

查看: 3099|回复: 11

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

[复制链接]
发表于 2023-5-4 17:03:35 | 显示全部楼层 |阅读模式
Editor 2023-5-4 17:03:35 3099 11 看全部
目录
  • 摘要:
  • 系统:mac、linux
  • 配置多个服务:
  • 总结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服务内容请搜索知鸟论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持知鸟论坛
  • 回复

    使用道具 举报

    发表于 2023-6-29 09:22:08 | 显示全部楼层
    xinting_6ym 2023-6-29 09:22:08 看全部
    我看不错噢 谢谢楼主!知鸟论坛越来越好!
    回复

    使用道具 举报

    发表于 2023-6-29 14:59:48 | 显示全部楼层
    123456868 2023-6-29 14:59:48 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-6-29 16:38:12 | 显示全部楼层
    我是的十八簿 2023-6-29 16:38:12 看全部
    楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
    回复

    使用道具 举报

    发表于 2023-6-29 16:57:51 | 显示全部楼层
    老橡树1 2023-6-29 16:57:51 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-30 06:44:42 | 显示全部楼层
    我的苦恼冉 2023-6-30 06:44:42 看全部
    楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-7-4 11:32:09 | 显示全部楼层
    风吹吹蛋蛋疼风w 2023-7-4 11:32:09 看全部
    楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
    回复

    使用道具 举报

    发表于 2023-7-4 23:13:14 | 显示全部楼层
    123456809 2023-7-4 23:13:14 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-7-5 01:32:55 | 显示全部楼层
    李志敏 2023-7-5 01:32:55 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    发表于 2023-7-5 08:44:43 | 显示全部楼层
    素色流年783 2023-7-5 08:44:43 看全部
    其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
    回复

    使用道具 举报

    • 您可能感兴趣
    点击右侧快捷回复 【请勿灌水】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则 返回列表

    RSS订阅| SiteMap| 小黑屋| 知鸟论坛
    联系邮箱E-mail:zniao@foxmail.com
    快速回复 返回顶部 返回列表