Ubuntu14配置nginx虛擬主機

一、建立項目目錄
# 在 /home/share/ 目錄下創建一個test項目目錄,並編輯一個子目錄 index.php 文件


sudo mkdir -p  /home/share/test




# 新建一個主頁 index.php文件

sudo vim /home/share/test/index.php

test爲項目根目錄

二、配置文件

# 創建 server 虛擬主機配置文件

sudo vim /etc/nginx/sites-available/test.com


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


        server_name test.com;
        root /home/share/test;
#配到index.php訪問的時候直接可以test.com
#配到test必須test.com/index.php訪問
#跟下面默認訪問文件配置相關


        index index.html index.htm index.nginx-debian.html;


        location / {
            try_files $uri $uri/ =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }


}


#因爲nginx的主配置文件nginx.conf會自動應用sites-enabled中的配置文件

#所以我們需要在sites-enabled中創建一個軟鏈接到 /etc/nginx/sites-enabled/ 目錄下,

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


使配置文件生效


【直接在sites-enabled建立虛擬主機文件也可以】


三、更改 hosts 
# 添加your_ip_address test.com
我是在windows下 Windows\System32\drivers\etc


四、重啓 
sudo /etc/init.d/nginx restart


重新加載nginx使配置生效 
$ sudo /etc/init.d/nginx reload


五、訪問windows瀏覽器訪問
http://test.com/index.php
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章