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


  • 2019-06-03 00:07:32

    Android程序Crash时的异常上报

    家都知道,android应用不可避免的会发生crash,无论你的程序写的多完美,总是无法完全避免crash的发生,可能是由于android系统底层的bug,也可能是由于不充分的机型适配或者是糟糕的网络状况。当crash发生时,系统会kill掉你的程序,表现就是闪退或者程序已停止运行,这对用户来说是很不友好的,也是开发者所不愿意看到的,更糟糕的是,当用户发生了crash,开发者却无法得知程序为何crash,即便你想去解决这个crash,但是由于你无法知道用户当时的crash信息,所以你也无能为力。是否真的这样呢,其实android中有处理这类问题的方法,请看下面Thread类中的一个方法#setDefaultUncaughtExceptionHandler

  • 2019-06-04 16:40:30

    为了美观当网页图片不存在时不显示叉叉图片

    当在页面显示的时候,万一图片被移动了位置或者丢失的话,将会在页面显示一个带X的图片,很是影响用户的体验。即使使用alt属性给出了”图片XX”的提示信息,也起不了多大作用。

  • 2019-06-04 17:38:44

    PHP时间转换今天昨天前天几天前

    实际情况应该是,昨天任何时间都算一天前,前天任意时间都算2天前,所以自己琢磨了一番,去动态更新时间与今天23:59:59相差的时间秒数与86400(24 x 3600)相除后,向下取整,这样就得到了相差的天数,比如昨天00:00~昨天23:59:59的任何时间与今天的23:59:59,都相差 86400~(86400 x 2) 天,也就是2天。

  • 2019-06-04 17:44:14

    【VUE】图片预览放大缩小插件

    在看项目时,突然看到预览图片的弹窗,感觉好僵硬,不能放大,不能切换,于是便在网上找下关于图片预览的插件,有找到三个插件,具体的优劣势的看自己的使用吧,我目前只是在电脑查看效果,分别是viewerjs插件、基于photoswipe的vue-photo-preview插件以及vue-picture-preview插件 作者:北极星丶超帅的 链接:https://www.jianshu.com/p/e3350aa1b0d0 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 2019-06-04 21:32:05

    Android 全局Dialog的简单实现

    这里所说的全局Dialog是指无论当前应用是处于哪一个页面上,都能够及时弹出Dialog来提示用户一些信息,用户体验不会很好,一般应用于优先级非常高的通知。