问题描述
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程序,打开绑定的域名就可以看到效果了如下图
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30096468/viewspace-1417222/
,如需转载,请注明出处,否则将追究法律责任。