使用NGINX部署Laravel項目詳解

注意:本文系統環境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

$ 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可以正常處理請求加載出頁面了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章