问题描述
Laravel是PHP下当今最受欢迎的web应用开发框架,github上start数远超第二名Symfony,以前我用这个框架做项目的时候通常就是扔到apache里面,然后配置.htaccess文件移除路由里面的public字样,达到Pretty URLs效果,这这两天在完善各个版本的微信墙,准备部署在azure上,结果发现以前装的是nginx,mysql这样的环境,于是乎花了一点时间研究了一下如何部署,便就有了这篇文章,废话少说,上干货:
配置环境
sudo apt-get install nginx php5-fpm php5-cli php5-mcrypt git
这里会安装 nginx 作为web server,同时会安装一些PHP工具,安装git是为了后期部署的时候拉取代码
更改PHP配置
安装完上诉组件之后,我们需要进行一些配置,首先需要打开fpm/php.ini,去更改fix_pathinfo为0
sudo vim /etc/php5/fpm/php.inicgi.fix_pathinfo=0
这里的设置是让PHP在请求的文件不在的时候别去尝试执行相似名字的脚本,防止攻击者欺骗PHP去执行一些不应该执行的代码,最后我们需要显式地启用MCrypt扩展并重启php5-fpm 服务以便重新载入让刚才的更改
sudo php5enmod mcryptsudo service php5-fpm restart
配置Nginx
下面我们要配置一下nginx,里面存在一些路径,这里我是使用apt-get安装的nginx,如果是手动编译安装的话请自寻路径,首先我们要创建一个目录以便放置我们的laravel代码,这里我直接放到/usr/share/nginx/laravel
sudo mkdir -p /usr/share/nginx/laravel
下面需要配置我们的nginx
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。
Ubuntu 14.04 上使用 Nginx 部署 Laravel 5.0 http://www.linuxidc.com/Linux/2015-08/121986.htm
更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-08/121988.htm
