Thinkphp5项目在nginx服务器部署

Thinkphp5项目在nginx服务器部署,第1张

1,切换到nginx的配置目录,找到nginx.conf文件

    cd   /usr/local/nginx/conf

    vim  nginx.conf

2,如果是单项目部署的话,只需要在nginx.conf文件里面加上以下

server{

        listen 80

        # 域名,本地测试可以使用127.0.0.1或localhost

        server_name www.zhangc.cn

        # php项目根目录

        root /home/data-www/blog

        location /{

                # 定义首页索引文件的名称

                index index.php index.html index.htm

                # 影藏入口文件

                if (-f $request_filename/index.html){

                            rewrite (.*) $1/index.html break

                }

                if (-f $request_filename/index.php){

                            rewrite (.*) $1/index.php

                }

                if (!-f $request_filename){

                            rewrite (.*) /index.php

                }

                try_files $uri $uri/ /index.php?$query_string

        }

        # PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.

        # Fastcgi服务器和程序(PHP)沟通的协议

        .location ~ .*\.php${

                # 设置监听端口

                fastcgi_pass 127.0.0.1:9000

                # 设置nginx的默认首页文件

                fastcgi_index index.php

                # 设置脚本文件请求的路径

                fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name

                # 引入fastcgi的配置文件

                includefastcgi_params

                fastcgi_split_path_info ^(.+?\.php)(/.*)$

                set $path_info $fastcgi_path_info

                fastcgi_param PATH_INFO $path_info

                try_files $fastcgi_script_name =404

        }

}

3,如果多项目部署,就需要配置vhost

第一步:编辑nginx.conf文件,在最后加上     include    vhost/*.conf

第二步:进入vhost文件夹,创建    域名.conf    文件,如创建一个:quanma.meyat.com.conf

第三步:编辑quanma.meyat.com.conf文件,内容如下:

        server

        {

                listen 80

                server_name quanma.meyat.com

                index index.html index.htm index.php default.html default.htm default.php

                root /data/wwwroot/default/quanma/public/

                #error_page 404 /404.html

                location / {

                        index index.html index.php

                        if (-f $request_filename/index.html){

                                rewrite (.*) $1/index.html break

                        }

                        if (-f $request_filename/index.php){

                                rewrite (.*) $1/index.php

                        }

                        if (!-f $request_filename){

                                rewrite (.*) /index.php

                        }

                        try_files $uri $uri/ /index.php?$query_string

                }

                location ~ [^/]\.php(/|$)

                {

                        # comment try_files $uri =404to enable pathinfo

                        #try_files $uri =404

                        fastcgi_pass 127.0.0.1:9000

                        fastcgi_index index.php

                        include fastcgi_params

                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

                        fastcgi_split_path_info ^(.+?\.php)(/.*)$

                        set $path_info $fastcgi_path_info

                        fastcgi_param PATH_INFO $path_info

                        try_files $fastcgi_script_name =404

                        #include fastcgi.conf

                        #include pathinfo.conf

            }

            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

            {

                    expires 30d

            }

            location ~ .*\.(js|css)?$

            {

                    expires 12h

            }

            # Disallow access to .ht, .svn, .bzr, .git, .hg, .cvs directories

            location ~ /\.(ht|svn|bzr|git|hg|cvs) {

                    deny all

            }

            #access_log /date/nginx/bmp.com.conf/access.log main

}

Nginx与PHP的两种通信方式-unix socket和tcp socket

1、两者Nginx配置

unix socket

需要在nginx配置文件中填写php-fpm运行的pid文件地址。

location ~ \.php$ {

    include fastcgi_params

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

    fastcgi_pass unix:/var/run/php5-fpm.sock

    fastcgi_index index.php

}

tcp socket

需要在nginx配置文件中填写php-fpm运行的ip地址和端口号。

location ~ \.php$ {

    include fastcgi_params

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name

    fastcgi_pass 127.0.0.1:9000

    fastcgi_index index.php

}

2、两者比较

从上面的图片可以看,unix socket减少了不必要的tcp开销,而tcp需要经过loopback,还要申请临时端口和tcp相关资源。但是,unix socket高并发时候不稳定,连接数爆发时,会产生大量的长时缓存,在没有面向连接协议的支撑下,大数据包可能会直接出错不返回异常。tcp这样的面向连接的协议,多少可以保证通信的正确性和完整性。

3、选择建议:如果是在同一台服务器上运行的nginx和php-fpm,并发量不超过1000,选择unix socket,因为是本地,可以避免一些检查操作(路由等),因此更快,更轻。 如果面临高并发业务,我会选择使用更可靠的tcp socket,以负载均衡、内核优化等运维手段维持效率。


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/251388.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存