使用NGINX部署Laravel项目详解

版权声明:尊重原创喔,转载请注明 https://blog.csdn.net/lgyaxx/article/details/79507525

注意:本文系统环境Ubuntu 16.04 LTS,已安装php7.2。我的Laravel项目文件位置/var/www/myproject

在阅读文章步骤之前,如果你目前正在运行Apache的话,请输入

$ sudo systemctl stop apache2

来停止Apache的运行。



那么我们开始,首先我们先安装 NGINX

$ sudo apt-get update
$ sudo apt-get install nginx

完成之后,我们配置一下防火墙:

$ sudo ufw app list

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

可以看到,有三个应用可以选择,我们这里使用Nginx Full(当然,如果不使用SSL你也完全可以选择HTTP):

$ ufw allow "Nginx Full"

由于Nginx不能原生处理PHP,我们需要安装php-fpm。我安装的php版本为7.2,所以我这里选择安装php7.2-fpm

$ sudo apt-get install php7.2-fpm

请根据你服务器php的版本号来选择相应fpm。安装完成后,我们需要对php进行一个安全配置:

$ sudo vim /etc/php/7.2/fpm/php.ini

找到

# cgi.fix_pathinfo=1

#去掉,并将1改为0

cgi.fix_pathinfo=0

保存退出。接下来重启php-fpm

扫描二维码关注公众号,回复: 3857430 查看本文章
$ sudo systemctl restart php7.2-fpm



好了,现在 php配置完成, nginx安装完成,我们需要配置 vhost,即 nginx中的 server block

这里有个概念先讲解一下,类似Apache的配置,Nginx配置中也存在sites-available这个文件夹,不过与Apache略有不同的是,Nginx配置完vhost后,使用symbolic link将配置文件链接至sites-enabled文件夹,这个过程与我们在Apache中使用a2ensite命令将我们的站点激活非常类似。

那么我们首先打开Nginx配置:

$ cd /etc/nginx/sites-available
$ cp default mysite.com

可以看到,我们将sites-available文件夹下的default配置文件拷贝后,生成了我们需要修改的名为mysite.com的配置文件。打开这个文件:

$ vim mysite.com

可以看到,里面有一段这样的配置:

# 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;
#       }
#}

我们将除了以上配置的其余配置全部删除,然后将以上配置反注释。接下来,我们需要修改其中的配置。

我在文章头部已经说过,我的Laravel项目文件在/var/www/myproject中,我们假设我们将要使用mysite.com这个域名作为我们网站地址。那么我们将以上配置做出修改如下:

server {
        listen 80;
        listen [::]:80;

        root /var/www/myproject/public;

        index index.php;

        server_name mysite.com www.mysite.com;

        location / {
                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;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

可以看到,我们在server_name中加上了mysite.comwww.mysite.com这个alias,所以两者都可以用来访问我们的项目。

index后面我们添加了index.php,因为我们需要php来动态加载页面。

接下来我们看到

        location / {
                try_files $uri $uri/ /index.php?$query_string;
        }

这段location配置非常重要,注意我们在try_files的最后,添加了/index.php?$query_string。这一步非常重要,因为为了使Laravel正常工作,所有的请求都应该被传递给Laravel本身,即所有的请求都被传递给了index.phpLaravel的应用主文件。如果这一步没有配置,那么我们只能够打开项目主页,其余页面将无法跳转。

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

这一段中我们设置好php-fpm的相关配置。然后保存退出。

接下来我们输入

$ sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/

以激活我们的网站。注意:这里一定要使用绝对路径,而不能使用相对路径(例如../sites-available),切记。

完成后,我们重启Nginx

$ sudo systemctl restart nginx

好了,这样一来,mysite.com就成功地被指向我们的项目地址,并且nginx可以正常处理请求加载出页面了。

猜你喜欢

转载自blog.csdn.net/lgyaxx/article/details/79507525