laravel官网提供的安装方式是composer方式,可在官网文档中看到,安装完毕,配置一下,

输入

php artisan serve

然后访问localhost:8000,你也可以看到相对应的Laravel默认页面:

grafana nginx模版id_php

这种方式是使用laravel 自动配置的虚拟站点,
它有如下缺点:

  • 修改代码后每次都要重新部署一遍很麻烦。
  • 如果电脑里有多个laravel 工程文件,默认的端口80需要修改,当然如果是homestead环境另当别论。

Lravel 工程目录结构图:

grafana nginx模版id_虚拟站点配置_02

下面使用nginx来配置虚拟站点,nginx是一个HTTP和反向代理服务器,使用nginx来配置Laravel工程的原理就是:

  1. 输入指定域名,如www.mooe.com
  2. nginx会查找对应的配置文件(Mac 下 Nginx、MySQL、PHP-FPM 的安装配置
  3. 在配置文件nginx.conf中检索laravel根目录下的public文件,在该文件中查找index.php作为首页入口。在.env正确配置后laravel就会自动加载该index.php

下面进入配置文件的设置:

1. 找到nginx对应的目录(我的目录不是nginx默认目录,默认根目录为/usr/local/nginx/)
➜  ~  cd /usr/local/etc/nginx
➜  nginx git:(master) ls
conf.d                
 sites-available
 sites-enabled
 nginx.conf            
➜  nginx git:(master) ```

默认会有nginx.conf,其他三个自己添加的
可以直接在nginx.conf修改,具体可参照nginx.conf

为了能配置多个站点,而且结构清晰,

2.首先先修改nginx.conf
➜  nginx git:(master) sudo vim nginx.conf
user  _www;
worker_processes  1;

#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;



    include /usr/local/etc/nginx/conf.d/*.conf;
    include /usr/local/etc/nginx/sites-enabled/*;
}

我这里的_www是我的一个用户组,权限相当于wheel,
可以看出它是incude了 conf.d目录下的所以.conf文件和sites-enabled下的所有文件

3.下面配置我的www.mooe.com的配置文件,我起名mooe.app,名字任意起的
➜  conf.d git:(master) cat mooe.app 
listen 80;
server_name mooe.com www.mooe.com;
error_log /var/log/nginx/mooeapp/mooe_error.log;
access_log /var/log/nginx/mooeapp/mooe_access.log php-fpm-main;
root   /var/www/cloudlab/cloudlab/src/cloudlab/public/;

这里的root 目录就是你的laravel工程目录下的public文件

➜  conf.d git:(master) cat logs.mooe.app 
server_name logs.mooeapp.com;
error_log /var/log/nginx/shamiapp/logs_error.log;
access_log /var/log/nginx/shamiapp/logs_access.log;
root  /Users/chenlei/cloudlab/cloudlab/src/cloudlab/storage/;
autoindex on;
charset utf-8;

这里的日志根目录是在storage里的

下面进入sites-available里的配置文件,这里之所以要设置site-available是为了设置一个可选列表,方便管理,真正启用配置的文件是在site-enabled里,
意思就是在site-available放所有的配置文件,最终启用哪个,再加到site-enabled才能起作用。

4.配置mooe.conf

sites-available->mooe.conf

➜  nginx git:(master) cd sites-available 
➜  sites-available git:(master) ls
h5.shami.conf   logs.mooe.conf  mooe.conf
img.shami.conf  logs.shami.conf shami.conf
➜  sites-available git:(master) cat mooe.conf 
server
{
    include conf.d/mooe.app; 

    location / {
    root  /var/www/cloudlab/cloudlab/src/cloudlab/public/;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }
#    Prevent any potentially-executable files in the uploads directory from
#    being executed by forcing their MIME type to text/plain
    location ~ ^/(img|upload*)/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }
    location ~ \.php$ {
    root  /var/www/cloudlab/cloudlab/src/cloudlab/public/;
        include fastcgi_params;
        fastcgi_pass    localhost_fastcgi_fpm;
    }
# 临时节约磁盘空间的设置
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log        off;
        expires           30d;
    }
# Block access to data folders
    location ~ /(data|conf|bin|inc)/ {
        deny all;
    }

# Block access to sensitive files
    location ~* \.(bak|swp|swo|txt|config|conf|DS_Store|inc)$ {
        deny all;
    }

# Protect Perl/CGI/etc files
    location ~* \.(pl|cgi|py|sh|lua)$ 
    {
        return 444;
    }

# Block access to all dot files
    location ~ /\. {
        deny  all;
    }
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }  
}
5.配置log.mooe.conf

sites-available->logs.mooe.conf

➜  sites-available git:(master) cat logs.mooe.conf 
server
{
    include conf.d/logs.mooe.app;
    location / {
        default_type text/plain;
    }
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }
}
➜  sites-available git:(master)
6.下面进入到sites-anabled修改mooe.conf和logs.mooe.conf

sites-enabled ->mooe.conf

➜  nginx git:(master) cd sites-enabled 
➜  sites-enabled git:(master) ls
h5.shami.conf   logs.mooe.conf  mooe.conf
img.shami.conf  logs.shami.conf shami.conf
➜  sites-enabled git:(master) cat mooe.conf 
server
{
    include conf.d/mooe.app; 

    location / {
    root  /var/www/cloudlab/cloudlab/src/cloudlab/public/;
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }
#    Prevent any potentially-executable files in the uploads directory from
#    being executed by forcing their MIME type to text/plain
    location ~ ^/(img|upload*)/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
    }
    location ~ \.php$ {
    root  /var/www/cloudlab/cloudlab/src/cloudlab/public/;
        include fastcgi_params;
        fastcgi_pass    localhost_fastcgi_fpm;
    }
# 临时节约磁盘空间的设置
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
        access_log        off;
        expires           30d;
    }
# Block access to data folders
    location ~ /(data|conf|bin|inc)/ {
        deny all;
    }

# Block access to sensitive files
    location ~* \.(bak|swp|swo|txt|config|conf|DS_Store|inc)$ {
        deny all;
    }

# Protect Perl/CGI/etc files
    location ~* \.(pl|cgi|py|sh|lua)$ 
    {
        return 444;
    }

# Block access to all dot files
    location ~ /\. {
        deny  all;
    }
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }  
}

sites-enabled ->logs.mooe.conf

➜  sites-enabled git:(master) cat logs.mooe.conf 
server
{
    include conf.d/logs.mooe.app;
    location / {
        default_type text/plain;
    }
    location = /robots.txt  { access_log off; log_not_found off; }
    location = /favicon.ico { access_log off; log_not_found off; }
}
**7.主要的配置文件修改完毕,修改hosts文件

**

➜  ~  sudo vim /etc/hosts
127.0.0.1   www.mooe.com
127.0.0.1    mooe.com
8.下面重启nginx
➜  ~  sudo nginx -s reload

当然php的服务也要启动,mac自带的php是不行的,需要配置一下,相关配置方法在Mac 下 Nginx、MySQL、PHP-FPM 的安装配置

运行成功后,在地址栏输入www.mooe.com就会跳转到laravel工程文件里写的index.php,渲染对应的主页了。

grafana nginx模版id_php_03

如果没有写相关的blade,自然就是默认欢迎页面,

grafana nginx模版id_php

**9.如果出现500访问资源错误,那么很有可能是Laravel里的权限设置有问题

可设置如下**

➜  cloudlab git:(master) sudo chmod -R 775 bootstrap
➜  cloudlab git:(master) ✗ sudo chmod -R 775 storage
➜  cloudlab git:(master) ✗ sudo chown -R _www:_www bootstrap
➜  cloudlab git:(master) ✗ sudo chown -R _www:_www storage

我这里的_www是nginx 所属组。