ubuntu16+nginx+Laravel5

前端之家收集整理的这篇文章主要介绍了ubuntu16+nginx+Laravel5前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Laravel是基于PHP的一个开源MVC框架,优点不少,缺点也有。这里就不细说,专注于Laravel的安装。由于国内被墙的关系,不能直接使用composer直接安装,本文主要展开讲解的是使用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,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,就表示大功告成。恭喜恭喜。。。。

原文链接:https://www.f2er.com/ubuntu/355346.html

猜你在找的Ubuntu相关文章