Chinaunix首页 | 论坛 | 博客
  • 博客访问: 253793
  • 博文数量: 159
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1619
  • 用 户 组: 普通用户
  • 注册时间: 2016-01-10 19:58
文章分类

全部博文(159)

分类: Web开发

2017-02-19 21:40:15

缘由

经常会有人问xx框架怎么配置运行环境,这里我就给贴出吉祥三宝(Yii2,Laravel5,Thinkphp5 )的Nginx和Apache的配置,供大家参考

Nginx

Yii2

server {    
    charset utf-8;    
    client_max_body_size 128M;    
    listen 80;    
    server_name yii.local.test;    
    root  /home/www/yii2/web;    
    index  index.php;    
    
    location ~* \.(eot|otf|ttf|woff)$ {    
        add_header Access-Control-Allow-Origin *;    
    }    
    
    location / {    
        try_files $uri $uri/ /index.php?$args;    
    }   
     
    location ~ \.php$ {    
        include   fastcgi_params;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;    
        fastcgi_pass   127.0.0.1:9000;    
        try_files $uri =404;    
    }    
}

Laravel5

server {    
    charset utf-8;    
    client_max_body_size 128M;    
    listen 80;    
    server_name laravel.local.test;    
    root  /home/www/laravel/public;    
    index  index.php;    
    
    location ~* \.(eot|otf|ttf|woff)$ {    
        add_header Access-Control-Allow-Origin *;    
    }    
    
    location / {    
        try_files $uri $uri/ /index.php?$args;    
    }   
     
    location ~ \.php$ {    
        include   fastcgi_params;
        fastcgi_index    index.php;
        fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;    
        fastcgi_pass   127.0.0.1:9000;    
        try_files $uri =404;    
    }    
}

ThinkPHP5

server {    
    charset utf-8;    
    client_max_body_size 128M;    
    listen 80;    
    server_name tp5.local.test;    
    root  /home/www/tp5/public;    
    index  index.php;    
    
    location ~* \.(eot|otf|ttf|woff)$ {    
        add_header Access-Control-Allow-Origin *;    
    }    
    
    location / {    
        index    index.html index.php;    
        if ( -f $request_filename) {    
            break;    
        } 
       
        if ( !-e $request_filename) {    
            rewrite ^/(.*)$ /index.php/$1 last;    
            break;    
        }    
    }    
    
    location ~ \.php {    
        set $script $uri;    
        set $path_info "";    
        if ($uri ~ "^(.+\.php)(/.+)") {    
            set $script $1;    
            set $path_info $2;    
        }    
    include   fastcgi_params;    
    fastcgi_index    index.php?IF_REWRITE=1;    
    fastcgi_pass   127.0.0.1:9000;    
    fastcgi_param    PATH_INFO    $path_info;    
    fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;    
    fastcgi_param    SCRIPT_NAME    $script;    
    try_files $uri =404;    
    }    
}

PS:tp5在nginx支持这块做的不够好,因为tp框架需要依赖一个服务变量 path_info ,这个变量nginx已经不再使用了,所以需要自己定义

Apache

Yii2

    
   	ServerName yii.local.test    
   	DocumentRoot /home/www/yii2/web    
   	#    
            #RewriteEngine on    
       	    #RewriteCond %{REQUEST_FILENAME} !-f    
       	    #RewriteCond %{REQUEST_FILENAME} !-d    
       	    #RewriteRule . index.php    
   	#       

PS: .htaccess 代码如下

RewriteEngine on    
# If a directory or a file exists, use it directly    
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
# Otherwise forward it to index.php    
RewriteRule . index.php

Laravel5

    
       	ServerName laravel.local.test    
       	DocumentRoot /home/www/laravel/public    
       	#    
            #RewriteEngine on    
       	    #RewriteCond %{REQUEST_FILENAME} !-f    
       	    #RewriteCond %{REQUEST_FILENAME} !-d    
       	    #RewriteRule . index.php    
       	#    

PS: .htaccess 代码如下

    
        
        Options -MultiViews    
        
    RewriteEngine On    
    # Redirect Trailing Slashes If Not A Folder...    
    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteRule ^(.*)/$ /$1 [L,R=301]    
    # Handle Front Controller...    
    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteCond %{REQUEST_FILENAME} !-f    
    RewriteRule ^ index.php [L]    

ThinkPHP5

    
   	ServerName tp5.local.test    
   	DocumentRoot /home/www/tp5/public/     

PS: .htaccess 代码如下

    
    Options +FollowSymlinks -Multiviews    
    RewriteEngine On    
    RewriteCond %{REQUEST_FILENAME} !-d    
    RewriteCond %{REQUEST_FILENAME} !-f    
    RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]    

最后

希望对大家有帮助

原文地址:标签:                                 

智能推荐

阅读(822) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~