問題描述php
Laravel是PHP下當今最受歡迎的web應用開發框架,github上start數遠超第二名Symfony,之前我用這個框架作項目的時候一般就是扔到apache裏面,而後配置.htaccess文件移除路由裏面的public字樣,達到Pretty URLs效果,這這兩天在完善各個版本的微信牆,準備部署在azure上,結果發現之前裝的是nginx,mysql這樣的環境,因而乎花了一點時間研究了一下如何部署,便就有了這篇文章,廢話少說,上乾貨:html
配置環境mysql
sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt git
這裏會安裝 nginx 做爲web server,同時會安裝一些PHP工具,安裝git是爲了後期部署的時候拉取代碼 nginx
更改PHP配置laravel
安裝完上訴組件以後,咱們須要進行一些配置,首先須要打開fpm/php.ini,去更改fix_pathinfo爲0git
sudo vim /etc/php5/fpm/php.inicgi.fix_pathinfo=0
這裏的設置是讓PHP在請求的文件不在的時候別去嘗試執行類似名字的腳本,防止攻擊者欺騙PHP去執行一些不該該執行的代碼,最後咱們須要顯式地啓用MCrypt擴展並重啓php5-fpm 服務以便從新載入讓剛纔的更改github
sudo php5enmod mcryptsudo service php5-fpm restart
配置Nginxweb
下面咱們要配置一下nginx,裏面存在一些路徑,這裏我是使用apt-get安裝的nginx,若是是手動編譯安裝的話請自尋路徑,首先咱們要建立一個目錄以便放置咱們的laravel代碼,這裏我直接放到/usr/share/nginx/laravelsql
sudo mkdir -p /usr/share/nginx/laravel
下面須要配置咱們的nginxdocker
sudo nano /etc/nginx/sites-available/default
這裏你看到的大概是這樣的
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } #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/share/nginx/html; #} #location ~ \.php$ { #fastcgi_split_path_info ^(.+\.php)(/.+)$; # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini # With php5-cgi alone: #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: #fastcgi_pass unix:/var/run/php5-fpm.sock; #fastcgi_index index.php; #include fastcgi_params; #} #}
須要把它替換成下面的配置文件,其中server_name要替換成你本身的域名或者ip,其中root裏面的內容就是剛纔咱們建立laravel的目錄而且多了一個public目錄,這裏public目錄的做用就是去掉咱們每次請求laravel路由裏面的public,讓路由語義更強
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/laravel/public; index index.php index.html index.htm; server_name server_domain_or_IP; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { try_files $uri /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; #With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
作完這些咱們的工做基本就完成了,在目錄中部署寫好的laravel程序,打開綁定的域名就能夠看到效果了以下圖
後記
今天有人在QQ上問我部署的問題,通常來說可使用ftp,在高級一點可使用sftp,或者搭建svn,不過自從有了git之後,我通常是先在git上建立一個項目,本地開發好push上去,在測試服務器上pull下來,測試經過後,在生產服務器上pull下來,這個足夠應對大部分場景了,不過在搭建分佈式項目的時候我通常是寫一個自動化的腳本去替我完成那些重複的勞動,令最近這階段心情足夠好的時候我會用docker。
注;github上面公開項目是免費的,可是私有項目要花錢,並且這兩天回家我家的網也連不上github了,因此國內我推薦作‘不可告人’的項目的時候使用oschina的git,速度至關贊,並且每一個帳號能夠免費建立1000個項目,公開或者私有都可。這半年的新項目以下圖都放到了oschina上了,.NET項目也放棄了TFS(由於速度緣由,有時會disconnect),除了偶爾的一些衝突vs無法解決須要上git的命令行,體驗仍是至關不錯的