红联Linux门户
Linux帮助

ubuntu16+nginx+Laravel5架设

发布时间:2016-12-21 09:40:55来源:linux网站作者:踏雪_无痕
Laravel是基于PHP的一个开源MVC框架,优点不少,缺点也有。这里就不细说,专注于Laravel的安装。由于国内的关系,不能直接使用composer直接安装,本文主要展开讲解的是使用Laravel提供的一键安装包(http://www.golaravel.com/download/)。
ubuntu16+nginx+Laravel5架设
 
下载Laravel一键安装包
下载地址(https://github.com/laravel/laravel),注意不同版本对PHP版本有要求,本文下载的版本是laravel-v5.2.15
 
安装PHP和对应的扩展
本文使用的Laravel版本是laravel-v5.2.15,对PHP版本要求如下:
PHP >= 5.6.4
OpenSSL PHP Extension
PDO PHP Extension
Mbstring PHP Extension
Tokenizer PHP Extension
XML PHP Extension
上边的PHP扩展有兴趣的可以研究一下是干什么的,这里不细说,有什么疑惑可以共同探讨。
本文使用的是ubuntu16.04,使用下面的命令安装PHP:
sudo apt-get install php
当前默认的版本是php7.0。然后使用下面的命令安装几个扩展:
sudo apt-get install php-fpm php-cli php-mcrypt
 
安装nginx
本文使用的是 nginx/1.10.0 (Ubuntu),使用下面的命令安装即可:
sudo apt-get install nginx
 
修改PHP配置
主要修改的是php.ini文件中的cgi.fix_pathinfo=1,默认是1,需要修改为0。执行下面的命令即可:
sudo gedit /etc/php/7.0/fpm/php.ini
找到 cgi.fix_pathinfo=1 修改为 cgi.fix_pathinfo=0
然后再执行下面的命令启动PHP:
phpenmod mcrypt
service php7.0-fpm restart
 
修改Nginx配置文件
Nginx安装好后默认的web发布路径是/var/www/html,使用下面的命令新建一个Laravel的目录。
cd /var/www/html
uzip laravel-v5.2.15.zip
mv laravel-v5.2.15 laravel
执行完上面的命令后就完成了发布laravel到Nginx服务器,但是现在还不能正常打开网页,需要再Nginx中对Laravel进行配置。先看看Nginx默认的配置文件如下:
sudo gedit /etc/nginx/sites-available/default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
**root /var/www/html/;**
# Add index.php to the list if you are using PHP
**index index.html index.htm index.nginx-debian.html;**
**server_name _;**
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
**try_files $uri $uri/=404;**
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
**#location ~ \.php$ {
#   include snippets/fastcgi-php.conf;
#
#   # With php7.0-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php7.0-fpm:
#   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}**
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
修改代码,修改成如下代码:
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
 
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html/laravel/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
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/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   include snippets/fastcgi-php.conf;
#
#   # With php7.0-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php7.0-fpm:
#   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#       try_files $uri $uri/ =404;
#   }
#}
最后再重新启动Nginx服务:
sudo service nginx restart
 
修改Laravel目录权限
sudo chown -R :www-data /var/www/html/laravel
sudo chmod -R 775 /var/www/html/laravel/storage
 
生成秘钥
cd /var/www/html/laravel
sudo php artisan key:generate
 
收获的季节
在浏览器中输入 http://localhost 即可看到一个页面中显示Laravel5,就表示大功告成。恭喜恭喜。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/27094.html