Ubuntu下使用Nginx部署Laravel
问题描述
Laravel是PHP下当今最受欢迎的web应用开发框架,github上start数远超第二名Symfony,以前我用这个框架做项目的时候通常就是扔到apache里面,然后配置.htaccess文件移除路由里面的public字样,达到Pretty URLs效果,这这两天在完善各个版本的微信墙,准备部署在azure上,结果发现以前装的是nginx,mysql这样的环境,于是乎花了一点时间研究了一下如何部署,便就有了这篇文章,废话少说,上干货:
配置环境
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install nginx php5</span><span class="pun">-</span><span class="pln">fpm php5</span><span class="pun">-</span><span class="pln">cli php5</span><span class="pun">-</span><span class="pln">mcrypt git</span>
这里会安装 nginx 作为web server,同时会安装一些PHP工具,安装git是为了后期部署的时候拉取代码
更改PHP配置
安装完上诉组件之后,我们需要进行一些配置,首先需要打开fpm/php.ini,去更改fix_pathinfo为0
<span class="pln">sudo vim </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">/</span><span class="pln">fpm</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini</span>
<span class="pln">cgi</span><span class="pun">.</span><span class="pln">fix_pathinfo</span><span class="pun">=</span><span class="lit">0</span>
这里的设置是让PHP在请求的文件不在的时候别去尝试执行相似名字的脚本,防止攻击者欺骗PHP去执行一些不应该执行的代码,最后我们需要显式地启用MCrypt扩展并重启php5-fpm 服务以便重新载入让刚才的更改
<span class="pln">sudo php5enmod mcrypt</span>
<span class="pln">sudo service php5</span><span class="pun">-</span><span class="pln">fpm restart</span>
配置Nginx
下面我们要配置一下nginx,里面存在一些路径,这里我是使用apt-get安装的nginx,如果是手动编译安装的话请自寻路径,首先我们要创建一个目录以便放置我们的laravel代码,这里我直接放到/usr/share/nginx/laravel
<span class="pln">sudo mkdir </span><span class="pun">-</span><span class="pln">p </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">laravel</span>
下面需要配置我们的nginx
<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">sites</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="kwd">default</span>
这里你看到的大概是这样的
<span class="pln">server </span><span class="pun">{</span>
<span class="pln">listen </span><span class="lit">80</span><span class="pln"> default_server</span><span class="pun">;</span>
<span class="pln">listen </span><span class="pun">[::]:</span><span class="lit">80</span><span class="pln"> default_server ipv6only</span><span class="pun">=</span><span class="pln">on</span><span class="pun">;</span>
<span class="pln">root </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">html</span><span class="pun">;</span>
<span class="pln">index index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm</span><span class="pun">;</span>
<span class="com"># Make site accessible from http://localhost/</span>
<span class="pln">server_name localhost</span><span class="pun">;</span>
<span class="pln">location </span><span class="pun">/</span><span class="pun">{</span>
<span class="com"># First attempt to serve request as file, then</span>
<span class="com"># as directory, then fall back to displaying a 404.</span>
<span class="pln">try_files $uri $uri</span><span class="pun">/</span><span class="pun">=</span><span class="lit">404</span><span class="pun">;</span>
<span class="com"># Uncomment to enable naxsi on this location</span>
<span class="com"># include /etc/nginx/naxsi.rules</span>
<span class="pun">}</span>
<span class="com">#error_page 404 /404.html;</span>
<span class="com"># redirect server error pages to the static page /50x.html</span>
<span class="com">#</span>
<span class="com">#error_page 500 502 503 504 /50x.html;</span>
<span class="com">#location = /50x.html {</span>
<span class="com"># root /usr/share/nginx/html;</span>
<span class="com">#}</span>
<span class="com">#location ~ \.php$ {</span>
<span class="com">#fastcgi_split_path_info ^(.+\.php)(/.+)$;</span>
<span class="com"># NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini</span>
<span class="com"># With php5-cgi alone:</span>
<span class="com">#fastcgi_pass 127.0.0.1:9000;</span>
<span class="com"># With php5-fpm:</span>
<span class="com">#fastcgi_pass unix:/var/run/php5-fpm.sock;</span>
<span class="com">#fastcgi_index index.php;</span>
<span class="com">#include</span><span class="pln"> fastcgi_params</span><span class="pun">;</span>
<span class="com">#}</span>
<span class="com">#}</span>
需要把它替换成下面的配置文件,其中server_name要替换成你自己的域名或者ip,其中root里面的内容就是刚才我们创建laravel的目录并且多了一个public目录,这里public目录的作用就是去掉我们每次请求laravel路由里面的public,让路由语义更强
<span class="pln">server </span><span class="pun">{</span>
<span class="pln">listen </span><span class="lit">80</span><span class="pln"> default_server</span><span class="pun">;</span>
<span class="pln">listen </span><span class="pun">[::]:</span><span class="lit">80</span><span class="pln"> default_server ipv6only</span><span class="pun">=</span><span class="pln">on</span><span class="pun">;</span>
<span class="pln">root </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">share</span><span class="pun">/</span><span class="pln">nginx</span><span class="pun">/</span><span class="pln">laravel</span><span class="pun">/</span><span class="pln">public</span><span class="pun">;</span>
<span class="pln">index index</span><span class="pun">.</span><span class="pln">php index</span><span class="pun">.</span><span class="pln">html index</span><span class="pun">.</span><span class="pln">htm</span><span class="pun">;</span>
<span class="pln">server_name server_domain_or_IP</span><span class="pun">;</span>
<span class="pln">location </span><span class="pun">/</span><span class="pun">{</span>
<span class="pln">try_files $uri $uri</span><span class="pun">/</span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">?</span><span class="pln">$query_string</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pln">location </span><span class="pun">~</span><span class="pln"> \.php$ </span><span class="pun">{</span>
<span class="pln">try_files $uri </span><span class="pun">/</span><span class="pln">index</span><span class="pun">.</span><span class="pln">php </span><span class="pun">=</span><span class="lit">404</span><span class="pun">;</span>
<span class="pln">fastcgi_split_path_info </span><span class="pun">^(.+</span><span class="pln">\.php</span><span class="pun">)(/.+)</span><span class="pln">$</span><span class="pun">;</span>
<span class="com">#With php5-fpm:</span>
<span class="pln">fastcgi_pass unix</span><span class="pun">:</span><span class="str">/var/</span><span class="pln">run</span><span class="pun">/</span><span class="pln">php5</span><span class="pun">-</span><span class="pln">fpm</span><span class="pun">.</span><span class="pln">sock</span><span class="pun">;</span>
<span class="pln">fastcgi_index index</span><span class="pun">.</span><span class="pln">php</span><span class="pun">;</span>
<span class="pln">fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name</span><span class="pun">;</span>
<span class="pln">include fastcgi_params</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pun">}</span>
做完这些我们的工作基本就完成了,在目录中部署写好的laravel程序,打开绑定的域名就可以看到效果了如下图
后记
今天有人在QQ上问我部署的问题,一般来讲可以使用ftp,在高级一点可以使用sftp,或者搭建svn,不过自从有了git以后,我一般是先在git上创建一个项目,本地开发好push上去,在测试服务器上pull下来,测试通��后,在生产服务器上pull下来,这个足够应对大部分场景了,不过在搭建分布式项目的时候我一般是写一个自动化的脚本去替我完成那些重复的劳动,令最近这阶段心情足够好的时候我会用docker。