Laravel作爲重量級的一款PHP框架,代碼簡潔優雅,如果想更深入的學習PHP建議瞭解Laravel這款框架。下面對Laravel做一個測試環境部署。
1、服務器環境(我這裏選取Laravel 6.x版本,操作系統是mac並未使用虛擬機,官網教程使用了虛擬機https://laravel.com/docs/6.x)
我這裏使用mac + nginx1.15 + php7.2 +mysql5.6(這裏不詳細說明、百度或查看我之前的博客),如下圖
然後php擴展支持如下:
BCMath PHP Extension
Ctype PHP Extension
Fileinfo PHP extension
JSON PHP Extension
Mbstring PHP Extension
OpenSSL PHP Extension
PDO PHP Extension
Tokenizer PHP Extension
XML PHP Extension
2、創建一個項目文件夾,mac中控制檯進入該目錄
利用composer創建Laravel項目,命令如下:
$ composer create-project laravel/laravel wwwlaravel --prefer-dist "6.*"
3、在nginx中配置laravel,將目錄指向laravel入口目錄:public
找到nginx中的*.conf文件,修改www.conf(或者修改nginx.conf,一般www.conf會被引用到nginx.conf中)
我這裏修改www.conf 如下:
server {
listen 8088;
server_name localhost;#charset koi8-r; #access_log logs/host.access.log main;
root /usr/local/var/wwwlaravel/public;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$query_string;}
#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 /usr/local/var/wwwlaravel/public; } # 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(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params;
}# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
4、修改storage目錄中所有文件的權限爲777,否則無法寫入日誌文件會報錯
直接在改目錄下執行:
$ sudo chmod -R 777 .
5、在第三步設置的端口爲8088,直接訪問http://127.0.0.1:8088/ ,如下:
6、文件夾名稱對應簡介
app 應用程序的業務邏輯代碼存放文件夾
app/Console 存放自定義 Artisan 命令文件
app/Http/Controllers 存放控制器文件
app/Http/Middleware 存放「中間件」文件
bootstrap 框架啓動與自動加載設置相關的文件
composer.json 應用依賴的擴展包
composer.lock 擴展包列表,確保這個應用的副本使用相同版本的擴展包
config 應用程序的配置文件
database 數據庫操作相關文件(數據庫遷移和數據填充)
node_modules 存放 NPM 依賴模塊
package.json 應用所需的 NPM 包配置文件
phpunit.xml 測試工具 PHPUnit 的配置文件
public 前端控制器和資源相關文件(圖片、JavaScript、CSS)
readme.md 項目介紹說明文件
resources 應用資源
resources/js 未編譯的 JavaScript 代碼
resources/sass 未編譯的 SASS 代碼 (將會編譯爲 CSS )
resources/lang 多語言文件
resources/views 視圖文件
routes/api.php 用於定義 API 類型的路由
routes/channels.php 事件廣播註冊信息
routes/console.php 用於定義 Artisan 命令
routes/web.php 用於定義 Web 類型的路由(重點,大部分情況下本書會用到)
server.php 使用 PHP 內置服務器時的 URL 重寫(類似於 Apache 的 "mod_rewrite" )
storage 編譯後的視圖、基於會話、文件緩存和其它框架生成的文件
storage/app 目錄可用於存儲應用程序使用的任何文件
storage/framework 目錄被用於保存框架生成的文件及緩存
storage/logs 應用程序的日誌文件
tests 應用測試相關文件
vendor Composer 依賴模塊
webpack.mix.js Laravel 的前端工作流配置文件
yarn.lock Yarn 依賴版本鎖定文件
.gitignore 被 Git 所忽略的文件
.env 環境變量配置文件