1. 程式人生 > >ubuntu部署nginx

ubuntu部署nginx

先更新本機內建的程式。

sudo apt-get update
sudo apt-get upgrade
再判斷系統是否內建了add-apt-repository命令,如果沒有執行下列命令安裝

sudo apt-get install software-properties-common

nginx

直接安裝nginx

sudo apt-get install nginx
nginx的操作命令如下:

service nginx start
service nginx stop
service nginx restart

檢視nginx使用的config配置檔案或者軟重啟的命令是

nginx -t
nginx -s reload
一般配置檔案在/etc/nginx目錄下。

不過要配置一個新的網站,不需要在nginx.conf裡新增,只需要在sites-enabled資料夾下加一個對應檔案即可(檔名隨意)

cd /etc/nginx/sites-enabled
touch demo
上面的demo檔案是隨便命名,用vi開啟

vim demo
然後新增下面的內容

server {
listen 80;
server_name demo.com;
root /home/www/demo/public;
index index.html index.htm index.php;
location 
/ { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

 

解釋一下上面的配置:

獨立的一個伺服器配置需要用server{}包裹起來
listen 80表示監聽80埠,也就是http訪問的預設埠,其它埠在瀏覽器上需要輸入埠號。
server_name表示繫結的是哪個域名。
root表示該域名訪問到路徑對應的實際資料夾
index表示域名的path為空的時候對應訪問哪個檔案,預設都是index.html、index.htm和index.php
location /這裡是對所有路徑做路由重寫,裡面的try_files $uri $uri/ /index.php?$query_string;是laravel框架的路徑重寫配置。
location ~ \.php$是指訪問所有後綴名帶.php的路徑時候,需要執行的操作,這裡也就是配置php的fastcgi
fastcgi裡的最重要部分是fastcgi_pass,它代表nginx伺服器如何與php通訊,這裡的unix:/run/php/php7.1-fpm.sock;是php7.1-fpm啟動後生成的套接字檔案,可以和nginx通訊。