Mac 本地 nginx + php + laravel

2019-12-01 08:19:48

参考地址 Mac 本地 nginx + php + laravel


Mac 本地 nginx + php + laravel


首先,我为了做一个自己的测试,所以搞了一个laravel框架,整理一些自己笔记


Mac自带apache,所以composer安装好laravel之后,直接进入到laravel/public就可以直接访问laravel的欢迎页面了。

因为入口文件在 public/index.php,index.php被自动识别。

这时的访问路径是:localhost/laravel/public(路径1)


但是当你写了一个controller文件,并在路由routes/web.php 编辑之后,如:


Route::middleware('auth')->group(function () {


    Route::get('/', 'IndexController@index');


});


Route::get('/login','LoginController@showLoginForm')->name('login');

1

2

3

4

5

6

7

LoginController.php 中的代码如下(测试输出 hello word):



<?php


namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;


class LoginController extends Controller

{


    /**

     * [showLoginForm 登陆首页,展示html页面]

     * @return [type] [description]

     */

    public function showLoginForm()

    {

        echo "hello word";die;

        return view('login/showloginform');

    }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

会发现,访问不通了。

这时的访问路径是:localhost/public/login(路径2)。


那么是什么原因呢,经过查询之后,在知道原来是路径不对,正确的路径应该是:

localhost/public/index.php/login(路径3)。

这时候访问路径3发现,咦~好了!


但是,怎么让index.php隐藏起来不显示呢?因为路由默认不会带上index.php的。

那么,现在需要在apache配置本地独立域名了。


编辑 :


sudo vim /etc/apache2/httpd.conf

1

新增代码如下:


<VirtualHost *:80>

    ServerName laravel.com

    DocumentRoot "/Users/zhangsan/projects/laravel/public/index.php"

    <Directory "/Users/zhangsani/projects/laravel/public/index.php">


        Options FollowSymLinks Multiviews Indexes

        MultiviewsMatch Any

        AllowOverride None


        Require all granted

    </Directory>

</VirtualHost>

1

2

3

4

5

6

7

8

9

10

11

12

编辑完重启apache :


sudo apachectl restart

1

这时候,访问(路径4) :laravel.com/login

发现可以输出了。


接下来,把LoginController文件中的echo “hello word”;die;注释掉,渲染视图(view)会发现样式没有引入。

我滴个神啊~~~


视图代码如下(部分代码):


<!DOCTYPE html>

<html>

<head>

    <meta charset="utf-8">

    <title>开始使用</title>

    <link rel="stylesheet" href="{{ URL::asset('./layui/css/layui.css') }}">

    <script type="text/javascript" src="{{ URL::asset('./layui/layui.js') }}"></script>

</head>

<body>

.........

.........

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

这是由于你在view层引入css/js文件时 {{ URL::asset(".……") }}

asset 默认的路径是 你的独立域名下,

所以 引入的路径是 localhost/laravel/public/idnex.php/layui/css/layui.css,

很明显不正确,index.php 不应该出现(localhost/laravel/public/layui/css/layui.css)。

因此独立域名还不能那么配置。

烦死了~


因此独立域名还是要指到 public下,不带index.php

DocumentRoot "/Users/zhangsan/projects/laravel/public"


那么问题来了,这时候会出现冲突

访问浏览器不通,因为路由缺少index.php

但是页面样式可以显示了,因为独立域名去掉了index.php

一边是需要有index.php,一边是不让有index.php

此时应该如何解决?

办法:apache规则重写,把URL 重写一下带上 index.php。

这时候访问浏览器地址路由就OK了,页面的样式加载也没问题。


在这里我没搞apache的重写,直接去搞直接上ngixn。参考【Mac下安装nginx】


1、查询要安装的软件是否存在


brew search nginx

1

2、查看信息,可以看到nginx在本地还未安装(Not installed)


brew info nginx  

1

3、开始安装


brew install nginx

1

现在安装完了,打开浏览器访问localhost:8080;出现nginx欢迎页面。

如果没有出现,一般是因为安装的nginx会默认在html(也就是/usr/local/var/www)没有生成index.html;

如果还不行,请自行Google。


现在开始配置 nginx.conf(/usr/local/etc/nginx/nginx.conf)


首先我在nginx.conf文件的http里面增加:


# 开启目录文件列表

autoindex on;

# 显示出文件的确切大小,单位是bytes

autoindex_exact_size off;

# 显示的文件时间为文件的服务器时间

autoindex_localtime on;

#设置字符集

charset utf-8,gbk;

1

2

3

4

5

6

7

8

server的配置如下:


server {

    listen       80;

    server_name  localhost;


    #charset koi8-r;


    #access_log  logs/host.access.log  main;


    location / {

        root   /Users/zhangsan/projects/;

        index index.php 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           /Users/zhangsan/projects/;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$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;

    #}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

1、端口改成 80;

2、根目录改变成自己的地址(/Users/zhangsan/projects/),

3、并让它识别index.php


location / {

    root   /Users/zhangsan/projects/;

    index index.php index.html index.htm;

}

1

2

3

4

然后把下面关于PHP的配置打开,路径设成自己( /Users/zhangsan/projects/),fastcgi_param 后面的参数也改一下 $document_root


location ~ \.php$ {

    root           /Users/zhangsan/projects/;

    fastcgi_pass   127.0.0.1:9000;

    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

    include        fastcgi_params;

}

1

2

3

4

5

6

7

这时候在你的根目录创建一个index.php文件已经可以识别了


<?php


    phpinfo();


?>

1

2

3

4

5

现在继续上面的laravel问题,开始配置独立域名;


在nginx.conf 里面有一行include servers/*;


我把它改成了 include servers/.conf*;


在同级目录下创建文件夹servers,并在改文件夹下创建文件laravel.conf


文件配置如下:


server {

    listen       80;

    server_name  laravel.com;


    location / {

        root   /Users/zhangsan/projects/laravel/public;

        index index.php index.html index.htm;

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

    }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #

    location ~ \.php$ {

        root           /Users/zhangsan/projects/laravel/public;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;

    }


}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

注意:这里有一行


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

1

目的是让URL地址重写,默认路由的时候自动带上index.php


重启nginx


/usr/local/Cellar/nginx/1.17.1/bin/nginx -s reload

1

现在无论浏览器访问地址,还是view记载样式,都各不冲突了。

浏览器地址:laravel.com/login 其实是:localhost/laravel/public/idnex.php/login(重写了)

view加载的样式 URL::asset(’./layui/css/layui.css’) 其实是:localhost/laravel/public/layui/css/layui.css


番外篇:


上面安装完nginx后,其实现在还不能使用,因为nginx使用的是php-fpm ,

所以还需要启动php-fpm


sudo vim /private/etc/php-fpm.conf

1

把 pid 和 error_log 对用的那一行(18行,26行),前面 ;去掉

然后把 = 后面的值改成


pid = /usr/local/var/run/php-fpm.pid

error_log = /usr/local/var/log/php-fpm.log

1

2

这时候 启动php-fpm(sudo php-fpm)报错;


执行下面的:


cd /private/etc/php-fpm.d

sudo cp www.conf.default www.conf

1

2

现在启动成功。



————————————————

版权声明:本文为CSDN博主「天尽头流浪」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/tianjintouliulang/article/details/95738301


  • 2020-12-21 09:42:42

    iframe.contentWindow 操作iframe

    注:iframe.contentWindow这里,返回的是iframe的window对象,所以后面可以接着调用document方法,再接着调用getElementByTagName。那么就可以对iframe里面的元素进行操作了。

  • 2020-12-21 14:00:19

    iframe + postMessage跨域通信

    在实际项目开发中可能会碰到在 a.com 页面中嵌套 b.com 页面,这时第一反应是使用 iframe,但是产品又提出在 a.com 中操作,b.com 中进行显示,或者相反。

  • 2020-12-22 12:02:41

    ios开发优秀的开源框架,demo集合

    期待大家和我们一起共同维护,同时也期望大家随时能提出宝贵的意见(直接提交issues即可)。请广大网友只按照目录结构(即使目录结构有问题)添加三方库,并提交pull request。目录问题大家提出issues后楼主会及时更改的。