问题描述
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
.
ini
-
cgi
.
fix_pathinfo
=
0
这里的设置是让PHP在请求的文件不在的时候别去尝试执行相似名字的脚本,防止攻击者欺骗PHP去执行一些不应该执行的代码,最后我们需要显式地启用MCrypt扩展并重启php5-fpm 服务以便重新载入让刚才的更改
-
sudo php5enmod mcrypt
-
sudo 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。
注;github上面公开项目是免费的,但是私有项目要花钱,而且这两天回家我家的网也连不上github了,所以国内我推荐做‘不可告人’的项目的时候使用oschina的git,速度相当赞,而且每个账号可以免费创建1000个项目,公开或者私有均可。这半年的新项目如下图都放到了oschina上了,.NET项目也放弃了TFS(因为速度原因,有时会disconnect),除了偶尔的一些冲突vs没法解决需要上git的命令行,体验还是相当不错的
原文:http://www.cnblogs.com/yeanzhi/p/4231040.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!